Repository navigation
fix(vectorindex): bound HNSW multi-index merge heap to LIMIT (#25637) - #25716
Conversation
…rigin#25637) The HNSW multi-index search pushed every per-file candidate (sum of min(LIMIT, cardinality) over files ~= shard_count * LIMIT) into an unbounded min-heap and truncated to LIMIT only at the end, so peak merge memory scaled with the number of index files. On fragmented indexes this is an OOM vector (follow-up to matrixorigin#25587; relates to matrixorigin#24041, matrixorigin#23904). Make SearchResultSafeHeap a concurrency-safe BOUNDED top-K max-heap: it retains only the `limit` smallest-distance results, evicting the current worst (heap root) in O(log k) once full; rejects are O(1). Peak merge memory is now O(min(LIMIT, total_cardinality)), independent of file count. A max-heap (worst at root) is required so the worst retained item can be evicted cheaply; the search drains it back-to-front to restore ascending (nearest-first) order. boundedHnswSearchLimits no longer needs to return the sum-based heap capacity; the heap bound is simply the result limit. The retained set is the exact global top-K regardless of push order (order-independent invariant), so parallel workers can keep pushing concurrently. Tests: TestSafeHeapBounded (4 shards push 40 candidates concurrently into a bound-10 heap -> retains exactly the 10 smallest); TestBoundedHnswSearchLimits updated for the 2-value signature. Co-Authored-By: Claude Opus 4.8 (1M context) <[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? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
Reviewed the bounded HNSW merge implementation across correctness, performance, concurrency, and unhappy paths. The max-heap invariant retains the exact global smallest K independently of push order; reverse draining preserves nearest-first output; zero/large limits and preallocation are bounded; and the existing worker cancellation/resource lifecycle remains intact. CI is green, and local race/property tests, vectorindex build, and vet all passed. No merge-blocking issues found.
Merge Queue Status
This pull request spent 1 minute 6 seconds in the queue, with no time running CI. ReasonPull request #25716 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. Tick the box to put this pull request back in the merge queue (same as
|
…rigin#25637) (matrixorigin#25716) ## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: issue matrixorigin#25637 ## What this PR does / why we need it: The HNSW multi-index search pushed every per-file candidate (sum of min(LIMIT, cardinality) over files ~= shard_count * LIMIT) into an unbounded min-heap and truncated to LIMIT only at the end, so peak merge memory scaled with the number of index files. On fragmented indexes this is an OOM vector (follow-up to matrixorigin#25587; relates to matrixorigin#24041, matrixorigin#23904). Make SearchResultSafeHeap a concurrency-safe BOUNDED top-K max-heap: it retains only the `limit` smallest-distance results, evicting the current worst (heap root) in O(log k) once full; rejects are O(1). Peak merge memory is now O(min(LIMIT, total_cardinality)), independent of file count. A max-heap (worst at root) is required so the worst retained item can be evicted cheaply; the search drains it back-to-front to restore ascending (nearest-first) order. boundedHnswSearchLimits no longer needs to return the sum-based heap capacity; the heap bound is simply the result limit. The retained set is the exact global top-K regardless of push order (order-independent invariant), so parallel workers can keep pushing concurrently. Tests: TestSafeHeapBounded (4 shards push 40 candidates concurrently into a bound-10 heap -> retains exactly the 10 smallest); TestBoundedHnswSearchLimits updated for the 2-value signature. Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: XuPeng-SH <[email protected]>
…rigin#25637) (matrixorigin#25716) ## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: issue matrixorigin#25637 ## What this PR does / why we need it: The HNSW multi-index search pushed every per-file candidate (sum of min(LIMIT, cardinality) over files ~= shard_count * LIMIT) into an unbounded min-heap and truncated to LIMIT only at the end, so peak merge memory scaled with the number of index files. On fragmented indexes this is an OOM vector (follow-up to matrixorigin#25587; relates to matrixorigin#24041, matrixorigin#23904). Make SearchResultSafeHeap a concurrency-safe BOUNDED top-K max-heap: it retains only the `limit` smallest-distance results, evicting the current worst (heap root) in O(log k) once full; rejects are O(1). Peak merge memory is now O(min(LIMIT, total_cardinality)), independent of file count. A max-heap (worst at root) is required so the worst retained item can be evicted cheaply; the search drains it back-to-front to restore ascending (nearest-first) order. boundedHnswSearchLimits no longer needs to return the sum-based heap capacity; the heap bound is simply the result limit. The retained set is the exact global top-K regardless of push order (order-independent invariant), so parallel workers can keep pushing concurrently. Tests: TestSafeHeapBounded (4 shards push 40 candidates concurrently into a bound-10 heap -> retains exactly the 10 smallest); TestBoundedHnswSearchLimits updated for the 2-value signature. Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: XuPeng-SH <[email protected]>
…rigin#25637) (matrixorigin#25716) ## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: issue matrixorigin#25637 ## What this PR does / why we need it: The HNSW multi-index search pushed every per-file candidate (sum of min(LIMIT, cardinality) over files ~= shard_count * LIMIT) into an unbounded min-heap and truncated to LIMIT only at the end, so peak merge memory scaled with the number of index files. On fragmented indexes this is an OOM vector (follow-up to matrixorigin#25587; relates to matrixorigin#24041, matrixorigin#23904). Make SearchResultSafeHeap a concurrency-safe BOUNDED top-K max-heap: it retains only the `limit` smallest-distance results, evicting the current worst (heap root) in O(log k) once full; rejects are O(1). Peak merge memory is now O(min(LIMIT, total_cardinality)), independent of file count. A max-heap (worst at root) is required so the worst retained item can be evicted cheaply; the search drains it back-to-front to restore ascending (nearest-first) order. boundedHnswSearchLimits no longer needs to return the sum-based heap capacity; the heap bound is simply the result limit. The retained set is the exact global top-K regardless of push order (order-independent invariant), so parallel workers can keep pushing concurrently. Tests: TestSafeHeapBounded (4 shards push 40 candidates concurrently into a bound-10 heap -> retains exactly the 10 smallest); TestBoundedHnswSearchLimits updated for the 2-value signature. Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: XuPeng-SH <[email protected]>
What type of PR is this?
Which issue(s) this PR fixes:
issue #25637
What this PR does / why we need it:
The HNSW multi-index search pushed every per-file candidate (sum of min(LIMIT, cardinality) over files ~= shard_count * LIMIT) into an unbounded min-heap and truncated to LIMIT only at the end, so peak merge memory scaled with the number of index files. On fragmented indexes this is an OOM vector (follow-up to #25587; relates to #24041, #23904).
Make SearchResultSafeHeap a concurrency-safe BOUNDED top-K max-heap: it retains only the
limitsmallest-distance results, evicting the current worst (heap root) in O(log k) once full; rejects are O(1). Peak merge memory is now O(min(LIMIT, total_cardinality)), independent of file count. A max-heap (worst at root) is required so the worst retained item can be evicted cheaply; the search drains it back-to-front to restore ascending (nearest-first) order. boundedHnswSearchLimits no longer needs to return the sum-based heap capacity; the heap bound is simply the result limit. The retained set is the exact global top-K regardless of push order (order-independent invariant), so parallel workers can keep pushing concurrently.Tests: TestSafeHeapBounded (4 shards push 40 candidates concurrently into a bound-10 heap -> retains exactly the 10 smallest); TestBoundedHnswSearchLimits updated for the 2-value signature.