Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal versions are summarized under 0.3.33.
- The consumer and `--rebuild` ignore unfinished `*_pending.npz` segments. (#4)
- A successful `--consume-only` run removes the producers' `producer_N.done` markers, and `vector_build_multi.sbatch` and each producer remove old ones when they start, so the next multi-node job waits for its producers and `--rebuild` is no longer blocked. (#5)
- The batch scripts and the cluster guide remove the writer guard litkit creates, `workspace/sqlite/faiss_writer.guard`, instead of `workspace/.writer_guard`. (#15)
- Builds commit articles in batches instead of one at a time. A producer commits a tar's articles only at the tar boundary, just before it finalizes the tar's segments, so after a failed flush or a job killed mid-tar, a restart without `--rebuild` processes them again instead of skipping them with no vectors. A writer commits at each flush, once the batch is embedded, at each checkpoint and at each tar boundary, so a failed embedding rolls back only the articles since the last of those; earlier rows of the batch still wait for the backfill. Without `--strict-ingest`, an error after which SQLite has rolled back the batch, such as a full disk, now stops the build instead of skipping the article. (#13)

## [0.3.36] - 2026-11-30

Expand Down
44 changes: 33 additions & 11 deletions src/litkit/build/ingest_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ def process_tar_files(
"""
cur = conn.cursor()

# A batch that spilled from the page cache to the database file would hold an
# EXCLUSIVE lock until its commit, locking searches out through the embedding. Keep
# it in memory until the next flush, checkpoint or tar boundary commits it (#13). A
# producer's tar can be large, and no one else reads its shard database.
if not embed_producer:
conn.execute("PRAGMA cache_spill=OFF")

# Load checkpoint
ckpt = seg_load_checkpoint(ckpt_path, shard_id=ckpt_shard_id)
ckpt_stream = ckpt.get("build_stream", {})
Expand Down Expand Up @@ -348,6 +355,11 @@ def _render(force: bool = False):
len(chunk_ords_buf),
)

# Every member since the last commit shares one transaction (#13). A
# SAVEPOINT that opened the transaction would commit this member alone at
# its RELEASE, and no commit or rollback below could include or undo it.
if not conn.in_transaction:
conn.execute("BEGIN")
conn.execute("SAVEPOINT member_sp")
try:
# ---------- BEGIN INGEST BODY ----------
Expand Down Expand Up @@ -459,6 +471,10 @@ def _render(force: bool = False):
conn.execute("RELEASE SAVEPOINT member_sp")
except Exception:
pass
if not conn.in_transaction:
# SQLite rolled back the whole transaction (disk full, I/O error):
# the members before this one are gone but still in the buffers
raise

# Truncate in-memory buffers back to pre-member state
(p_len, pt_len, pd_len, c_len, ct_len, cp_len, co_len) = buf_snapshot
Expand Down Expand Up @@ -512,10 +528,10 @@ def _render(force: bool = False):
progress_done_summary=False,
)
paper_seg_writer.write(doc_ids=u_doc_ids, vecs=Xp)
conn.commit()
papers_added_total += len(u_ids)
# No checkpoint until the tar-boundary finalize(): this segment
# is pending, and cleanup_pending() deletes it if that fails (#3)
# No commit or checkpoint until the tar-boundary finalize(): this
# segment is pending, and cleanup_pending() deletes it if that
# fails, so its rows must roll back with it (#3, #13)
except Exception as e:
conn.rollback()
_eprint(f"[flush] FATAL: paper segment flush failed: {e.__class__.__name__}: {e}")
Expand All @@ -530,6 +546,10 @@ def _render(force: bool = False):
batch_size=paper_embed_bs,
progress_done_summary=False,
)
# Commit the rows before a save can write their vectors: rolled
# back, they would get the same ids on the restart, and the HNSW
# paper index can't remove the first copy (#13)
conn.commit()
prior_ntotal = int(getattr(paper_index, "ntotal", 0) or 0)
with FileLock(faiss_lock):
# skip_dedup=rebuild: in rebuild mode, index starts empty, no dedup needed
Expand Down Expand Up @@ -586,13 +606,10 @@ def _render(force: bool = False):
)
t_seg = time.time() - t_seg_start

t_db_start = time.time()
conn.commit()
t_db = time.time() - t_db_start

chunks_added_total += len(u_ids)
# No checkpoint until the tar-boundary finalize(): this segment
# is pending, and cleanup_pending() deletes it if that fails (#3)
# No commit or checkpoint until the tar-boundary finalize(): this
# segment is pending, and cleanup_pending() deletes it if that
# fails, so its rows must roll back with it (#3, #13)

# --- CYCLE TIMING: emit summary ---
total_docs_for_avg += cycle_docs
Expand All @@ -604,7 +621,7 @@ def _render(force: bool = False):
_eprint(
f"[cycle] docs={cycle_docs} chunks={len(u_ids)} | "
f"scan={t_scan:.1f}s embed={t_embed:.1f}s "
f"seg={t_seg:.2f}s db={t_db:.2f}s | "
f"seg={t_seg:.2f}s | "
f"chunks/doc={avg_cpd:.1f}"
)
# Reset cycle tracking
Expand All @@ -625,6 +642,9 @@ def _render(force: bool = False):
batch_size=chunk_embed_bs,
progress_done_summary=False,
)
# Commit the rows before a save can write their vectors, as for
# papers: every vector a save writes then has a committed row (#13)
conn.commit()
prior_ntotal = int(getattr(chunk_index, "ntotal", 0) or 0)
with FileLock(faiss_lock):
# skip_dedup=rebuild: in rebuild mode, index starts empty, no dedup needed
Expand Down Expand Up @@ -717,7 +737,7 @@ def _render(force: bool = False):
chunk_paper_doc_ids_buf.clear()
chunk_ords_buf.clear()

# Phase 2: Commit DB (rows now durable)
# Phase 2: Commit DB: this tar's rows, all in one transaction (#13)
conn.commit()

# Phase 3: Finalize segments (atomic rename .pending → .npz)
Expand Down Expand Up @@ -860,6 +880,8 @@ def _render(force: bool = False):

# Final commit
conn.commit()
if not embed_producer:
conn.execute("PRAGMA cache_spill=ON")

# Return indices and counts (indices may have been wrapped)
return paper_index, chunk_index, papers_added_total, chunks_added_total
Expand Down
5 changes: 4 additions & 1 deletion src/litkit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1802,7 +1802,10 @@ def main():
ap.add_argument(
"--strict-ingest",
action="store_true",
help="Fail fast on ingest errors instead of mark-and-skip (default: skip failed members and continue)",
help=(
"Fail fast on ingest errors instead of mark-and-skip (default: skip failed members "
"and continue, unless SQLite rolled back the batch, e.g. when the disk is full)"
),
)

# index types / params
Expand Down
Loading
Loading