Skip to content

fix(engine) #5747: do not rebuild a persisted HNSW graph on close - #5787

Merged
lvca merged 1 commit into
ArcadeData:mainfrom
humemai:fix/5747-no-graph-rebuild-when-persisted
Aug 5, 2026
Merged

lvca merged 1 commit into
ArcadeData:mainfrom
humemai:fix/5747-no-graph-rebuild-when-persisted

Conversation

@tae898

@tae898 tae898 commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #5747.

LSMVectorIndex.flush() rebuilds the HNSW graph when graphState is LOADING
or MUTABLE. MUTABLE is right: there are pending changes. LOADING is not,
because it means "not loaded into memory", which is not the same as "not
persisted on disk".

The graph loads lazily on the first search, so a session that only reads
documents leaves the state at LOADING even when a complete graph is already on
disk, and close then rebuilds it from scratch to produce a file that already
exists. initializeGraphIndex() already draws exactly this distinction with
graphFile.hasPersistedGraph(); this applies the same test in flush().

Measured

One host, 64-dim COSINE, 9 reps with 3 warmup, median. Both arms are the same
harness on the same data; a classpath check confirms which jar each arm loaded.

vectors what the session did close before close after
100k read a document 6,197.79 ms 4.34 ms
1M read a document 216,068.3 ms 13.2 ms
1M two indexes, queried neither 379,014.3 ms 15.2 ms
1M two indexes, queried one 190,448.5 ms 14.9 ms
1M ran a vector search 14.1 ms 16.3 ms
3M read a document 1,006,000 ms not re-run

The fifth row is the safety check. Sessions that do search already skipped the
rebuild, and this leaves them where they were.

At 3M the close was 1,006 s against an original build of 1,011 s, so the close
rebuild is a complete re-run of the build. The cost is also per index rather
than per database: with two indexes and only one queried, the untouched one
still cost 190 s on its own.

Correctness

  • New test VectorIndexNoRebuildOnUnusedCloseTest asserts the persisted graph
    is byte-identical and unmodified after a session that reads documents and
    never searches. It fails on unpatched code and passes with the change.
  • Separately: after adding 200 vectors to an existing index, closing and
    reopening, all 200 new and all 200 pre-existing vectors are still found by
    search, identically on patched and unpatched builds. The MUTABLE branch
    still rebuilds as it should.
  • The full com.arcadedb.index.vector suite (22 classes) passes.

One behaviour change worth stating

If a session adds vectors and then crashes before close, the on-disk graph is
stale and the state is LOADING. The current unconditional rebuild repairs that
incidentally. With this change the repair happens at the next search instead,
through the staleness check already in ensureGraphAvailable()
(graphSize < rebuiltOrdinalToVectorId.length, added for #3722). So the repair
moves from incidental to deliberate rather than disappearing, but it is a real
change and you may prefer a different call on it.

Happy to adjust the approach or add cases to the test.

… close

`LSMVectorIndex.flush()` rebuilds the graph when `graphState` is LOADING or
MUTABLE. MUTABLE is correct: there are pending changes. LOADING is not, because
LOADING means "not loaded into memory", which is not the same as "not persisted
on disk".

The graph is lazily loaded on the first search, so a session that only reads
documents leaves the state at LOADING even when a complete graph is already on
disk. Close then rebuilds it from scratch to produce a file that already exists.
The cost is O(n log n) in the index size and lands on sessions that never
touched the index.

`initializeGraphIndex()` already draws exactly this distinction with
`graphFile.hasPersistedGraph()`; this applies the same test in `flush()`.
LOADING now rebuilds only when there is genuinely nothing on disk.

Measured on one host, 64-dim COSINE index, 9 reps with 3 warmup, median:

| vectors | session | close before | close after |
|---|---|---|---|
| 100k | reads a document | 6,197.79 ms | 4.34 ms |
| 1M | reads a document | 216,068.3 ms | 13.2 ms |
| 1M | runs a vector search | 14.1 ms | 16.3 ms |
| 3M | reads a document | 1,006,000 ms | - |

The third row is the safety check: sessions that do search the index already
skipped the rebuild, and this change leaves them alone. At 3M the close was
1,006 s against an original build of 1,011 s, so the rebuild is a complete
re-run of the build.

Correctness checks beyond the new test: after adding 200 vectors to an existing
index, closing and reopening, all 200 new and 200 pre-existing vectors are still
found by search, on both the patched and unpatched build. The full
com.arcadedb.index.vector suite (22 classes) passes.

One behaviour change worth stating: if a session adds vectors and then crashes
before close, the on-disk graph is stale and the state is LOADING. Today's
unconditional rebuild repairs that incidentally; with this change the repair
happens at the next search instead, via the existing staleness check in
`ensureGraphAvailable()` (`graphSize < rebuiltOrdinalToVectorId.length`). So the
repair moves from incidental to deliberate rather than disappearing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@mergify

mergify Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@lvca
lvca self-requested a review August 5, 2026 16:27
@lvca lvca added this to the 26.9.1 milestone Aug 5, 2026
@lvca
lvca merged commit 781bdba into ArcadeData:main Aug 5, 2026
16 of 20 checks passed
@lvca

lvca commented Aug 5, 2026

Copy link
Copy Markdown
Member

Thanks @tae898 !

tae898 added a commit to humemai/arcadedb-embedded-python that referenced this pull request Aug 11, 2026
Brings the fork up to 26.9.1-SNAPSHOT, which carries the August fixes we
filed: ArcadeData#5711 (HTTP row truncation), ArcadeData#5745/ArcadeData#5788 (GAV), ArcadeData#5746 (Cypher through
GAVFusedChain), ArcadeData#5747 (close-time HNSW rebuild, our PR ArcadeData#5787), ArcadeData#5796 (Redis
port), ArcadeData#5824 (@Rid IN replanned as FETCH FROM RIDs). All verified against
the shipped jars before this merge.

Conflicts resolved:
- 15 .github files kept deleted. Upstream CI the fork does not run; we carry
  only the five Python workflows.
- test-python-examples.yml took upstream's two dependabot SHA bumps
  (setup-java v5.6.0 -> v5.7.0, cache v4.3.0 -> v6.1.0), staying aligned
  with the SHA-pinning their review requires.
- test_server_wire_protocols.py kept ours. Upstream carries the version from
  PR ArcadeData#5807, which still has the redis xfail; ArcadeData#5796 is fixed and the marker
  was retired in a5d82c0.

Behaviour changes to watch, none of which renamed or removed a JPype-facing
API: ArcadeData#5969 now validates SQL function and method arity, ArcadeData#5908 makes sum/avg
reject non-numeric input, ArcadeData#5983 bounds regex backtracking, and ArcadeData#5786 caps
vector.neighbors groupBy output per index.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_0173qFfapQBiGxaV7apSZ4X8
tae898 added a commit to humemai/arcadedb-embedded-python that referenced this pull request Aug 19, 2026
…ebuild

The fp32 embedded arm cannot complete a deep10m cell in a 24g heap on 26.8.1,
under any graphBuildCacheSize or graphBuildCacheMaxHeapPercent setting we
tried: 25/15/10/5% and a fixed 100k bound all failed. That uniformity is the
clue. If the corpus cache were binding, a 1.2 GiB cache should not behave like
a 5.36 GiB one.

What every failing arm shares is that it CLOSES. Since ArcadeData#5747,
LSMVectorIndex.flush() rebuilds the graph whenever graphState is LOADING,
which means "not resident" rather than "not on disk", so a close constructs a
second graph while the first index and its caches are still referenced. The
evidence lines up: close_s tracks build_s on every dense embedded arm (5636 s
against a 5550 s build at deep10m, 154 s at small), the one OutOfMemoryError
was thrown INSIDE close(), the served arm is unaffected because its close() is
a no-op that never touches the database, and the int8 arm survives because its
quantized graph is small enough that two fit.

The fix is ours, PR ArcadeData#5787 merged 08-05, and it is NOT in 26.8.1, which was
tagged 08-03. No dev tag carries it either; it exists only on main.

So this flag answers the question one cell can answer: skip the close and see
whether the build ever needed the heap. The row records close_skipped and a
reason, because a cell that silently omits a phase is not comparable with one
that does not.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01E2Ytwsz6uQvs1dBoqGBSrV
@tae898
tae898 deleted the fix/5747-no-graph-rebuild-when-persisted branch August 20, 2026 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Any session that does not run a vector search pays a full HNSW rebuild on close (602-843 ms vs 17 ms)

2 participants