test_compact.py 41.0 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 174 175

    @pytest.fixture(
        scope="function",
176
        params=gen_simple_index()
177
    )
178
    def get_simple_index(self, request, connect):
179
        if str(connect._cmd("mode")[1]) == "CPU":
180
            if request.param["index_type"] not in [IndexType.IVF_SQ8, IndexType.IVFLAT, IndexType.FLAT, IndexType.IVF_PQ, IndexType.HNSW]:
181 182 183 184 185
                pytest.skip("Only support index_type: flat/ivf_flat/ivf_sq8")
        else:
            pytest.skip("Only support CPU mode")
        return request.param

X
Xiaohai Xu 已提交
186
    def test_compact_after_index_created(self, connect, collection, get_simple_index):
187
        '''
X
Xiaohai Xu 已提交
188
        target: test compact collection after index created
189 190 191 192
        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
193 194
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
195
        vectors = gen_vector(count, dim)
D
del-zhenwu 已提交
196
        status, ids = connect.insert(collection, vectors)
197
        assert status.OK()
X
Xiaohai Xu 已提交
198
        status = connect.flush([collection])
199
        assert status.OK()
X
Xiaohai Xu 已提交
200
        status = connect.create_index(collection, index_type, index_param) 
201
        assert status.OK()
X
Xiaohai Xu 已提交
202
        status = connect.flush([collection])
203
        assert status.OK()
X
Xiaohai Xu 已提交
204
        # get collection info before compact
D
del-zhenwu 已提交
205
        status, info = connect.get_collection_stats(collection)
206
        assert status.OK()
207 208
        size_before = info["partitions"][0]["segments"][0]["data_size"]
        logging.getLogger().info(info["partitions"])
209
        delete_ids = [ids[0], ids[-1]]
D
del-zhenwu 已提交
210
        status = connect.delete_entity_by_id(collection, delete_ids)
211
        assert status.OK()
X
Xiaohai Xu 已提交
212
        status = connect.flush([collection])
213
        assert status.OK()
X
Xiaohai Xu 已提交
214
        status = connect.compact(collection)
215
        assert status.OK()
X
Xiaohai Xu 已提交
216
        # get collection info after compact
D
del-zhenwu 已提交
217
        status, info = connect.get_collection_stats(collection)
218
        assert status.OK()
219 220
        logging.getLogger().info(info["partitions"])
        size_after = info["partitions"][0]["segments"][0]["data_size"]
221 222 223
        assert(size_before > size_after)
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
224
    def test_add_vector_and_compact_twice(self, connect, collection):
225 226
        '''
        target: test add vector and compact twice
X
Xiaohai Xu 已提交
227
        method: add vector and compact collection twice
228 229 230
        expected: status ok, data size no change
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
231
        status, ids = connect.insert(collection, vector)
232
        assert status.OK()
X
Xiaohai Xu 已提交
233
        status = connect.flush([collection])
234
        assert status.OK()
X
Xiaohai Xu 已提交
235
        # get collection info before compact
D
del-zhenwu 已提交
236
        status, info = connect.get_collection_stats(collection)
237
        assert status.OK()
238
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
239
        status = connect.compact(collection)
240
        assert status.OK()
X
Xiaohai Xu 已提交
241
        status = connect.flush([collection])
242
        assert status.OK()
X
Xiaohai Xu 已提交
243
        # get collection info after compact
D
del-zhenwu 已提交
244
        status, info = connect.get_collection_stats(collection)
245
        assert status.OK()
246
        size_after = info["partitions"][0]["segments"][0]["data_size"]
247
        assert(size_before == size_after)
X
Xiaohai Xu 已提交
248
        status = connect.compact(collection)
249
        assert status.OK()
X
Xiaohai Xu 已提交
250
        # get collection info after compact twice
D
del-zhenwu 已提交
251
        status, info = connect.get_collection_stats(collection)
252
        assert status.OK()
253
        size_after_twice = info["partitions"][0]["segments"][0]["data_size"]
254 255 256
        assert(size_after == size_after_twice)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
257
    def test_insert_delete_part_and_compact_twice(self, connect, collection):
258 259
        '''
        target: test add vectors, delete part of them and compact twice
X
Xiaohai Xu 已提交
260
        method: add vectors, delete part and compact collection twice
261 262 263
        expected: status ok, data size smaller after first compact, no change after second
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
264
        status, ids = connect.insert(collection, vectors)
265
        assert status.OK()
X
Xiaohai Xu 已提交
266
        status = connect.flush([collection])
267 268
        assert status.OK()
        delete_ids = [ids[0], ids[-1]]
D
del-zhenwu 已提交
269
        status = connect.delete_entity_by_id(collection, delete_ids)
270
        assert status.OK()
X
Xiaohai Xu 已提交
271
        status = connect.flush([collection])
272
        assert status.OK()
X
Xiaohai Xu 已提交
273
        # get collection info before compact
D
del-zhenwu 已提交
274
        status, info = connect.get_collection_stats(collection)
275
        assert status.OK()
276
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
277
        status = connect.compact(collection)
278
        assert status.OK()
X
Xiaohai Xu 已提交
279
        # get collection info after compact
D
del-zhenwu 已提交
280
        status, info = connect.get_collection_stats(collection)
281
        assert status.OK()
282
        size_after = info["partitions"][0]["segments"][0]["data_size"]
G
groot 已提交
283
        assert(size_before >= size_after)
X
Xiaohai Xu 已提交
284
        status = connect.compact(collection)
285
        assert status.OK()
X
Xiaohai Xu 已提交
286
        # get collection info after compact twice
D
del-zhenwu 已提交
287
        status, info = connect.get_collection_stats(collection)
288
        assert status.OK()
289
        size_after_twice = info["partitions"][0]["segments"][0]["data_size"]
290 291 292
        assert(size_after == size_after_twice)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
293
    def test_compact_multi_collections(self, connect):
294
        '''
X
Xiaohai Xu 已提交
295 296
        target: test compact works or not with multiple collections
        method: create 50 collections, add vectors into them and compact in turn
297 298 299
        expected: status ok
        '''
        nq = 100
X
Xiaohai Xu 已提交
300
        num_collections = 50
301
        vectors = gen_vectors(nq, dim)
X
Xiaohai Xu 已提交
302 303 304 305 306
        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,
307 308 309
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
310
            connect.create_collection(param)
311
        time.sleep(6)
X
Xiaohai Xu 已提交
312
        for i in range(num_collections):
D
del-zhenwu 已提交
313
            status, ids = connect.insert(collection_name=collection_list[i], records=vectors)
314
            assert status.OK()
X
Xiaohai Xu 已提交
315
            status = connect.compact(collection_list[i])
316 317 318
            assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
319
    def test_add_vector_after_compact(self, connect, collection):
320 321 322 323 324 325
        '''
        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 已提交
326
        status, ids = connect.insert(collection, vectors)
327
        assert status.OK()
X
Xiaohai Xu 已提交
328
        status = connect.flush([collection])
329
        assert status.OK()
X
Xiaohai Xu 已提交
330
        # get collection info before compact
D
del-zhenwu 已提交
331
        status, info = connect.get_collection_stats(collection)
332
        assert status.OK()
333
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
334
        status = connect.compact(collection)
335
        assert status.OK()
X
Xiaohai Xu 已提交
336
        # get collection info after compact
D
del-zhenwu 已提交
337
        status, info = connect.get_collection_stats(collection)
338
        assert status.OK()
339
        size_after = info["partitions"][0]["segments"][0]["data_size"]
340 341
        assert(size_before == size_after)
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
342
        status, ids = connect.insert(collection, vector)
343 344 345
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
346
    def test_index_creation_after_compact(self, connect, collection, get_simple_index):
347 348 349 350 351 352
        '''
        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 已提交
353
        status, ids = connect.insert(collection, vectors)
354
        assert status.OK()
X
Xiaohai Xu 已提交
355
        status = connect.flush([collection])
356
        assert status.OK()
D
del-zhenwu 已提交
357
        status = connect.delete_entity_by_id(collection, ids[:10])
358
        assert status.OK()
X
Xiaohai Xu 已提交
359
        status = connect.flush([collection])
360
        assert status.OK()
361 362
        status = connect.compact(collection)
        assert status.OK()
363 364
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
365
        status = connect.create_index(collection, index_type, index_param) 
366
        assert status.OK()
D
del-zhenwu 已提交
367
        status, result = connect.get_index_info(collection)
X
Xiaohai Xu 已提交
368
        assert result._collection_name == collection
369
        assert result._index_type == index_type
370 371

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
372
    def test_delete_vectors_after_compact(self, connect, collection):
373 374 375 376 377 378
        '''
        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 已提交
379
        status, ids = connect.insert(collection, vectors)
380
        assert status.OK()
X
Xiaohai Xu 已提交
381
        status = connect.flush([collection])
382
        assert status.OK()
X
Xiaohai Xu 已提交
383
        status = connect.compact(collection)
384
        assert status.OK()
X
Xiaohai Xu 已提交
385
        status = connect.flush([collection])
386
        assert status.OK()
D
del-zhenwu 已提交
387
        status = connect.delete_entity_by_id(collection, ids)
388
        assert status.OK()
X
Xiaohai Xu 已提交
389
        status = connect.flush([collection])
390 391 392
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
393
    def test_search_after_compact(self, connect, collection):
394 395 396 397 398 399
        '''
        target: test search after compact
        method: after compact operation, search vector
        expected: status ok
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
400
        status, ids = connect.insert(collection, vectors)
401
        assert status.OK()
X
Xiaohai Xu 已提交
402
        status = connect.flush([collection])
403
        assert status.OK()
X
Xiaohai Xu 已提交
404
        status = connect.compact(collection)
405 406
        assert status.OK()
        query_vecs = [vectors[0]]
D
del-zhenwu 已提交
407
        status, res = connect.search(collection, top_k, query_records=query_vecs) 
408 409 410
        logging.getLogger().info(res)
        assert status.OK()

411 412
    # TODO: enable
    def _test_compact_server_crashed_recovery(self, connect, collection):
413 414
        '''
        target: test compact when server crashed unexpectedly and restarted
X
Xiaohai Xu 已提交
415
        method: add vectors, delete and compact collection; server stopped and restarted during compact
416 417 418
        expected: status ok, request recovered
        '''
        vectors = gen_vector(nb * 100, dim)
D
del-zhenwu 已提交
419
        status, ids = connect.insert(collection, vectors)
420
        assert status.OK()
X
Xiaohai Xu 已提交
421
        status = connect.flush([collection])
422 423
        assert status.OK()
        delete_ids = ids[0:1000]
D
del-zhenwu 已提交
424
        status = connect.delete_entity_by_id(collection, delete_ids)
425
        assert status.OK()
X
Xiaohai Xu 已提交
426
        status = connect.flush([collection])
427 428 429
        assert status.OK()
        # start to compact, kill and restart server
        logging.getLogger().info("compact starting...")
X
Xiaohai Xu 已提交
430
        status = connect.compact(collection)
431 432
        # pdb.set_trace()
        assert status.OK()
X
Xiaohai Xu 已提交
433
        # get collection info after compact
D
del-zhenwu 已提交
434
        status, info = connect.get_collection_stats(collection)
435
        assert status.OK()
436
        assert info["partitions"][0].count == nb * 100 - 1000
437 438 439 440 441 442 443 444 445


class TestCompactJAC:
    """
    ******************************************************************
      The following cases are used to test `compact` function
    ******************************************************************
    """
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
446
    def test_add_vector_and_compact(self, connect, jac_collection):
447 448
        '''
        target: test add vector and compact 
X
Xiaohai Xu 已提交
449
        method: add vector and compact collection
450 451 452
        expected: status ok, vector added
        '''
        tmp, vector = gen_binary_vectors(1, dim)
D
del-zhenwu 已提交
453
        status, ids = connect.insert(jac_collection, vector)
454
        assert status.OK()
X
Xiaohai Xu 已提交
455
        status = connect.flush([jac_collection])
456
        assert status.OK()
X
Xiaohai Xu 已提交
457
        # get collection info before compact
D
del-zhenwu 已提交
458
        status, info = connect.get_collection_stats(jac_collection)
459
        assert status.OK()
460
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
461
        status = connect.compact(jac_collection)
462
        assert status.OK()
X
Xiaohai Xu 已提交
463
        # get collection info after compact
D
del-zhenwu 已提交
464
        status, info = connect.get_collection_stats(jac_collection)
465
        assert status.OK()
466
        size_after = info["partitions"][0]["segments"][0]["data_size"]
467 468 469
        assert(size_before == size_after)
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
470
    def test_insert_and_compact(self, connect, jac_collection):
471 472
        '''
        target: test add vectors and compact 
X
Xiaohai Xu 已提交
473
        method: add vectors and compact collection
474 475 476
        expected: status ok, vectors added
        '''
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
477
        status, ids = connect.insert(jac_collection, vectors)
478
        assert status.OK()
X
Xiaohai Xu 已提交
479
        status = connect.flush([jac_collection])
480
        assert status.OK()
X
Xiaohai Xu 已提交
481
        # get collection info before compact
D
del-zhenwu 已提交
482
        status, info = connect.get_collection_stats(jac_collection)
483
        assert status.OK()
484
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
485
        status = connect.compact(jac_collection)
486
        assert status.OK()
X
Xiaohai Xu 已提交
487
        # get collection info after compact
D
del-zhenwu 已提交
488
        status, info = connect.get_collection_stats(jac_collection)
489
        assert status.OK()
490
        size_after = info["partitions"][0]["segments"][0]["data_size"]
491 492 493
        assert(size_before == size_after)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
494
    def test_insert_delete_part_and_compact(self, connect, jac_collection):
495 496
        '''
        target: test add vectors, delete part of them and compact 
X
Xiaohai Xu 已提交
497
        method: add vectors, delete a few and compact collection
498 499 500
        expected: status ok, data size is smaller after compact
        '''
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
501
        status, ids = connect.insert(jac_collection, vectors)
502
        assert status.OK()
X
Xiaohai Xu 已提交
503
        status = connect.flush([jac_collection])
504 505
        assert status.OK()
        delete_ids = [ids[0], ids[-1]]
D
del-zhenwu 已提交
506
        status = connect.delete_entity_by_id(jac_collection, delete_ids)
507
        assert status.OK()
X
Xiaohai Xu 已提交
508
        status = connect.flush([jac_collection])
509
        assert status.OK()
X
Xiaohai Xu 已提交
510
        # get collection info before compact
D
del-zhenwu 已提交
511
        status, info = connect.get_collection_stats(jac_collection)
512
        assert status.OK()
513 514
        logging.getLogger().info(info["partitions"])
        size_before = info["partitions"][0]["segments"][0]["data_size"]
515
        logging.getLogger().info(size_before)
X
Xiaohai Xu 已提交
516
        status = connect.compact(jac_collection)
517
        assert status.OK()
X
Xiaohai Xu 已提交
518
        # get collection info after compact
D
del-zhenwu 已提交
519
        status, info = connect.get_collection_stats(jac_collection)
520
        assert status.OK()
521 522
        logging.getLogger().info(info["partitions"])
        size_after = info["partitions"][0]["segments"][0]["data_size"]
523
        logging.getLogger().info(size_after)
G
groot 已提交
524
        assert(size_before >= size_after)
525 526
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
527
    def test_insert_delete_all_and_compact(self, connect, jac_collection):
528 529
        '''
        target: test add vectors, delete them and compact 
X
Xiaohai Xu 已提交
530 531
        method: add vectors, delete all and compact collection
        expected: status ok, no data size in collection info because collection is empty
532 533
        '''
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
534
        status, ids = connect.insert(jac_collection, vectors)
535
        assert status.OK()
X
Xiaohai Xu 已提交
536
        status = connect.flush([jac_collection])
537
        assert status.OK()
D
del-zhenwu 已提交
538
        status = connect.delete_entity_by_id(jac_collection, ids)
539
        assert status.OK()
X
Xiaohai Xu 已提交
540
        status = connect.flush([jac_collection])
541
        assert status.OK()
X
Xiaohai Xu 已提交
542
        # get collection info before compact
D
del-zhenwu 已提交
543
        status, info = connect.get_collection_stats(jac_collection)
544
        assert status.OK()
X
Xiaohai Xu 已提交
545
        status = connect.compact(jac_collection)
546
        assert status.OK()
X
Xiaohai Xu 已提交
547
        # get collection info after compact
D
del-zhenwu 已提交
548
        status, info = connect.get_collection_stats(jac_collection)
549
        assert status.OK()
550 551
        logging.getLogger().info(info["partitions"])
        assert not info["partitions"][0]["segments"]
552 553
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
554
    def test_add_vector_and_compact_twice(self, connect, jac_collection):
555 556
        '''
        target: test add vector and compact twice
X
Xiaohai Xu 已提交
557
        method: add vector and compact collection twice
558 559 560
        expected: status ok
        '''
        tmp, vector = gen_binary_vectors(1, dim)
D
del-zhenwu 已提交
561
        status, ids = connect.insert(jac_collection, vector)
562
        assert status.OK()
X
Xiaohai Xu 已提交
563
        status = connect.flush([jac_collection])
564
        assert status.OK()
X
Xiaohai Xu 已提交
565
        # get collection info before compact
D
del-zhenwu 已提交
566
        status, info = connect.get_collection_stats(jac_collection)
567
        assert status.OK()
568
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
569
        status = connect.compact(jac_collection)
570
        assert status.OK()
X
Xiaohai Xu 已提交
571
        # get collection info after compact
D
del-zhenwu 已提交
572
        status, info = connect.get_collection_stats(jac_collection)
573
        assert status.OK()
574
        size_after = info["partitions"][0]["segments"][0]["data_size"]
575
        assert(size_before == size_after)
X
Xiaohai Xu 已提交
576
        status = connect.compact(jac_collection)
577
        assert status.OK()
X
Xiaohai Xu 已提交
578
        # get collection info after compact twice
D
del-zhenwu 已提交
579
        status, info = connect.get_collection_stats(jac_collection)
580
        assert status.OK()
581
        size_after_twice = info["partitions"][0]["segments"][0]["data_size"]
582 583 584
        assert(size_after == size_after_twice)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
585
    def test_insert_delete_part_and_compact_twice(self, connect, jac_collection):
586 587
        '''
        target: test add vectors, delete part of them and compact twice
X
Xiaohai Xu 已提交
588
        method: add vectors, delete part and compact collection twice
589 590 591
        expected: status ok, data size smaller after first compact, no change after second
        '''
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
592
        status, ids = connect.insert(jac_collection, vectors)
593
        assert status.OK()
X
Xiaohai Xu 已提交
594
        status = connect.flush([jac_collection])
595 596
        assert status.OK()
        delete_ids = [ids[0], ids[-1]]
D
del-zhenwu 已提交
597
        status = connect.delete_entity_by_id(jac_collection, delete_ids)
598
        assert status.OK()
X
Xiaohai Xu 已提交
599
        status = connect.flush([jac_collection])
600
        assert status.OK()
X
Xiaohai Xu 已提交
601
        # get collection info before compact
D
del-zhenwu 已提交
602
        status, info = connect.get_collection_stats(jac_collection)
603
        assert status.OK()
604
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
605
        status = connect.compact(jac_collection)
606
        assert status.OK()
X
Xiaohai Xu 已提交
607
        # get collection info after compact
D
del-zhenwu 已提交
608
        status, info = connect.get_collection_stats(jac_collection)
609
        assert status.OK()
610
        size_after = info["partitions"][0]["segments"][0]["data_size"]
G
groot 已提交
611
        assert(size_before >= size_after)
X
Xiaohai Xu 已提交
612
        status = connect.compact(jac_collection)
613
        assert status.OK()
X
Xiaohai Xu 已提交
614
        # get collection info after compact twice
D
del-zhenwu 已提交
615
        status, info = connect.get_collection_stats(jac_collection)
616
        assert status.OK()
617
        size_after_twice = info["partitions"][0]["segments"][0]["data_size"]
618 619 620
        assert(size_after == size_after_twice)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
621
    def test_compact_multi_collections(self, connect):
622
        '''
X
Xiaohai Xu 已提交
623 624
        target: test compact works or not with multiple collections
        method: create 50 collections, add vectors into them and compact in turn
625 626 627
        expected: status ok
        '''
        nq = 100
X
Xiaohai Xu 已提交
628
        num_collections = 10
629
        tmp, vectors = gen_binary_vectors(nq, dim)
X
Xiaohai Xu 已提交
630 631 632 633 634
        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,
635 636 637
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.JACCARD}
X
Xiaohai Xu 已提交
638 639
            connect.create_collection(param)
        for i in range(num_collections):
D
del-zhenwu 已提交
640
            status, ids = connect.insert(collection_name=collection_list[i], records=vectors)
641
            assert status.OK()
D
del-zhenwu 已提交
642
            status = connect.delete_entity_by_id(collection_list[i], [ids[0], ids[-1]])
643
            assert status.OK()
X
Xiaohai Xu 已提交
644
            status = connect.flush([collection_list[i]])
645
            assert status.OK()
X
Xiaohai Xu 已提交
646
            status = connect.compact(collection_list[i])
647
            assert status.OK()
648 649
            status = connect.drop_collection(collection_list[i])
            assert status.OK()
650 651

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
652
    def test_add_vector_after_compact(self, connect, jac_collection):
653 654 655 656 657 658
        '''
        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 已提交
659
        status, ids = connect.insert(jac_collection, vectors)
660
        assert status.OK()
X
Xiaohai Xu 已提交
661
        status = connect.flush([jac_collection])
662
        assert status.OK()
X
Xiaohai Xu 已提交
663
        # get collection info before compact
D
del-zhenwu 已提交
664
        status, info = connect.get_collection_stats(jac_collection)
665
        assert status.OK()
666
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
667
        status = connect.compact(jac_collection)
668
        assert status.OK()
X
Xiaohai Xu 已提交
669
        # get collection info after compact
D
del-zhenwu 已提交
670
        status, info = connect.get_collection_stats(jac_collection)
671
        assert status.OK()
672
        size_after = info["partitions"][0]["segments"][0]["data_size"]
673 674
        assert(size_before == size_after)
        tmp, vector = gen_binary_vectors(1, dim)
D
del-zhenwu 已提交
675
        status, ids = connect.insert(jac_collection, vector)
676 677 678
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
679
    def test_delete_vectors_after_compact(self, connect, jac_collection):
680 681 682 683 684 685
        '''
        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 已提交
686
        status, ids = connect.insert(jac_collection, vectors)
687
        assert status.OK()
X
Xiaohai Xu 已提交
688
        status = connect.flush([jac_collection])
689
        assert status.OK()
X
Xiaohai Xu 已提交
690
        status = connect.compact(jac_collection)
691
        assert status.OK()
X
Xiaohai Xu 已提交
692
        status = connect.flush([jac_collection])
693
        assert status.OK()
D
del-zhenwu 已提交
694
        status = connect.delete_entity_by_id(jac_collection, ids)
695
        assert status.OK()
X
Xiaohai Xu 已提交
696
        status = connect.flush([jac_collection])
697 698 699
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
700
    def test_search_after_compact(self, connect, jac_collection):
701 702 703 704 705 706
        '''
        target: test search after compact
        method: after compact operation, search vector
        expected: status ok
        '''
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
707
        status, ids = connect.insert(jac_collection, vectors)
708
        assert status.OK()
X
Xiaohai Xu 已提交
709
        status = connect.flush([jac_collection])
710
        assert status.OK()
X
Xiaohai Xu 已提交
711
        status = connect.compact(jac_collection)
712 713
        assert status.OK()
        query_vecs = [vectors[0]]
D
del-zhenwu 已提交
714
        status, res = connect.search(jac_collection, top_k, query_records=query_vecs) 
715 716 717 718 719 720 721 722 723 724 725
        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 已提交
726
    def test_add_vector_and_compact(self, connect, ip_collection):
727 728
        '''
        target: test add vector and compact 
X
Xiaohai Xu 已提交
729
        method: add vector and compact collection
730 731 732
        expected: status ok, vector added
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
733
        status, ids = connect.insert(ip_collection, vector)
734
        assert status.OK()
X
Xiaohai Xu 已提交
735
        status = connect.flush([ip_collection])
736
        assert status.OK()
X
Xiaohai Xu 已提交
737
        # get collection info before compact
D
del-zhenwu 已提交
738
        status, info = connect.get_collection_stats(ip_collection)
739
        assert status.OK()
740
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
741
        status = connect.compact(ip_collection)
742
        assert status.OK()
X
Xiaohai Xu 已提交
743
        status = connect.flush([ip_collection])
744
        assert status.OK()
X
Xiaohai Xu 已提交
745
        # get collection info after compact
D
del-zhenwu 已提交
746
        status, info = connect.get_collection_stats(ip_collection)
747
        assert status.OK()
748
        size_after = info["partitions"][0]["segments"][0]["data_size"]
749 750 751
        assert(size_before == size_after)
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
752
    def test_insert_and_compact(self, connect, ip_collection):
753 754
        '''
        target: test add vectors and compact 
X
Xiaohai Xu 已提交
755
        method: add vectors and compact collection
756 757 758
        expected: status ok, vectors added
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
759
        status, ids = connect.insert(ip_collection, vectors)
760
        assert status.OK()
X
Xiaohai Xu 已提交
761
        status = connect.flush([ip_collection])
762
        assert status.OK()
X
Xiaohai Xu 已提交
763
        # get collection info before compact
D
del-zhenwu 已提交
764
        status, info = connect.get_collection_stats(ip_collection)
765
        assert status.OK()
766
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
767
        status = connect.compact(ip_collection)
768
        assert status.OK()
X
Xiaohai Xu 已提交
769
        # get collection info after compact
D
del-zhenwu 已提交
770
        status, info = connect.get_collection_stats(ip_collection)
771
        assert status.OK()
772
        size_after = info["partitions"][0]["segments"][0]["data_size"]
773 774 775
        assert(size_before == size_after)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
776
    def test_insert_delete_part_and_compact(self, connect, ip_collection):
777 778
        '''
        target: test add vectors, delete part of them and compact 
X
Xiaohai Xu 已提交
779
        method: add vectors, delete a few and compact collection
780 781 782
        expected: status ok, data size is smaller after compact
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
783
        status, ids = connect.insert(ip_collection, vectors)
784
        assert status.OK()
X
Xiaohai Xu 已提交
785
        status = connect.flush([ip_collection])
786 787
        assert status.OK()
        delete_ids = [ids[0], ids[-1]]
D
del-zhenwu 已提交
788
        status = connect.delete_entity_by_id(ip_collection, delete_ids)
789
        assert status.OK()
X
Xiaohai Xu 已提交
790
        status = connect.flush([ip_collection])
791
        assert status.OK()
X
Xiaohai Xu 已提交
792
        # get collection info before compact
D
del-zhenwu 已提交
793
        status, info = connect.get_collection_stats(ip_collection)
794
        assert status.OK()
795 796
        logging.getLogger().info(info["partitions"])
        size_before = info["partitions"][0]["segments"][0]["data_size"]
797
        logging.getLogger().info(size_before)
X
Xiaohai Xu 已提交
798
        status = connect.compact(ip_collection)
799
        assert status.OK()
X
Xiaohai Xu 已提交
800
        # get collection info after compact
D
del-zhenwu 已提交
801
        status, info = connect.get_collection_stats(ip_collection)
802
        assert status.OK()
803 804
        logging.getLogger().info(info["partitions"])
        size_after = info["partitions"][0]["segments"][0]["data_size"]
805
        logging.getLogger().info(size_after)
G
groot 已提交
806
        assert(size_before >= size_after)
807 808
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
809
    def test_insert_delete_all_and_compact(self, connect, ip_collection):
810 811
        '''
        target: test add vectors, delete them and compact 
X
Xiaohai Xu 已提交
812 813
        method: add vectors, delete all and compact collection
        expected: status ok, no data size in collection info because collection is empty
814 815
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
816
        status, ids = connect.insert(ip_collection, vectors)
817
        assert status.OK()
X
Xiaohai Xu 已提交
818
        status = connect.flush([ip_collection])
819
        assert status.OK()
D
del-zhenwu 已提交
820
        status = connect.delete_entity_by_id(ip_collection, ids)
821
        assert status.OK()
X
Xiaohai Xu 已提交
822
        status = connect.flush([ip_collection])
823
        assert status.OK()
X
Xiaohai Xu 已提交
824
        # get collection info before compact
D
del-zhenwu 已提交
825
        status, info = connect.get_collection_stats(ip_collection)
826
        assert status.OK()
X
Xiaohai Xu 已提交
827
        status = connect.compact(ip_collection)
828
        assert status.OK()
X
Xiaohai Xu 已提交
829
        # get collection info after compact
D
del-zhenwu 已提交
830
        status, info = connect.get_collection_stats(ip_collection)
831
        assert status.OK()
832 833
        logging.getLogger().info(info["partitions"])
        assert not info["partitions"][0]["segments"]
834 835
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
836
    def test_add_vector_and_compact_twice(self, connect, ip_collection):
837 838
        '''
        target: test add vector and compact twice
X
Xiaohai Xu 已提交
839
        method: add vector and compact collection twice
840 841 842
        expected: status ok
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
843
        status, ids = connect.insert(ip_collection, vector)
844
        assert status.OK()
X
Xiaohai Xu 已提交
845
        status = connect.flush([ip_collection])
846
        assert status.OK()
X
Xiaohai Xu 已提交
847
        # get collection info before compact
D
del-zhenwu 已提交
848
        status, info = connect.get_collection_stats(ip_collection)
849
        assert status.OK()
850
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
851
        status = connect.compact(ip_collection)
852
        assert status.OK()
X
Xiaohai Xu 已提交
853
        # get collection info after compact
D
del-zhenwu 已提交
854
        status, info = connect.get_collection_stats(ip_collection)
855
        assert status.OK()
856
        size_after = info["partitions"][0]["segments"][0]["data_size"]
857
        assert(size_before == size_after)
X
Xiaohai Xu 已提交
858
        status = connect.compact(ip_collection)
859
        assert status.OK()
X
Xiaohai Xu 已提交
860
        # get collection info after compact twice
D
del-zhenwu 已提交
861
        status, info = connect.get_collection_stats(ip_collection)
862
        assert status.OK()
863
        size_after_twice = info["partitions"][0]["segments"][0]["data_size"]
864 865 866
        assert(size_after == size_after_twice)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
D
del-zhenwu 已提交
867
    def test_insert_delete_part_and_compact_twice(self, connect, ip_collection):
868 869
        '''
        target: test add vectors, delete part of them and compact twice
X
Xiaohai Xu 已提交
870
        method: add vectors, delete part and compact collection twice
871 872 873
        expected: status ok, data size smaller after first compact, no change after second
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
874
        status, ids = connect.insert(ip_collection, vectors)
875
        assert status.OK()
X
Xiaohai Xu 已提交
876
        status = connect.flush([ip_collection])
877 878
        assert status.OK()
        delete_ids = [ids[0], ids[-1]]
D
del-zhenwu 已提交
879
        status = connect.delete_entity_by_id(ip_collection, delete_ids)
880
        assert status.OK()
X
Xiaohai Xu 已提交
881
        status = connect.flush([ip_collection])
882
        assert status.OK()
X
Xiaohai Xu 已提交
883
        # get collection info before compact
D
del-zhenwu 已提交
884
        status, info = connect.get_collection_stats(ip_collection)
885
        assert status.OK()
886
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
887
        status = connect.compact(ip_collection)
888
        assert status.OK()
X
Xiaohai Xu 已提交
889
        status = connect.flush([ip_collection])
890
        assert status.OK()
X
Xiaohai Xu 已提交
891
        # get collection info after compact
D
del-zhenwu 已提交
892
        status, info = connect.get_collection_stats(ip_collection)
893
        assert status.OK()
894
        size_after = info["partitions"][0]["segments"][0]["data_size"]
G
groot 已提交
895
        assert(size_before >= size_after)
X
Xiaohai Xu 已提交
896
        status = connect.compact(ip_collection)
897
        assert status.OK()
X
Xiaohai Xu 已提交
898
        status = connect.flush([ip_collection])
899
        assert status.OK()
X
Xiaohai Xu 已提交
900
        # get collection info after compact twice
D
del-zhenwu 已提交
901
        status, info = connect.get_collection_stats(ip_collection)
902
        assert status.OK()
903
        size_after_twice = info["partitions"][0]["segments"][0]["data_size"]
904 905 906
        assert(size_after == size_after_twice)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
907
    def test_compact_multi_collections(self, connect):
908
        '''
X
Xiaohai Xu 已提交
909 910
        target: test compact works or not with multiple collections
        method: create 50 collections, add vectors into them and compact in turn
911 912 913
        expected: status ok
        '''
        nq = 100
X
Xiaohai Xu 已提交
914
        num_collections = 50
915
        vectors = gen_vectors(nq, dim)
X
Xiaohai Xu 已提交
916 917 918 919 920
        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,
921 922 923
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.IP}
X
Xiaohai Xu 已提交
924
            connect.create_collection(param)
925
        time.sleep(6)
X
Xiaohai Xu 已提交
926
        for i in range(num_collections):
D
del-zhenwu 已提交
927
            status, ids = connect.insert(collection_name=collection_list[i], records=vectors)
928
            assert status.OK()
X
Xiaohai Xu 已提交
929
            status = connect.compact(collection_list[i])
930 931 932
            assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
933
    def test_add_vector_after_compact(self, connect, ip_collection):
934 935 936 937 938 939
        '''
        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 已提交
940
        status, ids = connect.insert(ip_collection, vectors)
941
        assert status.OK()
X
Xiaohai Xu 已提交
942
        status = connect.flush([ip_collection])
943
        assert status.OK()
X
Xiaohai Xu 已提交
944
        # get collection info before compact
D
del-zhenwu 已提交
945
        status, info = connect.get_collection_stats(ip_collection)
946
        assert status.OK()
947
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
948
        status = connect.compact(ip_collection)
949
        assert status.OK()
X
Xiaohai Xu 已提交
950
        # get collection info after compact
D
del-zhenwu 已提交
951
        status, info = connect.get_collection_stats(ip_collection)
952
        assert status.OK()
953
        size_after = info["partitions"][0]["segments"][0]["data_size"]
954 955
        assert(size_before == size_after)
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
956
        status, ids = connect.insert(ip_collection, vector)
957 958 959
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
960
    def test_delete_vectors_after_compact(self, connect, ip_collection):
961 962 963 964 965 966
        '''
        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 已提交
967
        status, ids = connect.insert(ip_collection, vectors)
968
        assert status.OK()
X
Xiaohai Xu 已提交
969
        status = connect.flush([ip_collection])
970
        assert status.OK()
X
Xiaohai Xu 已提交
971
        status = connect.compact(ip_collection)
972
        assert status.OK()
D
del-zhenwu 已提交
973
        status = connect.delete_entity_by_id(ip_collection, ids)
974
        assert status.OK()
X
Xiaohai Xu 已提交
975
        status = connect.flush([ip_collection])
976 977 978
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
979
    def test_search_after_compact(self, connect, ip_collection):
980 981 982 983 984 985
        '''
        target: test search after compact
        method: after compact operation, search vector
        expected: status ok
        '''
        vectors = gen_vector(nb, dim)
D
del-zhenwu 已提交
986
        status, ids = connect.insert(ip_collection, vectors)
987
        assert status.OK()
X
Xiaohai Xu 已提交
988
        status = connect.flush([ip_collection])
989
        assert status.OK()
X
Xiaohai Xu 已提交
990
        status = connect.compact(ip_collection)
991 992
        assert status.OK()
        query_vecs = [vectors[0]]
D
del-zhenwu 已提交
993
        status, res = connect.search(ip_collection, top_k, query_records=query_vecs) 
994 995
        logging.getLogger().info(res)
        assert status.OK()