Skip to content

feat(hnsw): read and write HNSW models through the index-file cache - #32251

Merged
mergify[bot] merged 11 commits into
matrixorigin:mainfrom
cpegeric:hnsw_index_file
Nov 9, 2026
Merged

mergify[bot] merged 11 commits into
matrixorigin:mainfrom
cpegeric:hnsw_index_file

Conversation

@cpegeric

@cpegeric cpegeric commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #31109

What this PR does / why we need it:

Every HNSW model load streams the model's chunk rows through SQL into a temp file and MD5s it, so the first query after a restart or a change pays for the whole model (#31109: 85.7 s at 5M x 1536D). This moves HNSW onto the index-file layer and cache that FULLTEXT2 already uses (design: docs/design/20261024-index-file-io.md).

  • Search, CREATE/REINDEX and the ISCP sync read and write model files through the layer. The writing CN caches each version it writes; a load is served from the cache, or rebuilt once from the rows. Files are named by HNSW's MD5, since a sync rewrites a model under the same id.
  • Background jobs: the layer accepts a SqlProcess with only a SqlContext, reading in its transaction and account with the engine and cache it is given; a rebuild builds its readers on a process of its own. The ISCP sync uses it, so it reads the version the previous sync wrote from the cache.
  • Superseded holds: a cache hold can be released without counting as a use (IndexFileHold.ReleaseSuperseded). An HNSW search evicted because a newer generation replaced it (stale sweep, or the sync's own eviction, now with reason generation_changed) releases its models that way, so the replaced version is evicted first.
  • Sizing: after a sync the cache holds the version a loaded search still maps and the new one; the index-file capacity must be at least twice the largest model a CN keeps loaded (design doc, "HNSW files").

Results (SIFT 500k x 128, one CN, local disk, same data for both binaries):

main this PR
first query after restart 0.82 s 0.062 s (0.83 s with the cache wiped)
sync model load, steady state 0.90 s 0.12-0.14 s

Validation: unit tests in fileservice, indexfile, vectorindex/cache, hnsw, fulltext2, iscp, table_function, including a CREATE -> CDC sync -> search lifecycle test on a real cache that fails on any rebuild from rows or SQL read; new tests under -race -count=100; all HNSW BVT suites and the FULLTEXT2 index-file suites pass.

Limits: measured at 500k on one CN only; a model written on another CN is rebuilt once per version on each CN; searches map the cached file through /dev/fd/N, run on macOS locally and on Linux first in CI.

🤖 Generated with Claude Code

…atrixorigin#31109)

Searches, CREATE/REINDEX and the ISCP sync now read and write HNSW model files
through the index-file layer, so a model is served from the CN's index-file
cache instead of being streamed from its chunk rows through SQL on every load.
Files are named by the MD5 HNSW records, as a sync rewrites a model under its id.

The layer accepts a SqlProcess with only a SqlContext, as a background job has:
it reads in the SqlContext's transaction and account, and a rebuild builds its
readers on a process of its own. A cache hold can be released as superseded,
without counting as a use, which an HNSW search does when a newer generation
replaced it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

A rebuild from chunk rows in a job without a query process, such as an
HNSW CDC sync, opened the storage relation with a typed nil process. The
storage engine bound the relation to no process, and Ranges dereferenced
it: the CN crashed on the first cache miss of a sync (compose BVT,
vector_filtered_search_hnsw.sql), and the restart rebound its lock tables
("lock table bind changed").

scanChunks reopens the relation on the process it builds for such a read,
and openRelation passes no process as an untyped nil, so the engine falls
back to the transaction's process. The test relation opened without a
process now fails Ranges, as the engine does.

Adds HNSW index-file BVT cases like FULLTEXT2's: CLONE, the clone's first
background sync rebuilding its models from rows, REINDEX, DROP and
snapshot RESTORE; and CDC syncs rewriting models, with a named snapshot
reading the replaced version.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cpegeric
cpegeric requested a review from aunjgr November 7, 2026 08:09
# Conflicts:
#	pkg/fileservice/index_cache_test.go
@XuPeng-SH

XuPeng-SH commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: a41d9c6c91d5feb44b0678c039d81666d06397c8

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: a41d9c6c91d5feb44b0678c039d81666d06397c8

Assessment: approve

HNSW coherently reuses the existing transactional index-file cache. The hold and background-process changes preserve ownership without a parallel mechanism. Earlier generation-reuse, invalidation-race and CN-service findings are resolved; no actionable findings remain.

Model: gpt-6.1-sol / max

Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0

Validation:

  • Reviewed all 33 changed paths, affected owners and consumers, all 15 discussion records and linked issue 31109.

  • Final static audit passed 19 checks, including clean frozen source, complete discussion coverage and formatting/parsing of all 28 changed Go files. Receipt: checks/1794046368001079128-3.json; log: review-audit.log.

  • Isolation preflight passed. Receipt: checks/1794046013563159541-3.json; log: preflight.log.

  • Reused supplied current-head successful build, UT, SCA, coverage and PROXY/PESSIMISTIC BVT summaries. Check receipts and retained logs are correlated in validation-evidence.json.

Limitations:

  • Focused local tests stopped before compilation because libmo.so is missing and native artifact reuse was rejected. No fresh native, GPU or live-SQL pass is claimed; receipt checks/1794046042041086388-3.json matches focused-tests.log.

  • Raw CI and author execution logs were unavailable. Single-CN SIFT 500k performance figures remain author measurements; the original 5M-vector outlier and multi-CN performance were not independently reproduced.

The engine test harness sends transactions to the TN through a test
sender, which skips the RPC header that advances the CN's clock to the
TN's. The TN had its own HLC, so after the wall clock stepped back both
clocks advanced only their logical parts, independently, and a
transaction begun right after a commit could get an earlier snapshot:
a create table right after create database failed with "Unknown
database", and TestIndexFileWriteOpenThroughEngine read rows deleted
before its snapshot. The test TN now uses the CN runtime's clock.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@XuPeng-SH

XuPeng-SH commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: a0d1b3130fce645eb57d77524fcab96e4dd62a84

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: a0d1b3130fce645eb57d77524fcab96e4dd62a84

Assessment: approve

HNSW coherently reuses the approved transactional index-file cache. Holds and private background reader processes preserve ownership and CN service binding. The earlier generation-reuse, invalidation-race and prefetch findings are resolved. No actionable findings remain.

Model: gpt-6.1-sol / max

Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0

Validation:

  • Reviewed all 34 changed paths, affected owners, all 17 discussion records and linked issue 31109.

  • Isolation preflight and 12 static audit checks passed, including clean frozen source, parsing/formatting of 29 Go files and both SQL/result statement pairs.

  • Frozen evidence reports successful build, UT, static analysis, coverage and proxy/pessimistic BVT checks.

  • Focused real-engine prefetch tests stopped before compilation because libmo.so is absent and native artifact reuse was rejected. Receipts and logs are reconciled in validation-evidence.json.

Limitations:

  • No fresh local native, race, GPU or live-SQL execution; raw CI and author execution logs were unavailable.

  • Performance figures remain author measurements on one CN with 500k vectors; the original 5M-vector outlier was not independently reproduced.

@cpegeric

cpegeric commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

@aunjgr please review again. thanks

@XuPeng-SH

XuPeng-SH commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: a0d1b3130fce645eb57d77524fcab96e4dd62a84

New discussion was assessed; the previous review remains unchanged.

@aunjgr aunjgr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent re-review at a0d1b31. The HNSW adoption follows the approved index-file design: transaction/account/snapshot visibility stays with the existing chunk tables, full-MD5 file identities distinguish rewritten models, and native model destruction precedes release of the file hold.

My earlier generation-reuse blocker is resolved. Destroy clears the generation marker, and every wrapper teardown consumes the invalidation once, so a losing eviction claimant cannot mark a reused algorithm after teardown. The deterministic failed-load/invalidation tests cover that ordering. Background cold readers now retain the CN service binding, with a real four-block prefetch regression rather than only mock-reader coverage. I found no remaining actionable correctness or lifecycle issue in the current change map.

Evidence reused: current-head successful build, UT, SCA, coverage and both active BVT lanes, plus scoped author regression/race reports. No fresh local native, race or SQL run is claimed. The performance measurements remain one-CN/500k-vector author measurements, not independent verification of the original 5M-vector outlier.

@mergify

mergify Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

✅ Merged into main as 70952cb at 04:59 UTC

1 h 19 min in the queue, 1 h 18 min of it CI.

So far

  • ✅ Entered the queue Mon 9 Nov, 03:41 · rule main · triggered by rule Automatic queue on approval for main
  • ✅ Passed · in place
  • ✅ Merged 04:59 · 70952cb · squash
3 merge conditions met
  • #review-threads-unresolved = 0 · required by GitHub branch protection
  • github-review-approved · required by GitHub branch protection
  • any of · required by GitHub branch protection:
    • check-success = CI Required
    • check-neutral = CI Required
    • check-skipped = CI Required

Times in UTC · dashboard · how the merge queue works

This branch was previously deployed

1 inactive deployment
ci — 2e973a5d Deployed Nov 9, 2026 by mergify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants