Files
seaweedfs/seaweed-block/core/chunkkernel/step.go
T

153 lines
4.1 KiB
Go

// Package chunkkernel implements the pure chunk-kernel/2 transition table.
// It performs no storage I/O and has no production callers.
package chunkkernel
import (
"crypto/sha256"
_ "embed"
"fmt"
"strings"
)
const Version = "chunk-kernel/2"
// SHA256 of the model export; a model revision must update this pin with the table.
const tableSHA256 = "b0324908925b5c99ae5f749a498e0c006ca54b9a26fa99a0a0fec8411f1bb1b5"
type Kind string
const (
Absent Kind = "A"
Partial Kind = "P"
Unknown Kind = "U"
HashUnpublished Kind = "HU"
HashPublished Kind = "HP"
)
type Event string
const (
Write Event = "Write"
Fill Event = "Fill"
Hash Event = "Hash"
Commit Event = "Commit"
Evict Event = "Evict"
Move Event = "Move"
Changed Event = "Changed"
Rebuilt Event = "Rebuilt"
)
type Effect string
const (
None Effect = "none"
PageBeforeBytes Effect = "page_before_bytes"
DeclaredLoss Effect = "declared_loss"
Drop Effect = "drop"
CursorBack Effect = "cursor_back"
)
// 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 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.
// A refusal changes no state and requests no effect.
type CallerDefect struct {
State Kind
Event Event
Arg string
}
func (e *CallerDefect) Error() string {
return fmt.Sprintf("chunk kernel: refused %s/%s/%s", e.State, e.Event, e.Arg)
}
type edge struct {
state Kind
event Event
arg string
}
type transition struct {
result string
effect Effect
}
//go:embed chunk-kernel-2.tsv
var exportedTable string
var transitions = loadTable()
func loadTable() map[edge]transition {
if fmt.Sprintf("%x", sha256.Sum256([]byte(exportedTable))) != tableSHA256 {
panic("chunk kernel: exported table SHA256 mismatch")
}
table := map[edge]transition{}
version := ""
for _, line := range strings.Split(exportedTable, "\n") {
line = strings.TrimSuffix(line, "\r")
if line == "" {
continue
}
if strings.HasPrefix(line, "#") {
if _, v, ok := strings.Cut(line, "version: "); ok {
version = v
}
continue
}
c := strings.Split(line, "\t")
if len(c) != 5 {
panic("chunk kernel: malformed exported table row")
}
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 performs one fixed-table lookup, with no volume-sized work or I/O.
// Table verification and parsing happen once at package initialization. A refusal
// allocates the typed caller-defect error.
//
// 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{State{Kind(t.result)}, Applied}, t.effect, nil
}