Files
seaweedfs/seaweed-block/core/runtime/runner.go
T
Ping Qiu 60103e5f7b Block semantic engine + runnable block sparrow (#1)
* V3 mini engine: Phase 01 + 02 + 03 initial implementation

Phase 01 — Semantic Core (engine/):
- 5 truth domains, 11 events, 6 commands, deterministic Apply()
- Stale rejection, terminal truth, wrong-replica guard
- 17 conformance tests

Phase 02 — Reference Runtime (schema/ + runtime/ + conformance/):
- YAML case loader with strict validation
- Replay runner with expectation checker
- 7 cases, 7 determinism proofs, 5 loader tests

Phase 03 — Adapter (adapter/):
- Single route: observation -> engine -> command -> executor
- CommandExecutor with SetOnSessionClose callback
- Command dispatch outside lock, session ID owned by adapter
- 8 constraint tests (C8/C9/C10/C12 + callback + full route)

44 tests total, all passing.

* Phase 04: runnable block sparrow with real TCP transport

First public runnable slice of seaweed-block. Demonstrates three
recovery paths end-to-end through a deterministic semantic engine:

  1. Healthy: replica caught up (R >= H), no recovery needed
  2. Catch-up: short gap (R >= S, R < H), ship blocks via TCP
  3. Rebuild: long gap (R < S), full base copy via TCP

Package structure (renamed v3mini/ -> core/):
  core/engine     - deterministic Apply(state, event) reducer
  core/adapter    - single-route adapter with async callback wiring
  core/schema     - YAML conformance case loading (strict fields)
  core/runtime    - replay runner for conformance oracle
  core/conformance - 7 YAML cases + determinism + loader tests
  core/storage    - in-memory block store with LSN tracking
  core/transport  - TCP protocol, replica listener, block executor
  cmd/sparrow     - runnable demo binary

51 tests, all passing. Sparrow runs via: go run ./cmd/sparrow

Key design rules enforced:
  - Engine decides, executor executes (C12)
  - Same facts produce same decision (C8)
  - Timer only triggers probe, never recovery (C9)
  - No transport state in engine (C10, recursive reflect guard)
  - Terminal truth only from SessionClosedCompleted
  - Executor honors engine-issued targetLSN exactly
  - Session close callback wired from executor to adapter to engine
  - Command dispatch outside adapter mutex (no deadlock risk)
  - Rebuild uses AdvanceFrontier (no LBA 0 corruption)
  - Catch-up reports barrier-confirmed achievedLSN, not intent

* fix: address CodeRabbit/Gemini review findings

1. Max payload limit in ReadMsg (16MB cap, prevents memory exhaustion)
2. Session ID race fix: capture IDs under lock in buildSessionEvents,
   pass via map to executeCommand — no re-locking needed
3. Barrier response error handling in replica listener
4. Demo 1 uses waitForMode polling instead of fixed sleep
5. ApplyEntry validates data size matches blockSize
6. Rebuild test timing: small delay for replica Sync after MsgRebuildDone
2026-04-13 12:46:57 -07:00

137 lines
3.6 KiB
Go

// Package runtime hosts the V3 mini engine replay runner.
// It is intentionally boring: load case → apply events → collect results.
// No goroutines, no network, no side effects.
package runtime
import (
"fmt"
"github.com/seaweedfs/seaweed-block/core/engine"
"github.com/seaweedfs/seaweed-block/core/schema"
)
// ReplayResult holds the output of one conformance case replay.
type ReplayResult struct {
CaseName string
Passed bool
Failures []string
// Collected outputs from each Apply step.
Steps []StepResult
// Final state after all events.
FinalProjection engine.ReplicaProjection
// All commands emitted across all steps.
AllCommands []string
// All trace entries across all steps.
AllTrace []engine.TraceEntry
}
// StepResult holds the output of one Apply call within a replay.
type StepResult struct {
EventKind string
Commands []string
Projection engine.ReplicaProjection
Trace []engine.TraceEntry
}
// Replay executes one conformance case through the engine.
// Returns a ReplayResult with pass/fail status and collected outputs.
func Replay(c schema.ConformanceCase) ReplayResult {
result := ReplayResult{CaseName: c.Name}
st := &engine.ReplicaState{}
for i, ei := range c.Events {
ev, err := schema.ToEngineEvent(ei)
if err != nil {
result.Failures = append(result.Failures,
fmt.Sprintf("event %d: %v", i, err))
return result
}
ar := engine.Apply(st, ev)
step := StepResult{
EventKind: engine.EventKind(ev),
Projection: ar.Projection,
Trace: ar.Trace,
}
for _, cmd := range ar.Commands {
kind := engine.CommandKind(cmd)
step.Commands = append(step.Commands, kind)
result.AllCommands = append(result.AllCommands, kind)
}
result.AllTrace = append(result.AllTrace, ar.Trace...)
result.Steps = append(result.Steps, step)
}
if len(result.Steps) == 0 {
result.Failures = append(result.Failures, "no events to replay")
return result
}
result.FinalProjection = result.Steps[len(result.Steps)-1].Projection
// Check expectations.
result.Failures = checkExpectations(c.Expect, result)
result.Passed = len(result.Failures) == 0
return result
}
func checkExpectations(exp schema.Expectation, r ReplayResult) []string {
var failures []string
if exp.Mode != "" && string(r.FinalProjection.Mode) != exp.Mode {
failures = append(failures,
fmt.Sprintf("mode: got %s, want %s", r.FinalProjection.Mode, exp.Mode))
}
if exp.RecoveryDecision != "" && string(r.FinalProjection.RecoveryDecision) != exp.RecoveryDecision {
failures = append(failures,
fmt.Sprintf("recovery_decision: got %s, want %s", r.FinalProjection.RecoveryDecision, exp.RecoveryDecision))
}
if exp.SessionPhase != "" && string(r.FinalProjection.SessionPhase) != exp.SessionPhase {
failures = append(failures,
fmt.Sprintf("session_phase: got %s, want %s", r.FinalProjection.SessionPhase, exp.SessionPhase))
}
for _, must := range exp.MustEmitCommands {
if !containsStr(r.AllCommands, must) {
failures = append(failures,
fmt.Sprintf("missing command %s (emitted: %v)", must, r.AllCommands))
}
}
for _, mustNot := range exp.MustNotEmitCommands {
if containsStr(r.AllCommands, mustNot) {
failures = append(failures,
fmt.Sprintf("unexpected command %s", mustNot))
}
}
for _, step := range exp.MustHaveTraceSteps {
found := false
for _, te := range r.AllTrace {
if te.Step == step {
found = true
break
}
}
if !found {
failures = append(failures,
fmt.Sprintf("missing trace step %q", step))
}
}
return failures
}
func containsStr(ss []string, s string) bool {
for _, v := range ss {
if v == s {
return true
}
}
return false
}