Files
seaweedfs/seaweed-block/core/engine/projection.go
T
wzcheng99andClaude Opus 4.7 dec084485b P14 S1: proactive R>=H fence with ack-gated healthy publication
Adds an explicit FenceAtEpoch command + FenceCompleted/FenceFailed
event pair on the caught-up recovery branch. A replica with R >= H
under a newer identity epoch is no longer declared healthy until the
replica's lineage gate has been bumped to match Identity.Epoch via a
FenceCompleted — closing the window where stale old-epoch traffic
could still land on the replica while the projection already reads
healthy.

Contract surface:
  * Caught-up path emits ONLY FenceAtEpoch — no catch-up / no rebuild.
    Fence is a command+event pair, not a session lifecycle.
  * PublishHealthy held until FenceCompleted advances FencedEpoch.
    Publication and DeriveProjection both gate on this so the
    operator-visible mode reflects the ack-gated contract, not
    just the command stream.
  * FenceFailed is fail-closed: FencedEpoch is NOT advanced; retry
    is probe-driven (next successful probe re-runs decide() and
    re-emits FenceAtEpoch).
  * Session completion (catch-up / rebuild) implicitly fences,
    because mutating traffic at the current epoch already bumped
    the replica's gate.
  * Stale FenceCompleted (epoch < Identity.Epoch, or same epoch
    with older endpointVersion) is rejected without mutating state.
  * FencedEpoch is engine-internal; NOT exposed in ReplicaProjection.

Adapter-side fence dispatch:
  * fenceInFlight keyed by fence LINEAGE
    (replicaID, epoch, endpointVersion), NOT by replicaID alone.
    This prevents a newer-epoch fence from being starved by an
    older in-flight fence — both occupy distinct slots, the old
    one is dropped by the engine as stale when its callback lands,
    and the newer one drives publication to healthy without
    waiting for an unrelated later probe.

Proof tests:
  * engine: TestFence_CaughtUpPath_UsesNoDataRecovery
  * engine: TestFence_HealthyOnlyAfterFenceComplete
  * engine: TestFence_FailureLeavesFencedEpochUnchanged
  * engine: TestFence_StaleOldEpochRejectedAfterFenceOnCaughtUp
  * adapter: TestFullRoute_CaughtUp_NotHealthyUntilFenceComplete
  * adapter: TestFence_NewerEpochNotStarvedByOlderInFlightFence

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

64 lines
1.8 KiB
Go

package engine
// Mode is the operator-visible mode derived from truth domains.
type Mode string
const (
ModeHealthy Mode = "healthy"
ModeDegraded Mode = "degraded"
ModeRecovering Mode = "recovering"
ModeIdle Mode = "idle"
)
// ReplicaProjection is the operator-facing view derived from state.
// Intentionally descriptive. Must NEVER be reused as control truth.
type ReplicaProjection struct {
Mode Mode
RecoveryDecision RecoveryDecision
SessionKind SessionKind
SessionPhase SessionPhase
Epoch uint64
EndpointVersion uint64
R uint64
S uint64
H uint64
Reason string
}
// DeriveProjection computes the projection from current state.
// Pure function — no side effects, no mutation.
func DeriveProjection(st *ReplicaState) ReplicaProjection {
p := ReplicaProjection{
Epoch: st.Identity.Epoch,
EndpointVersion: st.Identity.EndpointVersion,
RecoveryDecision: st.Recovery.Decision,
SessionKind: st.Session.Kind,
SessionPhase: st.Session.Phase,
R: st.Recovery.R,
S: st.Recovery.S,
H: st.Recovery.H,
Reason: st.Recovery.DecisionReason,
}
switch {
case !st.Identity.MemberPresent:
p.Mode = ModeIdle
case st.Session.Phase == PhaseRunning || st.Session.Phase == PhaseStarting:
p.Mode = ModeRecovering
case st.Publication.Healthy:
p.Mode = ModeHealthy
case st.Recovery.Decision == DecisionNone &&
st.Reachability.Status == ProbeReachable &&
st.Identity.Epoch > st.Reachability.FencedEpoch:
// P14 S1: caught-up but fence not yet acked. Transitional
// — not healthy yet, but nothing is wrong. Show as
// recovering rather than degraded so operators read the
// transition correctly.
p.Mode = ModeRecovering
default:
p.Mode = ModeDegraded
}
return p
}