|
1 | 1 | import time |
2 | 2 | import pytest |
3 | | - |
| 3 | +import numpy as np |
| 4 | +from pymilvus import db |
4 | 5 | from base.client_base import TestcaseBase |
5 | 6 | from common import common_func as cf |
6 | 7 | from common import common_type as ct |
@@ -75,3 +76,123 @@ def test_milvus_restore_back(self, collection_type, collection_need_to_restore, |
75 | 76 | for name in restore_collections: |
76 | 77 | self.compare_collections(name, name+suffix) |
77 | 78 |
|
| 79 | + def test_milvus_restore_back_with_db_support(self): |
| 80 | + # prepare data |
| 81 | + self._connect() |
| 82 | + names_origin = [] |
| 83 | + for i in range(2): |
| 84 | + db_name = cf.gen_unique_str("db") |
| 85 | + db.create_database(db_name) |
| 86 | + db.using_database(db_name) |
| 87 | + collection_name = cf.gen_unique_str(prefix) |
| 88 | + self.prepare_data(name=collection_name, db_name=db_name, nb=3000, is_binary=False, auto_id=True, check_function=False) |
| 89 | + assert collection_name in self.utility_wrap.list_collections()[0] |
| 90 | + names_origin.append(f"{db_name}.{collection_name}") |
| 91 | + log.info(f"name_origin:{names_origin}") |
| 92 | + # create backup |
| 93 | + back_up_name = cf.gen_unique_str(backup_prefix) |
| 94 | + payload = { |
| 95 | + "async": False, |
| 96 | + "backup_name": back_up_name, |
| 97 | + "collection_names": names_origin, |
| 98 | + } |
| 99 | + log.info(f"payload: {payload}") |
| 100 | + res = client.create_backup(payload) |
| 101 | + log.info(f"create backup response: {res}") |
| 102 | + res = client.list_backup() |
| 103 | + log.info(f"list_backup {res}") |
| 104 | + if "data" in res: |
| 105 | + all_backup = [r["name"] for r in res["data"]] |
| 106 | + else: |
| 107 | + all_backup = [] |
| 108 | + assert back_up_name in all_backup |
| 109 | + payload = {"async": False, "backup_name": back_up_name, |
| 110 | + # "collection_names": names_origin, |
| 111 | + "collection_suffix": suffix} |
| 112 | + log.info(f"restore payload: {payload}") |
| 113 | + res = client.restore_backup(payload) |
| 114 | + log.info(f"restore_backup: {res}") |
| 115 | + for name in names_origin: |
| 116 | + db_name = name.split(".")[0] |
| 117 | + collection_name = name.split(".")[1] |
| 118 | + db.using_database(db_name) |
| 119 | + |
| 120 | + res, _ = self.utility_wrap.list_collections() |
| 121 | + log.info(f"collection list in db {db_name}: {res}") |
| 122 | + assert collection_name + suffix in res |
| 123 | + self.compare_collections(collection_name, collection_name + suffix) |
| 124 | + |
| 125 | + @pytest.mark.parametrize("include_partition_key", [True, False]) |
| 126 | + @pytest.mark.parametrize("include_dynamic", [True, False]) |
| 127 | + @pytest.mark.parametrize("include_json", [True, False]) |
| 128 | + def test_milvus_restore_back_with_new_feature_support(self, include_json, include_dynamic, include_partition_key): |
| 129 | + self._connect() |
| 130 | + name_origin = cf.gen_unique_str(prefix) |
| 131 | + back_up_name = cf.gen_unique_str(backup_prefix) |
| 132 | + if include_json: |
| 133 | + fields = [cf.gen_int64_field(name="int64", is_primary=True), |
| 134 | + cf.gen_int64_field(name="key"), |
| 135 | + cf.gen_json_field(name="json"), |
| 136 | + cf.gen_float_vec_field(name="float_vector", dim=128), |
| 137 | + ] |
| 138 | + else: |
| 139 | + fields = [cf.gen_int64_field(name="int64", is_primary=True), |
| 140 | + cf.gen_int64_field(name="key"), |
| 141 | + cf.gen_float_vec_field(name="float_vector", dim=128), |
| 142 | + ] |
| 143 | + if include_partition_key: |
| 144 | + partition_key = "key" |
| 145 | + default_schema = cf.gen_collection_schema(fields, |
| 146 | + enable_dynamic_field=include_dynamic, |
| 147 | + partition_key_field=partition_key) |
| 148 | + else: |
| 149 | + default_schema = cf.gen_collection_schema(fields, |
| 150 | + enable_dynamic_field=include_dynamic) |
| 151 | + |
| 152 | + collection_w = self.init_collection_wrap(name=name_origin, schema=default_schema, active_trace=True) |
| 153 | + nb = 3000 |
| 154 | + if include_json: |
| 155 | + data = [ |
| 156 | + [i for i in range(nb)], |
| 157 | + [i % 3 for i in range(nb)], |
| 158 | + [{f"key_{str(i)}": i} for i in range(nb)], |
| 159 | + [[np.float32(i) for i in range(128)] for _ in range(nb)], |
| 160 | + ] |
| 161 | + else: |
| 162 | + data = [ |
| 163 | + [i for i in range(nb)], |
| 164 | + [i % 3 for i in range(nb)], |
| 165 | + [[np.float32(i) for i in range(128)] for _ in range(nb)], |
| 166 | + ] |
| 167 | + collection_w.insert(data=data) |
| 168 | + |
| 169 | + res = client.create_backup({"async": False, "backup_name": back_up_name, "collection_names": [name_origin]}) |
| 170 | + log.info(f"create_backup {res}") |
| 171 | + res = client.list_backup() |
| 172 | + log.info(f"list_backup {res}") |
| 173 | + if "data" in res: |
| 174 | + all_backup = [r["name"] for r in res["data"]] |
| 175 | + else: |
| 176 | + all_backup = [] |
| 177 | + assert back_up_name in all_backup |
| 178 | + backup = client.get_backup(back_up_name) |
| 179 | + assert backup["data"]["name"] == back_up_name |
| 180 | + backup_collections = [backup["collection_name"]for backup in backup["data"]["collection_backups"]] |
| 181 | + assert name_origin in backup_collections |
| 182 | + res = client.restore_backup({"async": False, "backup_name": back_up_name, "collection_names": [name_origin], |
| 183 | + "collection_suffix": suffix}) |
| 184 | + log.info(f"restore_backup: {res}") |
| 185 | + res, _ = self.utility_wrap.list_collections() |
| 186 | + assert name_origin + suffix in res |
| 187 | + output_fields = None |
| 188 | + if not include_json: |
| 189 | + output_fields = [ct.default_int64_field_name] |
| 190 | + self.compare_collections(name_origin, name_origin + suffix, output_fields=output_fields) |
| 191 | + res = client.delete_backup(back_up_name) |
| 192 | + res = client.list_backup() |
| 193 | + if "data" in res: |
| 194 | + all_backup = [r["name"] for r in res["data"]] |
| 195 | + else: |
| 196 | + all_backup = [] |
| 197 | + assert back_up_name not in all_backup |
| 198 | + |
0 commit comments