Files
seaweedfs/seaweed-block/core/adapter/normalize.go
T

206 lines
6.4 KiB
Go

// Package adapter connects the V3 mini engine to real runtime observations
// and command execution. It is the boundary where runtime facts become
// semantic events and semantic commands become runtime actions.
//
// The adapter has exactly four jobs:
// 1. Gather runtime observations
// 2. Normalize them into engine events
// 3. Execute engine commands
// 4. Feed back session-close truth
//
// The adapter must NOT:
// - Invent identity truth
// - Decide recovery class from convenience state
// - Declare terminal success from progress/ack
// - Use projection as a hidden control channel
package adapter
import (
"github.com/seaweedfs/seaweed-block/core/engine"
)
// --- Runtime observations (raw facts from transport/heartbeat) ---
// AssignmentInfo is a raw assignment from the master heartbeat.
type AssignmentInfo struct {
VolumeID string
ReplicaID string
Epoch uint64
EndpointVersion uint64
DataAddr string
CtrlAddr string
}
// ProbeResult is the outcome of a transport probe.
type ProbeResult struct {
ReplicaID string
Success bool
EndpointVersion uint64
TransportEpoch uint64
FailReason string
// Recovery boundaries from the probe handshake.
ReplicaFlushedLSN uint64 // R
PrimaryTailLSN uint64 // S
PrimaryHeadLSN uint64 // H
}
// ProgressFact converts a successful probe into the canonical engine
// progress fact. Failed probes do not carry trusted R/S/H and therefore
// are represented as missing facts.
func (p ProbeResult) ProgressFact() engine.ReplicaProgressFact {
if !p.Success {
return engine.ReplicaProgressFact{
ReplicaID: p.ReplicaID,
EndpointVersion: p.EndpointVersion,
Source: engine.ProgressFromProbe,
Confidence: engine.ProgressMissing,
}
}
boundsKnown := !(p.ReplicaFlushedLSN == 0 && p.PrimaryTailLSN == 0 && p.PrimaryHeadLSN == 0)
return engine.ReplicaProgressFact{
ReplicaID: p.ReplicaID,
EndpointVersion: p.EndpointVersion,
ReplicaR: p.ReplicaFlushedLSN,
ReplicaRKnown: boundsKnown,
PrimaryS: p.PrimaryTailLSN,
PrimaryH: p.PrimaryHeadLSN,
PrimaryBoundsKnown: boundsKnown,
Source: engine.ProgressFromProbe,
Confidence: engine.ProgressLiveWire,
}
}
// SessionCloseResult is the outcome of a completed/failed recovery session.
//
// Per T4d-1 v0.3: `FailureKind` carries the engine-owned typed
// classification (transport extracts substrate kind via errors.As and
// maps to engine kind). `FailReason` stays as DIAGNOSTIC TEXT ONLY —
// engine MUST NOT parse it. The adapter uses `FailureKind` field
// directly when constructing `engine.SessionClosedFailed`.
type SessionCloseResult struct {
ReplicaID string
SessionID uint64
Success bool
AchievedLSN uint64
FailureKind engine.RecoveryFailureKind // typed branch field; engine-owned enum
FailReason string // DIAGNOSTIC TEXT ONLY — do NOT parse
}
// SessionStartResult reports that a recovery session has actually begun
// executing in the runtime.
type SessionStartResult struct {
ReplicaID string
SessionID uint64
}
// --- Normalization: raw facts → engine events ---
// NormalizeAssignment converts a master assignment into engine events.
// This is the identity truth ingress path.
func NormalizeAssignment(a AssignmentInfo) engine.Event {
return engine.AssignmentObserved{
VolumeID: a.VolumeID,
ReplicaID: a.ReplicaID,
Epoch: a.Epoch,
EndpointVersion: a.EndpointVersion,
DataAddr: a.DataAddr,
CtrlAddr: a.CtrlAddr,
}
}
// NormalizeProbe converts a probe result into engine events.
// A successful probe produces up to 2 events:
// 1. ProbeSucceeded (reachability)
// 2. RecoveryFactsObserved (R/S/H boundaries)
//
// A failed probe produces 1 event:
// 1. ProbeFailed
//
// The adapter NEVER decides recovery class here. It only reports facts.
// The engine's decide() function uses R/S/H to classify.
func NormalizeProbe(p ProbeResult) []engine.Event {
if !p.Success {
return []engine.Event{
engine.ProbeFailed{
ReplicaID: p.ReplicaID,
EndpointVersion: p.EndpointVersion,
TransportEpoch: p.TransportEpoch,
Reason: p.FailReason,
},
}
}
events := []engine.Event{
engine.ProbeSucceeded{
ReplicaID: p.ReplicaID,
EndpointVersion: p.EndpointVersion,
TransportEpoch: p.TransportEpoch,
},
}
events = append(events, engine.RecoveryFactsObserved{
ReplicaID: p.ReplicaID,
EndpointVersion: p.EndpointVersion,
TransportEpoch: p.TransportEpoch,
R: p.ReplicaFlushedLSN,
S: p.PrimaryTailLSN,
H: p.PrimaryHeadLSN,
})
return events
}
// NormalizeSessionClose converts a session close result into an engine event.
// This is one of only two terminal truth paths.
func NormalizeSessionClose(r SessionCloseResult) engine.Event {
if r.Success {
return engine.SessionClosedCompleted{
ReplicaID: r.ReplicaID,
SessionID: r.SessionID,
AchievedLSN: r.AchievedLSN,
}
}
return engine.SessionClosedFailed{
ReplicaID: r.ReplicaID,
SessionID: r.SessionID,
FailureKind: r.FailureKind, // T4d-1: typed branch field
Reason: r.FailReason, // diagnostic text only
}
}
// NormalizeSessionStart converts a runtime start callback into an engine event.
// This marks real execution start, not just command issuance.
func NormalizeSessionStart(r SessionStartResult) engine.Event {
return engine.SessionStarted{
ReplicaID: r.ReplicaID,
SessionID: r.SessionID,
}
}
// NormalizeDurableAck converts mid-session durable progress into an
// engine event. The event records progress only; engine policy decides
// later whether and how to use it for lag.
func NormalizeDurableAck(r DurableAckResult) engine.Event {
return engine.DurableAckObserved{
ReplicaID: r.ReplicaID,
EndpointVersion: r.EndpointVersion,
TransportEpoch: r.TransportEpoch,
DurableLSN: r.DurableLSN,
PrimaryTailLSN: r.PrimaryTailLSN,
PrimaryHeadLSN: r.PrimaryHeadLSN,
}
}
// NormalizeSessionPrepared creates a SessionPrepared event when the
// adapter has set up a recovery session in response to a Start* command.
func NormalizeSessionPrepared(replicaID string, sessionID uint64, kind engine.SessionKind, frontierHint uint64) engine.Event {
return engine.SessionPrepared{
ReplicaID: replicaID,
SessionID: sessionID,
Kind: kind,
FrontierHint: frontierHint,
TargetLSN: frontierHint,
}
}