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..ba292a9869 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/clean.go @@ -0,0 +1,61 @@ +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 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") + } + 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..598e04d7fc --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/clean_test.go @@ -0,0 +1,134 @@ +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}, + "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) + } + } +} + +// 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. +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 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) + } + }) +} + +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) + } + }) +} diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index f226468477..f73dcb2b00 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -528,6 +528,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 | @@ -536,10 +538,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 -335 rows: 264 ACTIVE, 67 STUB, 4 ARCHIVED; 15 kept from the internal ledger, 320 new. +336 rows: 265 ACTIVE, 67 STUB, 4 ARCHIVED; 15 kept from the internal ledger, 321 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. @@ -1086,6 +1090,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: