Files
seaweedfs/seaweed-block/core/engine/state.go
T
pingqiuandClaude Opus 4.7 4d73c3978e T4d-4 part C: full L2 matrix + 2 engine bug fixes (HARD GATE #3)
HARD GATE #3 surfaced 2 real bugs in engine-driven recovery flow that
synthetic OnSessionClose injection didn't expose. Both fixed here.

Bug #1 — catch-up exhaustion missed escalation when Decision was
re-classified mid-flight:
  Symptom: stray auto-probe arriving between catch-up retries
  re-classified Recovery.Decision (R<S → Rebuild). Exhaustion-branch
  checked stale Decision and fell through to terminal-degraded
  instead of round-47 escalation.
  Fix: key on st.Session.Kind == SessionCatchUp (truth = what
  session actually failed), not on mutable Recovery.Decision.

Bug #2 — WALRecycled set Decision=Rebuild but never emitted
StartRebuild; subsequent probe could downgrade:
  Symptom: WALRecycled escalation only emitted PublishDegraded; no
  rebuild command. A subsequent probe with R>=S could re-classify
  back to CatchUp before any rebuild fired.
  Fix: new RecoveryTruth.RebuildPinned sticky bool. Set by both
  WALRecycled escalation AND catch-up exhaustion. decide() honors
  it as override — forces Decision=Rebuild + emits StartRebuild
  regardless of R/S/H. Cleared by successful rebuild
  SessionCompleted (or identity reset).
  WALRecycled also now emits StartRebuild immediately (in addition
  to PublishDegraded for diagnostic surface).

Bug #3 (architect-required rule) — already correct:
  Rebuild's MaxRetries=0 routes the first rebuild failure to the
  existing terminal-degraded branch. No code change needed.

New invariants pinned:
  INV-REPL-REBUILD-DECISION-STICKY — once escalated to rebuild
    (WALRecycled or exhaustion), probe re-classification cannot
    downgrade to CatchUp before rebuild completes.
  INV-REPL-REBUILD-EMITTED-ON-WAL-RECYCLED — WALRecycled escalation
    emits StartRebuild immediately, not just PublishDegraded.

Full L2 tests (HARD GATE #3) — all PASS through real
engine→adapter→executor wiring (NOT synthetic injection):
  - TestT4d4_FullL2_CatchupRetryExhausted_EscalatesToRebuild
  - TestT4d4_FullL2_WALRecycled_EscalatesImmediate
  - TestT4d4_FullL2_LastSentMonotonic_AcrossRetries (QA #8 full form)

Files:
  - core/engine/state.go — RebuildPinned field with invariant doc
  - core/engine/apply.go — sticky-rebuild gate in decide();
    WALRecycled emits StartRebuild + pins; exhaustion uses
    Session.Kind + pins; rebuild SessionCompleted clears pin
  - core/engine/recovery_test.go — fixtures set Session.Kind:
    SessionCatchUp (was relying on old Decision-based check)
  - core/replication/component/t4d4_full_l2_matrix_test.go — NEW,
    3 HARD GATE #3 tests via engine-driven flow with substrate
    fault injection wraps

Verified: full V3 suite green; m01 -race ×20 stress on the 3 HARD
GATE tests, no flakes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-25 23:36:47 -07:00

160 lines
5.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package engine implements the V3 mini semantic core.
//
// The engine is a deterministic reducer: Apply(state, event) → (state, commands, projection).
// It reasons about one volume × replica pair. No runtime, no goroutines, no TCP.
//
// Five truth domains:
// - Identity: who this replica is (from master)
// - Reachability: can primary talk to it (from probe/transport)
// - Recovery: what recovery class is needed (from R/S/H boundaries)
// - Session: is there an active recovery contract (from session lifecycle)
// - Publication: what to project outward (derived, never fed back)
package engine
// ReplicaState is the full semantic state for one volume × replica.
type ReplicaState struct {
Identity IdentityTruth
Reachability ReachabilityTruth
Recovery RecoveryTruth
Session SessionTruth
Publication PublicationTruth
}
// IdentityTruth: who this replica is. Authority: master.
type IdentityTruth struct {
VolumeID string
ReplicaID string
Epoch uint64
EndpointVersion uint64
DataAddr string
CtrlAddr string
MemberPresent bool
}
// ProbeStatus represents the current reachability state.
type ProbeStatus string
const (
ProbeUnknown ProbeStatus = "unknown"
ProbeProbing ProbeStatus = "probing"
ProbeReachable ProbeStatus = "reachable"
ProbeUnreachable ProbeStatus = "unreachable"
)
// ContactKind represents the type of last successful contact.
type ContactKind string
const (
ContactNone ContactKind = "none"
ContactShip ContactKind = "ship"
ContactBarrier ContactKind = "barrier"
ContactProbe ContactKind = "probe"
ContactCtrlAck ContactKind = "ctrl_ack"
)
// ReachabilityTruth: can primary talk to the replica. Authority: primary.
type ReachabilityTruth struct {
Status ProbeStatus
LastContactKind ContactKind
ObservedEndpointVersion uint64
TransportEpoch uint64
// FencedEpoch is the highest epoch for which a FenceAtEpoch
// barrier has completed successfully on the replica's lineage
// gate. Bumped by applyFenceCompleted; reset on identity change.
// Used by decide() to gate PublishHealthy on the R>=H branch:
// caught-up handoff is only considered safe once the replica's
// lineage gate has observed the new epoch via mutating traffic
// (the fence barrier). Engine-internal; not exposed in
// ReplicaProjection.
FencedEpoch uint64
}
// RecoveryDecision represents the recovery classification.
type RecoveryDecision string
const (
DecisionUnknown RecoveryDecision = "unknown"
DecisionNone RecoveryDecision = "none"
DecisionCatchUp RecoveryDecision = "catch_up"
DecisionRebuild RecoveryDecision = "rebuild"
)
// RecoveryTruth: what recovery is needed. Authority: primary.
type RecoveryTruth struct {
R uint64 // replica durable/achieved boundary
S uint64 // primary recoverable start boundary (WAL tail)
H uint64 // primary target boundary (head)
Decision RecoveryDecision
DecisionReason string
// Attempts tracks how many StartCatchUp / StartRecovery commands
// the engine has emitted for the current Decision. Engine
// SessionFailed handler increments on close-with-non-recycled-
// error; cleared by SessionCompleted (success), by ErrWALRecycled
// escalation (Decision flips to Rebuild), and by identity changes
// (Recovery is reset wholesale).
//
// Per T4c-2 G-1 §4.1 architect Option B (retry budget lives in
// RuntimePolicy) + T4c-3 retry-loop wiring (round-38, this batch):
// when Attempts exceeds DefaultRuntimePolicyFor(kind).MaxRetries,
// the engine stops emitting fresh StartCatchUp and either escalates
// to Rebuild (if applicable) or falls back to Degraded.
Attempts int
// RebuildPinned makes the rebuild decision sticky against probe
// re-classification. Set by:
// - WALRecycled escalation (catch-up failed because primary
// truncated the requested LSN range — gap is unrecoverable
// via WAL replay)
// - catch-up retry-budget exhaustion (round-47:
// INV-REPL-CATCHUP-EXHAUSTION-ESCALATES-TO-REBUILD)
// Cleared by:
// - successful rebuild SessionCompleted
// - identity change / assignment reset
//
// While set, decide() forces Decision=Rebuild regardless of
// R/S/H — pins INV-REPL-REBUILD-DECISION-STICKY so a stale
// auto-probe arriving mid-flight cannot downgrade back to
// CatchUp before the rebuild lands.
RebuildPinned bool
}
// SessionKind identifies the type of recovery session.
type SessionKind string
const (
SessionNone SessionKind = ""
SessionCatchUp SessionKind = "catch_up"
SessionRebuild SessionKind = "rebuild"
)
// SessionPhase tracks the lifecycle of a recovery session.
type SessionPhase string
const (
PhaseNone SessionPhase = "" // zero value = no session
PhaseStarting SessionPhase = "starting"
PhaseRunning SessionPhase = "running"
PhaseCompleted SessionPhase = "completed"
PhaseFailed SessionPhase = "failed"
)
// SessionTruth: is there an active recovery contract. Authority: session owner.
type SessionTruth struct {
SessionID uint64
Kind SessionKind
TargetLSN uint64
AchievedLSN uint64
Phase SessionPhase
FailureReason string
}
// PublicationTruth: what to project outward. Derived, never fed back.
type PublicationTruth struct {
Publishable bool
Healthy bool
Degraded bool
NeedsAttention bool
}