// 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 { // ServerID is immutable for a moved authority line; an unknown moved // owner remains empty instead of falling back to placement. ServerID string OwnerFromMove bool VolumeID string ReplicaID string Epoch uint64 EndpointVersion uint64 DataAddr string CtrlAddr string PrimaryStartLSN uint64 // see engine.AssignmentObserved.PrimaryStartLSN; 0 = not known // Primary is how this line made its primary the primary, with the // evidence the master probed on the target; the target checks it before // adopting the role. The zero value is a line without a kind. Primary PrimaryEvidence // Incarnation is the primary process this line was issued for (its // reported incarnation when the line was minted; "" = not known). It is // compared only for equality: a primary that finds another one on its // line is a restarted process the master has not re-issued yet. Incarnation string } // PrimaryKind is how a line made its primary the primary. type PrimaryKind uint8 const ( PrimaryKindUnspecified PrimaryKind = iota // a line without a kind (a pre-release master) PrimaryKindBootstrap // the volume's first primary: the target holds no data PrimaryKindPromotion // promoted on the master's probed evidence PrimaryKindForced // the operator's forced promotion: no evidence ) // PrimaryEvidence is a line's kind and, for a promotion, what the master // probed on the target: its durable LSN and its epoch-table row at that LSN. type PrimaryEvidence struct { Kind PrimaryKind DurableLSN uint64 DataEpoch uint64 // the target's epoch-table row at DurableLSN DataFirstLSN uint64 StoreID [16]byte // the probed store's id (its superblock UUID) // FencedEpoch: the survivors made this epoch their durable floor before // answering (a fencing round); the line is minted only at this epoch. // 0: no fencing round (a planned move). FencedEpoch uint64 // Basis is why a promotion may be minted; a promotion without one is // refused. Basis PromotionBasis } // PromotionBasis is the one reason a promotion is minted. type PromotionBasis uint8 const ( BasisNone PromotionBasis = iota // BasisFenced: decided on a fencing round's answers; FencedEpoch is the // epoch minted. BasisFenced // BasisPlanned: a planned move after its guard (the target in every // acknowledgement set while the current primary is alive); no fencing // round, FencedEpoch 0. BasisPlanned // BasisHeal: the line's own primary continued at FencedEpoch after a // fencing round that published nothing. BasisHeal ) // 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, PrimaryStartLSN: a.PrimaryStartLSN, } } // NormalizeProbe converts a probe result into engine events. // A successful probe produces up to 2 events: // 1. RecoveryFactsObserved (R/S/H boundaries) // 2. ProbeSucceeded (reachability) // // Fresh boundaries precede reachability so its decision cannot reserve a // recovery using the previous probe's retention window. // // 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.RecoveryFactsObserved{ ReplicaID: p.ReplicaID, EndpointVersion: p.EndpointVersion, TransportEpoch: p.TransportEpoch, R: p.ReplicaFlushedLSN, S: p.PrimaryTailLSN, H: p.PrimaryHeadLSN, }, engine.ProbeSucceeded{ ReplicaID: p.ReplicaID, EndpointVersion: p.EndpointVersion, TransportEpoch: p.TransportEpoch, }, } 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, } } // FlowControlObservationFromDurableAck builds the write-pressure observation // shape from primary storage boundaries plus the latest durable ack. It does // not normalize to an engine.Event because flow control is not recovery truth. func FlowControlObservationFromDurableAck(primaryR, primaryS, primaryH uint64, ack DurableAckResult) engine.FlowControlObservation { return engine.FlowControlObservation{ PrimaryDurableLSN: primaryR, PrimaryTailLSN: primaryS, PrimaryHeadLSN: primaryH, PrimaryBoundsKnown: true, SlowestReplicaDurableLSN: ack.DurableLSN, SlowestReplicaDurableKnown: true, SessionDurableLSN: ack.DurableLSN, SessionDurableKnown: true, } } // 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, } }