Repository navigation
fix(hnsw): keep the graph reachable after DELETE then INSERT (#30660); upgrade usearch to 2.26.5 - #30823
Conversation
…rigin#30660) A CDC DELETE followed by INSERT calls usearch Remove then Add. Add reuses the removed node's slot and rewrites the node in place at the new vector's position, dropping its neighbor links without repairing them. Everything reachable only through the removed node became unreachable: on 1000 points on a line, deleting id 100 and inserting a far row left the index returning 901 rows (ids 1..99 missing) even after the new model was published and after a restart. Vendor the usearch fix (cpegeric/usearch matrixone_20261022 = concurrent_add + fix_delete_20261022): before reusing a removed slot, relink the removed node's old neighbors to each other on every level, and serialize slot-reusing adds. Add TestRemoveAddKeepsGraphReachable through HnswModel (remove 100, remove 500, and save/load then remove); it fails on the previous tarball and passes now. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
Mirrors the issue: 1000 rows on a line, single-threaded build + FORCE_SYNC, then DELETE id 100 + INSERT a far row (and later id 500 + another far row). Waits for the published model checksum to change before querying, then asserts the index returns all 1000 rows, every id below the deleted one, and the true nearest row. On the previous usearch tarball the same case returns 901 / 502 rows, no ids below the deleted one, and id 101 as the nearest row to [1,1,2]. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merge Queue Status🛑 Queue command has been cancelled |
🛑 The queue request has been cancelled: new commits were pushedDetails@cpegeric pushed to this pull request after |
Merge Queue Status
This pull request spent 2 hours 40 minutes 22 seconds in the queue, with no time running CI. ReasonPull request #30823 has been dequeued Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.
HintYou should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it. Waiting for any of
All conditions
Tick the box to put this pull request back in the merge queue (same as
|
What type of PR is this?
Which issue(s) this PR fixes:
Fixes #30660
What this PR does / why we need it:
After an HNSW index is built, a DELETE followed by an INSERT can make other live rows unreachable through the index. In the issue's repro (1000 rows on a line, delete id 100, insert a far row) the index returns 901 rows, with ids 1–99 missing. This persists after the new model is published and after a restart; only
FORCE_SYNC(a full rebuild) restores it.Root cause (usearch, not ISCP). The CDC path is correct:
HnswSync.sequentialUpdateappliesRemove(100)thenAdd(2001). The same two calls on bare usearch, with no SQL or ISCP involved, give the same 901 result. In usearch,remove()only tombstones the node and queues its slot. The nextadd()reuses that slot, andindex_gt::update()rewrites the node in place at the new vector's position, clearing its neighbour lists without repairing anything (upstream leaves a TODO there). Whatever was reachable only through the removed node is cut off. Points on a line make the removed node the only bridge, so the loss is a whole range; on realistic data the same mechanism causes partial recall loss. Full trace: #30660 (comment)Fix. The vendored usearch is upgraded from
usearch-2.25.3.tar.gztousearch-2.26.5.tar.gz, built withgit archive(same layout as before) from cpegeric/usearchmatrixone_20261022. That branch is upstream v2.26.5 plus the existingconcurrent_add(#735) fix plusfix_delete_20261022, which cherry-picks cleanly onto upstream for its own upstream PR. Upstream v2.26.5 still has this bug: the new line-reachability test fails on unmodified v2.26.5.Upgrading to 2.26.5 also brings upstream #769, which rehashes tombstones in the key-lookup table. In v2.25.3,
remove()could spin at 100% CPU under heavy delete/insert churn.Upstream #777 changes
Reserve(n): it now allocates exactlynslots, where it used to round up to the key-lookup table's power-of-two size (about +37%, e.g.Reserve(1000)gave 1365). MO always reserves its real capacity:MaxCapacityon build, which the build path never exceeds, andIndexCapacityon a sync load, which defaults to 1,000,000. Updates reuse the slot their ownRemovefreed. So no production path depended on that extra room. OnlyTestModel, which loaded withIndexCapacity0, relied on it, and it now sets a real capacity.update()relinks the node's old neighbours on every level. Each neighbour drops its link to the reused slot and re-picks its neighbours, using the existing heuristic, from its own list plus the removed node's other neighbours. Nodes are locked one at a time, as inform_reverse_links_. The snapshot uses a reused per-thread buffer, and out-of-memory is reported as an error.index_dense_gt::add_()serializes adds that reuse a slot, because the in-place rewrite is not safe against concurrent rewrites of neighbouring nodes. Fresh adds and searches stay concurrent. MO's CDC update path is already sequential.No memory growth: removed slots are still reused. Apart from the usearch upgrade, the only MO change is the
TestModelcapacity in its test setup.Tests
Validated on 2.26.5:
pkg/vectorindex/...packages pass.vector_hnsw_delete_insertpasses 44/44.pessimistic_transaction/vectorhas 0 failures,vectorpasses 3301/3301, andbranch_hnsw_async_refresh,fulltext2_hnsw_clone,index_cache_size_bounds,cloneandbranch_multi_index_restore_inithave 0 failures.The results below were first measured on the 2.25.3-based tarball:
TestRemoveAddKeepsGraphReachable(pkg/vectorindex/hnsw) goes throughHnswModel: remove 100, remove 500, and save→load then remove, each followed by a far add, asserting every live key is returned. All three fail on the previous tarball (901 / 501 rows) and pass with this one. Allpkg/vectorindex/hnsw/...andpkg/vectorindex/usearchexpackages pass.cpp/test.cppin the fork): the remove + far add on a line test forslot32_tanduint40_tfails without the fix. Remove/add churn rounds, single- and multi-threaded, assert every live key finds itself in an exhaustive search. On the merged branch, these and the Data lost after restart server #735 concurrent-add reachability test pass 10/10 runs with internal assertions enabled.pessimistic_transaction/vector/vector_hnsw_delete_insert. It mirrors the issue (single-threaded build +FORCE_SYNC, DELETE id 100 + far INSERT, then DELETE id 500 + another far INSERT) and waits for the published model's checksum to change before querying. With this tarball: 44/44, and the index returns all 1000 rows, every id below the deleted one, and the true nearest row. On the previous tarball the same case returns 901 / 502 rows, none of the ids below the deleted one, and id 101 as the nearest row to[1,1,2]. Thepessimistic_transaction/vector(703 pass, 0 failed) andvectorsuites stay green. The one suite-only failure, in IVFvector_filtered_search_knobs, passes 51/51 when run alone and doesn't involve HNSW.[1,1,2],[777.2,1,2]and[1000,1,2], and 1000/1000 in the delete-id-500 variant (was 501).🤖 Generated with Claude Code