diff --git a/seaweed-block/core/chunkkernel/step.go b/seaweed-block/core/chunkkernel/step.go index 493a8be876..2c831480aa 100644 --- a/seaweed-block/core/chunkkernel/step.go +++ b/seaweed-block/core/chunkkernel/step.go @@ -1,22 +1,23 @@ -// Package chunkkernel implements the pure chunk-kernel/1 transition table. -// It performs no storage I/O and is not connected to the serving path. +// Package chunkkernel implements the pure chunk-kernel/2 transition table. +// It performs no storage I/O and has no production callers. package chunkkernel import ( + _ "embed" "fmt" - "slices" + "strings" ) -const Version = "chunk-kernel/1" +const Version = "chunk-kernel/2" type Kind string const ( - Absent Kind = "A" - Partial Kind = "P" - Unknown Kind = "U" - Hashed Kind = "H" - Ignored Kind = "ignored" + Absent Kind = "A" + Partial Kind = "P" + Unknown Kind = "U" + HashUnpublished Kind = "HU" + HashPublished Kind = "HP" ) type Event string @@ -25,6 +26,7 @@ const ( Write Event = "Write" Fill Event = "Fill" Hash Event = "Hash" + Commit Event = "Commit" Evict Event = "Evict" Move Event = "Move" Changed Event = "Changed" @@ -41,21 +43,27 @@ const ( CursorBack Effect = "cursor_back" ) -// State describes local coverage. Blocks is the partial chunk's local-block -// bitmap represented as block indices; other kinds do not retain this bitmap. -// The shell owns bytes, hashes, persistence and the meaning of block indices. -type State struct { - Kind Kind - Blocks []uint64 -} +// State contains only the model state. The shell owns the durable bitmap, +// bytes, hashes and geometry; there is no second copy of those facts here. +type State struct{ Kind Kind } -// Arg.Value is the model's coverage (none/some/all), comparison (equal/differs) -// or '-' argument. WrittenBlocks names only this write's blocks. Changed and -// Rebuilt receive the store's complete local bitmap in LocalBlocks. -// Coverage is supplied by the shell; this kernel has no volume geometry. -type Arg struct { - Value string - WrittenBlocks, LocalBlocks []uint64 +// Arg.Value is coverage (none/some/all), comparison (equal/differs), or '-'. +// For Write, coverage describes the resulting chunk, not just this write. +type Arg struct{ Value string } + +type Outcome string + +const ( + Applied Outcome = "applied" + Ignored Outcome = "ignored" + Refused Outcome = "refused" +) + +// Result separates the state a caller can store from the transition outcome. +// Ignored and refused results preserve the exact input state. +type Result struct { + State State + Outcome Outcome } // CallerDefect is a refused table edge or an input outside the table domain. @@ -70,98 +78,64 @@ func (e *CallerDefect) Error() string { return fmt.Sprintf("chunk kernel: refused %s/%s/%s", e.State, e.Event, e.Arg) } -// Step returns the model result and the effect the shell must carry out. -// Ignored means no change and no record. Refusal is a typed caller defect. -// All returned bitmaps are fresh; neither input nor a previous result is mutated. -func Step(s State, e Event, a Arg) (State, Effect, error) { - refuse := func() (State, Effect, error) { return State{}, None, &CallerDefect{s.Kind, e, a.Value} } - if s.Kind != Absent && s.Kind != Partial && s.Kind != Unknown && s.Kind != Hashed { - return refuse() - } - valid := false - switch e { - case Write: - valid = a.Value == "some" || a.Value == "all" - case Fill, Hash: - valid = a.Value == "-" - case Evict, Move: - valid = a.Value == "equal" || a.Value == "differs" - case Changed, Rebuilt: - valid = a.Value == "none" || a.Value == "some" || a.Value == "all" - } - if !valid { - return refuse() - } - result, effect := State{}, None - switch e { - case Write: - if a.Value == "all" { - result.Kind = Unknown - } else if s.Kind == Absent || s.Kind == Partial { - result.Kind = Partial - // An absent chunk may carry stale caller memory: never merge that bitmap. - if s.Kind == Partial { - result.Blocks = append(result.Blocks, s.Blocks...) - } - result.Blocks = append(result.Blocks, a.WrittenBlocks...) - slices.Sort(result.Blocks) - result.Blocks = slices.Compact(result.Blocks) - } else { - return refuse() - } - case Fill: - switch s.Kind { - case Absent: - result.Kind = Hashed - case Partial: - result.Kind = Unknown - default: - result.Kind = Ignored +type edge struct { + state Kind + event Event + arg string +} +type transition struct { + result string + effect Effect +} + +//go:embed testdata/chunk-kernel-2.tsv +var exportedTable string +var transitions = loadTable() + +func loadTable() map[edge]transition { + table := map[edge]transition{} + version := "" + for _, line := range strings.Split(exportedTable, "\n") { + line = strings.TrimSuffix(line, "\r") + if line == "" { + continue } - case Hash: - switch s.Kind { - case Unknown: - result.Kind = Hashed - case Hashed: - result.Kind = Ignored - default: - return refuse() - } - case Evict: - switch { - case s.Kind == Absent: - result.Kind = Ignored - case s.Kind == Hashed && a.Value == "equal": - result.Kind = Absent - effect = PageBeforeBytes - default: - return refuse() - } - case Move: - result.Kind = Absent - switch s.Kind { - case Partial, Unknown: - effect = DeclaredLoss - case Hashed: - if a.Value == "equal" { - result.Kind = Hashed - } else { - effect = Drop + if strings.HasPrefix(line, "#") { + if _, v, ok := strings.Cut(line, "version: "); ok { + version = v } + continue } - case Changed, Rebuilt: - switch a.Value { - case "none": - result.Kind = Absent - case "some": - result.Kind = Partial - result.Blocks = append([]uint64(nil), a.LocalBlocks...) - case "all": - result.Kind = Unknown + c := strings.Split(line, "\t") + if len(c) != 5 { + panic("chunk kernel: malformed exported table row") } - if e == Rebuilt { - effect = CursorBack + key := edge{Kind(c[0]), Event(c[1]), c[2]} + if _, exists := table[key]; exists { + panic("chunk kernel: duplicate exported edge") } + table[key] = transition{c[3], Effect(c[4])} + } + if version != Version { + panic("chunk kernel: exported table version mismatch") + } + return table +} + +// Step reads the exported model table. Refused edges and inputs outside its +// domain return a typed caller defect, the input state and no effect. +// +// Commit is a shell premise: the known hash must equal the hash the committed +// HEAD names, at the captured version. State HU alone cannot establish this; +// another write and hash during a cut may have replaced the known hash. +// The shell also owns persistence and the ordering required by each Effect. +func Step(s State, e Event, a Arg) (Result, Effect, error) { + t, ok := transitions[edge{s.Kind, e, a.Value}] + if !ok || t.result == "refused" { + return Result{s, Refused}, None, &CallerDefect{s.Kind, e, a.Value} + } + if t.result == "ignored" { + return Result{s, Ignored}, t.effect, nil } - return result, effect, nil + return Result{State{Kind(t.result)}, Applied}, t.effect, nil } diff --git a/seaweed-block/core/chunkkernel/step_test.go b/seaweed-block/core/chunkkernel/step_test.go index c258ff55c0..4122945201 100644 --- a/seaweed-block/core/chunkkernel/step_test.go +++ b/seaweed-block/core/chunkkernel/step_test.go @@ -9,16 +9,20 @@ import ( ) func TestExportedModelEdges(t *testing.T) { - f, err := os.Open("testdata/chunk-kernel-1.tsv") + f, err := os.Open("testdata/chunk-kernel-2.tsv") if err != nil { t.Fatal(err) } defer f.Close() scan := bufio.NewScanner(f) seen := map[string]bool{} + version := "" for scan.Scan() { line := scan.Text() if strings.HasPrefix(line, "#") { + if _, v, ok := strings.Cut(line, "version: "); ok { + version = v + } continue } cols := strings.Split(line, "\t") @@ -31,55 +35,99 @@ func TestExportedModelEdges(t *testing.T) { } seen[key] = true t.Run(key, func(t *testing.T) { - result, effect, err := Step(State{Kind: Kind(cols[0])}, Event(cols[1]), Arg{Value: cols[2]}) + input := State{Kind: Kind(cols[0])} + result, effect, err := Step(input, Event(cols[1]), Arg{Value: cols[2]}) + if string(effect) != cols[4] { + t.Fatalf("effect=%s want%s", effect, cols[4]) + } if cols[3] == "refused" { var defect *CallerDefect - if !errors.As(err, &defect) { - t.Fatalf("refusal error=%v", err) - } - if effect != None { - t.Fatal(effect) + if !errors.As(err, &defect) || result.State != input || result.Outcome != Refused || effect != None { + t.Fatalf("refused edge changed input: %+v/%s/%v", result, effect, err) } return } if err != nil { t.Fatal(err) } - if string(result.Kind) != cols[3] || string(effect) != cols[4] { - t.Fatalf("got %s/%s want %s/%s", result.Kind, effect, cols[3], cols[4]) + if cols[3] == "ignored" { + if result.State != input || result.Outcome != Ignored { + t.Fatalf("ignored edge changed input: %+v", result) + } + } else if string(result.State.Kind) != cols[3] || result.Outcome != Applied { + t.Fatalf("got%+v want%s", result, cols[3]) } }) } if err := scan.Err(); err != nil { t.Fatal(err) } - if len(seen) != 56 { - t.Fatalf("edges=%d want56", len(seen)) + if version == "" || version != Version { + t.Fatalf("version=%q want%q", version, Version) + } + // Every input in the exported model's domain must appear exactly once. + domains := map[Event][]string{Write: {"some", "all"}, Fill: {"-"}, Hash: {"-"}, Commit: {"-"}, Evict: {"-"}, Move: {"equal", "differs"}, Changed: {"some", "all", "none"}, Rebuilt: {"some", "all", "none"}} + total := 0 + for _, s := range []Kind{Absent, Partial, Unknown, HashUnpublished, HashPublished} { + for e, args := range domains { + for _, a := range args { + total++ + if !seen[string(s)+"/"+string(e)+"/"+a] { + t.Errorf("missing %s/%s/%s", s, e, a) + } + } + } + } + if len(seen) != total { + t.Fatalf("edges=%d domain=%d", len(seen), total) } } -func TestAbsentWriteStartsBitmapAfresh(t *testing.T) { - stale := []uint64{1, 2} - written := []uint64{7} - got, _, err := Step(State{Kind: Absent, Blocks: stale}, Write, Arg{Value: "some", WrittenBlocks: written}) - if err != nil { - t.Fatal(err) - } - if got.Kind != Partial || len(got.Blocks) != 1 || got.Blocks[0] != 7 { - t.Fatalf("stale bitmap survived: %+v", got) - } - got.Blocks[0] = 9 - if stale[0] != 1 || written[0] != 7 { - t.Fatal("Step mutated its inputs") +func TestOutsideDomainPreservesInput(t *testing.T) { + for _, tc := range []struct { + s Kind + e Event + a string + }{ + {"not-a-state", Write, "all"}, {Absent, "not-an-event", "-"}, + {Partial, Fill, "equal"}, {HashPublished, Evict, "equal"}, + {Unknown, Write, "none"}, {HashUnpublished, Commit, "some"}, + {Absent, Write, ""}, {"", Fill, "-"}, + } { + t.Run(string(tc.s)+"/"+string(tc.e)+"/"+tc.a, func(t *testing.T) { + input := State{Kind: tc.s} + got, effect, err := Step(input, tc.e, Arg{Value: tc.a}) + var defect *CallerDefect + if !errors.As(err, &defect) { + t.Fatalf("error=%v want CallerDefect", err) + } + if got.State != input || got.Outcome != Refused || effect != None { + t.Fatalf("refusal changed input: %+v/%s", got, effect) + } + if defect.State != tc.s || defect.Event != tc.e || defect.Arg != tc.a { + t.Fatalf("wrong defect: %+v", defect) + } + }) } } -func TestPartialWriteKeepsExistingBlocks(t *testing.T) { - got, _, err := Step(State{Kind: Partial, Blocks: []uint64{2, 4}}, Write, Arg{Value: "some", WrittenBlocks: []uint64{4, 6}}) - if err != nil { - t.Fatal(err) +func TestHashedChunkNeedsPublicationBeforeEviction(t *testing.T) { + got, _, err := Step(State{Kind: Unknown}, Hash, Arg{Value: "-"}) + if err != nil || got.State.Kind != HashUnpublished { + t.Fatalf("hash: %+v %v", got, err) } - if len(got.Blocks) != 3 || got.Blocks[0] != 2 || got.Blocks[1] != 4 || got.Blocks[2] != 6 { - t.Fatalf("bitmap=%v", got.Blocks) + held, effect, err := Step(got.State, Evict, Arg{Value: "-"}) + var defect *CallerDefect + if !errors.As(err, &defect) || held.State != got.State || effect != None { + t.Fatalf("unpublished eviction: %+v/%s/%v", held, effect, err) + } + // The caller has independently established the committed HEAD names this hash. + published, _, err := Step(got.State, Commit, Arg{Value: "-"}) + if err != nil || published.State.Kind != HashPublished { + t.Fatalf("commit: %+v %v", published, err) + } + evicted, effect, err := Step(published.State, Evict, Arg{Value: "-"}) + if err != nil || evicted.State.Kind != Absent || effect != PageBeforeBytes { + t.Fatalf("published eviction: %+v/%s/%v", evicted, effect, err) } } diff --git a/seaweed-block/core/chunkkernel/testdata/chunk-kernel-1.tsv b/seaweed-block/core/chunkkernel/testdata/chunk-kernel-1.tsv deleted file mode 100644 index 07ec014f70..0000000000 --- a/seaweed-block/core/chunkkernel/testdata/chunk-kernel-1.tsv +++ /dev/null @@ -1,58 +0,0 @@ -# kernel table of the per-chunk state. version: chunk-kernel/1 -# state event arg result effect -A Write some P none -A Write all U none -A Fill - H none -A Hash - refused none -A Evict equal ignored none -A Evict differs ignored none -A Move equal A none -A Move differs A none -A Changed some P none -A Changed all U none -A Changed none A none -A Rebuilt some P cursor_back -A Rebuilt all U cursor_back -A Rebuilt none A cursor_back -P Write some P none -P Write all U none -P Fill - U none -P Hash - refused none -P Evict equal refused none -P Evict differs refused none -P Move equal A declared_loss -P Move differs A declared_loss -P Changed some P none -P Changed all U none -P Changed none A none -P Rebuilt some P cursor_back -P Rebuilt all U cursor_back -P Rebuilt none A cursor_back -U Write some refused none -U Write all U none -U Fill - ignored none -U Hash - H none -U Evict equal refused none -U Evict differs refused none -U Move equal A declared_loss -U Move differs A declared_loss -U Changed some P none -U Changed all U none -U Changed none A none -U Rebuilt some P cursor_back -U Rebuilt all U cursor_back -U Rebuilt none A cursor_back -H Write some refused none -H Write all U none -H Fill - ignored none -H Hash - ignored none -H Evict equal A page_before_bytes -H Evict differs refused none -H Move equal H none -H Move differs A drop -H Changed some P none -H Changed all U none -H Changed none A none -H Rebuilt some P cursor_back -H Rebuilt all U cursor_back -H Rebuilt none A cursor_back diff --git a/seaweed-block/core/chunkkernel/testdata/chunk-kernel-2.tsv b/seaweed-block/core/chunkkernel/testdata/chunk-kernel-2.tsv new file mode 100644 index 0000000000..a29a114ed7 --- /dev/null +++ b/seaweed-block/core/chunkkernel/testdata/chunk-kernel-2.tsv @@ -0,0 +1,72 @@ +# kernel table of the per-chunk state. version: chunk-kernel/2 +# state event arg result effect +A Write some P none +A Write all U none +A Fill - HP none +A Hash - refused none +A Commit - refused none +A Evict - ignored none +A Move equal A none +A Move differs A none +A Changed some P none +A Changed all U none +A Changed none A none +A Rebuilt some P cursor_back +A Rebuilt all U cursor_back +A Rebuilt none A cursor_back +P Write some P none +P Write all U none +P Fill - U none +P Hash - refused none +P Commit - refused none +P Evict - refused none +P Move equal A declared_loss +P Move differs A declared_loss +P Changed some P none +P Changed all U none +P Changed none A none +P Rebuilt some P cursor_back +P Rebuilt all U cursor_back +P Rebuilt none A cursor_back +U Write some refused none +U Write all U none +U Fill - ignored none +U Hash - HU none +U Commit - refused none +U Evict - refused none +U Move equal A declared_loss +U Move differs A declared_loss +U Changed some P none +U Changed all U none +U Changed none A none +U Rebuilt some P cursor_back +U Rebuilt all U cursor_back +U Rebuilt none A cursor_back +HU Write some refused none +HU Write all U none +HU Fill - ignored none +HU Hash - ignored none +HU Commit - HP none +HU Evict - refused none +HU Move equal HP none +HU Move differs A declared_loss +HU Changed some P none +HU Changed all U none +HU Changed none A none +HU Rebuilt some P cursor_back +HU Rebuilt all U cursor_back +HU Rebuilt none A cursor_back +HP Write some refused none +HP Write all U none +HP Fill - ignored none +HP Hash - ignored none +HP Commit - ignored none +HP Evict - A page_before_bytes +HP Move equal HP none +HP Move differs A drop +HP Changed some P none +HP Changed all U none +HP Changed none A none +HP Rebuilt some P cursor_back +HP Rebuilt all U cursor_back +HP Rebuilt none A cursor_back