Skip to content

feat(sqlite-embedded): commit sqlite transactions locally and flush them in the background - #5882

Draft
eersnington wants to merge 3 commits into
mainfrom
feat/sqlite-deferred-commits
Draft

eersnington wants to merge 3 commits into
mainfrom
feat/sqlite-deferred-commits

Conversation

@eersnington

@eersnington eersnington commented Oct 17, 2026 •

Copy link
Copy Markdown
Member
  • Adds a deferred commit mode to the SQLite VFS. A commit goes to a local overlay and returns. A background flusher makes it durable.
  • Awaited commits stay the default and do not change.
  • commit_seq returns the latest local commit. wait_for_flush(seq) resolves when that commit is durable.
  • One coalesced batch is in flight at a time, so durable state is always a prefix of commit history.
  • Reads check unflushed pages before depot.
  • When the overlay is full, the next commit waits for the flusher.
  • A flush retries with backoff for 30 s by default. Then the database breaks and waiters fail.
  • A retry after a lost reply recognizes the batch as saved. A different writer breaks the database.
  • Close drains held commits.
  • Statement errors carry the SQLite result code, message, and offset.
  • rivetkit-core opens databases in awaited mode and reports failures through wait_for_failure, so this PR compiles by itself.

Rebased onto main; fixed fork-start durability under deferred commits.

Stack: #5882 (this PR), #5883, #5911.

@railway-app

railway-app Bot commented Oct 17, 2026

Copy link
Copy Markdown

This PR was not deployed automatically as @eersnington does not have access to the Railway project.

In order to get automatic PR deploys, please add @eersnington to your workspace on Railway.

@claude

claude Bot commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Review: deferred SQLite commit mode

I read the core of this change: the vfs.rs flusher, overlay and close paths, database.rs, the flags, and the rivetkit-core wiring. I did not run the code or the tests. The design is coherent and matches the PR description. The single in-flight batch, flushed_seq and durable_head_txid publication under one lock, and break-before-notify ordering all look right. Nothing is blocking. These are the points I'd want addressed or confirmed.

Questions and possible bugs

  1. Rollback after SQLITE_FULL on the non-atomic path. stage_deferred_local_commit returns Full from io_sync and io_truncate, and leaves write_buffer.dirty populated.
    • SQLite then rolls back through the journal. That writes original pages and truncates, and both go through io_write/io_truncate and may commit again.
    • If the backlog is still full, pinned_pages > page_limit can fail the rollback commit too. A failed rollback is the dangerous case.
    • Please confirm a test covers a non-atomic (journal) transaction that hits Full, followed by a successful rollback and a subsequent write. The atomic path is clean because rollback_atomic_write clears the buffer.
  2. Opening a brand-new actor with the flag on. VfsContext::new rejects Deferred when initial_pages.head_txid.is_none(). Is head_txid always Some for an actor with no commits yet? The synthesize_empty_page path in Deferred mode suggests the empty-db case exists. If None is possible there, opening a fresh actor fails.
  3. Fence-mismatch recovery depends on parsing a message string. parse_actual_head_txid matches "current head txid ", "actual head txid " and "engine has ". If the engine wording changes, the lost-reply recovery silently degrades to HeadDiverged, which breaks the actor.
    • It fails safe, but I'd rather have a structured field, or a test pinned to the engine's real message (not a hand-built string).
  4. Permanent engine errors are retried for the full 30 s. CommitBufferError::Response is always retried, including non-transient errors such as validation or "too large". Consider classifying these, or at least noting that they delay the crash by the deadline.
  5. Env tunables are unbounded. u64_by_default has no min or max, unlike the neighbouring u64_bounded_by_default.
    • FLUSH_RETRY_BACKOFF_MIN_MS=0 gives a hot retry loop.
    • MAX < MIN is accepted.
    • DEADLINE_MS=0 breaks on the first transient error.
    • close_flush_timeout adds the deadline and the backoff max, so a huge value also makes close effectively unbounded.
      Please clamp these.

Style and cleanup

  • disabled_by_default returns true when the value is set, so the name reads as the opposite. Something like enabled_if_set or opt_in is clearer.
  • DeferredCommitConfig::default() and the VfsConfig flags construction duplicate the same three-field mapping. Have one call the other.
  • Overlay { ..Overlay::default() } in VfsState::new is a no-op wrapper and can be Overlay::default().
  • The unreachable! in the retry-class match relies on a comment ("the transport never reports full"). An invariant violation would panic the flusher task. The exit guard does turn that into a break, but I'd prefer the arm return an error.
  • commit_buffered_pages still does dirty_pages.as_ref().clone(), so each retry copies the whole batch (up to 32k pages, 128 MiB at 4 KiB). The Arc avoids a copy for the overlay but not for the request. Consider whether the protocol request can take shared bytes.
  • The flusher snapshot clones every overlay page's Vec<u8> while holding the state write lock, which blocks VFS reads and writes for the duration. If OverlayPage.bytes were an Arc<[u8]>, both the snapshot and the ack-side cache insert would be cheap.

Tests

The coverage in tests/inline/vfs.rs and rivetkit-core/tests/sqlite.rs looks broad. Beyond the points above, I'd want these explicitly covered:

  • A lost reply followed by a retry that sees the head at exactly base+1.
  • Close during an in-flight retry, which hits the deadline and then aborts.
  • wait_for_flush(seq) for a seq already flushed after a later commit has broken the database. progress.error is checked first, so it returns Err even though flushed_seq >= seq. If that is intended, a doc comment saying so would help.

Conventions

The code follows the repo's rules: parking_lot is used only in forced-sync VFS paths, tests live under tests/, logs are lowercase with structured fields, and the new flag is documented in the PR as internal. Please also check whether the new flush metrics trait methods need namespace_id where applicable.

@eersnington
eersnington force-pushed the feat/sqlite-deferred-commits branch from 7261035 to 1d1d6c8 Compare October 17, 2026 05:55
@eersnington
eersnington force-pushed the feat/sqlite-deferred-commits branch from 1d1d6c8 to 64ca955 Compare November 3, 2026 16:53
@eersnington eersnington changed the title feat(depot-client): commit sqlite transactions locally and flush them in the background feat(sqlite-embedded): commit sqlite transactions locally and flush them in the background Nov 3, 2026
@RivetAgent

RivetAgent commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

The changes look good on static review; no high-confidence findings.

No issues found. Checked for bugs and CLAUDE.md compliance; no CLAUDE.md files were present.

Validation limits: build/tests could not run because cargo is unavailable. PR status and prior comments could not be checked because GitHub CLI is unauthenticated.

Copy prompt to fix all
Fix all actionable findings in the latest Ironworks code review on PR rivet-dev/rivet#5882. Inspect every referenced location, address each finding without changing unrelated behavior, run the relevant tests, and summarize what changed.

@eersnington
eersnington force-pushed the feat/sqlite-deferred-commits branch 3 times, most recently from 7a32f03 to 08d0868 Compare November 8, 2026 14:29
@NathanFlurry
NathanFlurry force-pushed the feat/sqlite-deferred-commits branch from 08d0868 to 4cf6492 Compare November 12, 2026 00:57
@eersnington
eersnington force-pushed the feat/sqlite-deferred-commits branch 3 times, most recently from 1c4989b to bbf29d5 Compare November 23, 2026 20:32
…ay wait

A command from a blocking caller was refused whenever any other command
was queued or running on the database. A synchronous COMMIT then failed
as writer_busy behind a command that cannot wait, such as an uncork that
only waits for the open transaction to end. The ROLLBACK after it was
refused the same way, and the transaction coordinator closed the
database.

Such a command is now refused only while a statement, or an uncork that
will commit, is queued or running, since either may wait for storage or
the network. A ROLLBACK is never refused: it cannot wait for storage.
@eersnington
eersnington force-pushed the feat/sqlite-deferred-commits branch from bbf29d5 to fd3cb91 Compare November 23, 2026 21:12
…the engine socket

The socket writer always took the storage queue before the HTTP queue.
Deferred commits keep that queue full while the engine reads slower
than a busy node writes, so every HTTP response waited until storage
traffic stopped. At 16,000 deferred actors the reply to a start action
took up to 64 s after the action ran, and starts finished only once the
write window closed.

Pongs still go first. When both queues hold a frame the writer now
alternates between them, so a response waits behind at most one storage
frame and storage still gets every other write.

This branch has not been deployed

No deployments
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.

2 participants