Ping QiuandClaude Opus 5.5 69ff216bc3 storage: chunk layer under the walstore flusher (C1-C11 green, r4) (#102)
* storage: bound flusher snapshot to published LSN cut

* storage: make checkpoint snapshot atomic with write publication

* storage: chunk layer seam and RED contract tests C1-C11 (r4)

Successor to the earlier seam drafts, reworked after three reviews.

Capture is a quiesced cut: block writes, Sync, copy the chunks dirtied
since HEAD through the normal read path, release, upload outside walstore
locks, then CAS HEAD with RemoteLSN = H. The seam declares Capture,
MakeRemoteDurable, EvictChunk (per chunk; absence survives restart), Close,
an owner admission gate (the epoch fence is what stops a demoted primary),
an explicit fresh-from-HEAD mode, a bounded read-miss timeout, chunk refs
with SHA-256, and a persisted dirty-since-HEAD bitmap kept off the write
path: made durable inside persistCheckpoint before any checkpoint or WAL
tail advance on every path, a header plus delta archive per cut, and a
hash-compare fallback. A BASE rebuild is recorded in the header as
installing (interrupted: cuts stay refused after restart until a complete
install) or installed-awaiting-cut (EndBaseInstall only after its frontier
is durable; the next cut hashes fully).

The fakes log remote calls and CAS outcomes in order, gate Put and Get,
inject unknown and applied-but-lost CAS outcomes and pre-CAS takeovers,
enforce chunk hashes, detect buffer mutation on every Put exit, key reuse
and deletion of referenced keys, and park bitmap persistence with an entry
handshake. Every held worker is unblocked, joined and its resources closed
with a bound, from cleanups registered before it starts. The layer is not
implemented, so every C test fails at attachChunkLayer; the fakes' own
tests pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* storage: chunk layer under the walstore flusher (C1-C11 green)

Implements the seam pinned by the contract tests. Store wiring, each a
no-op without an attached layer:
- a capture write barrier (captureMu) held shared by Write, WriteBatch,
  ApplyEntry and WriteExtentDirect, exclusively by a cut;
- OnExtentWrite from the flusher's extent write (with LSN) and from
  WriteExtentDirect (BASE, LSN 0);
- BeforeCheckpointAdvance inside persistCheckpoint and inside Sync's
  direct-frontier path, before any metadata write; an error refuses the
  advance;
- OnReadMiss before readFromExtent takes its lock;
- the layer's Close from WALStore.Close, before the lifecycle lock.

The layer (chunk_layer.go):
- quiesced cut (barrier, Sync, copy dirty chunks through Read, release),
  upload of changed chunks under fresh immutable keys, CAS of HEAD at H;
- an uncertain or refused CAS is resolved by re-reading HEAD: our own
  proposal is adopted, a newer epoch or competing holder fences, anything
  else stays unresolved without a success claim; admission is re-checked
  before the CAS;
- dirty-since-HEAD bitmap off the write path: dirty map chunks plus
  in-memory marks, saved before every checkpoint advance, rolled over with
  a delta per cut, hash-compare fallback on a lost or mismatched header;
  entries at or below the latest cut do not re-mark their chunk;
- BASE install states (installing / installed-awaiting-cut) recorded
  durably; EndBaseInstall refuses until its frontier is durable;
- lazy fill of absent chunks with SHA-256 verification, never over a newer
  local block, bounded by ReadMissTimeout; eviction per clean chunk with
  absence (and local writes inside absent chunks) persisted next to the
  volume after an fsync.

Adds C3_WriteDuringCopyWaitsForTheCut, which holds a cut's copy on a lazy
fetch and requires a concurrent write to wait for it, and the bitmap
self-test's registered unblock and bounded join.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* storage: chunk layer fixes for presence, fill generation, stale attach

- Presence saves are serialized end to end (snapshot, volume fsync,
  sidecar replace, version update) under one mutex, so an older eviction
  snapshot can no longer overwrite a newer checkpoint-protected save; the
  sidecar's directory is fsynced after the rename.
- A lazy fill is bound to the chunk's absence generation (bumped on every
  eviction) and the HEAD ref it fetched; it installs blocks and clears
  absence only while both still match, so a fill held across
  fill/write/publish/re-evict cannot install older bytes.
- A HEAD that already belongs to a newer epoch or a competing holder
  fences the layer at attach and before the CAS, instead of serving as
  this holder's publication guard.
- Capture registers with Close in the same critical section that checks
  closure, and every concurrent Close returns only after shutdown is done.

Each fix comes with a deterministic schedule test (test-only hooks on the
layer, a one-shot held fetch in the fake) that fails on the previous
code. startCapture's cleanup releases registered unblockers before it
joins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* storage: chunk layer fixes for directory durability and stale fills

- A presence replacement counts as saved only after its directory entry
  is durable: syncDir returns open and fsync errors, persistPresence
  returns them without advancing the saved version, and so a checkpoint
  advance (or an eviction) that needs the save is refused. Windows cannot
  fsync a directory handle; there syncDir is a no-op.
- A read miss never authorizes reading an unresolved extent. Within one
  ReadMissTimeout it fetches the chunk as the current HEAD names it; a
  fill that went stale (eviction generation or HEAD ref changed) installs
  nothing and the read retries, so the extent is read only once the chunk
  is no longer absent or the block has a local version.
- A hash-compare cut no longer copies chunks that are absent and
  unwritten: they are HEAD's content by definition, and copying them made
  a fresh host's first cut fetch the whole volume.

Schedule tests: a failed directory open or fsync leaves the presence
unsaved and the checkpoint and physical WAL tail unchanged, and a retry
succeeds after the fault clears; a held fill that goes stale when a
competing holder publishes H2 returns H2's block or an error from the
original read, never the zero extent; a fresh host's first cut fetches
no untouched chunk. The stale-attach test also asserts fencing right
after attach.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* storage: chunk layer read re-resolves after a miss

A read miss that waited on a remote fetch could fall through to the
extent after the same block was written to the WAL (synced, not flushed):
the fill skipped the dirty block, cleared absence, and readFromExtent read
the untouched zero extent. Now:

- After OnReadMiss resolves, readFromExtent re-resolves through the normal
  path (dirty map/WAL first) before reading the extent. No lock is held
  there.
- A fill installs HEAD's block beneath an unflushed WAL write instead of
  skipping it. Reads resolve the dirty map first and the flusher replaces
  the extent later, so this is safe, and the extent holds HEAD's value
  rather than zero if that unsynced write is lost in a crash.

Schedule test: a fresh host's held read of LBA 9 overlaps a synced,
unflushed write to LBA 9; the original read returns HEAD's block or the
write or an error, never the zero extent; the next read returns the write;
the extent beneath it holds HEAD's block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 02:10:57 -08:00
Languages
Go 84.6%
Rust 7.8%
Shell 2.7%
templ 1.6%
Java 1%
Other 2%