mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
168 lines
4.1 KiB
Go
168 lines
4.1 KiB
Go
// 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
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
)
|
|
|
|
const Version = "chunk-kernel/1"
|
|
|
|
type Kind string
|
|
|
|
const (
|
|
Absent Kind = "A"
|
|
Partial Kind = "P"
|
|
Unknown Kind = "U"
|
|
Hashed Kind = "H"
|
|
Ignored Kind = "ignored"
|
|
)
|
|
|
|
type Event string
|
|
|
|
const (
|
|
Write Event = "Write"
|
|
Fill Event = "Fill"
|
|
Hash Event = "Hash"
|
|
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 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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// 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
|
|
}
|
|
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
|
|
}
|
|
}
|
|
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
|
|
}
|
|
if e == Rebuilt {
|
|
effect = CursorBack
|
|
}
|
|
}
|
|
return result, effect, nil
|
|
}
|