Skip to content

Commit 1db4e00

Browse files
authored
reading indexUpdateScheduled under the lock guard (#813)
* reading indexUpdateScheduled under the lock * dont lock twice
1 parent 46e69b4 commit 1db4e00

File tree

2 files changed

+42
-54
lines changed

2 files changed

+42
-54
lines changed

src/VecSim/algorithms/svs/svs_tiered.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -874,14 +874,17 @@ class TieredSVSIndex : public VecSimTieredIndex<DataType, float> {
874874
VecSimIndexDebugInfo debugInfo() const override {
875875
auto info = Base::debugInfo();
876876

877-
SvsTieredInfo svsTieredInfo = {.trainingTriggerThreshold = this->trainingTriggerThreshold,
878-
.updateTriggerThreshold = this->updateTriggerThreshold,
879-
.updateJobWaitTime = this->updateJobWaitTime,
880-
.indexUpdateScheduled =
881-
static_cast<bool>(this->indexUpdateScheduled.test())};
877+
SvsTieredInfo svsTieredInfo = {
878+
.trainingTriggerThreshold = this->trainingTriggerThreshold,
879+
.updateTriggerThreshold = this->updateTriggerThreshold,
880+
.updateJobWaitTime = this->updateJobWaitTime,
881+
};
882+
{
883+
std::lock_guard<std::mutex> lock(this->updateJobMutex);
884+
svsTieredInfo.indexUpdateScheduled =
885+
this->indexUpdateScheduled.test() == VecSimBool_TRUE;
886+
}
882887
info.tieredInfo.specificTieredBackendInfo.svsTieredInfo = svsTieredInfo;
883-
// prevent parallel updates
884-
std::lock_guard<std::mutex> lock(this->updateJobMutex);
885888
info.tieredInfo.backgroundIndexing =
886889
svsTieredInfo.indexUpdateScheduled && info.tieredInfo.frontendCommonInfo.indexSize > 0
887890
? VecSimBool_TRUE

tests/unit/test_svs_tiered.cpp

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,11 @@ TYPED_TEST(SVSTieredIndexTest, CreateIndexInstance) {
500500
ASSERT_EQ(tiered_index->GetBackendIndex()->indexSize(), 1);
501501
}
502502

503-
TYPED_TEST(SVSTieredIndexTest, addVector) {
503+
TYPED_TEST(SVSTieredIndexTest, background_indexing_check) {
504504
// Create TieredSVS index instance with a mock queue.
505-
size_t dim = 4;
505+
size_t dim = 2;
506+
constexpr size_t training_th = DEFAULT_BLOCK_SIZE;
507+
constexpr size_t update_th = DEFAULT_BLOCK_SIZE;
506508
SVSParams params = {.type = TypeParam::get_index_type(),
507509
.dim = dim,
508510
.metric = VecSimMetric_L2,
@@ -511,59 +513,42 @@ TYPED_TEST(SVSTieredIndexTest, addVector) {
511513

512514
auto mock_thread_pool = tieredIndexMock();
513515

514-
auto tiered_params = this->CreateTieredSVSParams(svs_params, mock_thread_pool, 1, 1);
516+
auto tiered_params =
517+
this->CreateTieredSVSParams(svs_params, mock_thread_pool, training_th, update_th);
515518
auto *tiered_index = this->CreateTieredSVSIndex(tiered_params, mock_thread_pool);
516519
ASSERT_INDEX(tiered_index);
517520

518-
// Get the allocator from the tiered index.
519-
auto allocator = tiered_index->getAllocator();
521+
mock_thread_pool.init_threads();
520522

521-
BFParams bf_params = {.type = TypeParam::get_index_type(),
522-
.dim = dim,
523-
.metric = VecSimMetric_L2,
524-
.multi = TypeParam::isMulti()};
523+
for (size_t i = 0; i < training_th; i++) {
524+
TEST_DATA_T vector[dim];
525+
GenerateVector<TEST_DATA_T>(vector, dim, i);
526+
VecSimIndex_AddVector(tiered_index, vector, i);
527+
}
525528

526-
size_t expected_mem = TieredFactory::EstimateInitialSize(&tiered_params);
527-
ASSERT_LE(expected_mem, tiered_index->getAllocationSize());
528-
ASSERT_GE(expected_mem * 1.02, tiered_index->getAllocationSize());
529-
ASSERT_EQ(mock_thread_pool.jobQ.size(), 0);
529+
while (tiered_index->debugInfo().tieredInfo.backgroundIndexing != VecSimBool_FALSE) {
530+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
531+
}
530532

531-
// Create a vector and add it to the tiered index.
532-
labelType vec_label = 1;
533-
TEST_DATA_T vector[dim];
534-
GenerateVector<TEST_DATA_T>(vector, dim, vec_label);
535-
VecSimIndex_AddVector(tiered_index, vector, vec_label);
536-
// Validate that the vector was inserted to the flat buffer properly.
537-
ASSERT_EQ(tiered_index->indexSize(), 1);
538-
ASSERT_EQ(tiered_index->GetFlatIndex()->indexSize(), 1);
539-
ASSERT_EQ(tiered_index->GetBackendIndex()->indexSize(), 0);
540-
ASSERT_EQ(tiered_index->GetFlatIndex()->indexCapacity(), DEFAULT_BLOCK_SIZE);
541-
ASSERT_EQ(tiered_index->indexCapacity(), DEFAULT_BLOCK_SIZE);
542-
ASSERT_EQ(tiered_index->GetFlatIndex()->getDistanceFrom_Unsafe(vec_label, vector), 0);
543-
ASSERT_EQ(mock_thread_pool.jobQ.size(), mock_thread_pool.thread_pool_size);
533+
ASSERT_EQ(tiered_index->GetBackendIndex()->indexSize(), training_th);
534+
ASSERT_EQ(tiered_index->GetFlatIndex()->indexSize(), 0);
535+
ASSERT_EQ(tiered_index->indexSize(), training_th);
544536

545-
// Account for the allocation of a new block due to the vector insertion.
546-
expected_mem += (BruteForceFactory::EstimateElementSize(&bf_params)) * DEFAULT_BLOCK_SIZE;
547-
// Account for the memory that was allocated in the labelToId map (approx.)
548-
expected_mem += sizeof(vecsim_stl::unordered_map<labelType, idType>::value_type) +
549-
sizeof(void *) + sizeof(size_t);
550-
// Account for the insert job that was created.
551-
expected_mem +=
552-
SVSMultiThreadJob::estimateSize(mock_thread_pool.thread_pool_size) + sizeof(size_t);
553-
auto actual_mem = tiered_index->getAllocationSize();
554-
ASSERT_GE(expected_mem * 1.02, tiered_index->getAllocationSize());
555-
ASSERT_LE(expected_mem, tiered_index->getAllocationSize());
556-
557-
if constexpr (TypeParam::isMulti()) {
558-
// Add another vector under the same label
559-
VecSimIndex_AddVector(tiered_index, vector, vec_label);
560-
ASSERT_EQ(tiered_index->indexSize(), 2);
561-
ASSERT_EQ(tiered_index->indexLabelCount(), 1);
562-
ASSERT_EQ(tiered_index->GetBackendIndex()->indexSize(), 0);
563-
ASSERT_EQ(tiered_index->GetFlatIndex()->indexSize(), 2);
564-
// Validate that there still 1 update jobs set
565-
ASSERT_EQ(mock_thread_pool.jobQ.size(), mock_thread_pool.thread_pool_size);
537+
constexpr size_t second_batch = 2500;
538+
539+
for (size_t i = 0; i < second_batch; i++) {
540+
TEST_DATA_T vector[dim];
541+
GenerateVector<TEST_DATA_T>(vector, dim, i);
542+
VecSimIndex_AddVector(tiered_index, vector, training_th + i);
566543
}
544+
545+
while (tiered_index->debugInfo().tieredInfo.backgroundIndexing != VecSimBool_FALSE) {
546+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
547+
}
548+
549+
ASSERT_GT(tiered_index->GetBackendIndex()->indexSize(), training_th + second_batch / update_th);
550+
ASSERT_LT(tiered_index->GetFlatIndex()->indexSize(), update_th);
551+
ASSERT_EQ(tiered_index->indexSize(), second_batch + training_th);
567552
}
568553

569554
TYPED_TEST(SVSTieredIndexTest, insertJob) {

0 commit comments

Comments
 (0)