Skip to content

Fix data loss and stale state in multi-node builds - #41

Merged
wshlavacek merged 1 commit into
mainfrom
fix/multi-node-build
Dec 2, 2026
Merged

wshlavacek merged 1 commit into
mainfrom
fix/multi-node-build

Conversation

@wshlavacek

Copy link
Copy Markdown
Collaborator

Fixes six bugs in multi-node (producer/consumer) builds. Each fix has a regression test that fails on main and passes here.

Fixes #1
Fixes #2
Fixes #3
Fixes #4
Fixes #5
Fixes #15

#1: shard merge stops after 10 shards

merge_shard_databases() now commits each shard's INSERTs before DETACH DATABASE. A shard whose merge fails is rolled back (merged completely or not at all) and kept for a rerun. A failed DETACH is no longer ignored: it raises ShardDetachError (a sqlite3.OperationalError) and ends the merge, because a shard left attached makes every ATTACH after the tenth fail.

Tests (tests/test_db.py): merging 12 shard databases brings in all 12 papers, chunks and files rows and leaves no shard attached (main: 10 of 12, too many attached databases - max 10). A connection that leaves a query open on the shard after each commit makes DETACH fail with "is locked" once shard 0's merge is committed; the merge now raises and stops before shard 1 (main: no error).

#2: consumer deletes segment files before their vectors are saved

Both segment ingesters keep each claimed .ingesting file until a successful save covers its vectors: the immediate save after an add, the periodic save, or a new forced save at the end of the function. Files that error after their vectors were added stay claimed instead of being renamed back.

A job killed after a save but before the delete leaves a claimed file whose vectors are already saved. On rerun, a claimed file skips IDs that an HNSW index already holds, so the paper index (which can't remove vectors) doesn't get duplicates; other indexes replace those vectors as before.

run_consume_only_mode() no longer force-saves both indexes at the end, since each ingest function now saves what it added; that avoids a second full write of each index. vector_resume_consumer.sbatch counts the *.npz.ingesting files that a stopped consumer keeps, so the documented resume path doesn't report "Nothing to do".

Tests (tests/test_segments.py), with _SAVE_MIN_SEC throttled so only the first forced save runs:

  • Papers and chunks: killed at the final save, then rerun from the saved index, all 8 vectors are saved and no segment files remain (main: only the first segment's 2 vectors; the other files were already deleted).
  • HNSW papers: killed at the first segment delete right after a save, the rerun ends with 8 vectors for 8 papers (main: 10).
  • A retried claimed chunk segment still replaces existing vectors in a flat index. This one guards the HNSW-only skip above, so it also passes on main.
  • tests/integration/test_multi_node.py: the resume script's segment count includes a claimed .npz.ingesting file (main: not counted; skipped on Windows).

#3: producer checkpoint covers segments that are later deleted

In producer mode, mid-tar flushes no longer advance the checkpoint, so it advances only at the tar boundary, after finalize().

Why not finalize at mid-tar flushes: the paper and chunk buffers flush independently (different batch sizes). At a paper flush, chunks for the same articles are usually still in memory, so finalizing there would publish paper vectors whose chunks may never be written, and a checkpoint advanced at that point would still cover articles whose chunk vectors exist nowhere on disk. Checkpointing only after the tar boundary, when both buffers are written and finalized, avoids both. Cost: a producer that stops mid-tar re-scans that tar on restart.

Remaining gap (#13, not fixed here): each article is committed on its own when its savepoint is released (#13). A producer restarted without --rebuild (the batch scripts' default) still skips those articles through the files table, whatever the checkpoint says. This PR makes the checkpoint correct; the files-table path needs #13.

Test (tests/test_segments.py): a producer with paper_batch=2 flushes papers once mid-tar, then its chunk embedder fails on the tar-boundary batch (as a CUDA OOM would). The checkpoint counts no article of that tar (main: 2), and a restart (with --rebuild, so only the checkpoint decides) produces segments for all 3 articles.

#4: consumer picks up *_pending.npz

_glob_segment_files() and has_segment_files() exclude names ending in _pending.npz, so the consumer, --rebuild's "unconsumed segments" check and shard validation see only finalized segments.

Test: the existing strict-xfail test_consumer_does_not_see_pending_segments now passes; its marker is removed.

#5: producer .done markers are never removed

run_consume_only_mode() calls cleanup_done_files() after a successful consume. Producers call the new ProducerCoordinator.clear_stale_marker() at startup, before any work, to remove their own marker from an earlier run. With node-local staging (the default in vector_build_multi.sbatch), producers write markers locally and rsync them to the shared emb_segments/ at the end, so they can't see an old marker there; the script therefore removes producer_*.done from the shared directory at job start, before launching producers.

Tests:

  • tests/test_segments.py: a consume that times out keeps the finished producer's marker; once both producers are done, the consume indexes everything and leaves no segments or markers (main: both markers remain).
  • New tests/integration/test_multi_node.py, running the real CLI with the fake embedders: two producers and a consumer index all 3 papers and 6 chunks, leave only build_meta.json, and a following --rebuild is not blocked (main: blocked with "Prior multi-node run (not consumed)"). A producer that fails right after starting has removed its own stale marker and left the other shard's (main: stale marker kept). The multi-node script removes the markers before it starts any producer (main: no such line).

Related, not fixed here (#40): vector_resume_consumer.sbatch writes .shard_XX_complete markers, which the consumer never reads. Since a successful consume now removes the real markers, rerunning that script after a consume that left some segments uningested waits for producers until it times out.

#15: batch scripts delete the wrong writer guard

The three batch scripts now rm -f .../sqlite/faiss_writer.guard. In vector_build_multi.sbatch the producer line uses ${PRODUCER_LOCAL_STAGE_HOST}/sqlite/faiss_writer.guard, since producers run with LITKIT_WORKSPACE=/local_stage, which is bound to that host directory. The troubleshooting step in LITKIT_CLUSTER_GUIDE.md and the comments in cli.py name the same path.

Test (tests/integration/test_writer_locking.py): derives the guard path from WorkspacePaths, fails if any shipped script, doc or source file names .writer_guard (CHANGELOG.md, which records the old path, is skipped), and checks that each batch script removes the real guard and the guide tells users to (main: 5 files name .writer_guard).

litkit_documentation.pdf still says rm workspace/.writer_guard. It's generated from the README and guides with pandoc and would also pick up every other doc change since it was last built, so it isn't regenerated here.

Testing

  • Local (Intel macOS, Python 3.12, numpy<2, faiss-cpu 1.8): pytest tests gives 353 passed, 8 xfailed, 1 skipped, versus 338 passed and 10 xfailed on main. The two test_cpu_install cases fail in both runs because litkit isn't installed as a package in that venv; CI covers them.
  • Ruff and black are clean on the changed lines.
  • Independently reviewed by Claude Fable over three rounds; the last two found no blocker or should-fix items.
  • CI: Linux ARM64, macOS ARM64, Windows x64.

🤖 Generated with Claude Code

- Shard merge commits each shard before DETACH and raises when a shard
  can't be detached, so producers 10 and up are no longer lost (#1).
- Segment ingest deletes a file only after a save covers its vectors and,
  on rerun, skips IDs an HNSW index already holds (#2). The consumer no
  longer repeats the ingesters' final save, and the resume script counts
  claimed .npz.ingesting files.
- Producer checkpoints advance only after the tar-boundary finalize() (#3).
- The consumer glob and has_segment_files() ignore *_pending.npz (#4).
- A successful consume removes producer_N.done markers; producers and
  vector_build_multi.sbatch remove stale ones at start (#5).
- Batch scripts, the cluster guide and cli.py comments name the real
  writer guard, sqlite/faiss_writer.guard (#15).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Bill Hlavacek <bill.hlavacek@gmail.com>
@wshlavacek
wshlavacek merged commit 1527199 into main Dec 2, 2026
4 checks passed
@wshlavacek
wshlavacek deleted the fix/multi-node-build branch December 2, 2026 03:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant