fix(hnsw): refresh dead-node set instead of caching it for the index lifetime#9755
Open
shaunpatterson wants to merge 1 commit into
Open
fix(hnsw): refresh dead-node set instead of caching it for the index lifetime#9755shaunpatterson wants to merge 1 commit into
shaunpatterson wants to merge 1 commit into
Conversation
c5a3212 to
a8e9924
Compare
…lifetime removeDeadNodes loaded the tombstoned-vector set (the persisted vecDead posting) into ph.deadNodes exactly once, on the first call, and never refreshed it (the `if ph.deadNodes == nil` guard, with a standing `// TODO add a path to delete deadNodes`). Any vector deleted after that first call stayed invisible to the neighbour filter for the lifetime of the index instance, so dead UIDs leaked back into edge lists during subsequent inserts and neighbour updates. Cache the set as an immutable snapshot tagged with the transaction read timestamp, published via an atomic.Pointer: - Correctness: a transaction with a newer StartTs reloads and observes deletions committed since the previous load. - Snapshot isolation: the shared cache only ever advances in time. If a newer snapshot is already cached, an older-ts caller is served its own ts-scoped set and does not install it, so it never observes deletions newer than its own snapshot. - Performance: a rebuild streams every key at one StartTs, so the JSON is parsed once and reused across the many removeDeadNodes calls per insert. - Concurrency: the index instance is shared across the goroutines that drive a rebuild. Publication is lock-free via atomic.Pointer + CompareAndSwap; the snapshot map is immutable after construction, so the filter reads it without synchronization. Tests cover the cross-timestamp refresh, within-snapshot stability, snapshot-isolation (older caller unaffected by a newer cached snapshot), and concurrent mixed-timestamp loads under -race. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
a8e9924 to
98a6a65
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
(*persistentHNSW).removeDeadNodesloaded the tombstoned-vector set (the persisted__vector_deadposting) intoph.deadNodesonce, on the first call, and never refreshed it:The index instance lives in the factory's
indexMapfor the life of the process, so once populated the set was frozen. Any vector deleted after the firstremoveDeadNodescall was never seen by the neighbour filter, and dead UIDs leaked back into edge lists during subsequent inserts and neighbour updates. The standing// TODO add a path to delete deadNodesacknowledged the gap.Fix
Cache the dead set as an immutable snapshot tagged with the transaction read timestamp (
TxnCache.Ts()), published viaatomic.Pointer[deadSnapshot]:StartTsreloads the set and observes deletions committed since the previous load.tscaller is served its ownts-scoped set and does not install it, so a transaction never observes deletions newer than its own snapshot.StartTs, so the JSON is parsed once and reused across the manyremoveDeadNodescalls per insert.atomic.Pointer+CompareAndSwap; the snapshot map is immutable after construction, so the filter reads it without synchronization.The hot path (
removeDeadNodes) delegates toloadDeadNodes(tc), which returns the cached snapshot on a timestamp match and otherwise loads, builds, and CAS-publishes a new one.Test
TestRemoveDeadNodesRefreshesAcrossTimestamps— cross-timestamp refresh + within-snapshot stabilityTestRemoveDeadNodesSnapshotIsolation— an older-tscaller is unaffected by a newer cached snapshotTestLoadDeadNodesConcurrent— concurrent mixed-timestamp loads under-racego test -race ./tok/...,go vet,gofmtall clean; golangci-lint (repo config) reports no new findings from this change.🤖 Generated with Claude Code