test_compact.py 41.2 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)
X
Xiaohai Xu 已提交
73
        status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
91
    def test_add_vectors_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)
X
Xiaohai Xu 已提交
98
        status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
115
    def test_add_vectors_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)
X
Xiaohai Xu 已提交
122
        status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
148
    def test_add_vectors_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)
X
Xiaohai Xu 已提交
155
        status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
196
        status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
231
        status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
257
    def test_add_vectors_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)
X
Xiaohai Xu 已提交
264
        status, ids = connect.add_vectors(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 313
        for i in range(num_collections):
            status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
326
        status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
342
        status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
353
        status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
379
        status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
400
        status, ids = connect.add_vectors(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]]
X
Xiaohai Xu 已提交
407
        status, res = connect.search_vectors(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)
X
Xiaohai Xu 已提交
419
        status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
453
        status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
470
    def test_add_vectors_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)
X
Xiaohai Xu 已提交
477
        status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
494
    def test_add_vectors_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)
X
Xiaohai Xu 已提交
501
        status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
527
    def test_add_vectors_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)
X
Xiaohai Xu 已提交
534
        status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
561
        status, ids = connect.add_vectors(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)
X
Xiaohai Xu 已提交
585
    def test_add_vectors_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)
X
Xiaohai Xu 已提交
592
        status, ids = connect.add_vectors(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
            connect.create_collection(param)
639
        time.sleep(6)
X
Xiaohai Xu 已提交
640 641
        for i in range(num_collections):
            status, ids = connect.add_vectors(collection_name=collection_list[i], records=vectors)
642
            assert status.OK()
D
del-zhenwu 已提交
643
            status = connect.delete_entity_by_id(collection_list[i], [ids[0], ids[-1]])
644
            assert status.OK()
X
Xiaohai Xu 已提交
645
            status = connect.flush([collection_list[i]])
646
            assert status.OK()
X
Xiaohai Xu 已提交
647
            status = connect.compact(collection_list[i])
648 649 650
            assert status.OK()

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

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
678
    def test_delete_vectors_after_compact(self, connect, jac_collection):
679 680 681 682 683 684
        '''
        target: test delete vectors after compact
        method: after compact operation, delete vectors
        expected: status ok, vectors deleted
        '''
        tmp, vectors = gen_binary_vectors(nb, dim)
X
Xiaohai Xu 已提交
685
        status, ids = connect.add_vectors(jac_collection, vectors)
686
        assert status.OK()
X
Xiaohai Xu 已提交
687
        status = connect.flush([jac_collection])
688
        assert status.OK()
X
Xiaohai Xu 已提交
689
        status = connect.compact(jac_collection)
690
        assert status.OK()
X
Xiaohai Xu 已提交
691
        status = connect.flush([jac_collection])
692
        assert status.OK()
D
del-zhenwu 已提交
693
        status = connect.delete_entity_by_id(jac_collection, ids)
694
        assert status.OK()
X
Xiaohai Xu 已提交
695
        status = connect.flush([jac_collection])
696 697 698
        assert status.OK()

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

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

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

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

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
932
    def test_add_vector_after_compact(self, connect, ip_collection):
933 934 935 936 937 938
        '''
        target: test add vector after compact 
        method: after compact operation, add vector
        expected: status ok, vector added
        '''
        vectors = gen_vector(nb, dim)
X
Xiaohai Xu 已提交
939
        status, ids = connect.add_vectors(ip_collection, vectors)
940
        assert status.OK()
X
Xiaohai Xu 已提交
941
        status = connect.flush([ip_collection])
942
        assert status.OK()
X
Xiaohai Xu 已提交
943
        # get collection info before compact
D
del-zhenwu 已提交
944
        status, info = connect.get_collection_stats(ip_collection)
945
        assert status.OK()
946
        size_before = info["partitions"][0]["segments"][0]["data_size"]
X
Xiaohai Xu 已提交
947
        status = connect.compact(ip_collection)
948
        assert status.OK()
X
Xiaohai Xu 已提交
949
        # get collection info after compact
D
del-zhenwu 已提交
950
        status, info = connect.get_collection_stats(ip_collection)
951
        assert status.OK()
952
        size_after = info["partitions"][0]["segments"][0]["data_size"]
953 954
        assert(size_before == size_after)
        vector = gen_single_vector(dim)
X
Xiaohai Xu 已提交
955
        status, ids = connect.add_vectors(ip_collection, vector)
956 957 958
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
959
    def test_delete_vectors_after_compact(self, connect, ip_collection):
960 961 962 963 964 965
        '''
        target: test delete vectors after compact
        method: after compact operation, delete vectors
        expected: status ok, vectors deleted
        '''
        vectors = gen_vector(nb, dim)
X
Xiaohai Xu 已提交
966
        status, ids = connect.add_vectors(ip_collection, vectors)
967
        assert status.OK()
X
Xiaohai Xu 已提交
968
        status = connect.flush([ip_collection])
969
        assert status.OK()
X
Xiaohai Xu 已提交
970
        status = connect.compact(ip_collection)
971
        assert status.OK()
D
del-zhenwu 已提交
972
        status = connect.delete_entity_by_id(ip_collection, ids)
973
        assert status.OK()
X
Xiaohai Xu 已提交
974
        status = connect.flush([ip_collection])
975 976 977
        assert status.OK()

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