Skip to content

Commit 3f6360b

Browse files
authored
Logs extension (#410)
1 parent cc14a65 commit 3f6360b

File tree

12 files changed

+87
-19
lines changed

12 files changed

+87
-19
lines changed

src/VecSim/algorithms/hnsw/hnsw.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,8 @@ idType HNSWIndex<DataType, DistType>::mutuallyConnectNewElement(
974974
// lock.
975975
if (new_node_level_data.numLinks == max_M_cur) {
976976
// The new node cannot add more neighbors
977-
this->log("Couldn't add all chosen neighbors upon inserting a new node");
977+
this->log(VecSimCommonStrings::LOG_DEBUG_STRING,
978+
"Couldn't add all chosen neighbors upon inserting a new node");
978979
unlockNodeLinks(new_node_level);
979980
unlockNodeLinks(neighbor_graph_data);
980981
break;
@@ -1350,6 +1351,8 @@ template <typename DataType, typename DistType>
13501351
void HNSWIndex<DataType, DistType>::resizeIndexCommon(size_t new_max_elements) {
13511352
assert(new_max_elements % this->blockSize == 0 &&
13521353
"new_max_elements must be a multiple of blockSize");
1354+
this->log(VecSimCommonStrings::LOG_VERBOSE_STRING,
1355+
"Updating HNSW index capacity from %zu to %zu", this->maxElements, new_max_elements);
13531356
resizeLabelLookup(new_max_elements);
13541357
visitedNodesHandlerPool.resize(new_max_elements);
13551358
idToMetaData.resize(new_max_elements);
@@ -1445,7 +1448,8 @@ void HNSWIndex<DataType, DistType>::mutuallyUpdateForRepairedNode(
14451448
for (auto chosen_id : chosen_neighbors) {
14461449
if (node_neighbors_idx == max_M_cur) {
14471450
// Cannot add more new neighbors, we reached the capacity.
1448-
this->log("Couldn't add all the chosen new nodes upon updating %u, as we reached the"
1451+
this->log(VecSimCommonStrings::LOG_DEBUG_STRING,
1452+
"Couldn't add all the chosen new nodes upon updating %u, as we reached the"
14491453
" maximum number of neighbors per node",
14501454
node_id);
14511455
break;
@@ -1803,7 +1807,8 @@ AddVectorCtx HNSWIndex<DataType, DistType>::storeNewElement(labelType label,
18031807
try {
18041808
new (cur_egd) ElementGraphData(state.elementMaxLevel, levelDataSize, this->allocator);
18051809
} catch (std::runtime_error &e) {
1806-
this->log("Error - allocating memory for new element failed due to low memory");
1810+
this->log(VecSimCommonStrings::LOG_WARNING_STRING,
1811+
"Error - allocating memory for new element failed due to low memory");
18071812
throw e;
18081813
}
18091814

src/VecSim/algorithms/hnsw/hnsw_serializer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ void HNSWIndex<DataType, DistType>::restoreGraph(std::ifstream &input) {
184184
try {
185185
new (tmpData) ElementGraphData(toplevel, this->levelDataSize, this->allocator);
186186
} catch (std::runtime_error &e) {
187-
this->log("Error - allocating memory for new element failed due to low memory");
187+
this->log(VecSimCommonStrings::LOG_WARNING_STRING,
188+
"Error - allocating memory for new element failed due to low memory");
188189
throw e;
189190
}
190191
// Add the current element to the current block, and update cur_egt to point to it.

src/VecSim/algorithms/hnsw/hnsw_tiered.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ class TieredHNSWIndex : public VecSimTieredIndex<DataType, DistType> {
206206
}
207207
void runGC() override {
208208
// Run no more than pendingSwapJobsThreshold value jobs.
209+
TIERED_LOG(VecSimCommonStrings::LOG_VERBOSE_STRING,
210+
"running asynchronous GC for tiered HNSW index");
209211
this->executeReadySwapJobs(this->pendingSwapJobsThreshold);
210212
}
211213
#ifdef BUILD_TESTS
@@ -286,6 +288,9 @@ void TieredHNSWIndex<DataType, DistType>::executeReadySwapJobs(size_t maxJobsToR
286288

287289
// Execute swap jobs - acquire hnsw write lock.
288290
this->mainIndexGuard.lock();
291+
TIERED_LOG(VecSimCommonStrings::LOG_VERBOSE_STRING,
292+
"Tiered HNSW index GC: there are %zu ready swap jobs. Start executing %zu swap jobs",
293+
readySwapJobs, MIN(readySwapJobs, maxJobsToRun));
289294

290295
vecsim_stl::vector<idType> idsToRemove(this->allocator);
291296
idsToRemove.reserve(idToSwapJob.size());
@@ -304,6 +309,8 @@ void TieredHNSWIndex<DataType, DistType>::executeReadySwapJobs(size_t maxJobsToR
304309
idToSwapJob.erase(id);
305310
}
306311
readySwapJobs -= idsToRemove.size();
312+
TIERED_LOG(VecSimCommonStrings::LOG_VERBOSE_STRING,
313+
"Tiered HNSW index GC: done executing %zu swap jobs", idsToRemove.size());
307314
this->mainIndexGuard.unlock();
308315
}
309316

src/VecSim/utils/vec_utils.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ const char *VecSimCommonStrings::BACKEND_INDEX_STRING = "BACKEND_INDEX";
5959
const char *VecSimCommonStrings::TIERED_HNSW_SWAP_JOBS_THRESHOLD_STRING =
6060
"TIERED_HNSW_SWAP_JOBS_THRESHOLD";
6161

62+
// Log levels
63+
const char *VecSimCommonStrings::LOG_DEBUG_STRING = "debug";
64+
const char *VecSimCommonStrings::LOG_VERBOSE_STRING = "verbose";
65+
const char *VecSimCommonStrings::LOG_NOTICE_STRING = "notice";
66+
const char *VecSimCommonStrings::LOG_WARNING_STRING = "warning";
67+
6268
void sort_results_by_id(VecSimQueryResult_List rl) {
6369
qsort(rl.results, VecSimQueryResult_Len(rl), sizeof(VecSimQueryResult),
6470
(__compar_fn_t)cmpVecSimQueryResultById);

src/VecSim/utils/vec_utils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ struct VecSimCommonStrings {
6565
static const char *FRONTEND_INDEX_STRING;
6666
static const char *BACKEND_INDEX_STRING;
6767
static const char *TIERED_HNSW_SWAP_JOBS_THRESHOLD_STRING;
68+
69+
// Log levels
70+
static const char *LOG_DEBUG_STRING;
71+
static const char *LOG_VERBOSE_STRING;
72+
static const char *LOG_NOTICE_STRING;
73+
static const char *LOG_WARNING_STRING;
6874
};
6975

7076
inline int cmpVecSimQueryResultById(const VecSimQueryResult *res1, const VecSimQueryResult *res2) {

src/VecSim/vec_sim_common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,10 @@ typedef int (*timeoutCallbackFunction)(void *ctx);
296296
/**
297297
* @brief A struct to pass 3rd party logging function to Vecsimlib.
298298
* @param ctx some generic context to pass to the function
299+
* @param level loglevel (in redis we should choose from: "warning", "notice", "verbose", "debug")
299300
* @param message the message to log
300301
*/
301-
typedef void (*logCallbackFunction)(void *ctx, const char *message);
302+
typedef void (*logCallbackFunction)(void *ctx, const char *level, const char *message);
302303

303304
typedef enum {
304305
VecSim_QueryResult_OK = VecSim_OK,

src/VecSim/vec_sim_index.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ struct VecSimIndexAbstract : public VecSimIndexInterface {
117117
return results;
118118
}
119119

120-
void log(const char *fmt, ...) const {
120+
void log(const char *level, const char *fmt, ...) const {
121121
if (VecSimIndexInterface::logCallback) {
122122
// Format the message and call the callback
123123
va_list args;
@@ -128,7 +128,7 @@ struct VecSimIndexAbstract : public VecSimIndexInterface {
128128
va_start(args, fmt);
129129
vsnprintf(buf, len + 1, fmt, args);
130130
va_end(args);
131-
logCallback(this->logCallbackCtx, buf);
131+
logCallback(this->logCallbackCtx, level, buf);
132132
delete[] buf;
133133
}
134134
}

src/VecSim/vec_sim_interface.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include <iostream>
1010

1111
// Print log messages to stdout
12-
void Vecsim_Log(void *ctx, const char *message) { std::cout << message << std::endl; }
12+
void Vecsim_Log(void *ctx, const char *level, const char *message) {
13+
std::cout << level << ": " << message << std::endl;
14+
}
1315

1416
timeoutCallbackFunction VecSimIndexInterface::timeoutCallback = [](void *ctx) { return 0; };
1517
logCallbackFunction VecSimIndexInterface::logCallback = Vecsim_Log;

src/VecSim/vec_sim_tiered_index.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <shared_mutex>
1010

11+
#define TIERED_LOG this->backendIndex->log
12+
1113
/**
1214
* Definition of generic job structure for asynchronous tiered index.
1315
*/
@@ -255,9 +257,9 @@ VecSimTieredIndex<DataType, DistType>::rangeQuery(const void *queryBlob, double
255257
template <typename DataType, typename DistType>
256258
VecSimIndexInfo VecSimTieredIndex<DataType, DistType>::info() const {
257259
VecSimIndexInfo info;
258-
this->flatIndexGuard.lock();
260+
this->flatIndexGuard.lock_shared();
259261
VecSimIndexInfo frontendInfo = this->frontendIndex->info();
260-
this->flatIndexGuard.unlock();
262+
this->flatIndexGuard.unlock_shared();
261263

262264
this->mainIndexGuard.lock();
263265
VecSimIndexInfo backendInfo = this->backendIndex->info();

tests/unit/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ add_executable(test_hnsw_parallel test_hnsw_parallel.cpp test_utils.cpp)
3333
add_executable(test_bruteforce test_bruteforce.cpp test_bruteforce_multi.cpp test_utils.cpp)
3434
add_executable(test_allocator test_allocator.cpp test_utils.cpp)
3535
add_executable(test_spaces test_spaces.cpp)
36-
add_executable(test_common test_common.cpp test_utils.cpp)
36+
add_executable(test_common ../utils/mock_thread_pool.cpp test_utils.cpp test_common.cpp)
3737

3838
target_link_libraries(test_hnsw PUBLIC gtest_main VectorSimilarity)
3939
target_link_libraries(test_hnsw_parallel PUBLIC gtest_main VectorSimilarity)

0 commit comments

Comments
 (0)