diff --git a/seaweed-block/core/engine/apply.go b/seaweed-block/core/engine/apply.go index 161ff88729..6d1af8d7ec 100644 --- a/seaweed-block/core/engine/apply.go +++ b/seaweed-block/core/engine/apply.go @@ -298,6 +298,7 @@ func applyRecoveryFacts(st *ReplicaState, e RecoveryFactsObserved, r *ApplyResul // Within one identity, delayed probes cannot undo completed durable work. // An identity-changing assignment resets the recovery facts. st.Recovery.R = max(st.Recovery.R, e.R) + st.Recovery.FactsMissing = !e.ProgressFact().PrimaryBoundsKnown st.Recovery.S = e.S st.Recovery.H = e.H @@ -311,6 +312,10 @@ func applyDurableAck(st *ReplicaState, e DurableAckObserved, r *ApplyResult, tra return } fact := e.ProgressFact() + if st.Recovery.FactsMissing { + st.Recovery.S, st.Recovery.H = fact.PrimaryS, fact.PrimaryH + st.Recovery.FactsMissing = false + } updateDurableAckLagState(st, fact) if fact.ReplicaR > st.Recovery.DurableAckR || !st.Recovery.DurableAckKnown { st.Recovery.DurableAckR = fact.ReplicaR @@ -443,6 +448,12 @@ func decide(st *ReplicaState, r *ApplyResult, trace func(string, string)) { trace("decision", "unknown (not reachable)") return } + if st.Recovery.FactsMissing { + st.Recovery.Decision = DecisionUnknown + st.Recovery.DecisionReason = "no_boundaries" + trace("decision", "unknown (missing probe facts)") + return + } fact := RecoveryFactsObserved{ ReplicaID: st.Identity.ReplicaID, diff --git a/seaweed-block/core/engine/recovery_probe_order_test.go b/seaweed-block/core/engine/recovery_probe_order_test.go index 011329815b..15908d0687 100644 --- a/seaweed-block/core/engine/recovery_probe_order_test.go +++ b/seaweed-block/core/engine/recovery_probe_order_test.go @@ -14,6 +14,21 @@ func TestRecoveryProbeKeepsCompletedRButAllowsNewWorkAndIdentity(t *testing.T) { if st.Recovery.R != 100 { t.Fatalf("old probe lowered completed R: %d", st.Recovery.R) } + // An unknown probe must not turn the retained R into invented primary + // boundaries or publish healthy. A later valid observation recovers it. + r = Apply(st, RecoveryFactsObserved{ReplicaID: "r1", EndpointVersion: 1, TransportEpoch: 1}) + if r.Projection.RecoveryDecision != DecisionUnknown { + t.Fatalf("retained R fabricated known boundaries: %+v", r.Projection) + } + r = Apply(st, ProbeSucceeded{ReplicaID: "r1", EndpointVersion: 1, TransportEpoch: 1}) + if r.Projection.RecoveryDecision != DecisionUnknown { + t.Fatal("reachability fabricated missing recovery facts") + } + assertNoCommand(t, r, "PublishHealthy") + r = Apply(st, DurableAckObserved{ReplicaID: "r1", EndpointVersion: 1, TransportEpoch: 1, DurableLSN: 100, PrimaryTailLSN: 10, PrimaryHeadLSN: 100}) + if r.Projection.RecoveryDecision != DecisionNone || st.Recovery.FactsMissing { + t.Fatalf("valid ack did not restore known facts: %+v", r.Projection) + } // A higher primary head remains real new work, starting after durable R. r = Apply(st, RecoveryFactsObserved{ReplicaID: "r1", EndpointVersion: 1, TransportEpoch: 1, R: 50, S: 10, H: 150}) start := findStartCatchUp(t, r.Commands) diff --git a/seaweed-block/core/engine/state.go b/seaweed-block/core/engine/state.go index bd90e8847a..c92f15734c 100644 --- a/seaweed-block/core/engine/state.go +++ b/seaweed-block/core/engine/state.go @@ -82,6 +82,9 @@ const ( // RecoveryTruth: what recovery is needed. Authority: primary. type RecoveryTruth struct { + // FactsMissing preserves an all-zero probe's unknown confidence even + // while R retains already-confirmed durable progress. + FactsMissing bool R uint64 // replica durable/achieved boundary S uint64 // primary recoverable start boundary (WAL tail) H uint64 // primary target boundary (head) diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 57d5a0fcb7..da54e306fe 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -127,7 +127,7 @@ stateDiagram-v2 | (every inbound frame) | — | a mutating frame is refused on a primary, below the floor, or before a role is known; the rebuild port applies the same rules; a failed apply closes the connection | `core/transport/replica.go` `acceptMutationLineage`, `admitMutation`, `EnterDualLane`, `handleConn` | `INV-FENCE-SELF-ON-PROMOTE-001`, `INV-FENCE-FLOOR-DURABLE-001`, `INV-REPL-REBUILD-PORT-FENCE-001`, `INV-REPL-APPLY-FAIL-001` | | Supporting | promotion candidate | member, reachable, caught up, fenced at the current epoch, post-rebuild durable acknowledgement delivered; for RF=3 also a fresh probe and coverage of the required frontier: known, or proven by at least P-K+1 fresh ready peers when it is unknown | `core/engine/promotion_ready.go` `BuildPromotionReadyFact`; `core/host/master/product_loop.go` `applyPromotionEvidenceGate` | `INV-REPL-PROMOTION-READY-001`, `INV-PROMOTE-FRONTIER-001` | -**Recovery observation and apply ordering.** A probe publishes its fresh boundaries before reachability, and each emitted recovery is prepared under the adapter mutex before the next event. Same-identity delayed probes retain completed durable R. Every lane and session shares one replica-wide per-LBA applied-LSN map under the apply mutex; session close releases coverage only. Recovery skips stale data while advancing its own coverage; stale live data fails without advancing recovery coverage. +**Recovery observation and apply ordering.** A probe publishes its fresh boundaries before reachability, and each emitted recovery is prepared under the adapter mutex before the next event. Same-identity delayed probes retain completed durable R; missing probe boundaries remain unknown until a valid observation arrives. Every lane and session shares one replica-wide per-LBA applied-LSN map under the apply mutex; session close releases coverage only. Recovery skips stale data while advancing its own coverage; stale live data fails without advancing recovery coverage. **Peer state, as the primary sees each replica** (type `ReplicaState` in `core/replication/peer.go`). A new peer starts `Healthy` (`NewReplicaPeer`). diff --git a/seaweed-block/docs/spec/README.md b/seaweed-block/docs/spec/README.md index 5e92eb1847..8190995f3e 100644 --- a/seaweed-block/docs/spec/README.md +++ b/seaweed-block/docs/spec/README.md @@ -319,4 +319,4 @@ end-to-end wall-clock latency guarantee. Keep long model runs off tp04. HostBudget also models guarded configuration initialization: staged bytes are invisible until atomic publication; an initializer crash permits a successor. `HostBudget_partial_config.cfg` must violate NoPartialConfig. The syscall-level crash boundary is covered by TestKilledInitializerDoesNotPublishPartialConfiguration. ## Replica apply ordering -`ReplicaApplyOrder.tla` models two admitted sessions on one LBA with three versions, plus the two events of one probe and a delayed post-completion probe. `R292_safe` checks `NoRegression` and `NoDuplicateRecovery`; the three `R292_red_*` configurations remove one guard each. It abstracts successful physical applies only: it does not establish restart persistence, authority, byte-level framing, or I/O deadlines. +`ReplicaApplyOrder.tla` models two admitted sessions on one LBA with three versions, plus the two events of one probe and a delayed post-completion probe. `R292_safe` checks `NoRegression`, `NoDuplicateRecovery` and `NoFalseKnown`; the four `R292_red_*` configurations remove one guard each. It abstracts successful physical applies only: it does not establish restart persistence, authority, byte-level framing, or I/O deadlines. diff --git a/seaweed-block/docs/spec/ReplicaApplyOrder.tla b/seaweed-block/docs/spec/ReplicaApplyOrder.tla index f277cbd544..3ff2348cff 100644 --- a/seaweed-block/docs/spec/ReplicaApplyOrder.tla +++ b/seaweed-block/docs/spec/ReplicaApplyOrder.tla @@ -4,13 +4,14 @@ EXTENDS Integers, TLC \* Two admitted sessions, one LBA, three versions. Bytes are represented by \* their LSN. Storage faults, restart persistence and authority are external \* obligations; this model checks successful apply ordering and probe dedupe. -CONSTANTS SharedVersions, PrepareEachEvent, MonotoneFacts -VARIABLES applied, local, bytes, seen, stage, active, starts, replicaR -vars == <> +CONSTANTS SharedVersions, PrepareEachEvent, MonotoneFacts, PreserveUnknownConfidence +VARIABLES applied, local, bytes, seen, stage, active, starts, replicaR, factsKnown, knownDecision +vars == <> Max(a,b) == IF a > b THEN a ELSE b Init == /\ applied = 0 /\ local = [s \in 1..2 |-> 0] /\ bytes = 0 /\ seen = 0 /\ stage = 0 /\ active = FALSE /\ starts = 0 /\ replicaR = 0 + /\ factsKnown = TRUE /\ knownDecision = TRUE \* One shared mutex covers stale check, physical apply and version publication. Apply(s,n) == LET floor == IF SharedVersions THEN applied ELSE local[s] @@ -18,27 +19,36 @@ Apply(s,n) == LET floor == IF SharedVersions THEN applied ELSE local[s] /\ applied' = Max(applied,n) /\ local' = [local EXCEPT ![s] = Max(@,n)] /\ seen' = Max(seen,n) - /\ UNCHANGED <> + /\ UNCHANGED <> \* A probe's two semantic events must observe the same reserved session. FirstEvent == /\ stage = 0 /\ stage' = 1 /\ starts' = 1 /\ active' = PrepareEachEvent - /\ UNCHANGED <> + /\ UNCHANGED <> SecondEvent == /\ stage = 1 /\ stage' = 2 /\ starts' = IF active THEN starts ELSE starts + 1 /\ active' = TRUE - /\ UNCHANGED <> + /\ UNCHANGED <> Complete == /\ stage = 2 /\ stage' = 3 /\ replicaR' = 3 /\ active' = FALSE - /\ UNCHANGED <> + /\ UNCHANGED <> DelayedProbe == /\ stage = 3 /\ stage' = 4 /\ replicaR' = IF MonotoneFacts THEN replicaR ELSE 1 /\ starts' = IF replicaR' < 3 THEN starts + 1 ELSE starts /\ active' = (replicaR' < 3) - /\ UNCHANGED <> + /\ UNCHANGED <> +\* Retaining durable R does not give an all-zero probe known confidence. +MissingProbe == /\ stage = 3 /\ stage' = 5 + /\ factsKnown' = FALSE + /\ knownDecision' = ~PreserveUnknownConfidence + /\ UNCHANGED <> +FreshAck == /\ stage = 5 /\ stage' = 3 + /\ factsKnown' = TRUE /\ knownDecision' = TRUE + /\ UNCHANGED <> Next == (\E s \in 1..2, n \in 1..3 : Apply(s,n)) - \/ FirstEvent \/ SecondEvent \/ Complete \/ DelayedProbe + \/ FirstEvent \/ SecondEvent \/ Complete \/ DelayedProbe \/ MissingProbe \/ FreshAck Spec == Init /\ [][Next]_vars NoRegression == bytes = seen NoDuplicateRecovery == starts <= 1 +NoFalseKnown == ~factsKnown => ~knownDecision ============================================================================= diff --git a/seaweed-block/docs/spec/cfg/R292_red_delayed_prepare.cfg b/seaweed-block/docs/spec/cfg/R292_red_delayed_prepare.cfg index a8d99dad2a..c2893f1af0 100644 --- a/seaweed-block/docs/spec/cfg/R292_red_delayed_prepare.cfg +++ b/seaweed-block/docs/spec/cfg/R292_red_delayed_prepare.cfg @@ -2,4 +2,5 @@ SPECIFICATION Spec CONSTANTS SharedVersions = TRUE PrepareEachEvent = FALSE MonotoneFacts = TRUE + PreserveUnknownConfidence = TRUE INVARIANT NoDuplicateRecovery diff --git a/seaweed-block/docs/spec/cfg/R292_red_probe_rollback.cfg b/seaweed-block/docs/spec/cfg/R292_red_probe_rollback.cfg index b01f07b424..662b75c378 100644 --- a/seaweed-block/docs/spec/cfg/R292_red_probe_rollback.cfg +++ b/seaweed-block/docs/spec/cfg/R292_red_probe_rollback.cfg @@ -2,4 +2,5 @@ SPECIFICATION Spec CONSTANTS SharedVersions = TRUE PrepareEachEvent = TRUE MonotoneFacts = FALSE + PreserveUnknownConfidence = TRUE INVARIANT NoDuplicateRecovery diff --git a/seaweed-block/docs/spec/cfg/R292_red_session_versions.cfg b/seaweed-block/docs/spec/cfg/R292_red_session_versions.cfg index efc9f01d4c..e457c7f536 100644 --- a/seaweed-block/docs/spec/cfg/R292_red_session_versions.cfg +++ b/seaweed-block/docs/spec/cfg/R292_red_session_versions.cfg @@ -2,4 +2,5 @@ SPECIFICATION Spec CONSTANTS SharedVersions = FALSE PrepareEachEvent = TRUE MonotoneFacts = TRUE + PreserveUnknownConfidence = TRUE INVARIANT NoRegression diff --git a/seaweed-block/docs/spec/cfg/R292_red_unknown_confidence.cfg b/seaweed-block/docs/spec/cfg/R292_red_unknown_confidence.cfg new file mode 100644 index 0000000000..762579b7e0 --- /dev/null +++ b/seaweed-block/docs/spec/cfg/R292_red_unknown_confidence.cfg @@ -0,0 +1,6 @@ +SPECIFICATION Spec +CONSTANTS SharedVersions = TRUE + PrepareEachEvent = TRUE + MonotoneFacts = TRUE + PreserveUnknownConfidence = FALSE +INVARIANT NoFalseKnown diff --git a/seaweed-block/docs/spec/cfg/R292_safe.cfg b/seaweed-block/docs/spec/cfg/R292_safe.cfg index d631657e24..47c0f9fd2f 100644 --- a/seaweed-block/docs/spec/cfg/R292_safe.cfg +++ b/seaweed-block/docs/spec/cfg/R292_safe.cfg @@ -2,4 +2,5 @@ SPECIFICATION Spec CONSTANTS SharedVersions = TRUE PrepareEachEvent = TRUE MonotoneFacts = TRUE -INVARIANTS NoRegression NoDuplicateRecovery + PreserveUnknownConfidence = TRUE +INVARIANTS NoRegression NoDuplicateRecovery NoFalseKnown