Repository navigation
Conversation
… neighbors
remove() only tombstones a node and queues its slot; the next add() pops that slot
and update() rewrites the node in place at the new vector's position. update()
cleared the node's neighbor lists without repairing anything, so the part of the
graph that was reachable only through the removed node became unreachable. On
points on a line (each node linked only to its two line neighbors), removing key k
and adding a far vector left every key below k unreachable: 1000 keys, remove 100,
add one far key -> search returns 901.
Fix:
- index_gt::update() first relinks the removed node's old neighbors on every
level: each neighbor drops its link to the reused slot and re-selects its
neighbors (heuristic refine) from its own list plus the removed node's other
neighbors. Nodes are locked one at a time, as in form_reverse_links_. The
snapshot uses a reused per-thread context buffer and reports OOM via
result.failed().
- index_dense_gt::add_() serializes slot-reusing adds, since update() is not
safe against concurrent updates of neighboring nodes. Fresh adds and searches
stay concurrent.
Tests (cpp/test.cpp): remove + far add on a line for slot32_t/uint40_t, and
remove/add churn rounds (single- and multi-threaded) asserting every live key
finds itself in an exhaustive search. The line test fails without the fix.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
`remove` followed by `add` reuses the removed node's slot: `update` rewrites the node at its new position. Whatever was reachable only through its old position is cut off. On points on a line, a search from the far side then misses the whole other half. Before the node moves, search around its old position without ever entering it, nor any node that a concurrent `update` is moving. Each old base-level neighbor that the search doesn't reach gets a link from the nodes it did reach, and a link back, as the moving node gave both ways. The move then drops only the links this bridge accounted for, and keeps those that concurrent insertions added meanwhile. Of two overlapping updates, at least one sees the other's node as moving. The bridge runs only if a neighbor would be left with at most one incoming link, the same threshold as for pruned links, so a move costs about as much as before. unum-cloud#801 by Ofek Lev and unum-cloud#821 by cpegeric fix the same cut. Their tests are here: unum-cloud#821's points on a line and churn rounds, and unum-cloud#801's two bridge tests.
`remove` followed by `add` reuses the removed node's slot: `update` rewrites the node at its new position. Whatever was reachable only through its old position is cut off. On points on a line, a search from the far side then misses the whole other half. Before the node moves, search around its old position without ever entering it, nor any node that a concurrent `update` is moving. Each old base-level neighbor that the search doesn't reach gets a link from the nodes it did reach, and a link back, as the moving node gave both ways. The move then drops only the links this bridge accounted for, and keeps those that concurrent insertions added meanwhile. Of two overlapping updates, at least one sees the other's node as moving. The bridge runs only if a neighbor would be left with at most one incoming link, the same threshold as for pruned links, so a move costs about as much as before. unum-cloud#801 by Ofek Lev and unum-cloud#821 by cpegeric fix the same cut. Their tests are here: unum-cloud#821's points on a line and churn rounds, and unum-cloud#801's two bridge tests.
|
@cpegeric, #805 now repairs slot reuse in The cost table in its description uses your benchmark. A |
|
@cpegeric, thanks for this one too! 3.0 has no |
Problem
remove()followed byadd()can leave live keys permanently unreachable bysearch().index_dense_gt::remove()only marks a node as removed and queues its slot infree_keys_. The nextadd()pops that slot and callsindex_gt::update(), which rewrites the node in place at the new vector's position.update()clears the node's neighbor lists and links it at the new position, but nothing repairs the graph around the node's old position. There is a// TODO: Go through existing neighbors removing reverse linksat that spot. Any part of the graph that was reachable only through the removed node is cut off. Neither saving and reloading nor further adds bring it back; only a full rebuild does.Minimal repro (L2, 3-D, default config, single thread):
Points on a line make each node link only to its two neighbors on the line, so the removed node is the only bridge and a whole range is lost. With
remove(500)the search returns 501 keys and 1..499 are lost. Re-adding at the same position loses nothing. On ordinary data the effect is partial. With 2000 keys, after 20 rounds of removing 10% and adding new keys, the number of live keys that an exhaustive self-search cannot find is:Fix
index_gt::update()first calls a newrelink_neighbors_of_(), which visits every level of the node being reused. For each of the node's old neighbors, it drops that neighbor's link to the reused slot and re-picks the neighbor's links with the existingrefine_heuristic. The candidates are the neighbor's own remaining links plus the removed node's other neighbors.form_reverse_links_does.context_tbuffer.result.failed()with the graph unchanged.index_dense_gt::add_()serializes the adds that reuse a slot with a new mutex.update()is not safe against concurrent updates of neighboring nodes, and the relink makes that window wider. Fresh adds and searches stay concurrent.Slots are still reused, so memory use doesn't grow.
Cost
The relink recomputes each old neighbor's links, so a remove+add costs more than before. This is a single-threaded micro-benchmark with 128-D vectors, 20k keys and 2000 remove+add operations:
addA cheaper variant would replace only each neighbor's link to the reused slot with the removed node's closest other neighbor. That is O(M²) per reuse instead of O(M³), but it repairs less. I'm happy to switch to it if you prefer.
Tests
New tests in
cpp/test.cpp:test_remove_add_line_reachability, forslot32_tanduint40_t: the repro above. It fails on currentmain-dev/main(v2.26.5) and passes with this change.test_remove_add_churn_reachability: repeated remove/add rounds, single- and multi-threaded. It asserts every live key finds itself in an exhaustive search.The full
test_cppsuite passes on macOS arm64 (Apple clang) in Release, and also withNDEBUGundefined so the internal assertions are active. The default Debug configuration enables ASan, whose runtime crashes at startup on this macOS, even on an empty program, so it could not be run locally.git clang-formatreports no changes.The fix is also in use in MatrixOne's vendored usearch. There the bug showed up as an HNSW index returning 901 of 1000 rows after a single
DELETE+INSERT(matrixorigin/matrixone#30660).