test_compact.py 42.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
import time
import pdb
import threading
import logging
from multiprocessing import Pool, Process
import pytest
from milvus import IndexType, MetricType
from utils import *

dim = 128
index_file_size = 10
12
COMPACT_TIMEOUT = 180
13 14 15 16 17 18 19 20 21 22 23 24 25
nprobe = 1
top_k = 1
tag = "1970-01-01"
nb = 6000


class TestCompactBase:
    """
    ******************************************************************
      The following cases are used to test `compact` function
    ******************************************************************
    """
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
26
    def test_compact_collection_name_None(self, connect, collection):
27
        '''
X
Xiaohai Xu 已提交
28 29
        target: compact collection where collection name is None
        method: compact with the collection_name: None
30 31
        expected: exception raised
        '''
X
Xiaohai Xu 已提交
32
        collection_name = None
33
        with pytest.raises(Exception) as e:
X
Xiaohai Xu 已提交
34
            status = connect.compact(collection_name)
35 36

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
37
    def test_compact_collection_name_not_existed(self, connect, collection):
38
        '''
X
Xiaohai Xu 已提交
39 40
        target: compact collection not existed
        method: compact with a random collection_name, which is not in db
41 42
        expected: status not ok
        '''
X
Xiaohai Xu 已提交
43 44
        collection_name = gen_unique_str("not_existed_collection")
        status = connect.compact(collection_name)
45 46 47 48
        assert not status.OK()
    
    @pytest.fixture(
        scope="function",
X
Xiaohai Xu 已提交
49
        params=gen_invalid_collection_names()
50
    )
X
Xiaohai Xu 已提交
51
    def get_collection_name(self, request):
52 53 54
        yield request.param

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
55
    def test_compact_collection_name_invalid(self, connect, get_collection_name):
56
        '''
X
Xiaohai Xu 已提交
57 58
        target: compact collection with invalid name
        method: compact with invalid collection_name
59 60
        expected: status not ok
        '''
X
Xiaohai Xu 已提交
61 62
        collection_name = get_collection_name
        status = connect.compact(collection_name)
63 64 65
        assert not status.OK()
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
66
    def test_add_vector_and_compact(self, connect, collection):
67 68
        '''
        target: test add vector and compact 
X
Xiaohai Xu 已提交
69
        method: add vector and compact collection
70 71 72
        expected: status ok, vector added
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
73
        status, ids = connect.insert(collection, vector)
74
        assert status.OK()
X
Xiaohai Xu 已提交
75
        status = connect.flush([collection])
76
        assert status.OK()
X
Xiaohai Xu 已提交
77
        # get collection info before compact
D
del-zhenwu 已提交
78
        status, info = connect.get_collection_stats(collection)
79 80
        assert status.OK()
        logging.getLogger().info(info)
81
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
82
        status = connect.compact(collection)
83
        assert status.OK()
X
Xiaohai Xu 已提交
84
        # get collection info after compact
D
del-zhenwu 已提交
85
        status, info = connect.get_collection_stats(collection)
86
        assert status.OK()
87
        size_after = info["partitions"][0]["segments"][0]["data_size"]
88 89 90
        assert(size_before == size_after)
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
91
    def test_insert_and_compact(self, connect, collection):
92 93
        '''
        target: test add vectors and compact 
X
Xiaohai Xu 已提交
94
        method: add vectors and compact collection
95 96 97
        expected: status ok, vectors added
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
98
        status, ids = connect.insert(collection, vectors)
99
        assert status.OK()
X
Xiaohai Xu 已提交
100
        status = connect.flush([collection])
101
        assert status.OK()
X
Xiaohai Xu 已提交
102
        # get collection info before compact
D
del-zhenwu 已提交
103
        status, info = connect.get_collection_stats(collection)
104
        assert status.OK()
105
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
106
        status = connect.compact(collection)
107
        assert status.OK()
X
Xiaohai Xu 已提交
108
        # get collection info after compact
D
del-zhenwu 已提交
109
        status, info = connect.get_collection_stats(collection)
110
        assert status.OK()
111
        size_after = info["partitions"][0]["segments"][0]["data_size"]
112 113 114
        assert(size_before == size_after)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
115
    def test_insert_delete_part_and_compact(self, connect, collection):
116 117
        '''
        target: test add vectors, delete part of them and compact 
X
Xiaohai Xu 已提交
118
        method: add vectors, delete a few and compact collection
119 120 121
        expected: status ok, data size is smaller after compact
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
122
        status, ids = connect.insert(collection, vectors)
123
        assert status.OK()
X
Xiaohai Xu 已提交
124
        status = connect.flush([collection])
125 126
        assert status.OK()
        delete_ids = [ids[0], ids[-1]]
D
del-zhenwu 已提交
127
        status = connect.delete_entity_by_id(collection, delete_ids)
128
        assert status.OK()
X
Xiaohai Xu 已提交
129
        status = connect.flush([collection])
130
        assert status.OK()
X
Xiaohai Xu 已提交
131
        # get collection info before compact
D
del-zhenwu 已提交
132
        status, info = connect.get_collection_stats(collection)
133
        assert status.OK()
134 135
        logging.getLogger().info(info["partitions"])
        size_before = info["partitions"][0]["segments"][0]["data_size"]
136
        logging.getLogger().info(size_before)
X
Xiaohai Xu 已提交
137
        status = connect.compact(collection)
138
        assert status.OK()
X
Xiaohai Xu 已提交
139
        # get collection info after compact
D
del-zhenwu 已提交
140
        status, info = connect.get_collection_stats(collection)
141
        assert status.OK()
142 143
        logging.getLogger().info(info["partitions"])
        size_after = info["partitions"][0]["segments"][0]["data_size"]
144
        logging.getLogger().info(size_after)
G
groot 已提交
145
        assert(size_before >= size_after)
146 147
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
148
    def test_insert_delete_all_and_compact(self, connect, collection):
149 150
        '''
        target: test add vectors, delete them and compact 
X
Xiaohai Xu 已提交
151 152
        method: add vectors, delete all and compact collection
        expected: status ok, no data size in collection info because collection is empty
153 154
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
155
        status, ids = connect.insert(collection, vectors)
156
        assert status.OK()
X
Xiaohai Xu 已提交
157
        status = connect.flush([collection])
158
        assert status.OK()
D
del-zhenwu 已提交
159
        status = connect.delete_entity_by_id(collection, ids)
160
        assert status.OK()
X
Xiaohai Xu 已提交
161
        status = connect.flush([collection])
162
        assert status.OK()
X
Xiaohai Xu 已提交
163
        # get collection info before compact
D
del-zhenwu 已提交
164
        status, info = connect.get_collection_stats(collection)
165
        assert status.OK()
X
Xiaohai Xu 已提交
166
        status = connect.compact(collection)
167
        assert status.OK()
X
Xiaohai Xu 已提交
168
        # get collection info after compact
D
del-zhenwu 已提交
169
        status, info = connect.get_collection_stats(collection)
170
        assert status.OK()
171 172
        logging.getLogger().info(info["partitions"])
        assert not info["partitions"][0]["segments"]
173

D
del-zhenwu 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    @pytest.mark.timeout(COMPACT_TIMEOUT)
    def test_insert_partition_delete_half_and_compact(self, connect, collection):
        '''
        target: test add vectors into partition, delete them and compact 
        method: add vectors, delete half of vectors in partition and compact collection
        expected: status ok, data_size less than the older version
        '''
        vectors = gen_vector(nb, dim)
        status = connect.create_partition(collection, tag)
        assert status.OK()
        status, ids = connect.insert(collection, vectors, partition_tag=tag)
        assert status.OK()
        status = connect.flush([collection])
        assert status.OK()
        status, info = connect.get_collection_stats(collection)
        assert status.OK()
        logging.getLogger().info(info["partitions"])

        delete_ids = ids[:3000]
        status = connect.delete_entity_by_id(collection, delete_ids)
        assert status.OK()
        status = connect.flush([collection])
        assert status.OK()
        # get collection info before compact
        status, info = connect.get_collection_stats(collection)
        assert status.OK()
        logging.getLogger().info(info["partitions"])
        status = connect.compact(collection)
        assert status.OK()
        # get collection info after compact
        status, info_after = connect.get_collection_stats(collection)
        assert status.OK()
        logging.getLogger().info(info_after["partitions"])
        assert info["partitions"][1]["segments"][0]["data_size"] > info_after["partitions"][1]["segments"][0]["data_size"]

209 210
    @pytest.fixture(
        scope="function",
211
        params=gen_simple_index()
212
    )
213
    def get_simple_index(self, request, connect):
214
        if str(connect._cmd("mode")[1]) == "CPU":
215
            if request.param["index_type"] not in [IndexType.IVF_SQ8, IndexType.IVFLAT, IndexType.FLAT, IndexType.IVF_PQ, IndexType.HNSW]:
216 217 218 219 220
                pytest.skip("Only support index_type: flat/ivf_flat/ivf_sq8")
        else:
            pytest.skip("Only support CPU mode")
        return request.param

X
Xiaohai Xu 已提交
221
    def test_compact_after_index_created(self, connect, collection, get_simple_index):
222
        '''
X
Xiaohai Xu 已提交
223
        target: test compact collection after index created
224 225 226 227
        method: add vectors, create index, delete part of vectors and compact
        expected: status ok, index description no change, data size smaller after compact
        '''
        count = 10
228 229
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
230
        vectors = gen_vector(count, dim)
D
del-zhenwu 已提交
231
        status, ids = connect.insert(collection, vectors)
232
        assert status.OK()
X
Xiaohai Xu 已提交
233
        status = connect.flush([collection])
234
        assert status.OK()
X
Xiaohai Xu 已提交
235
        status = connect.create_index(collection, index_type, index_param) 
236
        assert status.OK()
X
Xiaohai Xu 已提交
237
        status = connect.flush([collection])
238
        assert status.OK()
X
Xiaohai Xu 已提交
239
        # get collection info before compact
D
del-zhenwu 已提交
240
        status, info = connect.get_collection_stats(collection)
241
        assert status.OK()
242 243
        size_before = info["partitions"][0]["segments"][0]["data_size"]
        logging.getLogger().info(info["partitions"])
244
        delete_ids = [ids[0], ids[-1]]
D
del-zhenwu 已提交
245
        status = connect.delete_entity_by_id(collection, delete_ids)
246
        assert status.OK()
X
Xiaohai Xu 已提交
247
        status = connect.flush([collection])
248
        assert status.OK()
X
Xiaohai Xu 已提交
249
        status = connect.compact(collection)
250
        assert status.OK()
X
Xiaohai Xu 已提交
251
        # get collection info after compact
D
del-zhenwu 已提交
252
        status, info = connect.get_collection_stats(collection)
253
        assert status.OK()
254 255
        logging.getLogger().info(info["partitions"])
        size_after = info["partitions"][0]["segments"][0]["data_size"]
256 257 258
        assert(size_before > size_after)
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
259
    def test_add_vector_and_compact_twice(self, connect, collection):
260 261
        '''
        target: test add vector and compact twice
X
Xiaohai Xu 已提交
262
        method: add vector and compact collection twice
263 264 265
        expected: status ok, data size no change
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
266
        status, ids = connect.insert(collection, vector)
267
        assert status.OK()
X
Xiaohai Xu 已提交
268
        status = connect.flush([collection])
269
        assert status.OK()
X
Xiaohai Xu 已提交
270
        # get collection info before compact
D
del-zhenwu 已提交
271
        status, info = connect.get_collection_stats(collection)
272
        assert status.OK()
273
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
274
        status = connect.compact(collection)
275
        assert status.OK()
X
Xiaohai Xu 已提交
276
        status = connect.flush([collection])
277
        assert status.OK()
X
Xiaohai Xu 已提交
278
        # get collection info after compact
D
del-zhenwu 已提交
279
        status, info = connect.get_collection_stats(collection)
280
        assert status.OK()
281
        size_after = info["partitions"][0]["segments"][0]["data_size"]
282
        assert(size_before == size_after)
X
Xiaohai Xu 已提交
283
        status = connect.compact(collection)
284
        assert status.OK()
X
Xiaohai Xu 已提交
285
        # get collection info after compact twice
D
del-zhenwu 已提交
286
        status, info = connect.get_collection_stats(collection)
287
        assert status.OK()
288
        size_after_twice = info["partitions"][0]["segments"][0]["data_size"]
289 290 291
        assert(size_after == size_after_twice)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
292
    def test_insert_delete_part_and_compact_twice(self, connect, collection):
293 294
        '''
        target: test add vectors, delete part of them and compact twice
X
Xiaohai Xu 已提交
295
        method: add vectors, delete part and compact collection twice
296 297 298
        expected: status ok, data size smaller after first compact, no change after second
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
299
        status, ids = connect.insert(collection, vectors)
300
        assert status.OK()
X
Xiaohai Xu 已提交
301
        status = connect.flush([collection])
302 303
        assert status.OK()
        delete_ids = [ids[0], ids[-1]]
D
del-zhenwu 已提交
304
        status = connect.delete_entity_by_id(collection, delete_ids)
305
        assert status.OK()
X
Xiaohai Xu 已提交
306
        status = connect.flush([collection])
307
        assert status.OK()
X
Xiaohai Xu 已提交
308
        # get collection info before compact
D
del-zhenwu 已提交
309
        status, info = connect.get_collection_stats(collection)
310
        assert status.OK()
311
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
312
        status = connect.compact(collection)
313
        assert status.OK()
X
Xiaohai Xu 已提交
314
        # get collection info after compact
D
del-zhenwu 已提交
315
        status, info = connect.get_collection_stats(collection)
316
        assert status.OK()
317
        size_after = info["partitions"][0]["segments"][0]["data_size"]
G
groot 已提交
318
        assert(size_before >= size_after)
X
Xiaohai Xu 已提交
319
        status = connect.compact(collection)
320
        assert status.OK()
X
Xiaohai Xu 已提交
321
        # get collection info after compact twice
D
del-zhenwu 已提交
322
        status, info = connect.get_collection_stats(collection)
323
        assert status.OK()
324
        size_after_twice = info["partitions"][0]["segments"][0]["data_size"]
325 326 327
        assert(size_after == size_after_twice)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
328
    def test_compact_multi_collections(self, connect):
329
        '''
X
Xiaohai Xu 已提交
330 331
        target: test compact works or not with multiple collections
        method: create 50 collections, add vectors into them and compact in turn
332 333 334
        expected: status ok
        '''
        nq = 100
X
Xiaohai Xu 已提交
335
        num_collections = 50
336
        vectors = gen_vectors(nq, dim)
X
Xiaohai Xu 已提交
337 338 339 340 341
        collection_list = []
        for i in range(num_collections):
            collection_name = gen_unique_str("test_compact_multi_collection_%d" % i)
            collection_list.append(collection_name)
            param = {'collection_name': collection_name,
342 343 344
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
345
            connect.create_collection(param)
346
        time.sleep(6)
X
Xiaohai Xu 已提交
347
        for i in range(num_collections):
D
del-zhenwu 已提交
348
            status, ids = connect.insert(collection_name=collection_list[i], records=vectors)
349
            assert status.OK()
X
Xiaohai Xu 已提交
350
            status = connect.compact(collection_list[i])
351 352 353
            assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
354
    def test_add_vector_after_compact(self, connect, collection):
355 356 357 358 359 360
        '''
        target: test add vector after compact 
        method: after compact operation, add vector
        expected: status ok, vector added
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
361
        status, ids = connect.insert(collection, vectors)
362
        assert status.OK()
X
Xiaohai Xu 已提交
363
        status = connect.flush([collection])
364
        assert status.OK()
X
Xiaohai Xu 已提交
365
        # get collection info before compact
D
del-zhenwu 已提交
366
        status, info = connect.get_collection_stats(collection)
367
        assert status.OK()
368
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
369
        status = connect.compact(collection)
370
        assert status.OK()
X
Xiaohai Xu 已提交
371
        # get collection info after compact
D
del-zhenwu 已提交
372
        status, info = connect.get_collection_stats(collection)
373
        assert status.OK()
374
        size_after = info["partitions"][0]["segments"][0]["data_size"]
375 376
        assert(size_before == size_after)
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
377
        status, ids = connect.insert(collection, vector)
378 379 380
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
381
    def test_index_creation_after_compact(self, connect, collection, get_simple_index):
382 383 384 385 386 387
        '''
        target: test index creation after compact
        method: after compact operation, create index
        expected: status ok, index description no change
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
388
        status, ids = connect.insert(collection, vectors)
389
        assert status.OK()
X
Xiaohai Xu 已提交
390
        status = connect.flush([collection])
391
        assert status.OK()
D
del-zhenwu 已提交
392
        status = connect.delete_entity_by_id(collection, ids[:10])
393
        assert status.OK()
X
Xiaohai Xu 已提交
394
        status = connect.flush([collection])
395
        assert status.OK()
396 397
        status = connect.compact(collection)
        assert status.OK()
398 399
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
400
        status = connect.create_index(collection, index_type, index_param) 
401
        assert status.OK()
D
del-zhenwu 已提交
402
        status, result = connect.get_index_info(collection)
X
Xiaohai Xu 已提交
403
        assert result._collection_name == collection
404
        assert result._index_type == index_type
405 406

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
407
    def test_delete_vectors_after_compact(self, connect, collection):
408 409 410 411 412 413
        '''
        target: test delete vectors after compact
        method: after compact operation, delete vectors
        expected: status ok, vectors deleted
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
414
        status, ids = connect.insert(collection, vectors)
415
        assert status.OK()
X
Xiaohai Xu 已提交
416
        status = connect.flush([collection])
417
        assert status.OK()
X
Xiaohai Xu 已提交
418
        status = connect.compact(collection)
419
        assert status.OK()
X
Xiaohai Xu 已提交
420
        status = connect.flush([collection])
421
        assert status.OK()
D
del-zhenwu 已提交
422
        status = connect.delete_entity_by_id(collection, ids)
423
        assert status.OK()
X
Xiaohai Xu 已提交
424
        status = connect.flush([collection])
425 426 427
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
428
    def test_search_after_compact(self, connect, collection):
429 430 431 432 433 434
        '''
        target: test search after compact
        method: after compact operation, search vector
        expected: status ok
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
435
        status, ids = connect.insert(collection, vectors)
436
        assert status.OK()
X
Xiaohai Xu 已提交
437
        status = connect.flush([collection])
438
        assert status.OK()
X
Xiaohai Xu 已提交
439
        status = connect.compact(collection)
440 441
        assert status.OK()
        query_vecs = [vectors[0]]
D
del-zhenwu 已提交
442
        status, res = connect.search(collection, top_k, query_records=query_vecs) 
443 444 445
        logging.getLogger().info(res)
        assert status.OK()

446 447
    # TODO: enable
    def _test_compact_server_crashed_recovery(self, connect, collection):
448 449
        '''
        target: test compact when server crashed unexpectedly and restarted
X
Xiaohai Xu 已提交
450
        method: add vectors, delete and compact collection; server stopped and restarted during compact
451 452 453
        expected: status ok, request recovered
        '''
        vectors = gen_vector(nb * 100, dim)
D
del-zhenwu 已提交
454
        status, ids = connect.insert(collection, vectors)
455
        assert status.OK()
X
Xiaohai Xu 已提交
456
        status = connect.flush([collection])
457 458
        assert status.OK()
        delete_ids = ids[0:1000]
D
del-zhenwu 已提交
459
        status = connect.delete_entity_by_id(collection, delete_ids)
460
        assert status.OK()
X
Xiaohai Xu 已提交
461
        status = connect.flush([collection])
462 463 464
        assert status.OK()
        # start to compact, kill and restart server
        logging.getLogger().info("compact starting...")
X
Xiaohai Xu 已提交
465
        status = connect.compact(collection)
466 467
        # pdb.set_trace()
        assert status.OK()
X
Xiaohai Xu 已提交
468
        # get collection info after compact
D
del-zhenwu 已提交
469
        status, info = connect.get_collection_stats(collection)
470
        assert status.OK()
471
        assert info["partitions"][0].count == nb * 100 - 1000
472 473 474 475 476 477 478 479 480


class TestCompactJAC:
    """
    ******************************************************************
      The following cases are used to test `compact` function
    ******************************************************************
    """
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
481
    def test_add_vector_and_compact(self, connect, jac_collection):
482 483
        '''
        target: test add vector and compact 
X
Xiaohai Xu 已提交
484
        method: add vector and compact collection
485 486 487
        expected: status ok, vector added
        '''
        tmp, vector = gen_binary_vectors(1, dim)
D
del-zhenwu 已提交
488
        status, ids = connect.insert(jac_collection, vector)
489
        assert status.OK()
X
Xiaohai Xu 已提交
490
        status = connect.flush([jac_collection])
491
        assert status.OK()
X
Xiaohai Xu 已提交
492
        # get collection info before compact
D
del-zhenwu 已提交
493
        status, info = connect.get_collection_stats(jac_collection)
494
        assert status.OK()
495
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
496
        status = connect.compact(jac_collection)
497
        assert status.OK()
X
Xiaohai Xu 已提交
498
        # get collection info after compact
D
del-zhenwu 已提交
499
        status, info = connect.get_collection_stats(jac_collection)
500
        assert status.OK()
501
        size_after = info["partitions"][0]["segments"][0]["data_size"]
502 503 504
        assert(size_before == size_after)
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
505
    def test_insert_and_compact(self, connect, jac_collection):
506 507
        '''
        target: test add vectors and compact 
X
Xiaohai Xu 已提交
508
        method: add vectors and compact collection
509 510 511
        expected: status ok, vectors added
        '''
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
512
        status, ids = connect.insert(jac_collection, vectors)
513
        assert status.OK()
X
Xiaohai Xu 已提交
514
        status = connect.flush([jac_collection])
515
        assert status.OK()
X
Xiaohai Xu 已提交
516
        # get collection info before compact
D
del-zhenwu 已提交
517
        status, info = connect.get_collection_stats(jac_collection)
518
        assert status.OK()
519
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
520
        status = connect.compact(jac_collection)
521
        assert status.OK()
X
Xiaohai Xu 已提交
522
        # get collection info after compact
D
del-zhenwu 已提交
523
        status, info = connect.get_collection_stats(jac_collection)
524
        assert status.OK()
525
        size_after = info["partitions"][0]["segments"][0]["data_size"]
526 527 528
        assert(size_before == size_after)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
529
    def test_insert_delete_part_and_compact(self, connect, jac_collection):
530 531
        '''
        target: test add vectors, delete part of them and compact 
X
Xiaohai Xu 已提交
532
        method: add vectors, delete a few and compact collection
533 534 535
        expected: status ok, data size is smaller after compact
        '''
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
536
        status, ids = connect.insert(jac_collection, vectors)
537
        assert status.OK()
X
Xiaohai Xu 已提交
538
        status = connect.flush([jac_collection])
539 540
        assert status.OK()
        delete_ids = [ids[0], ids[-1]]
D
del-zhenwu 已提交
541
        status = connect.delete_entity_by_id(jac_collection, delete_ids)
542
        assert status.OK()
X
Xiaohai Xu 已提交
543
        status = connect.flush([jac_collection])
544
        assert status.OK()
X
Xiaohai Xu 已提交
545
        # get collection info before compact
D
del-zhenwu 已提交
546
        status, info = connect.get_collection_stats(jac_collection)
547
        assert status.OK()
548 549
        logging.getLogger().info(info["partitions"])
        size_before = info["partitions"][0]["segments"][0]["data_size"]
550
        logging.getLogger().info(size_before)
X
Xiaohai Xu 已提交
551
        status = connect.compact(jac_collection)
552
        assert status.OK()
X
Xiaohai Xu 已提交
553
        # get collection info after compact
D
del-zhenwu 已提交
554
        status, info = connect.get_collection_stats(jac_collection)
555
        assert status.OK()
556 557
        logging.getLogger().info(info["partitions"])
        size_after = info["partitions"][0]["segments"][0]["data_size"]
558
        logging.getLogger().info(size_after)
G
groot 已提交
559
        assert(size_before >= size_after)
560 561
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
562
    def test_insert_delete_all_and_compact(self, connect, jac_collection):
563 564
        '''
        target: test add vectors, delete them and compact 
X
Xiaohai Xu 已提交
565 566
        method: add vectors, delete all and compact collection
        expected: status ok, no data size in collection info because collection is empty
567 568
        '''
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
569
        status, ids = connect.insert(jac_collection, vectors)
570
        assert status.OK()
X
Xiaohai Xu 已提交
571
        status = connect.flush([jac_collection])
572
        assert status.OK()
D
del-zhenwu 已提交
573
        status = connect.delete_entity_by_id(jac_collection, ids)
574
        assert status.OK()
X
Xiaohai Xu 已提交
575
        status = connect.flush([jac_collection])
576
        assert status.OK()
X
Xiaohai Xu 已提交
577
        # get collection info before compact
D
del-zhenwu 已提交
578
        status, info = connect.get_collection_stats(jac_collection)
579
        assert status.OK()
X
Xiaohai Xu 已提交
580
        status = connect.compact(jac_collection)
581
        assert status.OK()
X
Xiaohai Xu 已提交
582
        # get collection info after compact
D
del-zhenwu 已提交
583
        status, info = connect.get_collection_stats(jac_collection)
584
        assert status.OK()
585 586
        logging.getLogger().info(info["partitions"])
        assert not info["partitions"][0]["segments"]
587 588
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
589
    def test_add_vector_and_compact_twice(self, connect, jac_collection):
590 591
        '''
        target: test add vector and compact twice
X
Xiaohai Xu 已提交
592
        method: add vector and compact collection twice
593 594 595
        expected: status ok
        '''
        tmp, vector = gen_binary_vectors(1, dim)
D
del-zhenwu 已提交
596
        status, ids = connect.insert(jac_collection, vector)
597
        assert status.OK()
X
Xiaohai Xu 已提交
598
        status = connect.flush([jac_collection])
599
        assert status.OK()
X
Xiaohai Xu 已提交
600
        # get collection info before compact
D
del-zhenwu 已提交
601
        status, info = connect.get_collection_stats(jac_collection)
602
        assert status.OK()
603
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
604
        status = connect.compact(jac_collection)
605
        assert status.OK()
X
Xiaohai Xu 已提交
606
        # get collection info after compact
D
del-zhenwu 已提交
607
        status, info = connect.get_collection_stats(jac_collection)
608
        assert status.OK()
609
        size_after = info["partitions"][0]["segments"][0]["data_size"]
610
        assert(size_before == size_after)
X
Xiaohai Xu 已提交
611
        status = connect.compact(jac_collection)
612
        assert status.OK()
X
Xiaohai Xu 已提交
613
        # get collection info after compact twice
D
del-zhenwu 已提交
614
        status, info = connect.get_collection_stats(jac_collection)
615
        assert status.OK()
616
        size_after_twice = info["partitions"][0]["segments"][0]["data_size"]
617 618 619
        assert(size_after == size_after_twice)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
620
    def test_insert_delete_part_and_compact_twice(self, connect, jac_collection):
621 622
        '''
        target: test add vectors, delete part of them and compact twice
X
Xiaohai Xu 已提交
623
        method: add vectors, delete part and compact collection twice
624 625 626
        expected: status ok, data size smaller after first compact, no change after second
        '''
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
627
        status, ids = connect.insert(jac_collection, vectors)
628
        assert status.OK()
X
Xiaohai Xu 已提交
629
        status = connect.flush([jac_collection])
630 631
        assert status.OK()
        delete_ids = [ids[0], ids[-1]]
D
del-zhenwu 已提交
632
        status = connect.delete_entity_by_id(jac_collection, delete_ids)
633
        assert status.OK()
X
Xiaohai Xu 已提交
634
        status = connect.flush([jac_collection])
635
        assert status.OK()
X
Xiaohai Xu 已提交
636
        # get collection info before compact
D
del-zhenwu 已提交
637
        status, info = connect.get_collection_stats(jac_collection)
638
        assert status.OK()
639
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
640
        status = connect.compact(jac_collection)
641
        assert status.OK()
X
Xiaohai Xu 已提交
642
        # get collection info after compact
D
del-zhenwu 已提交
643
        status, info = connect.get_collection_stats(jac_collection)
644
        assert status.OK()
645
        size_after = info["partitions"][0]["segments"][0]["data_size"]
G
groot 已提交
646
        assert(size_before >= size_after)
X
Xiaohai Xu 已提交
647
        status = connect.compact(jac_collection)
648
        assert status.OK()
X
Xiaohai Xu 已提交
649
        # get collection info after compact twice
D
del-zhenwu 已提交
650
        status, info = connect.get_collection_stats(jac_collection)
651
        assert status.OK()
652
        size_after_twice = info["partitions"][0]["segments"][0]["data_size"]
653 654 655
        assert(size_after == size_after_twice)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
656
    def test_compact_multi_collections(self, connect):
657
        '''
X
Xiaohai Xu 已提交
658 659
        target: test compact works or not with multiple collections
        method: create 50 collections, add vectors into them and compact in turn
660 661 662
        expected: status ok
        '''
        nq = 100
X
Xiaohai Xu 已提交
663
        num_collections = 10
664
        tmp, vectors = gen_binary_vectors(nq, dim)
X
Xiaohai Xu 已提交
665 666 667 668 669
        collection_list = []
        for i in range(num_collections):
            collection_name = gen_unique_str("test_compact_multi_collection_%d" % i)
            collection_list.append(collection_name)
            param = {'collection_name': collection_name,
670 671 672
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.JACCARD}
X
Xiaohai Xu 已提交
673 674
            connect.create_collection(param)
        for i in range(num_collections):
D
del-zhenwu 已提交
675
            status, ids = connect.insert(collection_name=collection_list[i], records=vectors)
676
            assert status.OK()
D
del-zhenwu 已提交
677
            status = connect.delete_entity_by_id(collection_list[i], [ids[0], ids[-1]])
678
            assert status.OK()
X
Xiaohai Xu 已提交
679
            status = connect.flush([collection_list[i]])
680
            assert status.OK()
X
Xiaohai Xu 已提交
681
            status = connect.compact(collection_list[i])
682
            assert status.OK()
D
del-zhenwu 已提交
683 684
            status = connect.drop_collection(collection_list[i])
            assert status.OK()
685 686

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
687
    def test_add_vector_after_compact(self, connect, jac_collection):
688 689 690 691 692 693
        '''
        target: test add vector after compact 
        method: after compact operation, add vector
        expected: status ok, vector added
        '''
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
694
        status, ids = connect.insert(jac_collection, vectors)
695
        assert status.OK()
X
Xiaohai Xu 已提交
696
        status = connect.flush([jac_collection])
697
        assert status.OK()
X
Xiaohai Xu 已提交
698
        # get collection info before compact
D
del-zhenwu 已提交
699
        status, info = connect.get_collection_stats(jac_collection)
700
        assert status.OK()
701
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
702
        status = connect.compact(jac_collection)
703
        assert status.OK()
X
Xiaohai Xu 已提交
704
        # get collection info after compact
D
del-zhenwu 已提交
705
        status, info = connect.get_collection_stats(jac_collection)
706
        assert status.OK()
707
        size_after = info["partitions"][0]["segments"][0]["data_size"]
708 709
        assert(size_before == size_after)
        tmp, vector = gen_binary_vectors(1, dim)
D
del-zhenwu 已提交
710
        status, ids = connect.insert(jac_collection, vector)
711 712 713
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
714
    def test_delete_vectors_after_compact(self, connect, jac_collection):
715 716 717 718 719 720
        '''
        target: test delete vectors after compact
        method: after compact operation, delete vectors
        expected: status ok, vectors deleted
        '''
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
721
        status, ids = connect.insert(jac_collection, vectors)
722
        assert status.OK()
X
Xiaohai Xu 已提交
723
        status = connect.flush([jac_collection])
724
        assert status.OK()
X
Xiaohai Xu 已提交
725
        status = connect.compact(jac_collection)
726
        assert status.OK()
X
Xiaohai Xu 已提交
727
        status = connect.flush([jac_collection])
728
        assert status.OK()
D
del-zhenwu 已提交
729
        status = connect.delete_entity_by_id(jac_collection, ids)
730
        assert status.OK()
X
Xiaohai Xu 已提交
731
        status = connect.flush([jac_collection])
732 733 734
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
735
    def test_search_after_compact(self, connect, jac_collection):
736 737 738 739 740 741
        '''
        target: test search after compact
        method: after compact operation, search vector
        expected: status ok
        '''
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
742
        status, ids = connect.insert(jac_collection, vectors)
743
        assert status.OK()
X
Xiaohai Xu 已提交
744
        status = connect.flush([jac_collection])
745
        assert status.OK()
X
Xiaohai Xu 已提交
746
        status = connect.compact(jac_collection)
747 748
        assert status.OK()
        query_vecs = [vectors[0]]
D
del-zhenwu 已提交
749
        status, res = connect.search(jac_collection, top_k, query_records=query_vecs) 
750 751 752 753 754 755 756 757 758 759 760
        logging.getLogger().info(res)
        assert status.OK()


class TestCompactIP:
    """
    ******************************************************************
      The following cases are used to test `compact` function
    ******************************************************************
    """
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
761
    def test_add_vector_and_compact(self, connect, ip_collection):
762 763
        '''
        target: test add vector and compact 
X
Xiaohai Xu 已提交
764
        method: add vector and compact collection
765 766 767
        expected: status ok, vector added
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
768
        status, ids = connect.insert(ip_collection, vector)
769
        assert status.OK()
X
Xiaohai Xu 已提交
770
        status = connect.flush([ip_collection])
771
        assert status.OK()
X
Xiaohai Xu 已提交
772
        # get collection info before compact
D
del-zhenwu 已提交
773
        status, info = connect.get_collection_stats(ip_collection)
774
        assert status.OK()
775
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
776
        status = connect.compact(ip_collection)
777
        assert status.OK()
X
Xiaohai Xu 已提交
778
        status = connect.flush([ip_collection])
779
        assert status.OK()
X
Xiaohai Xu 已提交
780
        # get collection info after compact
D
del-zhenwu 已提交
781
        status, info = connect.get_collection_stats(ip_collection)
782
        assert status.OK()
783
        size_after = info["partitions"][0]["segments"][0]["data_size"]
784 785 786
        assert(size_before == size_after)
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
787
    def test_insert_and_compact(self, connect, ip_collection):
788 789
        '''
        target: test add vectors and compact 
X
Xiaohai Xu 已提交
790
        method: add vectors and compact collection
791 792 793
        expected: status ok, vectors added
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
794
        status, ids = connect.insert(ip_collection, vectors)
795
        assert status.OK()
X
Xiaohai Xu 已提交
796
        status = connect.flush([ip_collection])
797
        assert status.OK()
X
Xiaohai Xu 已提交
798
        # get collection info before compact
D
del-zhenwu 已提交
799
        status, info = connect.get_collection_stats(ip_collection)
800
        assert status.OK()
801
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
802
        status = connect.compact(ip_collection)
803
        assert status.OK()
X
Xiaohai Xu 已提交
804
        # get collection info after compact
D
del-zhenwu 已提交
805
        status, info = connect.get_collection_stats(ip_collection)
806
        assert status.OK()
807
        size_after = info["partitions"][0]["segments"][0]["data_size"]
808 809 810
        assert(size_before == size_after)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
811
    def test_insert_delete_part_and_compact(self, connect, ip_collection):
812 813
        '''
        target: test add vectors, delete part of them and compact 
X
Xiaohai Xu 已提交
814
        method: add vectors, delete a few and compact collection
815 816 817
        expected: status ok, data size is smaller after compact
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
818
        status, ids = connect.insert(ip_collection, vectors)
819
        assert status.OK()
X
Xiaohai Xu 已提交
820
        status = connect.flush([ip_collection])
821 822
        assert status.OK()
        delete_ids = [ids[0], ids[-1]]
D
del-zhenwu 已提交
823
        status = connect.delete_entity_by_id(ip_collection, delete_ids)
824
        assert status.OK()
X
Xiaohai Xu 已提交
825
        status = connect.flush([ip_collection])
826
        assert status.OK()
X
Xiaohai Xu 已提交
827
        # get collection info before compact
D
del-zhenwu 已提交
828
        status, info = connect.get_collection_stats(ip_collection)
829
        assert status.OK()
830 831
        logging.getLogger().info(info["partitions"])
        size_before = info["partitions"][0]["segments"][0]["data_size"]
832
        logging.getLogger().info(size_before)
X
Xiaohai Xu 已提交
833
        status = connect.compact(ip_collection)
834
        assert status.OK()
X
Xiaohai Xu 已提交
835
        # get collection info after compact
D
del-zhenwu 已提交
836
        status, info = connect.get_collection_stats(ip_collection)
837
        assert status.OK()
838 839
        logging.getLogger().info(info["partitions"])
        size_after = info["partitions"][0]["segments"][0]["data_size"]
840
        logging.getLogger().info(size_after)
G
groot 已提交
841
        assert(size_before >= size_after)
842 843
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
844
    def test_insert_delete_all_and_compact(self, connect, ip_collection):
845 846
        '''
        target: test add vectors, delete them and compact 
X
Xiaohai Xu 已提交
847 848
        method: add vectors, delete all and compact collection
        expected: status ok, no data size in collection info because collection is empty
849 850
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
851
        status, ids = connect.insert(ip_collection, vectors)
852
        assert status.OK()
X
Xiaohai Xu 已提交
853
        status = connect.flush([ip_collection])
854
        assert status.OK()
D
del-zhenwu 已提交
855
        status = connect.delete_entity_by_id(ip_collection, ids)
856
        assert status.OK()
X
Xiaohai Xu 已提交
857
        status = connect.flush([ip_collection])
858
        assert status.OK()
X
Xiaohai Xu 已提交
859
        # get collection info before compact
D
del-zhenwu 已提交
860
        status, info = connect.get_collection_stats(ip_collection)
861
        assert status.OK()
X
Xiaohai Xu 已提交
862
        status = connect.compact(ip_collection)
863
        assert status.OK()
X
Xiaohai Xu 已提交
864
        # get collection info after compact
D
del-zhenwu 已提交
865
        status, info = connect.get_collection_stats(ip_collection)
866
        assert status.OK()
867 868
        logging.getLogger().info(info["partitions"])
        assert not info["partitions"][0]["segments"]
869 870
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
871
    def test_add_vector_and_compact_twice(self, connect, ip_collection):
872 873
        '''
        target: test add vector and compact twice
X
Xiaohai Xu 已提交
874
        method: add vector and compact collection twice
875 876 877
        expected: status ok
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
878
        status, ids = connect.insert(ip_collection, vector)
879
        assert status.OK()
X
Xiaohai Xu 已提交
880
        status = connect.flush([ip_collection])
881
        assert status.OK()
X
Xiaohai Xu 已提交
882
        # get collection info before compact
D
del-zhenwu 已提交
883
        status, info = connect.get_collection_stats(ip_collection)
884
        assert status.OK()
885
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
886
        status = connect.compact(ip_collection)
887
        assert status.OK()
X
Xiaohai Xu 已提交
888
        # get collection info after compact
D
del-zhenwu 已提交
889
        status, info = connect.get_collection_stats(ip_collection)
890
        assert status.OK()
891
        size_after = info["partitions"][0]["segments"][0]["data_size"]
892
        assert(size_before == size_after)
X
Xiaohai Xu 已提交
893
        status = connect.compact(ip_collection)
894
        assert status.OK()
X
Xiaohai Xu 已提交
895
        # get collection info after compact twice
D
del-zhenwu 已提交
896
        status, info = connect.get_collection_stats(ip_collection)
897
        assert status.OK()
898
        size_after_twice = info["partitions"][0]["segments"][0]["data_size"]
899 900 901
        assert(size_after == size_after_twice)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
902
    def test_insert_delete_part_and_compact_twice(self, connect, ip_collection):
903 904
        '''
        target: test add vectors, delete part of them and compact twice
X
Xiaohai Xu 已提交
905
        method: add vectors, delete part and compact collection twice
906 907 908
        expected: status ok, data size smaller after first compact, no change after second
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
909
        status, ids = connect.insert(ip_collection, vectors)
910
        assert status.OK()
X
Xiaohai Xu 已提交
911
        status = connect.flush([ip_collection])
912 913
        assert status.OK()
        delete_ids = [ids[0], ids[-1]]
D
del-zhenwu 已提交
914
        status = connect.delete_entity_by_id(ip_collection, delete_ids)
915
        assert status.OK()
X
Xiaohai Xu 已提交
916
        status = connect.flush([ip_collection])
917
        assert status.OK()
X
Xiaohai Xu 已提交
918
        # get collection info before compact
D
del-zhenwu 已提交
919
        status, info = connect.get_collection_stats(ip_collection)
920
        assert status.OK()
921
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
922
        status = connect.compact(ip_collection)
923
        assert status.OK()
X
Xiaohai Xu 已提交
924
        status = connect.flush([ip_collection])
925
        assert status.OK()
X
Xiaohai Xu 已提交
926
        # get collection info after compact
D
del-zhenwu 已提交
927
        status, info = connect.get_collection_stats(ip_collection)
928
        assert status.OK()
929
        size_after = info["partitions"][0]["segments"][0]["data_size"]
G
groot 已提交
930
        assert(size_before >= size_after)
X
Xiaohai Xu 已提交
931
        status = connect.compact(ip_collection)
932
        assert status.OK()
X
Xiaohai Xu 已提交
933
        status = connect.flush([ip_collection])
934
        assert status.OK()
X
Xiaohai Xu 已提交
935
        # get collection info after compact twice
D
del-zhenwu 已提交
936
        status, info = connect.get_collection_stats(ip_collection)
937
        assert status.OK()
938
        size_after_twice = info["partitions"][0]["segments"][0]["data_size"]
939 940 941
        assert(size_after == size_after_twice)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
942
    def test_compact_multi_collections(self, connect):
943
        '''
X
Xiaohai Xu 已提交
944 945
        target: test compact works or not with multiple collections
        method: create 50 collections, add vectors into them and compact in turn
946 947 948
        expected: status ok
        '''
        nq = 100
X
Xiaohai Xu 已提交
949
        num_collections = 50
950
        vectors = gen_vectors(nq, dim)
X
Xiaohai Xu 已提交
951 952 953 954 955
        collection_list = []
        for i in range(num_collections):
            collection_name = gen_unique_str("test_compact_multi_collection_%d" % i)
            collection_list.append(collection_name)
            param = {'collection_name': collection_name,
956 957 958
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.IP}
X
Xiaohai Xu 已提交
959
            connect.create_collection(param)
960
        time.sleep(6)
X
Xiaohai Xu 已提交
961
        for i in range(num_collections):
D
del-zhenwu 已提交
962
            status, ids = connect.insert(collection_name=collection_list[i], records=vectors)
963
            assert status.OK()
X
Xiaohai Xu 已提交
964
            status = connect.compact(collection_list[i])
965 966 967
            assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
968
    def test_add_vector_after_compact(self, connect, ip_collection):
969 970 971 972 973 974
        '''
        target: test add vector after compact 
        method: after compact operation, add vector
        expected: status ok, vector added
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
975
        status, ids = connect.insert(ip_collection, vectors)
976
        assert status.OK()
X
Xiaohai Xu 已提交
977
        status = connect.flush([ip_collection])
978
        assert status.OK()
X
Xiaohai Xu 已提交
979
        # get collection info before compact
D
del-zhenwu 已提交
980
        status, info = connect.get_collection_stats(ip_collection)
981
        assert status.OK()
982
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
983
        status = connect.compact(ip_collection)
984
        assert status.OK()
X
Xiaohai Xu 已提交
985
        # get collection info after compact
D
del-zhenwu 已提交
986
        status, info = connect.get_collection_stats(ip_collection)
987
        assert status.OK()
988
        size_after = info["partitions"][0]["segments"][0]["data_size"]
989 990
        assert(size_before == size_after)
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
991
        status, ids = connect.insert(ip_collection, vector)
992 993 994
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
995
    def test_delete_vectors_after_compact(self, connect, ip_collection):
996 997 998 999 1000 1001
        '''
        target: test delete vectors after compact
        method: after compact operation, delete vectors
        expected: status ok, vectors deleted
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
1002
        status, ids = connect.insert(ip_collection, vectors)
1003
        assert status.OK()
X
Xiaohai Xu 已提交
1004
        status = connect.flush([ip_collection])
1005
        assert status.OK()
X
Xiaohai Xu 已提交
1006
        status = connect.compact(ip_collection)
1007
        assert status.OK()
D
del-zhenwu 已提交
1008
        status = connect.delete_entity_by_id(ip_collection, ids)
1009
        assert status.OK()
X
Xiaohai Xu 已提交
1010
        status = connect.flush([ip_collection])
1011 1012 1013
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
1014
    def test_search_after_compact(self, connect, ip_collection):
1015 1016 1017 1018 1019 1020
        '''
        target: test search after compact
        method: after compact operation, search vector
        expected: status ok
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
1021
        status, ids = connect.insert(ip_collection, vectors)
1022
        assert status.OK()
X
Xiaohai Xu 已提交
1023
        status = connect.flush([ip_collection])
1024
        assert status.OK()
X
Xiaohai Xu 已提交
1025
        status = connect.compact(ip_collection)
1026 1027
        assert status.OK()
        query_vecs = [vectors[0]]
D
del-zhenwu 已提交
1028
        status, res = connect.search(ip_collection, top_k, query_records=query_vecs) 
1029 1030
        logging.getLogger().info(res)
        assert status.OK()