Skip to content

Commit 9c364ee

Browse files
committed
Use generation guard for HNSW graph for search and multi-threaded put.
1 parent 5e766ab commit 9c364ee

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

searchlib/src/vespa/searchlib/tensor/hnsw_index.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,17 @@ PreparedAddNode::PreparedAddNode(std::vector<Links>&& connections_in) noexcept
109109
PreparedAddNode::~PreparedAddNode() = default;
110110
PreparedAddNode::PreparedAddNode(PreparedAddNode&& other) noexcept = default;
111111

112-
PreparedAddDoc::PreparedAddDoc(uint32_t docid_in, ReadGuard read_guard_in) noexcept
112+
PreparedAddDoc::PreparedAddDoc(uint32_t docid_in, ReadGuard read_guard_in, ReadGuard hnsw_graph_read_guard_in) noexcept
113113
: docid(docid_in),
114114
read_guard(std::move(read_guard_in)),
115+
hnsw_graph_read_guard(std::move(hnsw_graph_read_guard_in)),
115116
nodes()
116117
{}
118+
117119
PreparedAddDoc::~PreparedAddDoc() = default;
120+
118121
PreparedAddDoc::PreparedAddDoc(PreparedAddDoc&& other) noexcept = default;
122+
119123
}
120124

121125
SelectResult::SelectResult() noexcept = default;
@@ -688,7 +692,8 @@ void
688692
HnswIndex<type>::add_document(uint32_t docid)
689693
{
690694
vespalib::GenerationHandler::Guard no_guard_needed;
691-
PreparedAddDoc op(docid, std::move(no_guard_needed));
695+
vespalib::GenerationHandler::Guard no_hnsw_graph_guard_needed;
696+
PreparedAddDoc op(docid, std::move(no_guard_needed), std::move(no_hnsw_graph_guard_needed));
692697
auto input_vectors = get_vectors(docid);
693698
auto subspaces = input_vectors.subspaces();
694699
op.nodes.reserve(subspaces);
@@ -705,7 +710,7 @@ template <HnswIndexType type>
705710
PreparedAddDoc
706711
HnswIndex<type>::internal_prepare_add(uint32_t docid, VectorBundle input_vectors, vespalib::GenerationHandler::Guard read_guard) const
707712
{
708-
PreparedAddDoc op(docid, std::move(read_guard));
713+
PreparedAddDoc op(docid, std::move(read_guard), _graph.make_guard());
709714
auto entry = _graph.get_entry_node();
710715
auto subspaces = input_vectors.subspaces();
711716
op.nodes.reserve(subspaces);
@@ -1096,6 +1101,7 @@ std::vector<NearestNeighborIndex::Neighbor>
10961101
HnswIndex<type>::find_top_k(Stats &stats, uint32_t k, const BoundDistanceFunction &df, uint32_t explore_k, double exploration_slack, bool prefetch_tensors,
10971102
const vespalib::Doom& doom, double distance_threshold) const
10981103
{
1104+
auto guard = _graph.make_guard();
10991105
return top_k_by_docid(stats, k, df, nullptr, false, 0.0, explore_k, exploration_slack, prefetch_tensors, doom, distance_threshold);
11001106
}
11011107

@@ -1104,6 +1110,7 @@ std::vector<NearestNeighborIndex::Neighbor>
11041110
HnswIndex<type>::find_top_k_with_filter(Stats &stats, uint32_t k, const BoundDistanceFunction &df, const GlobalFilter &filter, bool low_hit_ratio, double exploration,
11051111
uint32_t explore_k, double exploration_slack, bool prefetch_tensors, const vespalib::Doom& doom, double distance_threshold) const
11061112
{
1113+
auto guard = _graph.make_guard();
11071114
return top_k_by_docid(stats, k, df, &filter, low_hit_ratio, exploration, explore_k, exploration_slack, prefetch_tensors, doom, distance_threshold);
11081115
}
11091116

searchlib/src/vespa/searchlib/tensor/hnsw_index.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ struct PreparedAddDoc final : public PrepareResult {
7777
using ReadGuard = vespalib::GenerationHandler::Guard;
7878
uint32_t docid;
7979
ReadGuard read_guard;
80+
ReadGuard hnsw_graph_read_guard;
8081
std::vector<PreparedAddNode> nodes;
81-
PreparedAddDoc(uint32_t docid_in, ReadGuard read_guard_in) noexcept;
82+
PreparedAddDoc(uint32_t docid_in, ReadGuard read_guard_in, ReadGuard hnsw_graph_read_guard_in) noexcept;
8283
~PreparedAddDoc() override;
8384
PreparedAddDoc(PreparedAddDoc&& other) noexcept;
8485
};

0 commit comments

Comments
 (0)