Repository navigation
Commit each batch of articles in one transaction - #44
Merged
Merged
Conversation
process_tar_files() wrapped each member in SAVEPOINT member_sp with no transaction open, so the savepoint was the outermost one and its RELEASE committed the article on its own. The batch-level commit and rollback calls did nothing: a failed embedding, FAISS or segment flush left the articles committed with no vectors, and a restart without --rebuild skipped them through the files table. Open a transaction before the first member after each commit, so the member savepoints nest inside it. Producers no longer commit after mid-tar flushes, whose segments stay pending until the tar boundary: each tar's rows commit only there, before finalize(), and roll back with cleanup_pending() if the flush fails. Writers also commit after a flush whose FAISS save was throttled, so a later save never writes vectors for rows a rollback could remove. An error after which SQLite rolls back the whole transaction now stops the build, since the buffers still hold the members it removed. Fixes #13 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Bill Hlavacek <bill.hlavacek@gmail.com>
With one transaction per batch, a writer's batch that outgrows the page cache spills to the database file and holds an EXCLUSIVE lock until the commit, through the embedding and the FAISS save. Searches, with a 30 s busy timeout, failed with "database is locked". Writer and plain runs now turn cache_spill off for the tar loop; a batch stays in memory until the next flush or checkpoint commits it. Producers keep spilling into their own shard databases, which no one else reads. Also scope the CHANGELOG entry to what a writer's rollback covers (the rows since its last commit), note in the --strict-ingest help that an error after which SQLite rolls back the batch stops the build, and fix two test docstrings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Bill Hlavacek <bill.hlavacek@gmail.com>
The writer committed a batch's rows only after the FAISS save. If anything failed, or SIGTERM arrived, between the save and the commit, the rollback removed rows whose vectors were already on disk; the restart added the same articles again under the same ids, since the AUTOINCREMENT counter rolls back too, and the HNSW paper index, which can't remove vectors, kept both copies. Commit the rows at each flush once the batch is embedded, before it is added to the index. A failed embedding still rolls back the rows since the last commit (a flush, a checkpoint at least every --ckpt-every articles, or a tar boundary); a failed FAISS add or save leaves the rows at in_index=0, which the writer's reconcile and backfill after the tar loop repair, as before. This also makes the commit after a throttled save unnecessary, and a writer's open transaction now spans the embedding but not the save. The writer tests now use an HNSW paper index and load the indices a failed run saved, as a restart does. The CHANGELOG says how much of a batch a writer's rollback covers and that an error after which SQLite rolls back the batch stops the build. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Bill Hlavacek <bill.hlavacek@gmail.com>
wshlavacek
added a commit
that referenced
this pull request
Dec 2, 2026
Keep this branch's batch publication in ingest_loop.py and port what #44 added on top of per-member commits: - Writers turn off cache_spill while a batch is open, so readers are not locked out while the batch is embedded. - When SQLite has rolled back the whole batch (disk full, I/O error), the member's error handler re-raises instead of skipping the member. #44's tests now check this branch's publication: a writer killed after its commit, before or after the index files move into place, finishes that batch on restart; other connections see papers only after their batch commits. The two #13 CHANGELOG entries are now one. Also sort the local imports ruff flags and run black on the test files that were clean on main.
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.
Fixes #13
Problem
process_tar_files()wrapped each article inSAVEPOINT member_spwith no transaction open. The savepoint was therefore the outermost transaction, and itsRELEASEcommitted the article on its own. None of the batch-levelconn.commit()/conn.rollback()calls could include or undo anything. When a flush failed, the articles stayed committed with no vectors, and a restart without--rebuildskipped them through the files table. For producers, nothing in the multi-node job repairs that: the consumer merges those rows atin_index=0, and only a later writer run's backfill would embed them.vector_build_multi.sbatchruns producers without--rebuild, and the gap shows up whenever the shard database survives the failure: withUSE_LOCAL_STAGING=0, when the script falls back to shared storage, or on a manual rerun.What the fix does
One transaction per batch. Before an article's
SAVEPOINT,BEGINruns if no transaction is open. Each article's savepoint now nests inside the batch transaction, and itsRELEASEno longer commits. The existing commit and rollback points close that transaction, so they do what they say.Producers commit only at the tar boundary, just before
finalize(). See below.Writers commit at each flush, once the batch is embedded and before its vectors go into FAISS. They also still commit at each checkpoint and at each tar boundary. A failed embedding, the common failure (for example a CUDA out-of-memory error), rolls back only the articles since the last commit, and the restart processes those again. With the defaults (
--ckpt-every 500,--paper-batch 20000), that is at most 500 articles of a failed paper batch. The batch's earlier rows stay committed atin_index=0and wait for the backfill, as on main. For writers, the main gain is speed: one commit per checkpoint or flush instead of an fsync per article.Committing after the FAISS save instead would roll back rows whose vectors are already on disk whenever something fails, or SIGTERM arrives, between the save and the commit. The restart would then add the same articles again under the same ids, because the
AUTOINCREMENTcounter rolls back too, and the HNSW paper index can't remove the first copies.With this order, a failed FAISS add or save leaves the batch's rows at
in_index=0. The writer's reconcile and backfill after the tar loop repair that, as on main. The commit aftermark_in_indexnow carries only the marks.Searches keep reading while a writer embeds a batch. In the default rollback-journal mode, a transaction that outgrows the page cache (2 MB by default) spills to the database file. That takes an EXCLUSIVE lock, held until the commit. A batch would then lock searches out through its embedding, which can take longer than their 30 s busy timeout.
Writer and plain runs therefore set
PRAGMA cache_spill=OFFfor the tar loop and restore it at the end. A batch stays in memory until the next flush, checkpoint or tar boundary commits it: at most--ckpt-everyarticles (default 500), or one paper or chunk batch. Producers keep spilling, because a whole tar may not fit in memory and no one else reads a shard database.An error that makes SQLite roll back the whole transaction stops the build. On disk full or an I/O error, SQLite may roll back the entire transaction, not just the statement, and the transaction now holds every article since the last commit. The embedding buffers still reference those rows, so skipping the article and continuing (the default without
--strict-ingest) would embed rows that no longer exist. The build stops, and a restart redoes those articles. The--strict-ingesthelp text says so.The producer's
[cycle]log line dropsdb=, because there is no mid-tar commit left to time.Producers: commit only at the tar boundary
A mid-tar flush leaves its segments pending until
finalize()at the tar boundary. If the tar-boundary flush fails,cleanup_pending()deletes them. A mid-tar commit would make their rows durable anyway, and the restart would skip them through the files table. That is the #3 data loss again, reached through the files table instead of the checkpoint. With the commit only at the boundary:cleanup_pending()deletes;The cost is that a shard database's transaction spans a whole tar. Each shard database belongs to one producer, and the consumer merges them only after every
producer_N.doneexists, so no other connection waits on that lock. The commit also happens once per tar instead of once per article, which removes an fsync per article (synchronous=FULL) on NFS and Lustre.Interactions checked
isolation_level="DEFERRED", Python 3.12).SAVEPOINTdoesn't issueBEGIN, and anINSERTinside an open savepoint doesn't either. An explicitBEGINinside an open transaction raises "cannot start a transaction within a transaction". TheBEGINtherefore runs only whenconn.in_transactionis false.conn.commit()andconn.rollback()end the explicit transaction as usual. I checked each of these with the Python sqlite3 module.mark_in_index. The rows are already committed by then, so these commits carry only the marks. A failure between the save and the marks leaves rows atin_index=0whose vectors are saved. Reconcile marks those rows present, as on main.seen_this_path. ItsDELETEandUPDATEunderFileLock(db_lock)are no longer committed at the article'sRELEASE. Only one writer runs per database, so the file lock still only orders statements, as before. If the batch rolls back after that branch saved its FAISS removal, the old rows come back within_index=1but their chunk vectors are gone. So is the paper vector, for a flat paper index; the HNSW paper index can't remove it, so it stays. The rolled-backfilesrow keeps the old size and mtime, so the restart processes that article again and replaces its rows and vectors. A failed article left the same state on main. Every vector in the in-memory index belongs to a committed row, so that branch's forced save never writes vectors for rows a rollback could remove.--ckpt-every, default 500 articles) instead of every article. Producers still don't checkpoint mid-tar.--rebuild'sVACUUM. It runs beforeprocess_tar_files(), after its own commit, withisolation_level=None.process_tar_files()ends withconn.commit(), so it never returns with a transaction open.Not changed here
in_index=0until the backfill that a writer runs after its tar loop re-embeds them (see Backfill re-adds the same batch while FAISS saves are throttled #6). The restart skips them through the checkpoint or the files table, as before.finalize()swallows rename errors.finalize()logs a warning and carries on if renaming a pending segment fails. A kill between the tar-boundary commit and the renames has the same effect. Either way, the tar's rows are committed while a segment is still pending, and the restart skips those articles. This is unchanged, but the window shrinks from the whole tar to the renames.Tests
All in
tests/test_segments.py. The writer helper now uses an HNSW paper index, the default, and loads the indices a failed run saved, as a restart does. The first four tests fail on main (1527199) and pass here:test_producer_checkpoint_counts_no_article_whose_segment_is_deleted[rebuild|update]: the Producer checkpoint can cover segments that are later deleted #3 test, now with a restart without--rebuild(update). After the failed tar-boundary flush, the shard database has no rows, and the restart produces paper and chunk segments for all 3 articles. On main, theupdaterestart produces none.test_writer_restart_indexes_every_article_after_a_failed_flush: after a failed writer embedding, with no checkpoint since the last commit, no rows are left. The restart without--rebuildindexes every paper and chunk (in_index=1, and the saved FAISS ids match the rows). On main,process_tar_files()left 2 papers atin_index=0. In a CLI run, the backfill after the tar loop would have re-embedded them; producers have no such backfill.test_other_connections_see_no_article_before_its_batch_commits[producer|writer]: a second connection counts the papers at each embedding call and fails at once if the database is locked. The producer sees[0, 0, 0, 0, 0]until the tar boundary. The writer sees[0, 2, 4, 7, 7], one commit per flush, even though only the first, forced, save runs. On main, both see[2, 4, 6, 7, 7]. Without the commit after embedding, the writer sees[0, 2, 2, 7, 7]. The writer's page cache is set to 10 pages, so its batch outgrows it as a large batch outgrows the default; withoutcache_spill=OFF, the reader gets "database is locked".test_error_that_ends_the_batch_transaction_stops_the_build: a simulatedSQLITE_FULLrollback stops the build instead of skipping the article. On main, the build continues.test_writer_restart_after_a_failure_past_the_save_adds_no_second_vector: a failure right after the first FAISS save, then a restart. The HNSW paper index holds ids[1, 2, 3]. With the rows committed only after the save, it held[1, 1, 2, 2, 3]. This test passes on main by design, because main committed every article before any save; it guards the ordering above.Full local run on an Intel Mac: 359 passed, 8 xfailed, 1 skipped. The 2
test_cpu_installfailures occur only because litkit isn't installed as a package there; CI covers them. Ruff (--no-fix) and Black report nothing on the changed lines.This closes the restart gap #41 left in #3. #41 kept the producer checkpoint from counting articles whose segments a failed flush deletes, but the restart in its test still needed
--rebuild. The files table skipped those articles anyway, because each one had been committed on its own.🤖 Generated with Claude Code