From 9dd23468d4ba05f9328a73016022d3f1d7c803ef Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 24 Nov 2026 12:37:35 -0800 Subject: [PATCH 1/4] layout: open and close a cleaning session BeginClean moves activity idle -> clean(session) and EndClean moves it back to idle, each by one conditional update for the holder at HEAD's epoch. A cleaning and a snapshot exclude each other through the one activity field; a claim voids an open cleaning. Replays of a lost reply write nothing. Removal of pool names inside the session comes separately. Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/chunkstore/layout/clean.go | 60 +++++++++ .../frontend/chunkstore/layout/clean_test.go | 120 ++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 seaweed-block/core/frontend/chunkstore/layout/clean.go create mode 100644 seaweed-block/core/frontend/chunkstore/layout/clean_test.go diff --git a/seaweed-block/core/frontend/chunkstore/layout/clean.go b/seaweed-block/core/frontend/chunkstore/layout/clean.go new file mode 100644 index 0000000000..d26ce973ee --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/clean.go @@ -0,0 +1,60 @@ +package layout + +import ( + "context" + "errors" + "fmt" +) + +// BeginClean is transition 6: activity idle -> clean(session), by the +// holder at HEAD's epoch. While it is open no snapshot session can begin, +// so no name is removed while a snapshot is open. BeginClean of the +// cleaning already open is a replay; any other open activity is ErrBusy. +func (h Head) BeginClean(me HolderID, session string) (next Head, changed bool, err error) { + if session == "" { + return Head{}, false, errors.New("layout: BeginClean needs a session id") + } + if !h.heldBy(me) { + return Head{}, false, fmt.Errorf("%w: HEAD epoch %d holder %q, caller epoch %d holder %q", ErrNotHolder, h.Epoch, h.Holder, me.Epoch, me.Holder) + } + switch { + case h.Activity == Activity{Kind: ActClean, Session: session}: + return h, false, nil + case h.Activity.Kind != ActIdle: + return Head{}, false, fmt.Errorf("%w: %s(%s)", ErrBusy, h.Activity.Kind, h.Activity.Session) + } + next = h.clone() + next.Activity = Activity{Kind: ActClean, Session: session} + return next, true, nil +} + +// EndClean is transition 8: activity clean(session) -> idle, by the holder +// at HEAD's epoch. EndClean over idle is a replay of a lost reply; another +// open session or cleaning is ErrNoSession. +func (h Head) EndClean(me HolderID, session string) (next Head, changed bool, err error) { + if session == "" { + return Head{}, false, errors.New("layout: EndClean needs a session id") + } + if !h.heldBy(me) { + return Head{}, false, fmt.Errorf("%w: HEAD epoch %d holder %q, caller epoch %d holder %q", ErrNotHolder, h.Epoch, h.Holder, me.Epoch, me.Holder) + } + switch { + case h.Activity.Kind == ActIdle: + return h, false, nil + case h.Activity != Activity{Kind: ActClean, Session: session}: + return Head{}, false, fmt.Errorf("%w: HEAD has %s(%s), not clean(%s)", ErrNoSession, h.Activity.Kind, h.Activity.Session, session) + } + next = h.clone() + next.Activity = Activity{Kind: ActIdle} + return next, true, nil +} + +// RunBeginClean applies BeginClean by one conditional update. +func RunBeginClean(ctx context.Context, st HeadStore, me HolderID, session string) (Head, error) { + return runTransition(ctx, st, func(h Head) (Head, bool, error) { return h.BeginClean(me, session) }) +} + +// RunEndClean applies EndClean by one conditional update. +func RunEndClean(ctx context.Context, st HeadStore, me HolderID, session string) (Head, error) { + return runTransition(ctx, st, func(h Head) (Head, bool, error) { return h.EndClean(me, session) }) +} diff --git a/seaweed-block/core/frontend/chunkstore/layout/clean_test.go b/seaweed-block/core/frontend/chunkstore/layout/clean_test.go new file mode 100644 index 0000000000..1c392b4364 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/clean_test.go @@ -0,0 +1,120 @@ +package layout + +import ( + "context" + "errors" + "testing" +) + +func cleanHead(session string) Head { + h := snapHead("x") + h.Activity = Activity{Kind: ActClean, Session: session} + return h +} + +func TestBeginClean_Closure(t *testing.T) { + h := idleHead() + next, changed, err := h.BeginClean(me, "c1") + if err != nil || !changed || next.Activity != (Activity{Kind: ActClean, Session: "c1"}) { + t.Fatalf("BeginClean from idle: %+v %t %v", next, changed, err) + } + if again, changed, err := next.BeginClean(me, "c1"); err != nil || changed || again.Activity != next.Activity { + t.Fatalf("BeginClean of the open cleaning: %+v %t %v; want a replay", again, changed, err) + } + for name, tc := range map[string]struct { + h Head + me HolderID + want error + }{ + "snapshot open": {snapHead("s1"), me, ErrBusy}, + "other cleaning": {cleanHead("c2"), me, ErrBusy}, + "other epoch": {h, HolderID{Epoch: 4, Holder: "srv-a"}, ErrNotHolder}, + "other holder": {h, HolderID{Epoch: 3, Holder: "srv-b"}, ErrNotHolder}, + } { + if _, _, err := tc.h.BeginClean(tc.me, "c1"); !errors.Is(err, tc.want) { + t.Errorf("%s: %v; want %v", name, err, tc.want) + } + } + if _, _, err := h.BeginClean(me, ""); err == nil { + t.Error("BeginClean without a session id accepted") + } +} + +func TestEndClean_Closure(t *testing.T) { + h := cleanHead("c1") + next, changed, err := h.EndClean(me, "c1") + if err != nil || !changed || next.Activity != (Activity{Kind: ActIdle}) || next.Position != h.Position || len(next.Snapshots) != len(h.Snapshots) { + t.Fatalf("EndClean: %+v %t %v", next, changed, err) + } + if again, changed, err := next.EndClean(me, "c1"); err != nil || changed { + t.Fatalf("EndClean over idle: %+v %t %v; want a replay", again, changed, err) + } + for name, tc := range map[string]struct { + h Head + me HolderID + want error + }{ + "snapshot open": {snapHead("c1"), me, ErrNoSession}, + "other cleaning": {cleanHead("c2"), me, ErrNoSession}, + "other epoch": {h, HolderID{Epoch: 4, Holder: "srv-a"}, ErrNotHolder}, + } { + if _, _, err := tc.h.EndClean(tc.me, "c1"); !errors.Is(err, tc.want) { + t.Errorf("%s: %v; want %v", name, err, tc.want) + } + } +} + +// A cleaning and a snapshot exclude each other: no snapshot begins while a +// cleaning is open, no cleaning begins while a snapshot is open; a claim +// voids an open cleaning. +func TestClean_ExcludesSnapshot(t *testing.T) { + ctx := context.Background() + h := idleHead() + st := &memHead{h: &h} + if _, err := RunBeginClean(ctx, st, me, "c1"); err != nil { + t.Fatal(err) + } + if _, err := RunBegin(ctx, st, me, "s1"); !errors.Is(err, ErrBusy) { + t.Fatalf("Begin during a cleaning: %v; want ErrBusy", err) + } + if _, err := RunEndClean(ctx, st, me, "c1"); err != nil { + t.Fatal(err) + } + if _, err := RunBegin(ctx, st, me, "s1"); err != nil { + t.Fatal(err) + } + if _, err := RunBeginClean(ctx, st, me, "c2"); !errors.Is(err, ErrBusy) { + t.Fatalf("BeginClean during a snapshot: %v; want ErrBusy", err) + } + c := cleanHead("c1") + st2 := &memHead{h: &c} + if _, err := RunClaim(ctx, st2, &fakeLease{ended: true}, newer); err != nil { + t.Fatal(err) + } + if _, err := RunEndClean(ctx, st2, me, "c1"); !errors.Is(err, ErrNotHolder) { + t.Fatalf("the old holder's EndClean after a claim: %v; want ErrNotHolder", err) + } +} + +func TestCleanSession_InterruptAtEveryStep(t *testing.T) { + t.Run("BeginClean", func(t *testing.T) { + interruptHead(t, idleHead(), func(st HeadStore) error { + _, err := RunBeginClean(context.Background(), st, me, "c1") + return err + }, func(t *testing.T, h Head) { + if h.Activity != (Activity{Kind: ActClean, Session: "c1"}) { + t.Fatalf("HEAD after BeginClean: %+v", h.Activity) + } + }) + }) + t.Run("EndClean", func(t *testing.T) { + interruptHead(t, cleanHead("c1"), func(st HeadStore) error { + _, err := RunEndClean(context.Background(), st, me, "c1") + return err + }, func(t *testing.T, h Head) { + if h.Activity != (Activity{Kind: ActIdle}) { + t.Fatalf("HEAD after EndClean: %+v", h.Activity) + } + }) + }) +} From 048db98b3cab77f4f022e2860f8aa77e045b73ad Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 24 Nov 2026 12:43:46 -0800 Subject: [PATCH 2/4] test: one interrupt test per cleaning-session transition Co-Authored-By: Claude Opus 5.5 --- .../frontend/chunkstore/layout/clean_test.go | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/seaweed-block/core/frontend/chunkstore/layout/clean_test.go b/seaweed-block/core/frontend/chunkstore/layout/clean_test.go index 1c392b4364..b7e20285ff 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/clean_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/clean_test.go @@ -96,25 +96,24 @@ func TestClean_ExcludesSnapshot(t *testing.T) { } } -func TestCleanSession_InterruptAtEveryStep(t *testing.T) { - t.Run("BeginClean", func(t *testing.T) { - interruptHead(t, idleHead(), func(st HeadStore) error { - _, err := RunBeginClean(context.Background(), st, me, "c1") - return err - }, func(t *testing.T, h Head) { - if h.Activity != (Activity{Kind: ActClean, Session: "c1"}) { - t.Fatalf("HEAD after BeginClean: %+v", h.Activity) - } - }) +func TestRunBeginClean_InterruptAtEveryStep(t *testing.T) { + interruptHead(t, idleHead(), func(st HeadStore) error { + _, err := RunBeginClean(context.Background(), st, me, "c1") + return err + }, func(t *testing.T, h Head) { + if h.Activity != (Activity{Kind: ActClean, Session: "c1"}) { + t.Fatalf("HEAD after BeginClean: %+v", h.Activity) + } }) - t.Run("EndClean", func(t *testing.T) { - interruptHead(t, cleanHead("c1"), func(st HeadStore) error { - _, err := RunEndClean(context.Background(), st, me, "c1") - return err - }, func(t *testing.T, h Head) { - if h.Activity != (Activity{Kind: ActIdle}) { - t.Fatalf("HEAD after EndClean: %+v", h.Activity) - } - }) +} + +func TestRunEndClean_InterruptAtEveryStep(t *testing.T) { + interruptHead(t, cleanHead("c1"), func(st HeadStore) error { + _, err := RunEndClean(context.Background(), st, me, "c1") + return err + }, func(t *testing.T, h Head) { + if h.Activity != (Activity{Kind: ActIdle}) { + t.Fatalf("HEAD after EndClean: %+v", h.Activity) + } }) } From 4accfbf9ed159f93ed70a148ba0e5c520f8ab98f Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 24 Nov 2026 13:12:06 -0800 Subject: [PATCH 3/4] layout: EndClean over idle means only that no activity remains EndClean over idle succeeds without a write whether the cleaning ended or never began. A same-epoch caller that is another server is refused (control added; with the holder check reduced to the epoch it fails). Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/chunkstore/layout/clean.go | 5 +++-- .../frontend/chunkstore/layout/clean_test.go | 21 ++++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/seaweed-block/core/frontend/chunkstore/layout/clean.go b/seaweed-block/core/frontend/chunkstore/layout/clean.go index d26ce973ee..ba292a9869 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/clean.go +++ b/seaweed-block/core/frontend/chunkstore/layout/clean.go @@ -29,8 +29,9 @@ func (h Head) BeginClean(me HolderID, session string) (next Head, changed bool, } // EndClean is transition 8: activity clean(session) -> idle, by the holder -// at HEAD's epoch. EndClean over idle is a replay of a lost reply; another -// open session or cleaning is ErrNoSession. +// at HEAD's epoch. EndClean over idle succeeds and writes nothing: it means +// only that no activity remains (the cleaning ended, or never began). +// Another open session or cleaning is ErrNoSession. func (h Head) EndClean(me HolderID, session string) (next Head, changed bool, err error) { if session == "" { return Head{}, false, errors.New("layout: EndClean needs a session id") diff --git a/seaweed-block/core/frontend/chunkstore/layout/clean_test.go b/seaweed-block/core/frontend/chunkstore/layout/clean_test.go index b7e20285ff..598e04d7fc 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/clean_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/clean_test.go @@ -54,9 +54,10 @@ func TestEndClean_Closure(t *testing.T) { me HolderID want error }{ - "snapshot open": {snapHead("c1"), me, ErrNoSession}, - "other cleaning": {cleanHead("c2"), me, ErrNoSession}, - "other epoch": {h, HolderID{Epoch: 4, Holder: "srv-a"}, ErrNotHolder}, + "snapshot open": {snapHead("c1"), me, ErrNoSession}, + "other cleaning": {cleanHead("c2"), me, ErrNoSession}, + "other epoch": {h, HolderID{Epoch: 4, Holder: "srv-a"}, ErrNotHolder}, + "same epoch, other holder": {h, HolderID{Epoch: 3, Holder: "srv-b"}, ErrNotHolder}, } { if _, _, err := tc.h.EndClean(tc.me, "c1"); !errors.Is(err, tc.want) { t.Errorf("%s: %v; want %v", name, err, tc.want) @@ -64,6 +65,20 @@ func TestEndClean_Closure(t *testing.T) { } } +// EndClean over idle succeeds and writes nothing whatever came before (a +// cleaning ended, or never begun): it means only that no activity remains. +func TestEndClean_IdleMeansNoActivityRemains(t *testing.T) { + for name, h := range map[string]Head{ + "never begun": idleHead(), + "already ended": func() Head { n, _, _ := cleanHead("c1").EndClean(me, "c1"); return n }(), + } { + next, changed, err := h.EndClean(me, "c1") + if err != nil || changed || next.Activity.Kind != ActIdle { + t.Errorf("EndClean over idle (%s): %+v %t %v; want unchanged, no error", name, next, changed, err) + } + } +} + // A cleaning and a snapshot exclude each other: no snapshot begins while a // cleaning is open, no cleaning begins while a snapshot is open; a claim // voids an open cleaning. From f213daf8a5a89f7d169497401595c8dfb13e7eb7 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 24 Nov 2026 13:15:52 -0800 Subject: [PATCH 4/4] docs: snapshot kernel ledger rows for this step Co-Authored-By: Claude Opus 5.5 --- seaweed-block/docs/design/invariants.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 0d23066b64..81819a41ff 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -509,6 +509,8 @@ stateDiagram-v2 idle --> idle: Claim(e) snap --> idle: Claim(e), s void clean --> idle: Claim(e), c void + idle --> clean: BeginClean(c) + clean --> idle: EndClean(c) ``` | From | To | Guard | Enforced in | Protects | @@ -517,10 +519,12 @@ stateDiagram-v2 | snap(s) | idle | holder at HEAD's epoch; activity is snap(s); over idle it succeeds without a write and means only that no activity remains | head.go `Head.Abort` | `INV-SNAPK-ABORT-001` | | snap(s) | idle, current = root | holder at HEAD's epoch; activity is snap(s); the previous manifest builds to HEAD's current root; the new own chunks, every segment and the root are present | commit.go `Head.Commit`, `RunCommit` | `INV-SNAPK-COMMIT-REACH-001`, `INV-SNAPK-TXN-INTERRUPT-001` | | any | idle at epoch e | e above HEAD's epoch (the same epoch and server is a replay); every older holder's lease has ended, checked after the read and before the update | claim.go `Head.Claim`, `RunClaim` (`LeaseWait.OldLeaseEnded`) | `INV-SNAPK-CLAIM-LEASE-001` | +| idle | clean(c) | holder at HEAD's epoch; activity idle (clean(c) again is a replay) | clean.go `Head.BeginClean` | `INV-SNAPK-CLEAN-EXCL-001` | +| clean(c) | idle | holder at HEAD's epoch; activity clean(c); over idle it succeeds without a write | clean.go `Head.EndClean` | `INV-SNAPK-CLEAN-EXCL-001` | ## Part B. Invariant ledger -333 rows: 263 ACTIVE, 67 STUB, 3 ARCHIVED; 15 kept from the internal ledger, 318 new. +334 rows: 264 ACTIVE, 67 STUB, 3 ARCHIVED; 15 kept from the internal ledger, 319 new. The "Machine / transition" column refers to Part A. Notes under a table record what a row does not cover and, for kept rows, what was narrowed from the internal ledger. @@ -1064,6 +1068,7 @@ Notes: | `INV-SNAPK-COMMIT-REACH-001` | A commit never names an object that is not written: before its one HEAD update it finds present the own chunks new against HEAD's current root (that root rebuilt from the previous manifest, an empty image included), every segment and the root; unchanged and foreign chunks are not looked up. | M9 snap → idle (Commit) | `core/frontend/chunkstore/layout` `commit.go` `RunCommit`, `Head.Commit` | `TestCommit_Closure`, `TestRunCommit_LooksUpOnlyWhatIsNew`, `TestRunCommit_RefusesUnwrittenObjects`, `TestRunCommit_StaleBaseAndLookupFailure`, `TestRunCommit_AfterAnEmptyPublishedRoot`, `TestRunCommit_InterruptAtEveryStep` | — (the snapshot kernel model is not in this repository) | 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 | +| `INV-SNAPK-CLEAN-EXCL-001` | A cleaning and a snapshot exclude each other through HEAD's one activity field, so no name is removed while a snapshot is open; a claim voids an open cleaning. | M9 idle → clean → idle | `core/frontend/chunkstore/layout` `clean.go` `Head.BeginClean`, `Head.EndClean` | `TestBeginClean_Closure`, `TestEndClean_Closure`, `TestEndClean_IdleMeansNoActivityRemains`, `TestClean_ExcludesSnapshot`, `TestRunBeginClean_InterruptAtEveryStep`, `TestRunEndClean_InterruptAtEveryStep` | — (the snapshot kernel model is not in this repository) | ACTIVE | Notes: