feat(storage): v3 SeedChunkFrontier through the one checkpoint publication (row 9c)

Plan row 9: SeedChunkFrontier uses the checkpoint's map/metadata barrier
and the sole descriptor publication. The mapped v3 checkpoint body is
extracted into publishCheckpointV3 (metadata and chunk barriers, owned-map
candidate outside every lock, the sole slot publisher under syncMu with the
recovery-lifetime check), which the flusher's checkpoint and the seed both
call. The v3 seed fsyncs the data, then raises W and CheckpointLSN to the
inherited frontier in that one publication, and moves the volatile
frontier only after it; a failure publishes nothing and a retry works. It
refuses local writes, an install, an attached chunk layer and a store
without an owned map, writing nothing.

The dark provider still refuses a HEAD restore onto a v3 store
(errV3RestoreNotYet in PrepareRestore, before any seed or claim) until the
v3 FreshFromHead restore on the owned map lands (codex03), so codex03's
TestDurableProviderV3_UnwiredHEADRestoreRefusesBeforeClaim stays as is.
WALStore.FormatV3 reports the dark format.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-17 12:39:07 -08:00
1 parent 73e4c1c4c5
commit 09d1663f13
6 files changed
+215 -31

No files matched your search

@@ -341,6 +341,12 @@ func (p *DurableProvider) newChunkLayer(ctx context.Context, volumeID string, h
// one before the takeover CAS and before the volume serves I/O,
// serialized with observer installs. A refused rebase fails the
// restore here, so HEAD is never claimed for a mis-ordered ship.
if ws.FormatV3() {
// The v3 seed exists (plan row 9c), but the v3 chunk layer's
// FreshFromHead restore on the owned map is codex03's to prove;
// until then a dark v3 restore refuses before any seed or claim.
return errV3RestoreNotYet
}
return h.backend.seedAndRebase(func() error { return ws.SeedChunkFrontier(head.RemoteLSN) })
}
head, err := storage.ClaimChunkHead(ctx, remote, cfg)
@@ -353,6 +359,12 @@ func (p *DurableProvider) newChunkLayer(ctx context.Context, volumeID string, h
return storage.AttachChunkLayer(ws, remote, cfg)
}
// errV3RestoreNotYet: a HEAD restore onto a dark v3 store is refused until
// the v3 FreshFromHead restore on the owned map lands with its own positive
// control (codex03, chunk layer/provider; DESIGN 8.7 and plan row 9c). The
// v3 SeedChunkFrontier itself is wired.
var errV3RestoreNotYet = errors.New("durable: HEAD restore onto a v3 store awaits the v3 FreshFromHead restore")
// closeBoundRemote ends every remote call when the provider closes, so a
// shutdown never waits on the remote for an attach or a cut. While an
// attach runs, its calls also end with the attach caller's context.
+54 -1
View File
@@ -12,7 +12,7 @@ import (
// Order: captureMu -> lifecycleMu -> mu; no remote call or layer callback.
func (s *WALStore) SeedChunkFrontier(lsn uint64) error {
if s.v3 != nil {
return fmt.Errorf("%w: seed requires the v3 descriptor install transaction", errV3NotYet)
return s.seedChunkFrontierV3(lsn)
}
s.captureMu.Lock()
defer s.captureMu.Unlock()
@@ -55,3 +55,56 @@ func (s *WALStore) SeedChunkFrontier(lsn uint64) error {
s.pendingDirectFrontierLSN = 0
return nil
}
// seedChunkFrontierV3 is SeedChunkFrontier on a dark v3 store (plan row 9):
// the same preconditions (an open, unattached store with no local writes
// beyond its checkpoint), a data fsync, and then the one checkpoint
// publication (publishCheckpointV3): the metadata barrier, the owned-map
// candidate at lsn and the sole slot publisher, which raises W and the
// checkpoint to lsn together. The volatile frontier moves only after that
// publication succeeded; any failure publishes nothing and may be retried.
// An unmapped v3 store has no map candidate for the new checkpoint and is
// refused. Order: captureMu -> lifecycleMu -> (syncMu ->) mu.
func (s *WALStore) seedChunkFrontierV3(lsn uint64) error {
v := s.v3
s.captureMu.Lock()
defer s.captureMu.Unlock()
s.lifecycleMu.RLock()
defer s.lifecycleMu.RUnlock()
s.mu.Lock()
if s.closed || s.chunk.Load() != nil {
s.mu.Unlock()
return errors.New("storage: seed frontier requires an open, unattached store")
}
if err := s.stickyIOFault(); err != nil {
s.mu.Unlock()
return err
}
d := v.pub.current()
if lsn == ^uint64(0) || lsn < d.CheckpointLSN || v.appended.Load() > d.CheckpointLSN ||
s.walHead > s.checkpointLSN || s.nextLSN > lsn+1 || d.BaseState != BaseNone {
s.mu.Unlock()
return errors.New("storage: seed frontier cannot replace local writes, an install or wrap LSN")
}
if v.ownedMap == nil {
s.mu.Unlock()
return fmt.Errorf("%w: a v3 seed needs the owned map for its checkpoint coverage", errV3NotYet)
}
if d.CheckpointLSN == lsn && d.W >= lsn && s.nextLSN == lsn+1 {
s.mu.Unlock()
return nil
}
gen := v.recoverGen.Load()
s.mu.Unlock()
if err := s.syncFile(nil, s.fd.Sync); err != nil {
return fmt.Errorf("storage: seed data sync: %w", err)
}
if _, err := s.publishCheckpointV3(lsn, true, gen); err != nil {
return fmt.Errorf("storage: seed frontier %d: %w", lsn, err)
}
s.mu.Lock()
defer s.mu.Unlock()
s.checkpointLSN, s.syncedLSN, s.walHead, s.nextLSN = lsn, lsn, lsn, lsn+1
s.pendingDirectFrontierLSN = 0
return nil
}
@@ -1,33 +1,125 @@
package storage
import (
"bytes"
"errors"
"os"
"testing"
)
// Until row9 installs the v3 seed transaction, the legacy seed API must
// refuse rather than overwrite a v3 descriptor using the v2 serializer.
func TestWALStoreV3_SeedNeverUsesLegacyDescriptorWriter(t *testing.T) {
// Plan row 9c controls (owner claude03): SeedChunkFrontier on a dark v3
// store uses the checkpoint's map/metadata barrier and the sole descriptor
// publication (publishCheckpointV3), never the legacy superblock writer.
// A seed publishes CheckpointLSN = W = map coverage = lsn in exactly one
// slot publication with a fresh map root; the allocator continues at lsn+1
// and a crash and mapped reopen keep the frontier and a later write.
// Mutant: a seed that does not raise W refuses (no candidate above W) and
// fails here.
func TestWALStoreV3SeedPublishesTheCheckpointThroughTheMap(t *testing.T) {
s, path := newMappedCheckpointStoreV3(t)
before, err := os.ReadFile(path)
if err != nil {
before := s.v3.pub.current()
writes := countSlotWritesV3(s)
if err := s.SeedChunkFrontier(7); err != nil {
t.Fatal(err)
}
d := s.v3.pub.current()
err = s.SeedChunkFrontier(7)
if !errors.Is(err, errV3NotYet) {
t.Errorf("unwired v3 seed must refuse: %v", err)
if writes.Load() != 1 {
t.Fatalf("seed wrote %d slots, want one publication", writes.Load())
}
after, e := os.ReadFile(path)
if e != nil {
t.Fatal(e)
if d.CheckpointLSN != 7 || d.W != 7 || d.MapCoveredCheckpoint != 7 || d.MapGeneration <= before.MapGeneration {
t.Fatalf("seed published checkpoint %d W %d coverage %d map gen %d (was %d)",
d.CheckpointLSN, d.W, d.MapCoveredCheckpoint, d.MapGeneration, before.MapGeneration)
}
if !bytes.Equal(before, after) {
t.Error("legacy seed rewrote v3 file bytes")
if s.NextLSN() != 8 {
t.Fatalf("NextLSN %d, want 8", s.NextLSN())
}
if s.v3.pub.current() != d || s.NextLSN() != 1 {
t.Error("refused seed published local progress")
if err := s.SeedChunkFrontier(7); err != nil || writes.Load() != 1 {
t.Fatalf("repeated seed: err %v, slot writes %d, want an idempotent no-op", err, writes.Load())
}
s.v3.testBeforeSlotWrite = nil
if lsn, err := s.Write(3, blk(3)); err != nil || lsn != 8 {
t.Fatalf("write after seed: LSN %d err %v, want 8", lsn, err)
}
if _, err := s.Sync(); err != nil {
t.Fatal(err)
}
crashRecoveryHardeningStore(t, s)
r, err := openWALStoreV3ForTest(path)
if err != nil {
t.Fatal(err)
}
r.DisableAutoFlushForRecoveryTest()
t.Cleanup(func() { crashRecoveryHardeningStore(t, r) })
if _, err := r.Recover(); err != nil {
t.Fatal(err)
}
if got := r.v3.pub.current(); got.CheckpointLSN != 7 || got.W < 8 {
t.Fatalf("reopen: checkpoint %d W %d, want 7 and >= 8", got.CheckpointLSN, got.W)
}
readAllV3(t, r, 3, 3)
}
// The barrier precedes the publication: when the map candidate is
// persisted, no slot has been published yet. A failure after it (here a
// recovery lifetime change) publishes nothing and moves no volatile
// frontier; a retry then succeeds. Mutant: moving the volatile frontier
// before the publication fails here.
func TestWALStoreV3SeedFailurePublishesNothingAndRetries(t *testing.T) {
s, _ := newMappedCheckpointStoreV3(t)
before := s.v3.pub.current()
fired := false
s.v3.testMapPrepared = func() {
if fired {
return
}
fired = true
if s.v3.pub.current() != before {
t.Error("a slot was published before the map candidate was persisted")
}
s.v3.recoverGen.Add(2) // a recovery ran between preparation and publication
}
if err := s.SeedChunkFrontier(7); !errors.Is(err, ErrMapCoverageStale) {
t.Fatalf("seed across a lifetime change: err = %v, want ErrMapCoverageStale", err)
}
if s.v3.pub.current() != before || s.NextLSN() != 1 || s.v3.wack.Load() != 0 {
t.Fatal("a failed seed published or moved the volatile frontier")
}
if err := s.SeedChunkFrontier(7); err != nil {
t.Fatalf("retry: %v", err)
}
if d := s.v3.pub.current(); d.CheckpointLSN != 7 || s.NextLSN() != 8 {
t.Fatalf("retry: checkpoint %d NextLSN %d", d.CheckpointLSN, s.NextLSN())
}
}
// Refusals, writing nothing: local writes beyond the checkpoint, an
// unmapped store (no map candidate for the new checkpoint) and an attached
// chunk layer.
func TestWALStoreV3SeedRefusals(t *testing.T) {
t.Run("local writes", func(t *testing.T) {
s, _ := newMappedCheckpointStoreV3(t)
writeSynced(t, s, 1, 1)
before := s.v3.pub.current()
if err := s.SeedChunkFrontier(7); err == nil || s.v3.pub.current() != before {
t.Fatalf("seed over a local write: err %v", err)
}
})
t.Run("unmapped", func(t *testing.T) {
s, _ := newStoreV3(t, WModeStrict)
t.Cleanup(func() { crashRecoveryHardeningStore(t, s) })
before := s.v3.pub.current()
if err := s.SeedChunkFrontier(7); !errors.Is(err, errV3NotYet) || s.v3.pub.current() != before {
t.Fatalf("unmapped seed: err %v", err)
}
})
t.Run("attached chunk layer", func(t *testing.T) {
s, _ := newMappedCheckpointStoreV3(t)
cfg := ctConfig(1)
cfg.Bitmap = nil
cfg.BarrierBound = 1
mustAttach(t, s, newFakeChunkRemote(), cfg)
before := s.v3.pub.current()
if err := s.SeedChunkFrontier(7); err == nil || s.v3.pub.current() != before {
t.Fatalf("seed with a layer attached: err %v", err)
}
})
}
@@ -66,21 +66,40 @@ func (s *WALStore) persistOwnedMapCheckpointV3(highestLSN, recoveryGen uint64) e
if cp <= v.pub.current().CheckpointLSN {
return nil
}
// The extent was fsynced by the flusher. Optional metadata and chunk
// callbacks settle before map persistence and the sole slot publisher.
// The extent was fsynced by the flusher.
d, err := s.publishCheckpointV3(cp, false, recoveryGen)
if err != nil {
return err
}
s.mu.Lock()
s.checkpointLSN = d.CheckpointLSN
s.mu.Unlock()
return nil
}
// publishCheckpointV3 is the one v3 checkpoint publication (plan row 9: the
// flusher's checkpoint and SeedChunkFrontier both use it). The optional
// metadata and chunk callbacks settle first, then the owned-map candidate
// is persisted outside every lock, then the sole slot publisher runs under
// the leader role (syncMu), refusing a candidate from another recovery
// lifetime or one above the acknowledged W. seed raises the acknowledged W
// to cp in the same leader section (the inherited remote frontier). The
// caller has made the extent durable. Nothing is published on any error.
func (s *WALStore) publishCheckpointV3(cp uint64, seed bool, recoveryGen uint64) (descriptorV3, error) {
v := s.v3
if s.metadata != nil {
if err := s.metadata.BeforeCheckpointAdvance(cp); err != nil {
return err
return descriptorV3{}, err
}
}
if h := s.chunk.Load(); h != nil {
if err := h.sink.BeforeCheckpointAdvance(cp); err != nil {
return err
return descriptorV3{}, err
}
}
prepared, err := s.prepareOwnedMapCheckpointV3(cp)
if err != nil {
return err
return descriptorV3{}, err
}
if h := v.testMapPrepared; h != nil {
h()
@@ -90,11 +109,14 @@ func (s *WALStore) persistOwnedMapCheckpointV3(highestLSN, recoveryGen uint64) e
// The candidate proof alone is insufficient: it was prepared after extent
// I/O and could belong to a newer Recover lifetime than those bytes.
if !v.flushLifetimeValidV3(recoveryGen) {
return ErrMapCoverageStale
return descriptorV3{}, ErrMapCoverageStale
}
wack := v.wack.Load()
if seed {
wack = max(wack, cp)
}
if wack < cp {
return ErrMapCoverageStale
return descriptorV3{}, ErrMapCoverageStale
}
d, err := v.pub.publishMapV3(&s.mu, nil, prepared, func(n *descriptorV3) error {
n.W = max(n.W, wack)
@@ -102,10 +124,11 @@ func (s *WALStore) persistOwnedMapCheckpointV3(highestLSN, recoveryGen uint64) e
return nil
})
if err != nil {
return err
return descriptorV3{}, err
}
s.mu.Lock()
s.checkpointLSN = d.CheckpointLSN
s.mu.Unlock()
return nil
if seed {
v.appended.Store(max(v.appended.Load(), cp))
v.wack.Store(wack)
}
return d, nil
}
@@ -127,6 +127,10 @@ var errV3NotYet = errors.New("storage: operation not yet supported on a v3 store
// tests.
var testSkipFenceOnRecoverV3 bool
// FormatV3 reports whether the store runs the dark v3 format (only test
// constructors build one before activation, plan row 12b).
func (s *WALStore) FormatV3() bool { return s.v3 != nil }
func (s *WALStore) latchV3(err error) {
if err != nil {
s.ioFault.CompareAndSwap(nil, NewSubstrateIOFailure(err,
+1 -1
View File
@@ -306,7 +306,7 @@ Notes:
- `INV-WAL-CHECKPOINT-SNAPSHOT-001`: The SIGKILL test is Linux only.
- `INV-FE-FLUSH-SYNC-001`: The failed-`Sync` case is asserted for ublk; for NVMe/TCP only the dispatch to `Sync` is asserted here.
- `INV-WAL-V3-RESET-001`: Format bump v3, plan row 8 (DESIGN 8.6). Dark: no production path calls the reset, and it must not be activated before the reset model (r4.5) passes. No v3 tail release exists yet, so the precondition is CheckpointLSN >= W rather than an empty retained range: retained old-incarnation bytes at or below the checkpoint are preserved in the extent. The map root is unchanged by a reset, so no map page is retired.
- `INV-WAL-V3-BASE-001`: Format bump v3, plan row 9a (DESIGN r17.3.1 sections 8.1, 8.2, 8.4, 8.5, 8.7). Dark: only tests reach it. Row 9b makes the seam `BaseInstallPreparer.BeginBaseInstall(baseLSN, installID) (InstallHandle, error)`: the dual-lane `Receiver` begins at SessionStart (BaseLSN = FromLSN, a fresh random ID) and its `RebuildSession` writes, applies and completes only through the handle, propagating the `MarkBaseComplete` error; stores without the seam keep their exact pre-seam behaviour through the lazy legacy handle (parallelwal's staging is now `BaseStager.BeginBaseStage`). The legacy single-lane caller is row 10. Until the chunk layer's side of the v3 install protocol lands (codex03), a v3 begin with an attached chunk layer is refused (`errV3NotYet`), writing nothing; the v3 `SeedChunkFrontier` is not wired yet; the installing reopen, the fence before a resume or superseding begin, and the same-ID resume rule are row 11. A begin while installed is refused until retirement (8.5 has no installed -> installing edge).
- `INV-WAL-V3-BASE-001`: Format bump v3, plan row 9a (DESIGN r17.3.1 sections 8.1, 8.2, 8.4, 8.5, 8.7). Dark: only tests reach it. Row 9b makes the seam `BaseInstallPreparer.BeginBaseInstall(baseLSN, installID) (InstallHandle, error)`: the dual-lane `Receiver` begins at SessionStart (BaseLSN = FromLSN, a fresh random ID) and its `RebuildSession` writes, applies and completes only through the handle, propagating the `MarkBaseComplete` error; stores without the seam keep their exact pre-seam behaviour through the lazy legacy handle (parallelwal's staging is now `BaseStager.BeginBaseStage`). The legacy single-lane caller is row 10. Until the chunk layer's side of the v3 install protocol lands (codex03), a v3 begin with an attached chunk layer is refused (`errV3NotYet`), writing nothing; since row 9c the v3 `SeedChunkFrontier` publishes CheckpointLSN = W = map coverage = lsn through the one checkpoint publication (`publishCheckpointV3`: data fsync, metadata and chunk barriers, owned-map candidate, the sole slot publisher under syncMu with the lifetime check), moves the volatile frontier only after it, publishes nothing on failure, and refuses local writes, an install, an attached layer or a store without an owned map; a HEAD restore onto a dark v3 store still refuses in the provider (`errV3RestoreNotYet`) until codex03's v3 FreshFromHead restore lands; the installing reopen, the fence before a resume or superseding begin, and the same-ID resume rule are row 11. A begin while installed is refused until retirement (8.5 has no installed -> installing edge).
- `INV-WAL-V3-W-001`, `INV-WAL-V3-FENCE-001`: Format bump v3, plan rows 5 and 5b (DESIGN r15 sections 6.1 to 6.6). Since 5b the v3 format runs inside the real `WALStore`, through the shared committer, dirty map, flusher and leaf. It is reachable only through `createWALStoreV3ForTest`/`openWALStoreV3ForTest`. Without the row-7 map candidate the v3 checkpoint and tail do not advance. They are STUBs until activation (row 12b). The test "crash" closes the file without further writes, so the slot-before-records reordering that mode C's lagged W guards against is covered by the composed model (claude05), not by these tests. Installing recovery is row 11.
- `INV-WAL-V3-PUBLISH-001`: Format bump v3, plan row 3 (DESIGN r15 sections 4, 5 and 5.1). It is a STUB until the v3 store is wired in (rows 4 and 5) and activated (row 12b). The v2 checkpoint path (`persistCheckpointReleasing`) is unchanged and is retired in row 14a. The map coverage predicates against the root are added by row 7, and the conditions on BASE edges by rows 9 to 11.
- `INV-WAL-V3-DESCRIPTOR-001`: Format bump v3, plan row 1 (DESIGN r15 section 2). It is a STUB until the activation commit (plan row 12b): no production open or create path reaches the v3 codec before then. The map-dependency checks (root and pages present and hash-valid) are added by plan row 7. The tests decode vectors from an independent generator (`core/storage/testdata/format_v3/slot/gen_slot_vectors.py`).