fix(diskann, hnsw sparse builder): lost-wakeup race in builder progress loops (#530)

This commit is contained in:
Jalin Wang
2026-06-26 16:35:24 +08:00
committed by GitHub
parent 0a008ebfbf
commit cdef8686c7
5 changed files with 115 additions and 40 deletions
@@ -201,16 +201,18 @@ int MultiChunkClusterAlgorithm::cluster(IndexThreads::Pointer threads,
threads->count(), &cents, &finished));
}
while (!task_group->is_finished()) {
{
std::unique_lock<std::mutex> lk(mutex_);
cond_.wait_until(lk, std::chrono::system_clock::now() +
std::chrono::seconds(check_interval_secs_));
if (error_.load(std::memory_order_acquire)) {
LOG_ERROR("Failed to cluster while waiting finish");
return errcode_;
while (finished.load() < chunk_count_) {
cond_.wait_until(lk, std::chrono::system_clock::now() +
std::chrono::seconds(check_interval_secs_));
if (error_.load(std::memory_order_acquire)) {
LOG_ERROR("Failed to cluster while waiting finish");
return errcode_;
}
LOG_INFO("Finish Chunk Count %zu, Finished Percent %.3f%%",
finished.load(), finished.load() * 100.0f / chunk_count_);
}
LOG_INFO("Finish Chunk Count %zu, Finished Percent %.3f%%", finished.load(),
finished.load() * 100.0f / chunk_count_);
}
if (error_.load(std::memory_order_acquire)) {
@@ -284,16 +286,18 @@ int MultiChunkClusterAlgorithm::label(IndexThreads::Pointer threads,
threads->count(), cents, out, &finished));
}
while (!task_group->is_finished()) {
{
std::unique_lock<std::mutex> lk(mutex_);
cond_.wait_until(lk, std::chrono::system_clock::now() +
std::chrono::seconds(check_interval_secs_));
if (error_.load(std::memory_order_acquire)) {
LOG_ERROR("Failed to cluster while waiting finish");
return errcode_;
while (finished.load() < features_count) {
cond_.wait_until(lk, std::chrono::system_clock::now() +
std::chrono::seconds(check_interval_secs_));
if (error_.load(std::memory_order_acquire)) {
LOG_ERROR("Failed to cluster while waiting finish");
return errcode_;
}
LOG_INFO("Finish label cnt %zu, finished percent %.3f%%", finished.load(),
finished.load() * 100.0f / features_count);
}
LOG_INFO("Finish label cnt %zu, finished percent %.3f%%", finished.load(),
finished.load() * 100.0f / features_count);
}
if (error_.load(std::memory_order_acquire)) {
@@ -231,6 +231,10 @@ void MultiChunkNumericalAlgorithm<T>::do_cluster(
chunk, algorithm.centroids().count(), features_->count(), cost);
(*finished)++;
{
std::lock_guard<std::mutex> lk(mutex_);
cond_.notify_one();
}
}
return;
@@ -267,6 +271,10 @@ void MultiChunkNumericalAlgorithm<T>::do_label(
}
(*finished)++;
{
std::lock_guard<std::mutex> lk(mutex_);
cond_.notify_one();
}
}
}
@@ -372,6 +380,10 @@ void MultiChunkNumericalInnerProductAlgorithm<T>::do_cluster(
chunk, algorithm.centroids().count(), features_->count(), cost);
(*finished)++;
{
std::lock_guard<std::mutex> lk(mutex_);
cond_.notify_one();
}
}
}
@@ -406,6 +418,10 @@ void MultiChunkNumericalInnerProductAlgorithm<T>::do_label(
}
(*finished)++;
{
std::lock_guard<std::mutex> lk(mutex_);
cond_.notify_one();
}
}
}
+24 -16
View File
@@ -267,17 +267,21 @@ int DiskAnnBuilder::build_internal(IndexThreads::Pointer threads) {
threads->count(), &finished));
}
while (!task_group->is_finished()) {
{
std::unique_lock<std::mutex> lk(mutex_);
cond_.wait_until(lk, std::chrono::system_clock::now() +
std::chrono::seconds(check_interval_secs_));
if (error_.load(std::memory_order_acquire)) {
LOG_ERROR("Failed to build index while waiting finish");
return errcode_;
while (finished.load() < entity_.doc_cnt()) {
cond_.wait_until(lk, std::chrono::system_clock::now() +
std::chrono::seconds(check_interval_secs_));
if (error_.load(std::memory_order_acquire)) {
LOG_ERROR("Failed to build index while waiting finish");
return errcode_;
}
LOG_INFO("Built cnt %zu, finished percent %.3f%%",
(size_t)finished.load(),
finished.load() * 100.0f / entity_.doc_cnt());
}
LOG_INFO("Built cnt %zu, finished percent %.3f%%", (size_t)finished.load(),
finished.load() * 100.0f / entity_.doc_cnt());
}
if (error_.load(std::memory_order_acquire)) {
LOG_ERROR("Failed to build index while waiting finish");
return errcode_;
@@ -300,17 +304,21 @@ int DiskAnnBuilder::prune_internal(IndexThreads::Pointer threads) {
threads->count(), &finished));
}
while (!task_group->is_finished()) {
{
std::unique_lock<std::mutex> lk(mutex_);
cond_.wait_until(lk, std::chrono::system_clock::now() +
std::chrono::seconds(check_interval_secs_));
if (error_.load(std::memory_order_acquire)) {
LOG_ERROR("Failed to purne index while waiting finish");
return errcode_;
while (finished.load() < entity_.doc_cnt()) {
cond_.wait_until(lk, std::chrono::system_clock::now() +
std::chrono::seconds(check_interval_secs_));
if (error_.load(std::memory_order_acquire)) {
LOG_ERROR("Failed to prune index while waiting finish");
return errcode_;
}
LOG_INFO("Prune cnt %zu, finished percent %.3f%%",
(size_t)finished.load(),
finished.load() * 100.0f / entity_.doc_cnt());
}
LOG_INFO("Prune cnt %zu, finished percent %.3f%%", (size_t)finished.load(),
finished.load() * 100.0f / entity_.doc_cnt());
}
if (error_.load(std::memory_order_acquire)) {
LOG_ERROR("Failed to prune index while waiting finish");
return errcode_;
@@ -307,17 +307,20 @@ int HnswSparseBuilder::build_graph(IndexThreads::Pointer threads,
i, threads->count(), &finished));
}
while (!task_group->is_finished()) {
{
std::unique_lock<std::mutex> lk(mutex_);
cond_.wait_until(lk, std::chrono::system_clock::now() +
std::chrono::seconds(check_interval_secs_));
if (error_.load(std::memory_order_acquire)) {
LOG_ERROR("Failed to build index while waiting finish");
return errcode_;
while (finished.load() < entity_.doc_cnt()) {
cond_.wait_until(lk, std::chrono::system_clock::now() +
std::chrono::seconds(check_interval_secs_));
if (error_.load(std::memory_order_acquire)) {
LOG_ERROR("Failed to build index while waiting finish");
return errcode_;
}
LOG_INFO("Built cnt %u, finished percent %.3f%%", finished.load(),
finished.load() * 100.0f / entity_.doc_cnt());
}
LOG_INFO("Built cnt %u, finished percent %.3f%%", finished.load(),
finished.load() * 100.0f / entity_.doc_cnt());
}
if (error_.load(std::memory_order_acquire)) {
LOG_ERROR("Failed to build index while waiting finish");
return errcode_;