From f17d6ad8c4cf1e54e7ba17a1efaa515da0059bc5 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 24 Nov 2026 12:38:53 -0800 Subject: [PATCH 1/3] test: enumerate the HEAD writers of the layout package A source scan fails when UpdateHead is called anywhere but the shared read-apply-update path, when a transition is added or removed without being listed, when the HEAD store gains a way to create a HEAD, or when the old-lease wait moves out of the claim. Every pure transition is checked to refuse a holder below HEAD's epoch in every activity. Co-Authored-By: Claude Opus 5.5 --- .../frontend/chunkstore/layout/guard_test.go | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 seaweed-block/core/frontend/chunkstore/layout/guard_test.go diff --git a/seaweed-block/core/frontend/chunkstore/layout/guard_test.go b/seaweed-block/core/frontend/chunkstore/layout/guard_test.go new file mode 100644 index 0000000000..c66b728424 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/guard_test.go @@ -0,0 +1,133 @@ +package layout + +import ( + "go/ast" + "go/parser" + "go/token" + "os" + "path/filepath" + "reflect" + "sort" + "strings" + "testing" +) + +// layoutCalls maps each non-test function of this package to the calls it +// makes, by called name. +func layoutCalls(t *testing.T) map[string]map[string]bool { + t.Helper() + files, err := filepath.Glob("*.go") + if err != nil { + t.Fatal(err) + } + out := map[string]map[string]bool{} + for _, f := range files { + if strings.HasSuffix(f, "_test.go") { + continue + } + src, err := os.ReadFile(f) + if err != nil { + t.Fatal(err) + } + file, err := parser.ParseFile(token.NewFileSet(), f, src, 0) + if err != nil { + t.Fatal(err) + } + for _, d := range file.Decls { + fn, ok := d.(*ast.FuncDecl) + if !ok || fn.Body == nil { + continue + } + name := fn.Name.Name + if fn.Recv != nil && len(fn.Recv.List) == 1 { + var typ string + switch x := fn.Recv.List[0].Type.(type) { + case *ast.Ident: + typ = x.Name + case *ast.StarExpr: + typ = x.X.(*ast.Ident).Name + } + name = typ + "." + name + } + calls := map[string]bool{} + ast.Inspect(fn.Body, func(n ast.Node) bool { + if c, ok := n.(*ast.CallExpr); ok { + switch x := c.Fun.(type) { + case *ast.SelectorExpr: + calls[x.Sel.Name] = true + case *ast.Ident: + calls[x.Name] = true + } + } + return true + }) + out[name] = calls + } + } + return out +} + +// Guard enumeration for HEAD writes: the only call of UpdateHead is in +// runTransition (read, apply one pure step, one conditional update), and +// HeadStore offers no way to create a HEAD. Every Run function that +// writes HEAD goes through runTransition. A new writer fails this test +// until it is listed here with its own guard tests. +func TestHeadWriters_SourceGuard(t *testing.T) { + calls := layoutCalls(t) + var writers, runs []string + for fn, cs := range calls { + if cs["UpdateHead"] { + writers = append(writers, fn) + } + if cs["runTransition"] { + runs = append(runs, fn) + } + } + sort.Strings(writers) + sort.Strings(runs) + if !reflect.DeepEqual(writers, []string{"runTransition"}) { + t.Fatalf("functions calling UpdateHead: %v; want only runTransition", writers) + } + wantRuns := []string{"RunAbort", "RunBegin", "RunBeginClean", "RunClaim", "RunCommit", "RunEndClean"} + if !reflect.DeepEqual(runs, wantRuns) { + t.Fatalf("HEAD transitions: %v; want %v (add a new one here with its closure, replay and interrupt tests)", runs, wantRuns) + } + st := reflect.TypeOf((*HeadStore)(nil)).Elem() + var methods []string + for i := 0; i < st.NumMethod(); i++ { + methods = append(methods, st.Method(i).Name) + } + if !reflect.DeepEqual(methods, []string{"ReadHead", "UpdateHead"}) { + t.Fatalf("HeadStore methods %v; want ReadHead and UpdateHead only (no path creates a HEAD)", methods) + } + // Claim's lease wait sits in RunClaim, inside its transition. + if !calls["RunClaim"]["OldLeaseEnded"] { + t.Fatal("RunClaim does not call OldLeaseEnded") + } + for fn, cs := range calls { + if cs["OldLeaseEnded"] && fn != "RunClaim" { + t.Fatalf("%s calls OldLeaseEnded; only RunClaim claims", fn) + } + } +} + +// Every pure transition refuses a caller that is not the holder at HEAD's +// epoch (Claim refuses an older epoch or the same epoch of another server). +func TestTransitions_RefuseAStaleHolder(t *testing.T) { + stale := HolderID{Epoch: 2, Holder: "srv-a"} + rn := RootName{Hash: Sum([]byte("r"))} + for name, step := range map[string]func(Head) error{ + "Begin": func(h Head) error { _, _, err := h.Begin(stale, "s"); return err }, + "Abort": func(h Head) error { _, _, err := h.Abort(stale, "s"); return err }, + "Commit": func(h Head) error { _, _, err := h.Commit(stale, "s", rn, 1, ""); return err }, + "BeginClean": func(h Head) error { _, _, err := h.BeginClean(stale, "c"); return err }, + "EndClean": func(h Head) error { _, _, err := h.EndClean(stale, "c"); return err }, + "Claim": func(h Head) error { _, _, err := h.Claim(stale); return err }, + } { + for _, h := range []Head{idleHead(), snapHead("s"), cleanHead("c")} { + if err := step(h); err == nil { + t.Errorf("%s by epoch 2 over a HEAD at epoch 3 (%s) succeeded", name, h.Activity.Kind) + } + } + } +} From f31db828e7febaaff19bfd8af6c4a50083eb47ac Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 24 Nov 2026 13:12:45 -0800 Subject: [PATCH 2/3] test: stale-holder table covers another server and every transition Each transition is refused for an older epoch of the same server and for the same epoch held by another server, in idle, snap and clean; the table must name every Head method returning (Head, bool, error), so a new transition fails until it is added. Co-Authored-By: Claude Opus 5.5 --- .../frontend/chunkstore/layout/stale_test.go | 56 ++++++++++++++----- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/seaweed-block/core/frontend/chunkstore/layout/stale_test.go b/seaweed-block/core/frontend/chunkstore/layout/stale_test.go index 06643ebd56..832a436731 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/stale_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/stale_test.go @@ -1,24 +1,52 @@ package layout -import "testing" +import ( + "reflect" + "sort" + "testing" +) // Every pure transition refuses a caller that is not the holder at HEAD's -// epoch (Claim refuses an older epoch or the same epoch of another server). +// epoch: an older epoch of the same server, and the same epoch held by +// another server, in idle, snap and clean. The table must name every +// method of Head that returns (Head, bool, error), so a new transition +// fails here until it is added. func TestTransitions_RefuseAStaleHolder(t *testing.T) { - stale := HolderID{Epoch: 2, Holder: "srv-a"} rn := RootName{Hash: Sum([]byte("r"))} - for name, step := range map[string]func(Head) error{ - "Begin": func(h Head) error { _, _, err := h.Begin(stale, "s"); return err }, - "Abort": func(h Head) error { _, _, err := h.Abort(stale, "s"); return err }, - "Commit": func(h Head) error { _, _, err := h.Commit(stale, "s", rn, 1, ""); return err }, - "BeginClean": func(h Head) error { _, _, err := h.BeginClean(stale, "c"); return err }, - "EndClean": func(h Head) error { _, _, err := h.EndClean(stale, "c"); return err }, - "Claim": func(h Head) error { _, _, err := h.Claim(stale); return err }, - } { - for _, h := range []Head{idleHead(), snapHead("s"), cleanHead("c")} { - if err := step(h); err == nil { - t.Errorf("%s by epoch 2 over a HEAD at epoch 3 (%s) succeeded", name, h.Activity.Kind) + steps := map[string]func(Head, HolderID) error{ + "Begin": func(h Head, c HolderID) error { _, _, err := h.Begin(c, "s"); return err }, + "Abort": func(h Head, c HolderID) error { _, _, err := h.Abort(c, "s"); return err }, + "Commit": func(h Head, c HolderID) error { _, _, err := h.Commit(c, "s", rn, 1, ""); return err }, + "BeginClean": func(h Head, c HolderID) error { _, _, err := h.BeginClean(c, "c"); return err }, + "EndClean": func(h Head, c HolderID) error { _, _, err := h.EndClean(c, "c"); return err }, + "Claim": func(h Head, c HolderID) error { _, _, err := h.Claim(c); return err }, + } + callers := map[string]HolderID{ + "older epoch, same server": {Epoch: 2, Holder: "srv-a"}, + "same epoch, another server": {Epoch: 3, Holder: "srv-b"}, + } + for name, step := range steps { + for who, c := range callers { + for _, h := range []Head{idleHead(), snapHead("s"), cleanHead("c")} { + if err := step(h, c); err == nil { + t.Errorf("%s by %s over a HEAD at epoch 3 held by srv-a (%s) succeeded", name, who, h.Activity.Kind) + } } } } + headT, boolT := reflect.TypeOf(Head{}), reflect.TypeOf(false) + errT := reflect.TypeOf((*error)(nil)).Elem() + var missing []string + for i := 0; i < headT.NumMethod(); i++ { + m := headT.Method(i).Type + if m.NumOut() == 3 && m.Out(0) == headT && m.Out(1) == boolT && m.Out(2) == errT { + if _, ok := steps[headT.Method(i).Name]; !ok { + missing = append(missing, headT.Method(i).Name) + } + } + } + sort.Strings(missing) + if len(missing) != 0 { + t.Fatalf("transitions not in the stale-holder table: %v", missing) + } } From cb3f23e37342422e201c68239ac7732a2e4b84f5 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 24 Nov 2026 13:16:05 -0800 Subject: [PATCH 3/3] docs: snapshot kernel ledger rows for this step Co-Authored-By: Claude Opus 5.5 --- seaweed-block/docs/design/invariants.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 81819a41ff..93db41f0f4 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -524,7 +524,7 @@ stateDiagram-v2 ## Part B. Invariant ledger -334 rows: 264 ACTIVE, 67 STUB, 3 ARCHIVED; 15 kept from the internal ledger, 319 new. +335 rows: 265 ACTIVE, 67 STUB, 3 ARCHIVED; 15 kept from the internal ledger, 320 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. @@ -1069,6 +1069,7 @@ Notes: | `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 | +| `INV-SNAPK-STALE-HOLDER-001` | Every transition refuses an older epoch of the same server and the same epoch held by another server, in idle, snap and clean; a new transition must be added to the table. | M9 every transition | `core/frontend/chunkstore/layout` `Head.heldBy` and each transition | `TestTransitions_RefuseAStaleHolder` | — (the snapshot kernel model is not in this repository) | ACTIVE | Notes: