Skip to content

Commit each batch of articles in one transaction - #44

Merged
wshlavacek merged 3 commits into
mainfrom
fix/batch-transactions
Dec 2, 2026
Merged

wshlavacek merged 3 commits into
mainfrom
fix/batch-transactions

Conversation

@wshlavacek

Copy link
Copy Markdown
Collaborator

Fixes #13

Problem

process_tar_files() wrapped each article in SAVEPOINT member_sp with no transaction open. The savepoint was therefore the outermost transaction, and its RELEASE committed the article on its own. None of the batch-level conn.commit() / conn.rollback() calls could include or undo anything. When a flush failed, the articles stayed committed with no vectors, and a restart without --rebuild skipped them through the files table. For producers, nothing in the multi-node job repairs that: the consumer merges those rows at in_index=0, and only a later writer run's backfill would embed them. vector_build_multi.sbatch runs producers without --rebuild, and the gap shows up whenever the shard database survives the failure: with USE_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, BEGIN runs if no transaction is open. Each article's savepoint now nests inside the batch transaction, and its RELEASE no 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 at in_index=0 and 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 AUTOINCREMENT counter 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 after mark_in_index now 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=OFF for 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-every articles (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-ingest help text says so.

  • The producer's [cycle] log line drops db=, 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:

  • a failed tar-boundary flush rolls back every article whose segments cleanup_pending() deletes;
  • a producer killed mid-tar (for example at the SLURM time limit) leaves nothing committed for that tar, so the restart processes the whole tar again. On main, those articles were committed, their pending segments were never finalized, and the restart skipped them;
  • the commit and the checkpoint now move together, because producers already checkpoint only at the boundary (Fix data loss and stale state in multi-node builds #41).

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.done exists, 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

  • Legacy transaction control (isolation_level="DEFERRED", Python 3.12). SAVEPOINT doesn't issue BEGIN, and an INSERT inside an open savepoint doesn't either. An explicit BEGIN inside an open transaction raises "cannot start a transaction within a transaction". The BEGIN therefore runs only when conn.in_transaction is false. conn.commit() and conn.rollback() end the explicit transaction as usual. I checked each of these with the Python sqlite3 module.
  • Writer commits after FAISS saves and 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 at in_index=0 whose vectors are saved. Reconcile marks those rows present, as on main.
  • seen_this_path. Its DELETE and UPDATE under FileLock(db_lock) are no longer committed at the article's RELEASE. 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 with in_index=1 but 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-back files row 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.
  • Periodic checkpoint commit. Unchanged. For writers it is now a commit point between flushes (every --ckpt-every, default 500 articles) instead of every article. Producers still don't checkpoint mid-tar.
  • Final flushes after the tar loop. The tar-boundary commit has already committed the rows, and the writer's final flush commits its marks. The producer's final-flush branch is unreachable with non-empty buffers, because the tar-boundary flush either clears them or raises.
  • --rebuild's VACUUM. It runs before process_tar_files(), after its own commit, with isolation_level=None. process_tar_files() ends with conn.commit(), so it never returns with a transaction open.

Not changed here

  • Writer rows that are committed before their vectors exist. A writer commit at a checkpoint, at a flush of the other buffer, or at the tar boundary includes rows whose vectors are still buffered. So does a flush whose FAISS add or save fails. Those rows stay at in_index=0 until 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.
  • Producer 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, the update restart 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 --rebuild indexes every paper and chunk (in_index=1, and the saved FAISS ids match the rows). On main, process_tar_files() left 2 papers at in_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; without cache_spill=OFF, the reader gets "database is locked".
  • test_error_that_ends_the_batch_transaction_stops_the_build: a simulated SQLITE_FULL rollback 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_install failures 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

wshlavacek and others added 3 commits December 1, 2026 20:35
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
wshlavacek merged commit 2cdde71 into main Dec 2, 2026
4 checks passed
@wshlavacek
wshlavacek deleted the fix/batch-transactions branch December 2, 2026 05:16
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.
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.

Each article is committed separately, and batch rollback doesn't undo it

1 participant