test_compact.py 42.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
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
COMPACT_TIMEOUT = 30
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 78
        # get collection info before compact
        status, info = connect.collection_info(collection)
79 80 81
        assert status.OK()
        logging.getLogger().info(info)
        size_before = info.partitions_stat[0].segments_stat[0].data_size
X
Xiaohai Xu 已提交
82
        status = connect.compact(collection)
83
        assert status.OK()
X
Xiaohai Xu 已提交
84
        status = connect.flush([collection])
85
        assert status.OK()
X
Xiaohai Xu 已提交
86 87
        # get collection info after compact
        status, info = connect.collection_info(collection)
88 89 90 91 92
        assert status.OK()
        size_after = info.partitions_stat[0].segments_stat[0].data_size
        assert(size_before == size_after)
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
93
    def test_add_vectors_and_compact(self, connect, collection):
94 95
        '''
        target: test add vectors and compact 
X
Xiaohai Xu 已提交
96
        method: add vectors and compact collection
97 98 99
        expected: status ok, vectors added
        '''
        vectors = gen_vector(nb, dim)
X
Xiaohai Xu 已提交
100
        status, ids = connect.add_vectors(collection, vectors)
101
        assert status.OK()
X
Xiaohai Xu 已提交
102
        status = connect.flush([collection])
103
        assert status.OK()
X
Xiaohai Xu 已提交
104 105
        # get collection info before compact
        status, info = connect.collection_info(collection)
106 107
        assert status.OK()
        size_before = info.partitions_stat[0].segments_stat[0].data_size
X
Xiaohai Xu 已提交
108
        status = connect.compact(collection)
109
        assert status.OK()
X
Xiaohai Xu 已提交
110
        status = connect.flush([collection])
111
        assert status.OK()
X
Xiaohai Xu 已提交
112 113
        # get collection info after compact
        status, info = connect.collection_info(collection)
114 115 116 117 118
        assert status.OK()
        size_after = info.partitions_stat[0].segments_stat[0].data_size
        assert(size_before == size_after)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
119
    def test_add_vectors_delete_part_and_compact(self, connect, collection):
120 121
        '''
        target: test add vectors, delete part of them and compact 
X
Xiaohai Xu 已提交
122
        method: add vectors, delete a few and compact collection
123 124 125
        expected: status ok, data size is smaller after compact
        '''
        vectors = gen_vector(nb, dim)
X
Xiaohai Xu 已提交
126
        status, ids = connect.add_vectors(collection, vectors)
127
        assert status.OK()
X
Xiaohai Xu 已提交
128
        status = connect.flush([collection])
129 130
        assert status.OK()
        delete_ids = [ids[0], ids[-1]]
X
Xiaohai Xu 已提交
131
        status = connect.delete_by_id(collection, delete_ids)
132
        assert status.OK()
X
Xiaohai Xu 已提交
133
        status = connect.flush([collection])
134
        assert status.OK()
X
Xiaohai Xu 已提交
135 136
        # get collection info before compact
        status, info = connect.collection_info(collection)
137 138 139 140
        assert status.OK()
        logging.getLogger().info(info.partitions_stat)
        size_before = info.partitions_stat[0].segments_stat[0].data_size
        logging.getLogger().info(size_before)
X
Xiaohai Xu 已提交
141
        status = connect.compact(collection)
142
        assert status.OK()
X
Xiaohai Xu 已提交
143
        status = connect.flush([collection])
144
        assert status.OK()
X
Xiaohai Xu 已提交
145 146
        # get collection info after compact
        status, info = connect.collection_info(collection)
147 148 149 150 151 152 153
        assert status.OK()
        logging.getLogger().info(info.partitions_stat)
        size_after = info.partitions_stat[0].segments_stat[0].data_size
        logging.getLogger().info(size_after)
        assert(size_before > size_after)
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
154
    def test_add_vectors_delete_all_and_compact(self, connect, collection):
155 156
        '''
        target: test add vectors, delete them and compact 
X
Xiaohai Xu 已提交
157 158
        method: add vectors, delete all and compact collection
        expected: status ok, no data size in collection info because collection is empty
159 160
        '''
        vectors = gen_vector(nb, dim)
X
Xiaohai Xu 已提交
161
        status, ids = connect.add_vectors(collection, vectors)
162
        assert status.OK()
X
Xiaohai Xu 已提交
163
        status = connect.flush([collection])
164
        assert status.OK()
X
Xiaohai Xu 已提交
165
        status = connect.delete_by_id(collection, ids)
166
        assert status.OK()
X
Xiaohai Xu 已提交
167
        status = connect.flush([collection])
168
        assert status.OK()
X
Xiaohai Xu 已提交
169 170
        # get collection info before compact
        status, info = connect.collection_info(collection)
171
        assert status.OK()
X
Xiaohai Xu 已提交
172
        status = connect.compact(collection)
173
        assert status.OK()
X
Xiaohai Xu 已提交
174
        status = connect.flush([collection])
175
        assert status.OK()
X
Xiaohai Xu 已提交
176 177
        # get collection info after compact
        status, info = connect.collection_info(collection)
178 179 180 181 182 183
        assert status.OK()
        logging.getLogger().info(info.partitions_stat)
        assert(len(info.partitions_stat[0].segments_stat) == 0)

    @pytest.fixture(
        scope="function",
184
        params=gen_simple_index()
185
    )
186
    def get_simple_index(self, request, connect):
187 188 189 190 191 192 193
        if str(connect._cmd("mode")[1]) == "CPU":
            if request.param["index_type"] not in [IndexType.IVF_SQ8, IndexType.IVFLAT, IndexType.FLAT]:
                pytest.skip("Only support index_type: flat/ivf_flat/ivf_sq8")
        else:
            pytest.skip("Only support CPU mode")
        return request.param

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

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
269
    def test_add_vectors_delete_part_and_compact_twice(self, connect, collection):
270 271
        '''
        target: test add vectors, delete part of them and compact twice
X
Xiaohai Xu 已提交
272
        method: add vectors, delete part and compact collection twice
273 274 275
        expected: status ok, data size smaller after first compact, no change after second
        '''
        vectors = gen_vector(nb, dim)
X
Xiaohai Xu 已提交
276
        status, ids = connect.add_vectors(collection, vectors)
277
        assert status.OK()
X
Xiaohai Xu 已提交
278
        status = connect.flush([collection])
279 280
        assert status.OK()
        delete_ids = [ids[0], ids[-1]]
X
Xiaohai Xu 已提交
281
        status = connect.delete_by_id(collection, delete_ids)
282
        assert status.OK()
X
Xiaohai Xu 已提交
283
        status = connect.flush([collection])
284
        assert status.OK()
X
Xiaohai Xu 已提交
285 286
        # get collection info before compact
        status, info = connect.collection_info(collection)
287 288
        assert status.OK()
        size_before = info.partitions_stat[0].segments_stat[0].data_size
X
Xiaohai Xu 已提交
289
        status = connect.compact(collection)
290
        assert status.OK()
X
Xiaohai Xu 已提交
291
        status = connect.flush([collection])
292
        assert status.OK()
X
Xiaohai Xu 已提交
293 294
        # get collection info after compact
        status, info = connect.collection_info(collection)
295 296 297
        assert status.OK()
        size_after = info.partitions_stat[0].segments_stat[0].data_size
        assert(size_before > size_after)
X
Xiaohai Xu 已提交
298
        status = connect.compact(collection)
299
        assert status.OK()
X
Xiaohai Xu 已提交
300
        status = connect.flush([collection])
301
        assert status.OK()
X
Xiaohai Xu 已提交
302 303
        # get collection info after compact twice
        status, info = connect.collection_info(collection)
304 305 306 307 308
        assert status.OK()
        size_after_twice = info.partitions_stat[0].segments_stat[0].data_size
        assert(size_after == size_after_twice)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
309
    def test_compact_multi_collections(self, connect):
310
        '''
X
Xiaohai Xu 已提交
311 312
        target: test compact works or not with multiple collections
        method: create 50 collections, add vectors into them and compact in turn
313 314 315
        expected: status ok
        '''
        nq = 100
X
Xiaohai Xu 已提交
316
        num_collections = 50
317
        vectors = gen_vectors(nq, dim)
X
Xiaohai Xu 已提交
318 319 320 321 322
        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,
323 324 325
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
326
            connect.create_collection(param)
327
        time.sleep(6)
X
Xiaohai Xu 已提交
328 329
        for i in range(num_collections):
            status, ids = connect.add_vectors(collection_name=collection_list[i], records=vectors)
330
            assert status.OK()
X
Xiaohai Xu 已提交
331
            status = connect.compact(collection_list[i])
332 333 334
            assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
335
    def test_add_vector_after_compact(self, connect, collection):
336 337 338 339 340 341
        '''
        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 已提交
342
        status, ids = connect.add_vectors(collection, vectors)
343
        assert status.OK()
X
Xiaohai Xu 已提交
344
        status = connect.flush([collection])
345
        assert status.OK()
X
Xiaohai Xu 已提交
346 347
        # get collection info before compact
        status, info = connect.collection_info(collection)
348 349
        assert status.OK()
        size_before = info.partitions_stat[0].segments_stat[0].data_size
X
Xiaohai Xu 已提交
350
        status = connect.compact(collection)
351
        assert status.OK()
X
Xiaohai Xu 已提交
352
        status = connect.flush([collection])
353
        assert status.OK()
X
Xiaohai Xu 已提交
354 355
        # get collection info after compact
        status, info = connect.collection_info(collection)
356 357 358 359
        assert status.OK()
        size_after = info.partitions_stat[0].segments_stat[0].data_size
        assert(size_before == size_after)
        vector = gen_single_vector(dim)
X
Xiaohai Xu 已提交
360
        status, ids = connect.add_vectors(collection, vector)
361 362 363
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
364
    def test_index_creation_after_compact(self, connect, collection, get_simple_index):
365 366 367 368 369 370
        '''
        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 已提交
371
        status, ids = connect.add_vectors(collection, vectors)
372
        assert status.OK()
X
Xiaohai Xu 已提交
373
        status = connect.flush([collection])
374
        assert status.OK()
X
Xiaohai Xu 已提交
375
        status = connect.compact(collection)
376
        assert status.OK()
X
Xiaohai Xu 已提交
377
        status = connect.flush([collection])
378
        assert status.OK()
379 380
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
381
        status = connect.create_index(collection, index_type, index_param) 
382
        assert status.OK()
X
Xiaohai Xu 已提交
383 384
        status, result = connect.describe_index(collection)
        assert result._collection_name == collection
385
        assert result._index_type == index_type
386 387

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
388
    def test_delete_vectors_after_compact(self, connect, collection):
389 390 391 392 393 394
        '''
        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 已提交
395
        status, ids = connect.add_vectors(collection, vectors)
396
        assert status.OK()
X
Xiaohai Xu 已提交
397
        status = connect.flush([collection])
398
        assert status.OK()
X
Xiaohai Xu 已提交
399
        status = connect.compact(collection)
400
        assert status.OK()
X
Xiaohai Xu 已提交
401
        status = connect.flush([collection])
402
        assert status.OK()
X
Xiaohai Xu 已提交
403
        status = connect.delete_by_id(collection, ids)
404
        assert status.OK()
X
Xiaohai Xu 已提交
405
        status = connect.flush([collection])
406 407 408
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
409
    def test_search_after_compact(self, connect, collection):
410 411 412 413 414 415
        '''
        target: test search after compact
        method: after compact operation, search vector
        expected: status ok
        '''
        vectors = gen_vector(nb, dim)
X
Xiaohai Xu 已提交
416
        status, ids = connect.add_vectors(collection, vectors)
417
        assert status.OK()
X
Xiaohai Xu 已提交
418
        status = connect.flush([collection])
419
        assert status.OK()
X
Xiaohai Xu 已提交
420
        status = connect.compact(collection)
421
        assert status.OK()
X
Xiaohai Xu 已提交
422
        status = connect.flush([collection])
423 424
        assert status.OK()
        query_vecs = [vectors[0]]
X
Xiaohai Xu 已提交
425
        status, res = connect.search_vectors(collection, top_k, query_records=query_vecs) 
426 427 428
        logging.getLogger().info(res)
        assert status.OK()

X
Xiaohai Xu 已提交
429
    def test_compact_server_crashed_recovery(self, connect, collection):
430 431
        '''
        target: test compact when server crashed unexpectedly and restarted
X
Xiaohai Xu 已提交
432
        method: add vectors, delete and compact collection; server stopped and restarted during compact
433 434 435
        expected: status ok, request recovered
        '''
        vectors = gen_vector(nb * 100, dim)
X
Xiaohai Xu 已提交
436
        status, ids = connect.add_vectors(collection, vectors)
437
        assert status.OK()
X
Xiaohai Xu 已提交
438
        status = connect.flush([collection])
439 440
        assert status.OK()
        delete_ids = ids[0:1000]
X
Xiaohai Xu 已提交
441
        status = connect.delete_by_id(collection, delete_ids)
442
        assert status.OK()
X
Xiaohai Xu 已提交
443
        status = connect.flush([collection])
444 445 446
        assert status.OK()
        # start to compact, kill and restart server
        logging.getLogger().info("compact starting...")
X
Xiaohai Xu 已提交
447
        status = connect.compact(collection)
448 449
        # pdb.set_trace()
        assert status.OK()
X
Xiaohai Xu 已提交
450
        status = connect.flush([collection])
451
        assert status.OK()
X
Xiaohai Xu 已提交
452 453
        # get collection info after compact
        status, info = connect.collection_info(collection)
454 455 456 457 458 459 460 461 462 463 464
        assert status.OK()
        assert info.partitions_stat[0].count == nb * 100 - 1000


class TestCompactJAC:
    """
    ******************************************************************
      The following cases are used to test `compact` function
    ******************************************************************
    """
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
465
    def test_add_vector_and_compact(self, connect, jac_collection):
466 467
        '''
        target: test add vector and compact 
X
Xiaohai Xu 已提交
468
        method: add vector and compact collection
469 470 471
        expected: status ok, vector added
        '''
        tmp, vector = gen_binary_vectors(1, dim)
X
Xiaohai Xu 已提交
472
        status, ids = connect.add_vectors(jac_collection, vector)
473
        assert status.OK()
X
Xiaohai Xu 已提交
474
        status = connect.flush([jac_collection])
475
        assert status.OK()
X
Xiaohai Xu 已提交
476 477
        # get collection info before compact
        status, info = connect.collection_info(jac_collection)
478 479
        assert status.OK()
        size_before = info.partitions_stat[0].segments_stat[0].data_size
X
Xiaohai Xu 已提交
480
        status = connect.compact(jac_collection)
481
        assert status.OK()
X
Xiaohai Xu 已提交
482
        status = connect.flush([jac_collection])
483
        assert status.OK()
X
Xiaohai Xu 已提交
484 485
        # get collection info after compact
        status, info = connect.collection_info(jac_collection)
486 487 488 489 490
        assert status.OK()
        size_after = info.partitions_stat[0].segments_stat[0].data_size
        assert(size_before == size_after)
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
491
    def test_add_vectors_and_compact(self, connect, jac_collection):
492 493
        '''
        target: test add vectors and compact 
X
Xiaohai Xu 已提交
494
        method: add vectors and compact collection
495 496 497
        expected: status ok, vectors added
        '''
        tmp, vectors = gen_binary_vectors(nb, dim)
X
Xiaohai Xu 已提交
498
        status, ids = connect.add_vectors(jac_collection, vectors)
499
        assert status.OK()
X
Xiaohai Xu 已提交
500
        status = connect.flush([jac_collection])
501
        assert status.OK()
X
Xiaohai Xu 已提交
502 503
        # get collection info before compact
        status, info = connect.collection_info(jac_collection)
504 505
        assert status.OK()
        size_before = info.partitions_stat[0].segments_stat[0].data_size
X
Xiaohai Xu 已提交
506
        status = connect.compact(jac_collection)
507
        assert status.OK()
X
Xiaohai Xu 已提交
508
        status = connect.flush([jac_collection])
509
        assert status.OK()
X
Xiaohai Xu 已提交
510 511
        # get collection info after compact
        status, info = connect.collection_info(jac_collection)
512 513 514 515 516
        assert status.OK()
        size_after = info.partitions_stat[0].segments_stat[0].data_size
        assert(size_before == size_after)

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

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

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

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
686
    def test_add_vector_after_compact(self, connect, jac_collection):
687 688 689 690 691 692
        '''
        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 已提交
693
        status, ids = connect.add_vectors(jac_collection, vectors)
694
        assert status.OK()
X
Xiaohai Xu 已提交
695
        status = connect.flush([jac_collection])
696
        assert status.OK()
X
Xiaohai Xu 已提交
697 698
        # get collection info before compact
        status, info = connect.collection_info(jac_collection)
699 700
        assert status.OK()
        size_before = info.partitions_stat[0].segments_stat[0].data_size
X
Xiaohai Xu 已提交
701
        status = connect.compact(jac_collection)
702
        assert status.OK()
X
Xiaohai Xu 已提交
703
        status = connect.flush([jac_collection])
704
        assert status.OK()
X
Xiaohai Xu 已提交
705 706
        # get collection info after compact
        status, info = connect.collection_info(jac_collection)
707 708 709 710
        assert status.OK()
        size_after = info.partitions_stat[0].segments_stat[0].data_size
        assert(size_before == size_after)
        tmp, vector = gen_binary_vectors(1, dim)
X
Xiaohai Xu 已提交
711
        status, ids = connect.add_vectors(jac_collection, vector)
712 713 714
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
715
    def test_delete_vectors_after_compact(self, connect, jac_collection):
716 717 718 719 720 721
        '''
        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 已提交
722
        status, ids = connect.add_vectors(jac_collection, vectors)
723
        assert status.OK()
X
Xiaohai Xu 已提交
724
        status = connect.flush([jac_collection])
725
        assert status.OK()
X
Xiaohai Xu 已提交
726
        status = connect.compact(jac_collection)
727
        assert status.OK()
X
Xiaohai Xu 已提交
728
        status = connect.flush([jac_collection])
729
        assert status.OK()
X
Xiaohai Xu 已提交
730
        status = connect.delete_by_id(jac_collection, ids)
731
        assert status.OK()
X
Xiaohai Xu 已提交
732
        status = connect.flush([jac_collection])
733 734 735
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
736
    def test_search_after_compact(self, connect, jac_collection):
737 738 739 740 741 742
        '''
        target: test search after compact
        method: after compact operation, search vector
        expected: status ok
        '''
        tmp, vectors = gen_binary_vectors(nb, dim)
X
Xiaohai Xu 已提交
743
        status, ids = connect.add_vectors(jac_collection, vectors)
744
        assert status.OK()
X
Xiaohai Xu 已提交
745
        status = connect.flush([jac_collection])
746
        assert status.OK()
X
Xiaohai Xu 已提交
747
        status = connect.compact(jac_collection)
748
        assert status.OK()
X
Xiaohai Xu 已提交
749
        status = connect.flush([jac_collection])
750 751
        assert status.OK()
        query_vecs = [vectors[0]]
X
Xiaohai Xu 已提交
752
        status, res = connect.search_vectors(jac_collection, top_k, query_records=query_vecs) 
753 754 755 756 757 758 759 760 761 762 763
        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 已提交
764
    def test_add_vector_and_compact(self, connect, ip_collection):
765 766
        '''
        target: test add vector and compact 
X
Xiaohai Xu 已提交
767
        method: add vector and compact collection
768 769 770
        expected: status ok, vector added
        '''
        vector = gen_single_vector(dim)
X
Xiaohai Xu 已提交
771
        status, ids = connect.add_vectors(ip_collection, vector)
772
        assert status.OK()
X
Xiaohai Xu 已提交
773
        status = connect.flush([ip_collection])
774
        assert status.OK()
X
Xiaohai Xu 已提交
775 776
        # get collection info before compact
        status, info = connect.collection_info(ip_collection)
777 778
        assert status.OK()
        size_before = info.partitions_stat[0].segments_stat[0].data_size
X
Xiaohai Xu 已提交
779
        status = connect.compact(ip_collection)
780
        assert status.OK()
X
Xiaohai Xu 已提交
781
        status = connect.flush([ip_collection])
782
        assert status.OK()
X
Xiaohai Xu 已提交
783 784
        # get collection info after compact
        status, info = connect.collection_info(ip_collection)
785 786 787 788 789
        assert status.OK()
        size_after = info.partitions_stat[0].segments_stat[0].data_size
        assert(size_before == size_after)
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
790
    def test_add_vectors_and_compact(self, connect, ip_collection):
791 792
        '''
        target: test add vectors and compact 
X
Xiaohai Xu 已提交
793
        method: add vectors and compact collection
794 795 796
        expected: status ok, vectors added
        '''
        vectors = gen_vector(nb, dim)
X
Xiaohai Xu 已提交
797
        status, ids = connect.add_vectors(ip_collection, vectors)
798
        assert status.OK()
X
Xiaohai Xu 已提交
799
        status = connect.flush([ip_collection])
800
        assert status.OK()
X
Xiaohai Xu 已提交
801 802
        # get collection info before compact
        status, info = connect.collection_info(ip_collection)
803 804
        assert status.OK()
        size_before = info.partitions_stat[0].segments_stat[0].data_size
X
Xiaohai Xu 已提交
805
        status = connect.compact(ip_collection)
806
        assert status.OK()
X
Xiaohai Xu 已提交
807
        status = connect.flush([ip_collection])
808
        assert status.OK()
X
Xiaohai Xu 已提交
809 810
        # get collection info after compact
        status, info = connect.collection_info(ip_collection)
811 812 813 814 815
        assert status.OK()
        size_after = info.partitions_stat[0].segments_stat[0].data_size
        assert(size_before == size_after)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
816
    def test_add_vectors_delete_part_and_compact(self, connect, ip_collection):
817 818
        '''
        target: test add vectors, delete part of them and compact 
X
Xiaohai Xu 已提交
819
        method: add vectors, delete a few and compact collection
820 821 822
        expected: status ok, data size is smaller after compact
        '''
        vectors = gen_vector(nb, dim)
X
Xiaohai Xu 已提交
823
        status, ids = connect.add_vectors(ip_collection, vectors)
824
        assert status.OK()
X
Xiaohai Xu 已提交
825
        status = connect.flush([ip_collection])
826 827
        assert status.OK()
        delete_ids = [ids[0], ids[-1]]
X
Xiaohai Xu 已提交
828
        status = connect.delete_by_id(ip_collection, delete_ids)
829
        assert status.OK()
X
Xiaohai Xu 已提交
830
        status = connect.flush([ip_collection])
831
        assert status.OK()
X
Xiaohai Xu 已提交
832 833
        # get collection info before compact
        status, info = connect.collection_info(ip_collection)
834 835 836 837
        assert status.OK()
        logging.getLogger().info(info.partitions_stat)
        size_before = info.partitions_stat[0].segments_stat[0].data_size
        logging.getLogger().info(size_before)
X
Xiaohai Xu 已提交
838
        status = connect.compact(ip_collection)
839
        assert status.OK()
X
Xiaohai Xu 已提交
840
        status = connect.flush([ip_collection])
841
        assert status.OK()
X
Xiaohai Xu 已提交
842 843
        # get collection info after compact
        status, info = connect.collection_info(ip_collection)
844 845 846 847 848 849 850
        assert status.OK()
        logging.getLogger().info(info.partitions_stat)
        size_after = info.partitions_stat[0].segments_stat[0].data_size
        logging.getLogger().info(size_after)
        assert(size_before > size_after)
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
851
    def test_add_vectors_delete_all_and_compact(self, connect, ip_collection):
852 853
        '''
        target: test add vectors, delete them and compact 
X
Xiaohai Xu 已提交
854 855
        method: add vectors, delete all and compact collection
        expected: status ok, no data size in collection info because collection is empty
856 857
        '''
        vectors = gen_vector(nb, dim)
X
Xiaohai Xu 已提交
858
        status, ids = connect.add_vectors(ip_collection, vectors)
859
        assert status.OK()
X
Xiaohai Xu 已提交
860
        status = connect.flush([ip_collection])
861
        assert status.OK()
X
Xiaohai Xu 已提交
862
        status = connect.delete_by_id(ip_collection, ids)
863
        assert status.OK()
X
Xiaohai Xu 已提交
864
        status = connect.flush([ip_collection])
865
        assert status.OK()
X
Xiaohai Xu 已提交
866 867
        # get collection info before compact
        status, info = connect.collection_info(ip_collection)
868
        assert status.OK()
X
Xiaohai Xu 已提交
869
        status = connect.compact(ip_collection)
870
        assert status.OK()
X
Xiaohai Xu 已提交
871
        status = connect.flush([ip_collection])
872
        assert status.OK()
X
Xiaohai Xu 已提交
873 874
        # get collection info after compact
        status, info = connect.collection_info(ip_collection)
875 876 877 878 879
        assert status.OK()
        logging.getLogger().info(info.partitions_stat)
        assert(len(info.partitions_stat[0].segments_stat) == 0)
    
    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
880
    def test_add_vector_and_compact_twice(self, connect, ip_collection):
881 882
        '''
        target: test add vector and compact twice
X
Xiaohai Xu 已提交
883
        method: add vector and compact collection twice
884 885 886
        expected: status ok
        '''
        vector = gen_single_vector(dim)
X
Xiaohai Xu 已提交
887
        status, ids = connect.add_vectors(ip_collection, vector)
888
        assert status.OK()
X
Xiaohai Xu 已提交
889
        status = connect.flush([ip_collection])
890
        assert status.OK()
X
Xiaohai Xu 已提交
891 892
        # get collection info before compact
        status, info = connect.collection_info(ip_collection)
893 894
        assert status.OK()
        size_before = info.partitions_stat[0].segments_stat[0].data_size
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 900
        # get collection info after compact
        status, info = connect.collection_info(ip_collection)
901 902 903
        assert status.OK()
        size_after = info.partitions_stat[0].segments_stat[0].data_size
        assert(size_before == size_after)
X
Xiaohai Xu 已提交
904
        status = connect.compact(ip_collection)
905
        assert status.OK()
X
Xiaohai Xu 已提交
906
        status = connect.flush([ip_collection])
907
        assert status.OK()
X
Xiaohai Xu 已提交
908 909
        # get collection info after compact twice
        status, info = connect.collection_info(ip_collection)
910 911 912 913 914
        assert status.OK()
        size_after_twice = info.partitions_stat[0].segments_stat[0].data_size
        assert(size_after == size_after_twice)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
915
    def test_add_vectors_delete_part_and_compact_twice(self, connect, ip_collection):
916 917
        '''
        target: test add vectors, delete part of them and compact twice
X
Xiaohai Xu 已提交
918
        method: add vectors, delete part and compact collection twice
919 920 921
        expected: status ok, data size smaller after first compact, no change after second
        '''
        vectors = gen_vector(nb, dim)
X
Xiaohai Xu 已提交
922
        status, ids = connect.add_vectors(ip_collection, vectors)
923
        assert status.OK()
X
Xiaohai Xu 已提交
924
        status = connect.flush([ip_collection])
925 926
        assert status.OK()
        delete_ids = [ids[0], ids[-1]]
X
Xiaohai Xu 已提交
927
        status = connect.delete_by_id(ip_collection, delete_ids)
928
        assert status.OK()
X
Xiaohai Xu 已提交
929
        status = connect.flush([ip_collection])
930
        assert status.OK()
X
Xiaohai Xu 已提交
931 932
        # get collection info before compact
        status, info = connect.collection_info(ip_collection)
933 934
        assert status.OK()
        size_before = info.partitions_stat[0].segments_stat[0].data_size
X
Xiaohai Xu 已提交
935
        status = connect.compact(ip_collection)
936
        assert status.OK()
X
Xiaohai Xu 已提交
937
        status = connect.flush([ip_collection])
938
        assert status.OK()
X
Xiaohai Xu 已提交
939 940
        # get collection info after compact
        status, info = connect.collection_info(ip_collection)
941 942 943
        assert status.OK()
        size_after = info.partitions_stat[0].segments_stat[0].data_size
        assert(size_before > size_after)
X
Xiaohai Xu 已提交
944
        status = connect.compact(ip_collection)
945
        assert status.OK()
X
Xiaohai Xu 已提交
946
        status = connect.flush([ip_collection])
947
        assert status.OK()
X
Xiaohai Xu 已提交
948 949
        # get collection info after compact twice
        status, info = connect.collection_info(ip_collection)
950 951 952 953 954
        assert status.OK()
        size_after_twice = info.partitions_stat[0].segments_stat[0].data_size
        assert(size_after == size_after_twice)

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
955
    def test_compact_multi_collections(self, connect):
956
        '''
X
Xiaohai Xu 已提交
957 958
        target: test compact works or not with multiple collections
        method: create 50 collections, add vectors into them and compact in turn
959 960 961
        expected: status ok
        '''
        nq = 100
X
Xiaohai Xu 已提交
962
        num_collections = 50
963
        vectors = gen_vectors(nq, dim)
X
Xiaohai Xu 已提交
964 965 966 967 968
        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,
969 970 971
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.IP}
X
Xiaohai Xu 已提交
972
            connect.create_collection(param)
973
        time.sleep(6)
X
Xiaohai Xu 已提交
974 975
        for i in range(num_collections):
            status, ids = connect.add_vectors(collection_name=collection_list[i], records=vectors)
976
            assert status.OK()
X
Xiaohai Xu 已提交
977
            status = connect.compact(collection_list[i])
978 979 980
            assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
981
    def test_add_vector_after_compact(self, connect, ip_collection):
982 983 984 985 986 987
        '''
        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 已提交
988
        status, ids = connect.add_vectors(ip_collection, vectors)
989
        assert status.OK()
X
Xiaohai Xu 已提交
990
        status = connect.flush([ip_collection])
991
        assert status.OK()
X
Xiaohai Xu 已提交
992 993
        # get collection info before compact
        status, info = connect.collection_info(ip_collection)
994 995
        assert status.OK()
        size_before = info.partitions_stat[0].segments_stat[0].data_size
X
Xiaohai Xu 已提交
996
        status = connect.compact(ip_collection)
997
        assert status.OK()
X
Xiaohai Xu 已提交
998
        status = connect.flush([ip_collection])
999
        assert status.OK()
X
Xiaohai Xu 已提交
1000 1001
        # get collection info after compact
        status, info = connect.collection_info(ip_collection)
1002 1003 1004 1005
        assert status.OK()
        size_after = info.partitions_stat[0].segments_stat[0].data_size
        assert(size_before == size_after)
        vector = gen_single_vector(dim)
X
Xiaohai Xu 已提交
1006
        status, ids = connect.add_vectors(ip_collection, vector)
1007 1008 1009
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
1010
    def test_delete_vectors_after_compact(self, connect, ip_collection):
1011 1012 1013 1014 1015 1016
        '''
        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 已提交
1017
        status, ids = connect.add_vectors(ip_collection, vectors)
1018
        assert status.OK()
X
Xiaohai Xu 已提交
1019
        status = connect.flush([ip_collection])
1020
        assert status.OK()
X
Xiaohai Xu 已提交
1021
        status = connect.compact(ip_collection)
1022
        assert status.OK()
X
Xiaohai Xu 已提交
1023
        status = connect.flush([ip_collection])
1024
        assert status.OK()
X
Xiaohai Xu 已提交
1025
        status = connect.delete_by_id(ip_collection, ids)
1026
        assert status.OK()
X
Xiaohai Xu 已提交
1027
        status = connect.flush([ip_collection])
1028 1029 1030
        assert status.OK()

    @pytest.mark.timeout(COMPACT_TIMEOUT)
X
Xiaohai Xu 已提交
1031
    def test_search_after_compact(self, connect, ip_collection):
1032 1033 1034 1035 1036 1037
        '''
        target: test search after compact
        method: after compact operation, search vector
        expected: status ok
        '''
        vectors = gen_vector(nb, dim)
X
Xiaohai Xu 已提交
1038
        status, ids = connect.add_vectors(ip_collection, vectors)
1039
        assert status.OK()
X
Xiaohai Xu 已提交
1040
        status = connect.flush([ip_collection])
1041
        assert status.OK()
X
Xiaohai Xu 已提交
1042
        status = connect.compact(ip_collection)
1043
        assert status.OK()
X
Xiaohai Xu 已提交
1044
        status = connect.flush([ip_collection])
1045 1046
        assert status.OK()
        query_vecs = [vectors[0]]
X
Xiaohai Xu 已提交
1047
        status, res = connect.search_vectors(ip_collection, top_k, query_records=query_vecs) 
1048 1049
        logging.getLogger().info(res)
        assert status.OK()