Skip to content

Store backends: free the writer lane after quarantine, PG lane barrier, checkpoint fullfsync (#2 #3 #10 #11 #20) - #764

Merged
yifanxuaaa merged 3 commits into
layerfs_v2_0_0from
claude/v2-store-liveness-fixes-8550bd
Nov 23, 2026
Merged

yifanxuaaa merged 3 commits into
layerfs_v2_0_0from
claude/v2-store-liveness-fixes-8550bd

Conversation

@yifanxuaaa

@yifanxuaaa yifanxuaaa commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes the Store backend liveness and durability findings #2, #3, #10, #11 and #20 from the LayerFS 2.0.0 review (layerfs_v2_0_0 at 343c59b). Every finding was re-checked in code before it was fixed. #3 was partly wrong, see below.

#11 narrowed to the idle/final checkpoint (owner decision, 2026-11-23)

a31a74243f had the checkpoint thread sync every routine pass with F_FULLFSYNC on macOS. Merged with the other review PRs, that alone pushed three write-heavy targets past the 120 s cap on macOS:

target base with #11 #11 reverted
persistence kept_node_closure 6.7 s >120 s 6.1 s
persistence kept_objects_sort_files 24.7 s >120 s 24.4 s
store-service scale_lineage_cap 32.9 s >120 s 34.4 s

The owner chose to narrow it. With fb3b37bdf4:

  • Routine passes: routine PASSIVE passes on the checkpoint thread use plain fsync. The thread's connection sets checkpoint_fullfsync=0 and reads it back.
  • Idle and final checkpoints: the idle TRUNCATE, checkpoint_holding and the last close run on the session's own connection, which keeps checkpoint_fullfsync=1.

Remaining window (macOS only, documented in store/sqlite.md):

  • After a routine pass, the database file has had only a plain fsync, which does not flush the drive cache.
  • Once a pass has copied every frame, the next write restarts the log over those frames. A power loss before the next full sync can then lose database pages.
  • SQLite syncs the Store file during a checkpoint only when that checkpoint copies at least one frame. So an idle TRUNCATE or last close that finds every frame already copied truncates or deletes the log with no sync at all.

Linux is unaffected. No power-loss guarantee is claimed until layerfs-project#155 §7.5/§7.6.

Measured with fb3b37bdf4 on the macOS host (2026-11-23, one sample each, 120 s cap, 1-min load 5.5 to 11.9):

target time
persistence kept_node_closure 6.7 s
persistence kept_objects_sort_files 35.9 s
store-service scale_lineage_cap 39.6 s
background_checkpoint 1.1 s
sqlite_publication (Normal case) 0.7 s
sqlite_quarantine_release 0.7 s
diagnostics_during_lane 3.7 s

What changed

#2 SQLite: a quarantined writer kept the directory flock until process exit.
A quarantined Session now closes its connection while it still holds the lane, then releases the lane. The lane is held either by the job's own writer or by a LaneHold around nested work. sqlite3_close rolls back the write transaction that the uncertain attempt left open, so SQLite teardown still comes before the advisory release. The checkpoint thread's connection may then be SQLite's last; it closes with NO_CKPT_ON_CLOSE instead of checkpointing outside the lane. The Session stays quarantined.

#20 SQLite: the flock wait happened under the Session state mutex.
run_decided now waits for the cross-process lock before it takes the state mutex, so diagnostics() no longer queues behind another process's lane. The FIFO turn still serializes the Session. The Store identity is now checked with the lane held.

#3 PostgreSQL: a quarantined session kept its connection.
Quarantine now closes the session's ordinary connection. The review overstated one part of this finding. When the server raises an error, PostgreSQL aborts the transaction at once and releases its row locks, so in that case store_lane was not held. Observed: the backend sat idle in transaction (aborted) with a NULL backend_xid. These parts were real:

  • the backend stayed connected until process exit;
  • it kept its session advisory keys (mount and compaction presence);
  • when the uncertain outcome left the transaction live (a lost reply, or a body error classed uncertain), it kept every row lock, store_lane included.

SQLSTATE classes 21 and P0 (including P0001 from the pack_immutable trigger) are now definite Integrity failures. 58xxx and XX codes stay unknown.

#10 PostgreSQL: in-lane writes relied on a lock held by another connection.
This adds a lane barrier and needs no schema change:

  • Lane acquisition: every writer that locks store_lane takes and immediately releases an exclusive two-key advisory lock, in the same batch.
  • Nested writes: a write nested in a held lane holds that key shared from BEGIN. Before COMMIT it checks that the lane connection is still in the transaction recorded at hold_lane, which costs one round trip.
  • Lost lane: the nested write rolls back and answers Closed (OwnershipUnavailable).

A new lane holder therefore cannot read anything until the previous holder's in-flight nested write has finished. That keeps the head check and the object_location existence probe exact. The branch-advance SQL is untouched: v2-commit-path adds its head_commit_id = expected predicate there.

#11 SQLite: the checkpoint thread's checkpoint_fullfsync was never set or checked.
The thread's setting is now applied explicitly and read back, and the open is refused on a mismatch. It is reported as ConnectionProfile::checkpoint_thread_checkpoint_fullfsync. After the owner's narrowing it is 0, and F_FULLFSYNC stays on the session connection, which runs the idle and final checkpoints (see above).

Tests

New tests:

The uncertain SQLite outcome comes from ordinary behaviour: a raw connection adds a trigger that calls an unknown function, which makes the Store's UPDATE store_policy fail with a generic SQLITE_ERROR. No test-only code is in src/.

Changed assertion: postgresql_commit_round_trips now counts 15 round trips for the lane + head + commit_branch phase instead of 14; the extra one is the lane check. No test was deleted. diagnostics_during_lane joins the CI SLOW list (6.06 s).

Checks (macOS host, Rust 1.85.1, private worktree target)

  • cargo test -p layerfs-persistence --all-targets --no-run: builds with no warnings.
  • cargo clippy -p layerfs-persistence --all-targets -- -D warnings: clean.
  • rustfmt --check on the changed files: clean. cargo fmt --all --check reports drift only in files this PR does not touch, all already on the base.
  • tools/check_product_boundary.py: PASS. Tools unit tests: 181 OK.
  • On the parent: every new test fails on 343c59b and passes here. One correction: the first perf(fs): optimize bytesToHex encoding #3 test also passed on the parent; that is how the overstatement above was found, and the test now asserts what actually differs.
  • Covering run: 89 layerfs-persistence test binaries, 120 s cap each, against a private postgres:17 server, with LAYERFS_CONSTRUCTION_WORKERS=1.
  • Not run on Linux.

Gaps

  • SQLite, close refused: if SQLite ever refused the close with a transaction still open, the lane would stay with the connection, as before. With rusqlite owning the connection this cannot happen, but nothing kills the process.
  • SQLite, poisoned state mutex: a panic inside a write body still leaves the writer in a poisoned State until the Session drops. Not changed here.
  • PostgreSQL, mixed versions: the barrier only protects writers that run this version. A mixed deployment with older binaries keeps the old exposure.
  • Docs: the Off (default) row in postgresql.md's synchronous_commit table (review V-DOC-1) is unchanged.

Production LOC: 206,537 -> 206,729 (delta +192) over the range. Per commit: +171 for 7c2f2b6276, +21 for a31a74243f, 0 for fb3b37bdf4. All in engine/layerfs-persistence; core, control and api are unchanged.

🤖 Generated with Claude Code

yifanxuaaa and others added 3 commits November 23, 2026 14:50
…G lane

Fixes the Store backend liveness findings #2, #3, #10 and #20 of the
LayerFS 2.0.0 review. (#11 follows in its own commit.)

#2 SQLite: a quarantined Session closes its connection while it still holds
the Store directory flock (the job's own writer, or a held LaneHold around
nested work), then releases the flock. sqlite3_close rolls back any write
transaction the uncertain attempt left open, so SQLite teardown still
precedes advisory release, and other Sessions and processes take the lane at
once instead of waiting until the Session drops (process exit for a daemon).
The checkpoint thread's connection, which may then be SQLite's last, closes
with NO_CKPT_ON_CLOSE. The Session stays quarantined.

#20 SQLite: run_decided waits for the cross-process flock before it takes
the Session state mutex, so diagnostics() no longer queues behind another
process's lane. The FIFO turn still serializes the Session; the Store
identity is now checked with the lane held.

#3 PostgreSQL: quarantine closes the session's ordinary connection.
Corrected scope: a transaction the server aborts with an error already
releases its row locks, so the store_lane row was not held in that case.
What stayed until process exit was the backend with its session advisory
keys, and every row lock when the uncertain outcome left the transaction
live. SQLSTATE classes 21 and P0 (P0001 from pack_immutable) are now
definite Integrity failures.

#10 PostgreSQL: a lane barrier. Every writer that locks store_lane takes and
at once releases an exclusive two-key advisory lock in the same batch; a
write nested in a held lane holds that key shared from BEGIN and checks
before COMMIT that the lane connection is still in the transaction recorded
at hold_lane (one round trip). A lost lane rolls the nested write back and
answers Closed. A new holder therefore cannot read until the previous
holder's in-flight nested write ends, which keeps the head check and the
object_location existence probe exact without a schema change. The
branch-advance SQL is untouched (v2-commit-path adds its predicate there).

Tests: new sqlite_quarantine_release (run and in-lane cases) and
postgresql_lane_liveness (#3, #10), a foreign-lane case in
diagnostics_during_lane (#20), SQLSTATE cases in postgresql_sqlstate. Each
new case fails on the parent 343c59b and passes here.
postgresql_commit_round_trips now counts 15 round trips for the lane + head
+ commit_branch phase (the lane check). No test deleted.
diagnostics_during_lane joins the CI SLOW list (6.06 s).

Docs: store/sqlite.md (write sequence, Quarantine), store/postgresql.md
(quarantine, SQLSTATE classes, Lane barrier), store/README.md (error table,
demonstrations).

Production LOC: 206,537 -> 206,708 (delta +171)
  core     40,988 -> 40,988 (0)
  engine   95,899 -> 96,070 (+171)  layerfs-persistence +171
  control  59,325 -> 59,325 (0)
  api      10,325 -> 10,325 (0)
  reference absent (retired R9c; last counted 65,417 at tree 498dd19)
  Method: tools/production_loc.py --detail on parent 343c59b and the staged tree

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The checkpoint thread's connection set fullfsync=0 and left
checkpoint_fullfsync at SQLite's default 0; only the session's own
connection set it to 1. With wal_autocheckpoint=0 nearly every checkpoint
runs on that thread, so on macOS each pass synced the Store file with plain
fsync before the writer could restart the log over the copied frames. It
now sets checkpoint_fullfsync=1, reads it back, refuses the open on a
mismatch, and reports it as ConnectionProfile::
checkpoint_thread_checkpoint_fullfsync.

Cost, measured on the macOS host, 2026-11-23, one sample each: the
kept_objects_sort_files test target took 27.7 s at the previous commit
and did not finish within the 120 s ceiling with this change (its last
case, a 9,200-page mark, was still running). Linux ignores the setting.
Kept as its own commit so it can be dropped if the owner prefers the speed.

Tests: background_checkpoint and sqlite_publication assert the read-back.
Docs: store/sqlite.md (checkpoint thread row, checkpoint rule with the cost).

Production LOC: 206,708 -> 206,729 (delta +21)
  core     40,988 -> 40,988 (0)
  engine   96,070 -> 96,091 (+21)  layerfs-persistence +21
  control  59,325 -> 59,325 (0)
  api      10,325 -> 10,325 (0)
  reference absent (retired R9c; last counted 65,417 at tree 498dd19)
  Method: tools/production_loc.py --detail on parent 7c2f2b6 and the staged tree

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Owner decision, 2026-11-23: F_FULLFSYNC applies only to the idle and final
checkpoint, not to every routine pass. With a31a742, three write-heavy
test targets passed the 120 s ceiling on macOS (persistence
kept_node_closure, kept_objects_sort_files, store-service
scale_lineage_cap).

The checkpoint thread's connection now sets checkpoint_fullfsync=0, reads
it back, and refuses the open on a mismatch;
ConnectionProfile::checkpoint_thread_checkpoint_fullfsync reports 0. The
idle TRUNCATE (checkpoint_idle), checkpoint_holding and the last close
already run on the session's own connection, which sets
checkpoint_fullfsync=1 (profile.rs). Neither connection changes its
setting after open.

Remaining window, documented in store/sqlite.md: on macOS, fsync does not
flush the drive cache. Once a routine pass has copied every frame, the next
write restarts the log over them, and a power loss can then lose database
pages. SQLite syncs the Store file during a checkpoint only when that
checkpoint copies a frame. So an idle TRUNCATE or last close that finds
every frame copied truncates or deletes the log without any sync, and the
pages from routine passes then have no F_FULLFSYNC behind them. Linux is
unaffected. No power-loss guarantee is claimed until layerfs-project#155
§7.5/§7.6.

Tests: background_checkpoint asserts the session reads back 1 and the
thread 0, that a routine pass leaves the log in place, and that the idle
checkpoint (session connection only) truncates it. sqlite_publication's
Normal case asserts the thread reads back 0. No test deleted.

Timings on the macOS host, 2026-11-23, one sample each, 120 s cap,
LAYERFS_CONSTRUCTION_WORKERS=1, 1-min load 5.5-11.9:
kept_node_closure 6.7 s, kept_objects_sort_files 35.9 s, scale_lineage_cap
39.6 s, background_checkpoint 1.1 s, sqlite_publication (Normal case)
0.7 s, sqlite_quarantine_release 0.7 s, diagnostics_during_lane 3.7 s.

Production LOC: 206,729 -> 206,729 (delta 0)
  core     40,988 -> 40,988 (0)
  engine   96,091 -> 96,091 (0)
  control  59,325 -> 59,325 (0)
  api      10,325 -> 10,325 (0)
  reference absent (retired R9c; last counted 65,417 at tree 498dd19)
  Method: tools/production_loc.py --detail on parent a31a742 and the staged tree

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yifanxuaaa
yifanxuaaa merged commit 4519e86 into layerfs_v2_0_0 Nov 23, 2026
2 checks passed
@yifanxuaaa
yifanxuaaa deleted the claude/v2-store-liveness-fixes-8550bd branch November 23, 2026 08:15
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.

1 participant