From fb7fbad2936055e4b78ff743433d3f39366122f9 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 29 Nov 2026 15:57:20 -0800 Subject: [PATCH 1/2] chunkstore: a snapshot name is recorded only by a commit at or past the request's position Co-Authored-By: Claude Opus 5.5 --- .../chunkstore/clean_metadata_test.go | 4 +- .../frontend/chunkstore/head_readers_test.go | 2 +- .../chunkstore/layout_clean_e2e_test.go | 4 +- .../layout_commit_check_guards_test.go | 2 +- .../core/frontend/chunkstore/layout_remote.go | 27 +++- .../chunkstore/layout_remote_readback_test.go | 4 +- .../chunkstore/layout_remote_states_test.go | 2 +- .../frontend/chunkstore/layout_remote_test.go | 14 +- .../chunkstore/layout_restore_read_test.go | 2 +- .../layout_snapshot_position_test.go | 127 ++++++++++++++++++ .../durable/provider_layout_snapshot.go | 10 +- seaweed-block/docs/design/invariants.md | 1 + 12 files changed, 174 insertions(+), 25 deletions(-) create mode 100644 seaweed-block/core/frontend/chunkstore/layout_snapshot_position_test.go diff --git a/seaweed-block/core/frontend/chunkstore/clean_metadata_test.go b/seaweed-block/core/frontend/chunkstore/clean_metadata_test.go index 91c16f73dc..f79f00fb9f 100644 --- a/seaweed-block/core/frontend/chunkstore/clean_metadata_test.go +++ b/seaweed-block/core/frontend/chunkstore/clean_metadata_test.go @@ -12,13 +12,13 @@ func TestCleaningRemovesUnreferencedRootsAndSegments(t *testing.T) { s, layer, remote := lh.attach("metadata", true, true) defer s.Close() writeSync(t, s, map[uint32][]byte{0: block(1)}) - remote.SetSnapshot("old", layout.SnapshotMeta{CreatedNs: 1, Class: layout.ClassSemantic}) + remote.SetSnapshot("old", layout.SnapshotMeta{CreatedNs: 1, Class: layout.ClassSemantic}, 0) if _, err := layer.Capture(ctx); err != nil { t.Fatal(err) } old := *lh.head().Current writeSync(t, s, map[uint32][]byte{0: block(2)}) - remote.SetSnapshot("keep", layout.SnapshotMeta{CreatedNs: 2, Class: layout.ClassSemantic}) + remote.SetSnapshot("keep", layout.SnapshotMeta{CreatedNs: 2, Class: layout.ClassSemantic}, 0) if _, err := layer.Capture(ctx); err != nil { t.Fatal(err) } diff --git a/seaweed-block/core/frontend/chunkstore/head_readers_test.go b/seaweed-block/core/frontend/chunkstore/head_readers_test.go index a990aee369..41205b625b 100644 --- a/seaweed-block/core/frontend/chunkstore/head_readers_test.go +++ b/seaweed-block/core/frontend/chunkstore/head_readers_test.go @@ -206,7 +206,7 @@ func TestHeadReaders_AbsentHeadProbes(t *testing.T) { if err != nil { t.Fatal(err) } - r.SetSnapshot("x", layout.SnapshotMeta{}) + r.SetSnapshot("x", layout.SnapshotMeta{}, 0) if err := r.CommitPendingSnapshot(ctx); !errors.Is(err, layout.ErrHeadAbsent) || len(f.written) != 0 { t.Fatalf("naming over an absent HEAD: %v (%d writes); want ErrHeadAbsent and none", err, len(f.written)) } diff --git a/seaweed-block/core/frontend/chunkstore/layout_clean_e2e_test.go b/seaweed-block/core/frontend/chunkstore/layout_clean_e2e_test.go index d1adf966b9..d46e8e8e49 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_clean_e2e_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout_clean_e2e_test.go @@ -40,13 +40,13 @@ func TestLayoutRemote_CleanAfterOverwriteAndSnapshotDelete(t *testing.T) { defer s.Close() want := map[uint32][]byte{0: block(1), 9: block(2), 63: block(4)} writeSync(t, s, want) - r.SetSnapshot("s1", layout.SnapshotMeta{CreatedNs: 1, Class: layout.ClassSemantic}) + r.SetSnapshot("s1", layout.SnapshotMeta{CreatedNs: 1, Class: layout.ClassSemantic}, 0) if _, err := l.Capture(ctx); err != nil { t.Fatalf("capture s1: %v", err) } want[9] = block(9) writeSync(t, s, map[uint32][]byte{9: want[9]}) - r.SetSnapshot("s2", layout.SnapshotMeta{CreatedNs: 2, Class: layout.ClassSemantic}) + r.SetSnapshot("s2", layout.SnapshotMeta{CreatedNs: 2, Class: layout.ClassSemantic}, 0) if _, err := l.Capture(ctx); err != nil { t.Fatalf("capture s2: %v", err) } diff --git a/seaweed-block/core/frontend/chunkstore/layout_commit_check_guards_test.go b/seaweed-block/core/frontend/chunkstore/layout_commit_check_guards_test.go index a08dde9fde..7f36c54f9a 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_commit_check_guards_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout_commit_check_guards_test.go @@ -181,7 +181,7 @@ func TestCommitPendingSnapshotLooksUpTheRootAndSegments(t *testing.T) { roots.Add(1) } } - w.LayoutRemote.SetSnapshot("s1", layout.SnapshotMeta{}) + w.LayoutRemote.SetSnapshot("s1", layout.SnapshotMeta{}, 0) if err := w.LayoutRemote.CommitPendingSnapshot(context.Background()); err != nil { t.Fatal(err) } diff --git a/seaweed-block/core/frontend/chunkstore/layout_remote.go b/seaweed-block/core/frontend/chunkstore/layout_remote.go index 84d703c6ed..c83fc09c0a 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_remote.go +++ b/seaweed-block/core/frontend/chunkstore/layout_remote.go @@ -94,6 +94,9 @@ type LayoutRemote struct { type pendingSnapshot struct { name string meta layout.SnapshotMeta + // from is the log position of the last write acknowledged when the + // name was set: only a commit at or past it may record the name. + from uint64 } // ProbeSnapshots reads only the snapshot names for maintenance scheduling. A @@ -144,12 +147,16 @@ func NewLayoutRemote(cat *BCatalog, v VolumeRef, me layout.HolderID) (*LayoutRem return &LayoutRemote{st: st, r: r, vol: v, me: me}, nil } -// SetSnapshot names the snapshot the next commit records, with its -// retention facts; a commit consumes it. -func (l *LayoutRemote) SetSnapshot(name string, meta layout.SnapshotMeta) { +// SetSnapshot names the snapshot the next commit at or past position from +// records, with its retention facts: from is the last write acknowledged +// when the request arrived, so every such write is in the image. A commit +// before it (a cut already past its barrier) commits unnamed and leaves the +// name pending for the request's own cut; the commit that records it +// consumes it. +func (l *LayoutRemote) SetSnapshot(name string, meta layout.SnapshotMeta, from uint64) { l.mu.Lock() defer l.mu.Unlock() - l.pending = &pendingSnapshot{name: name, meta: meta} + l.pending = &pendingSnapshot{name: name, meta: meta, from: from} } // DropSnapshot withdraws a pending snapshot name whose request gave up (its @@ -624,7 +631,7 @@ func (l *LayoutRemote) CASHead(ctx context.Context, expected storage.ChunkHeadVe } req.Plan = &plan } - if l.pending != nil { + if l.pending != nil && next.RemoteLSN >= l.pending.from { if err := layout.CheckSnapshot(l.pending.name, l.pending.meta); err != nil { // A name or facts the commit would refuse: the cut commits // unnamed and the request gets the refusal. @@ -689,7 +696,10 @@ func (l *LayoutRemote) CASHead(ctx context.Context, expected storage.ChunkHeadVe // which also re-derives this session from HEAD (syncSessionLocked). return 0, layoutRemoteErr(err) } - l.session, l.pending, l.lastCommit, l.uncommitted = "", nil, got, false + l.session, l.lastCommit, l.uncommitted = "", got, false + if req.Snapshot != "" { + l.pending = nil // recorded; a name this commit was before stays pending + } l.ownGen, l.visible, l.seen = got.Gen, got.Gen, imageOf(got) l.clearCheckedLocked() // the session is closed: its check covers nothing more return got.Gen, nil @@ -809,6 +819,11 @@ func (l *LayoutRemote) CommitPendingSnapshot(ctx context.Context) error { return layout.ErrHeadAbsent } l.viewLocked(h) + if h.Position < p.from { + // HEAD's image is older than the request: naming it would leave out + // writes acknowledged before the request. + return fmt.Errorf("%w: HEAD at position %d is before the snapshot request's %d", storage.ErrChunkHeadPrecondition, h.Position, p.from) + } m := map[uint64]layout.Ref{} if h.Current != nil { if m, err = l.manifest(ctx, *h.Current); err != nil { diff --git a/seaweed-block/core/frontend/chunkstore/layout_remote_readback_test.go b/seaweed-block/core/frontend/chunkstore/layout_remote_readback_test.go index d864546e74..eab7d9d165 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_remote_readback_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout_remote_readback_test.go @@ -67,7 +67,7 @@ func TestLayoutRemote_OperatorHEADWriteDuringCommitRetries(t *testing.T) { s, layer, remote := lh.attach("active", true, true) defer s.Close() writeSync(t, s, map[uint32][]byte{2: block(2)}) - remote.SetSnapshot("old", layout.SnapshotMeta{CreatedNs: 1}) + remote.SetSnapshot("old", layout.SnapshotMeta{CreatedNs: 1}, 0) if _, err := layer.Capture(ctx); err != nil { t.Fatal(err) } @@ -168,7 +168,7 @@ func TestLayoutRemote_OperatorHEADWriteBetweenCommitAndReadback(t *testing.T) { t.Fatal(err) } writeSync(t, s, map[uint32][]byte{2: block(2)}) - inner.SetSnapshot("old", layout.SnapshotMeta{CreatedNs: 1}) + inner.SetSnapshot("old", layout.SnapshotMeta{CreatedNs: 1}, 0) if _, err := layer.Capture(ctx); err != nil { t.Fatal(err) } diff --git a/seaweed-block/core/frontend/chunkstore/layout_remote_states_test.go b/seaweed-block/core/frontend/chunkstore/layout_remote_states_test.go index 249aa7233a..056dbdeac5 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_remote_states_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout_remote_states_test.go @@ -105,7 +105,7 @@ func TestLayoutRemote_StateByCallTable(t *testing.T) { } r.Abort() case "pending": - r.SetSnapshot("p", layout.SnapshotMeta{CreatedNs: 1}) + r.SetSnapshot("p", layout.SnapshotMeta{CreatedNs: 1}, 0) case "cleaning": // a cleaning tick that runs until a session's call stops it // (or the test ends) diff --git a/seaweed-block/core/frontend/chunkstore/layout_remote_test.go b/seaweed-block/core/frontend/chunkstore/layout_remote_test.go index 67689105d5..40917e25b9 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_remote_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout_remote_test.go @@ -141,7 +141,7 @@ func TestLayoutRemote_CutCommitsThroughTheKernel(t *testing.T) { } want[9] = block(9) writeSync(t, s, map[uint32][]byte{9: want[9]}) - r.SetSnapshot("daily", layout.SnapshotMeta{CreatedNs: 5, Class: layout.ClassSemantic}) + r.SetSnapshot("daily", layout.SnapshotMeta{CreatedNs: 5, Class: layout.ClassSemantic}, 0) if _, err := l.Capture(ctx); err != nil { t.Fatalf("named capture: %v", err) } @@ -345,12 +345,12 @@ func TestLayoutRemote_ARefusedSnapshotNameStillCommitsTheCut(t *testing.T) { s, l, r := lh.attach("a", true, true) defer s.Close() writeSync(t, s, map[uint32][]byte{1: block(1)}) - r.SetSnapshot("daily", layout.SnapshotMeta{CreatedNs: 1}) + r.SetSnapshot("daily", layout.SnapshotMeta{CreatedNs: 1}, 0) if _, err := l.Capture(ctx); err != nil || r.TakeSnapshotErr() != nil { t.Fatal(err) } writeSync(t, s, map[uint32][]byte{2: block(2)}) - r.SetSnapshot("daily", layout.SnapshotMeta{CreatedNs: 2}) + r.SetSnapshot("daily", layout.SnapshotMeta{CreatedNs: 2}, 0) h2, err := l.Capture(ctx) if err != nil { t.Fatalf("cut with a taken snapshot name: %v", err) @@ -428,7 +428,7 @@ func TestLayoutRemote_AnAbandonedNamedCutDropsItsName(t *testing.T) { fail := &failingCreateStore{sessionStore: r.st, armed: true} r.st = fail writeSync(t, s, map[uint32][]byte{5: block(5)}) - r.SetSnapshot("weekly", layout.SnapshotMeta{CreatedNs: 1}) + r.SetSnapshot("weekly", layout.SnapshotMeta{CreatedNs: 1}, 0) if _, err := l.Capture(ctx); err == nil { t.Fatal("a cut whose upload failed committed") } @@ -529,7 +529,7 @@ func TestLayoutRemote_ABadSnapshotNameIsRefusedBeforeTheCommit(t *testing.T) { s, l, r := lh.attach("a", true, true) defer s.Close() writeSync(t, s, map[uint32][]byte{1: block(1)}) - r.SetSnapshot(strings.Repeat("n", layout.MaxSnapshotNameBytes+1), layout.SnapshotMeta{}) + r.SetSnapshot(strings.Repeat("n", layout.MaxSnapshotNameBytes+1), layout.SnapshotMeta{}, 0) h, err := l.Capture(ctx) if err != nil { t.Fatalf("cut with a too-long snapshot name: %v", err) @@ -551,7 +551,7 @@ func TestLayoutRemote_AForeignSnapshotDeleteDoesNotMoveTheView(t *testing.T) { s, l, r := lh.attach("a", true, true) defer s.Close() writeSync(t, s, map[uint32][]byte{2: block(2)}) - r.SetSnapshot("old", layout.SnapshotMeta{CreatedNs: 1}) + r.SetSnapshot("old", layout.SnapshotMeta{CreatedNs: 1}, 0) if _, err := l.Capture(ctx); err != nil { t.Fatal(err) } @@ -609,7 +609,7 @@ func TestLayoutRemote_AbortOpenSession(t *testing.T) { if got, err := r.AbortOpenSession(ctx); err != nil || got != SessionIdle { t.Fatalf("nothing open: %q %v", got, err) } - r.SetSnapshot("x", layout.SnapshotMeta{}) + r.SetSnapshot("x", layout.SnapshotMeta{}, 0) if _, err := r.Begin(ctx); err != nil { t.Fatal(err) } diff --git a/seaweed-block/core/frontend/chunkstore/layout_restore_read_test.go b/seaweed-block/core/frontend/chunkstore/layout_restore_read_test.go index e93e7ca991..db47f69a9f 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_restore_read_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout_restore_read_test.go @@ -21,7 +21,7 @@ func TestLayoutRemote_RestoreTargetReadsItsSource(t *testing.T) { s, l, r := src.attach("src", true, true) want := map[uint32][]byte{0: block(1), 9: block(2), 17: block(3), 63: block(4)} writeSync(t, s, want) - r.SetSnapshot("daily", layout.SnapshotMeta{CreatedNs: 1, Class: layout.ClassSemantic}) + r.SetSnapshot("daily", layout.SnapshotMeta{CreatedNs: 1, Class: layout.ClassSemantic}, 0) if _, err := l.Capture(ctx); err != nil { t.Fatalf("source capture: %v", err) } diff --git a/seaweed-block/core/frontend/chunkstore/layout_snapshot_position_test.go b/seaweed-block/core/frontend/chunkstore/layout_snapshot_position_test.go new file mode 100644 index 0000000000..3a50a51790 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout_snapshot_position_test.go @@ -0,0 +1,127 @@ +package chunkstore + +import ( + "context" + "errors" + "path/filepath" + "sync/atomic" + "testing" + + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout" + "github.com/seaweedfs/seaweed-block/core/storage" +) + +// requestDuringCutRemote is the holder's layout remote whose first commit +// check runs a snapshot request: the cut is then past its barrier. +type requestDuringCutRemote struct { + *LayoutRemote + request func() + done atomic.Bool +} + +func (r *requestDuringCutRemote) CheckCommit(ctx context.Context, e storage.ChunkHeadVersion, n storage.ChunkHead) error { + if r.request != nil && r.done.CompareAndSwap(false, true) { + r.request() + } + return r.LayoutRemote.CheckCommit(ctx, e, n) +} + +func positionLayer(t *testing.T) (*layoutHost, *storage.WALStore, *requestDuringCutRemote, storage.ChunkLayer) { + t.Helper() + lh := newLayoutHost(t) + s, err := storage.CreateWALStoreWithOptions(filepath.Join(lh.dir, "a"), lrBlocks, 4096, storage.WALStoreOptions{ChunkBlocks: lrChunkBlocks}) + if err != nil { + t.Fatal(err) + } + if _, err := s.Recover(); err != nil { + t.Fatal(err) + } + inner, err := NewLayoutRemote(lh.cat, lh.v, lh.me) + if err != nil { + t.Fatal(err) + } + r := &requestDuringCutRemote{LayoutRemote: inner} + l, err := storage.AttachChunkLayer(s, r, storage.ChunkLayerConfig{ChunkBlocks: lrChunkBlocks, Epoch: lh.me.Epoch, Holder: lh.me.Holder, FreshFromHead: true}) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = l.Close(); _ = s.Close() }) + return lh, s, r, l +} + +// A snapshot request that arrives while a cut is already past its barrier +// is not named by that cut: its image is older than the request (a write +// acknowledged just before the request is not in it). That cut commits +// unnamed; the request's own cut takes the name, at or past the request. +func TestASnapshotNameIsNotTakenByACutPastItsBarrier(t *testing.T) { + ctx := context.Background() + lh, s, r, l := positionLayer(t) + writeSync(t, s, map[uint32][]byte{0: block(1)}) + var from uint64 + r.request = func() { + // The write acknowledged just before the request, then the request. + writeSync(t, s, map[uint32][]byte{lrChunkBlocks: block(2)}) + from = s.NextLSN() - 1 + r.SetSnapshot("daily", layout.SnapshotMeta{CreatedNs: 1}, from) + } + first, err := l.Cut(ctx) + if err != nil { + t.Fatal(err) + } + if first.H >= from { + t.Fatalf("setup: the first cut at %d covers the request's position %d; want it past its barrier before the request", first.H, from) + } + if _, named := lh.head().Snapshots["daily"]; named { + t.Fatalf("the cut at %d took the name of a request at %d: its image is older than the request", first.H, from) + } + second, err := l.Cut(ctx) + if err != nil { + t.Fatal(err) + } + h := lh.head() + if _, named := h.Snapshots["daily"]; !named { + t.Fatalf("the request's own cut (at %d) did not take the name; snapshots %v", second.H, h.Snapshots) + } + if second.H < from || h.Position < from { + t.Fatalf("the named image is at %d (HEAD %d); want at or past the request's %d", second.H, h.Position, from) + } + lh.readBack("reader", map[uint32][]byte{0: block(1), lrChunkBlocks: block(2)}) +} + +// A request that gives up withdraws its name: no later cut names an image +// for it, even one at or past its position. +func TestAWithdrawnSnapshotNameIsNeverRecorded(t *testing.T) { + ctx := context.Background() + lh, s, r, l := positionLayer(t) + writeSync(t, s, map[uint32][]byte{0: block(1)}) + r.SetSnapshot("daily", layout.SnapshotMeta{CreatedNs: 1}, s.NextLSN()-1) + r.DropSnapshot() + if _, err := l.Cut(ctx); err != nil { + t.Fatal(err) + } + if err := r.CommitPendingSnapshot(ctx); err != nil { + t.Fatal(err) + } + if _, named := lh.head().Snapshots["daily"]; named { + t.Fatal("a withdrawn name was recorded by a later cut") + } +} + +// Naming the current image without a cut is refused when HEAD is older +// than the request: it would leave out writes acknowledged before it. +func TestAPendingNameIsNotPutOnAnImageOlderThanTheRequest(t *testing.T) { + ctx := context.Background() + lh, s, r, l := positionLayer(t) + writeSync(t, s, map[uint32][]byte{0: block(1)}) + if _, err := l.Cut(ctx); err != nil { + t.Fatal(err) + } + writeSync(t, s, map[uint32][]byte{lrChunkBlocks: block(2)}) + r.SetSnapshot("daily", layout.SnapshotMeta{CreatedNs: 1}, s.NextLSN()-1) + if err := r.CommitPendingSnapshot(ctx); !errors.Is(err, storage.ErrChunkHeadPrecondition) { + t.Fatalf("naming HEAD older than the request: %v; want refused (precondition)", err) + } + if _, named := lh.head().Snapshots["daily"]; named { + t.Fatal("the name was put on an image older than its request") + } +} diff --git a/seaweed-block/core/frontend/durable/provider_layout_snapshot.go b/seaweed-block/core/frontend/durable/provider_layout_snapshot.go index efd9afbe05..2ed12354b0 100644 --- a/seaweed-block/core/frontend/durable/provider_layout_snapshot.go +++ b/seaweed-block/core/frontend/durable/provider_layout_snapshot.go @@ -11,7 +11,7 @@ import ( // layoutSnapshotter is the named-snapshot surface of the attached layout // remote (chunkstore.LayoutRemote). type layoutSnapshotter interface { - SetSnapshot(name string, meta layout.SnapshotMeta) + SetSnapshot(name string, meta layout.SnapshotMeta, from uint64) DropSnapshot() TakeSnapshotErr() error CommitPendingSnapshot(ctx context.Context) error @@ -60,7 +60,13 @@ func (p *DurableProvider) SnapshotVolume(ctx context.Context, volumeID, name str if len(h.Snapshots) >= layout.MaxSnapshots { return layout.Head{}, fmt.Errorf("%w: at most %d per volume; delete a snapshot, or let retention prune", layout.ErrTooManySnapshots, layout.MaxSnapshots) } - sr.SetSnapshot(name, meta) + // The name may only ride on a commit that holds every write acknowledged + // before this request: one at or past the last written position now. + h0, _, err := p.chunkHandle(volumeID) + if err != nil { + return layout.Head{}, err + } + sr.SetSnapshot(name, meta, h0.storage.NextLSN()-1) // A request that fails withdraws its name: the cut may keep its session // for a retry, and the name must not ride onto that later image. if _, err := p.CutVolume(ctx, volumeID, 0); err != nil { diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 11a5f4f236..eed7a33a83 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -1129,6 +1129,7 @@ Notes: | `INV-SNAPK-ABORT-001` | Abort closes the holder's own snap(s) and changes nothing else; after its own crash a holder rolls the open session back before beginning another; over idle Abort writes nothing and its success never says whether the work was committed. | M9 snap → idle (Abort) | `core/frontend/chunkstore/layout` `head.go` `Head.Abort`, `RunAbort`; `core/frontend/durable/provider_layout.go` `settleLayoutStop` | `TestLayoutStopAbortMustConfirmItsOutcome`, `TestLayoutRemote_AbortOpenSessionDoesNotWaitForAHeldCommit`, `TestLayoutRemote_AbortOpenSessionFencesAForeignHead`, `TestLayoutRemoteSessionFieldSites`, `TestAbort_Closure`, `TestAbort_IdleMeansNoActivityRemains`, `TestAbort_RollBackAfterOwnCrash`, `TestRunAbort_InterruptAtEveryStep` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-COMMIT-REACH-001` | A commit never names an unwritten object: before its HEAD update it finds present, written by its session or adopted from an earlier one of this volume, the own chunks new against HEAD's current root, every segment and the root; unchanged and foreign chunks are not looked up. | M9 snap → idle (Commit) | `core/frontend/chunkstore/layout` `commit.go` `RunCommit`, `CheckCommitObjects`, `Head.Commit`; the lookups run with the cut's budget before the bounded HEAD update (`layout_remote.go` `LayoutRemote.CheckCommit`, called by `core/storage/chunk_layer.go`), and the CAS skips them only for the (session, root) checked | `TestCommit_Closure`, `TestRunCommit_LooksUpOnlyWhatIsNew`, `TestRunCommit_RefusesUnwrittenObjects`, `TestRunCommit_StaleBaseAndLookupFailure`, `TestRunCommit_AfterAnEmptyPublishedRoot`, `TestRunCommit_InterruptAtEveryStep`; its verification is bounded parallel and stays all-or-nothing: `TestCommitVerifiesManyNewChunksWithinTheBudget`, `TestADetachingCommitOfManyNewChunksCompletesWithinTheBudget`, `TestCommitRefusesWhenOneOfManyNewChunksIsNotPrepared`, `TestCommitLookupsNeverExceedTheCap`, `TestACancelledCommitStopsJoinsAndWritesNothing`, `TestTheEarliestFailureByPositionDecidesTheError` (`commit_verify_scale_test.go`, `commit_verify_controls_test.go`); outside the 2 s update: `TestCutCommitsWhenItsObjectCheckOutlastsTheHeadUpdate`, `TestCASSkipsTheLookupsCheckedForItsSessionAndRoot`, `TestCASWithNoPassedCheckDoesTheLookups`, `TestCASAfterACheckOfAnotherRootDoesTheLookups`, `TestBeginCleanBetweenTheCheckAndTheUpdateIsBusy`, `TestAPreCheckThatFindsAChunkMissingEndsWithNoLookupInTheCAS` (`core/frontend/chunkstore/layout_commit_check_budget_test.go`); `TestObjectsCheckedIsWrittenOnlyByCASHead`, `TestCASAfterAnAbortAndANewSessionAtTheSameRootDoesTheLookups`, `TestCommitPendingSnapshotLooksUpTheRootAndSegments`, `TestMetadataRetryLooksUpOnlyWithoutAPassedCheck` (`core/frontend/chunkstore/layout_commit_check_guards_test.go`); through the provider's wrapped remote: `TestProviderCutCommitsWhenItsObjectCheckOutlastsTheHeadUpdate`, `TestProviderWrappedRemoteKeepsTheLayoutRemotesOptionalInterfaces` (`core/frontend/durable/commit_check_budget_test.go`); `TestLayoutRemoteUploadFinishedAfterSessionEndCannotCommit` (`core/frontend/chunkstore/layout_upload_concurrency_test.go`) | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-SNAPSHOT-META-001` | Snapshot retention metadata (creation time, class, expiry and at most four bounded labels) is written by the commit that creates the snapshot name and never changed by a replay. | M9 snap -> idle (named Commit) | `core/frontend/chunkstore/layout/head.go` `SnapshotMeta`, codec; `commit.go` `Head.Commit`, `RunCommit`; `core/host/volume/chunk_control.go` `chunkControl.Snapshot`, `chunkControl.ListSnapshots` | `TestSnapshotMeta_WrittenOnceByTheCommit`, `TestHead_BoundsRefused`, `TestGuard_SafetyIgnoresSnapshotMeta`; `TestChunkControlPassesRetentionFactsToTheNamedCommit`, `TestChunkControlUsesTheBoundLayoutIdentity` | Retention facts affect policy only; the safety guard forbids their use in reachability/removal. Actual provider and scheduled capture integration remain cutover work; no live-path claim. | ACTIVE | +| `INV-SNAPK-SNAPSHOT-POSITION-001` | A snapshot name is recorded only by a commit at or past the position of the last write acknowledged when the request set it; a cut already past its barrier commits unnamed and leaves it pending for the request's own cut, and a withdrawn name is never recorded. | M9 snap -> idle (named Commit) | `core/frontend/chunkstore/layout_remote.go` `SetSnapshot`, `CASHead`, `CommitPendingSnapshot`; `core/frontend/durable/provider_layout_snapshot.go` `SnapshotVolume` (scheduled snapshots and volume backup take this path) | `TestASnapshotNameIsNotTakenByACutPastItsBarrier`, `TestAWithdrawnSnapshotNameIsNeverRecorded`, `TestAPendingNameIsNotPutOnAnImageOlderThanTheRequest` (`core/frontend/chunkstore/layout_snapshot_position_test.go`) | — (the snapshot-name model is not in this repository) | ACTIVE | | `INV-SNAPK-DETACHED-001` | A detached HEAD remains closed to its same-epoch holder; only a higher-epoch Claim returns it to live. | M9 snap → detached → higher-epoch idle | `core/frontend/chunkstore/layout` `commit.go`, `head.go`, `claim.go`, `start.go` | `TestLayoutStopAbortMustConfirmItsOutcome`, `TestWorkspaceLayoutDetachedProof`, `TestCommitDetached_ProofAndLostReply`, `TestDetachedHEAD_RefusesEveryHolderTransition`, `TestDetachedHEAD_NewGrantStillWaitsForTheOldLease` | Caller write-barrier and synced-position join, master clean-proof reader and composed model review remain cutover gates. | ACTIVE | | `INV-SNAPK-TXN-INTERRUPT-001` | A snapshot transaction killed at any of its writes (Begin, each object, Commit), before the write or after it applied with its reply lost, never leaves HEAD reaching a missing object, and the restarted holder (roll back, new session, base recomputed from HEAD) ends at the committed image. | M9 idle → snap → idle | `core/frontend/chunkstore/layout` `head.go`, `commit.go` (the transitions above) | `TestSnapshotSession_InterruptAtEveryStep`, `TestReachableMissing_Controls`, `TestAbort_AfterCommitLeavesTheCommit` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `INV-SNAPK-CLAIM-LEASE-001` | A claim takes HEAD only at a newer epoch and only after every older holder's lease has ended (checked after the read, before the update); it voids any open session or cleaning, so the old holder's commit, abort and begin are refused. | M9 any → idle (Claim) | `core/frontend/chunkstore/layout` `claim.go` `RunClaim`, `Head.Claim` | `TestClaim_Closure`, `TestRunClaim_WaitsForTheOldLease`, `TestRunClaim_VoidsTheOldSession`, `TestRunClaim_InterruptAtEveryStep` | — (the snapshot kernel model is not in this repository) | ACTIVE | From df9d40c7b9b3c121c3fef8573cdc32d2019a5996 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 29 Nov 2026 16:32:47 -0800 Subject: [PATCH 2/2] chunkstore, durable: the named snapshot is the request's own image, through SnapshotVolume too Co-Authored-By: Claude Opus 5.5 --- .../layout_snapshot_position_test.go | 6 + .../provider_snapshot_position_test.go | 111 ++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 seaweed-block/core/frontend/durable/provider_snapshot_position_test.go diff --git a/seaweed-block/core/frontend/chunkstore/layout_snapshot_position_test.go b/seaweed-block/core/frontend/chunkstore/layout_snapshot_position_test.go index 3a50a51790..5042ceefe7 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_snapshot_position_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout_snapshot_position_test.go @@ -85,6 +85,12 @@ func TestASnapshotNameIsNotTakenByACutPastItsBarrier(t *testing.T) { if second.H < from || h.Position < from { t.Fatalf("the named image is at %d (HEAD %d); want at or past the request's %d", second.H, h.Position, from) } + // The named snapshot is this image itself: its own root and position, + // not an older root kept under the name. + snap := h.Snapshots["daily"] + if h.Current == nil || snap.Root != *h.Current || snap.Position != h.Position || snap.Position < from { + t.Fatalf("snapshot daily: root %v position %d; want HEAD's root %v and position %d (>= %d)", snap.Root, snap.Position, h.Current, h.Position, from) + } lh.readBack("reader", map[uint32][]byte{0: block(1), lrChunkBlocks: block(2)}) } diff --git a/seaweed-block/core/frontend/durable/provider_snapshot_position_test.go b/seaweed-block/core/frontend/durable/provider_snapshot_position_test.go new file mode 100644 index 0000000000..bd66bfd011 --- /dev/null +++ b/seaweed-block/core/frontend/durable/provider_snapshot_position_test.go @@ -0,0 +1,111 @@ +package durable_test + +import ( + "context" + "sync" + "testing" + + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore" + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout" + "github.com/seaweedfs/seaweed-block/core/frontend/durable" + "github.com/seaweedfs/seaweed-block/core/frontend/durable/testutil" + "github.com/seaweedfs/seaweed-block/core/storage" +) + +// heldCutRemote holds the first cut at its commit check (past its barrier) +// and reports when a snapshot request has set its name. +type heldCutRemote struct { + *chunkstore.LayoutRemote + entered, release, named chan struct{} + enter, name sync.Once +} + +func (r *heldCutRemote) CheckCommit(ctx context.Context, e storage.ChunkHeadVersion, n storage.ChunkHead) error { + held := false + r.enter.Do(func() { held = true }) + if held { + close(r.entered) + <-r.release + } + return r.LayoutRemote.CheckCommit(ctx, e, n) +} + +func (r *heldCutRemote) SetSnapshot(name string, meta layout.SnapshotMeta, from uint64) { + r.LayoutRemote.SetSnapshot(name, meta, from) + r.name.Do(func() { close(r.named) }) +} + +// A snapshot request that arrives through the provider while a cut is past +// its barrier gets an image holding every write acknowledged before it: +// that earlier cut does not take the name, the request's own cut does. +func TestASnapshotRequestDuringACutPastItsBarrierNamesItsOwnImage(t *testing.T) { + ctx := context.Background() + fixture := testutil.NewLayoutFixture(t, "v1", pcBlocks, pcChunkBlocks) + remote := &heldCutRemote{entered: make(chan struct{}), release: make(chan struct{}), named: make(chan struct{})} + cfg := durable.ProviderConfig{Impl: durable.ImplWALStoreChunk, StorageRoot: t.TempDir(), BlockSize: 4096, NumBlocks: pcBlocks, ChunkBlocks: pcChunkBlocks} + fixture.Configure(&cfg) + cfg.ChunkLayoutRemoteFor = func(_ string, me layout.HolderID) (storage.ChunkRemote, error) { + inner, err := chunkstore.NewLayoutRemote(fixture.Catalog, fixture.Ref, me) + if err != nil { + return nil, err + } + remote.LayoutRemote = inner + return remote, nil + } + p, err := durable.NewDurableProvider(cfg, v1View()) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = p.Close() }) + openRecovered(t, p) + b, err := p.Open(ctx, "v1") + if err != nil { + t.Fatal(err) + } + if _, err := b.Write(ctx, 0, block(1)); err != nil { + t.Fatal(err) + } + type cutOut struct { + r storage.ChunkCutResult + err error + } + earlier := make(chan cutOut, 1) + go func() { + r, err := p.CutVolume(ctx, "v1", 0) + earlier <- cutOut{r, err} + }() + <-remote.entered // that cut is past its barrier + // A write acknowledged before the request, then the request. + if _, err := b.Write(ctx, int64(4096*pcChunkBlocks), block(2)); err != nil { + t.Fatal(err) + } + type snapOut struct { + h layout.Head + err error + } + request := make(chan snapOut, 1) + go func() { + h, err := p.SnapshotVolume(ctx, "v1", "daily", layout.SnapshotMeta{CreatedNs: 1}) + request <- snapOut{h, err} + }() + <-remote.named + close(remote.release) + first := <-earlier + if first.err != nil { + t.Fatal(first.err) + } + got := <-request + if got.err != nil { + t.Fatalf("the snapshot request: %v", got.err) + } + snap, ok := got.h.Snapshots["daily"] + if !ok { + t.Fatalf("the snapshot is not in HEAD: %v", got.h.Snapshots) + } + if snap.Position <= first.r.H { + t.Fatalf("snapshot daily is at %d, the earlier cut's position (%d): that cut, past its barrier when the request came, took the name", snap.Position, first.r.H) + } + if !bytesEqual(layoutHeadBlock(t, fixture, uint32(pcChunkBlocks)), block(2)) || got.h.Current == nil || snap.Root != *got.h.Current { + t.Fatal("the named image does not hold the write acknowledged before the request") + } +}