From b3c7ae2b0b369e5b081204cabfe710dfd4a8f873 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 24 Nov 2026 12:41:40 -0800 Subject: [PATCH] layout: one HEAD write path and its guard Begin now writes through runTransition, the one read-apply-update path every transition will use. A source-scan guard holds for the whole series without edits: only runTransition calls UpdateHead and the store cannot create a HEAD; every Run transition must have its closure and interrupt tests; only a claim waits for the old lease; and no function removes a store object until removal brings its own lease re-check. Co-Authored-By: Claude Opus 5.5 --- .../frontend/chunkstore/layout/guard_test.go | 136 ++++++++++++++++++ .../core/frontend/chunkstore/layout/head.go | 8 +- 2 files changed, 143 insertions(+), 1 deletion(-) 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..d8e2868c1d --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/guard_test.go @@ -0,0 +1,136 @@ +package layout + +import ( + "go/ast" + "go/parser" + "go/token" + "os" + "path/filepath" + "reflect" + "sort" + "strings" + "testing" +) + +// funcCalls maps each function of the package's files (tests or not) to +// the names it calls. +func funcCalls(t *testing.T, tests bool) 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") != tests { + 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 { + switch x := fn.Recv.List[0].Type.(type) { + case *ast.Ident: + name = x.Name + "." + name + case *ast.StarExpr: + name = x.X.(*ast.Ident).Name + "." + 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 the facts this package writes. It holds at every +// step of the series without edits: a new writer, transition or remover +// fails it until it brings the tests named here. +// +// - HEAD: the only UpdateHead call is in runTransition (read, one pure +// step, one conditional update); the store has no way to create a HEAD. +// - Transitions: every Run through runTransition has a closure test +// (Test_Closure) and an interrupt test (TestRun_InterruptAtEveryStep). +// - Lease, claim half: only RunClaim calls OldLeaseEnded, and RunClaim, +// where it exists, calls it. +// - Pool: no function removes a store object yet; the removal that comes +// with cleaning must update this guard and bring its lease re-check. +func TestGuard_WritersTransitionsAndRemovers(t *testing.T) { + code := funcCalls(t, false) + tests := funcCalls(t, true) + var writers []string + for fn, cs := range code { + if cs["UpdateHead"] { + writers = append(writers, fn) + } + } + sort.Strings(writers) + if !reflect.DeepEqual(writers, []string{"runTransition"}) { + t.Errorf("functions calling UpdateHead: %v; want only runTransition", writers) + } + 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.Errorf("HeadStore methods %v; want ReadHead and UpdateHead only (no path creates a HEAD)", methods) + } + var runs []string + for fn, cs := range code { + if !cs["runTransition"] { + continue + } + runs = append(runs, fn) + x, ok := strings.CutPrefix(fn, "Run") + if !ok { + t.Errorf("%s calls runTransition; transitions are exported Run functions", fn) + continue + } + for _, want := range []string{"Test" + x + "_Closure", "TestRun" + x + "_InterruptAtEveryStep"} { + if _, ok := tests[want]; !ok { + t.Errorf("transition %s has no %s", fn, want) + } + } + } + if len(runs) == 0 { + t.Fatal("no transition found: the scan is broken") + } + for fn, cs := range code { + if cs["OldLeaseEnded"] && fn != "RunClaim" { + t.Errorf("%s calls OldLeaseEnded; only RunClaim claims", fn) + } + } + if cs, ok := code["RunClaim"]; ok && !cs["OldLeaseEnded"] { + t.Error("RunClaim does not wait for the old lease") + } + for fn, cs := range code { + for c := range cs { + if strings.HasPrefix(c, "Delete") || strings.HasPrefix(c, "Remove") { + t.Errorf("%s calls %s: a store remover needs its lease re-check and its own guard row", fn, c) + } + } + } +} diff --git a/seaweed-block/core/frontend/chunkstore/layout/head.go b/seaweed-block/core/frontend/chunkstore/layout/head.go index e8962acc69..d93c76a41d 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/head.go +++ b/seaweed-block/core/frontend/chunkstore/layout/head.go @@ -223,6 +223,12 @@ type HeadStore interface { // finds snap(session) and returns without writing. An absent HEAD refuses // (no path creates a HEAD here). func RunBegin(ctx context.Context, st HeadStore, me HolderID, session string) (Head, error) { + return runTransition(ctx, st, func(h Head) (Head, bool, error) { return h.Begin(me, session) }) +} + +// runTransition is read HEAD, apply t, and one conditional update when t +// changed it. An absent HEAD is refused: no transition creates one. +func runTransition(ctx context.Context, st HeadStore, t func(Head) (Head, bool, error)) (Head, error) { h, found, err := st.ReadHead(ctx) if err != nil { return Head{}, err @@ -230,7 +236,7 @@ func RunBegin(ctx context.Context, st HeadStore, me HolderID, session string) (H if !found { return Head{}, ErrHeadAbsent } - next, changed, err := h.Begin(me, session) + next, changed, err := t(h) if err != nil || !changed { return next, err }