From 13e13ee5f0dde2aa4d698f82762fec79717ecc4a Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 2 May 2026 11:42:24 -0700 Subject: [PATCH] engine: act on durable ack lag policy --- seaweed-block/core/engine/apply.go | 101 ++++++++++++++++- seaweed-block/core/engine/durable_ack_test.go | 107 ++++++++++++++++++ seaweed-block/core/engine/state.go | 11 +- 3 files changed, 216 insertions(+), 3 deletions(-) diff --git a/seaweed-block/core/engine/apply.go b/seaweed-block/core/engine/apply.go index aa9a6f95c5..8e6d8380bd 100644 --- a/seaweed-block/core/engine/apply.go +++ b/seaweed-block/core/engine/apply.go @@ -309,11 +309,110 @@ func applyDurableAck(st *ReplicaState, e DurableAckObserved, r *ApplyResult, tra return } fact := e.ProgressFact() + updateDurableAckLagState(st, fact) if fact.ReplicaR > st.Recovery.DurableAckR || !st.Recovery.DurableAckKnown { st.Recovery.DurableAckR = fact.ReplicaR st.Recovery.DurableAckKnown = true } - trace("durable_ack_observed", fmt.Sprintf("R=%d", fact.ReplicaR)) + lag := durableAckLagDecision(st, fact) + st.Recovery.LagDecision = lag + trace("durable_ack_observed", + fmt.Sprintf("R=%d lag=%s stalled_samples=%d", + st.Recovery.DurableAckR, lag, st.Recovery.DurableAckStalledSample)) + + // A single lagging ack is normal. Only a durable-ack stall observed + // over the policy window becomes a coordinator action. This keeps + // feeder progress as the first answer and uses recovery only when + // progress stops while the primary head keeps moving. + if lag == LagDecisionStartCatchUp && canEmitRecoveryFromDurableAck(st) { + st.Recovery.R = st.Recovery.DurableAckR + st.Recovery.S = fact.PrimaryS + st.Recovery.H = fact.PrimaryH + st.Recovery.Decision = DecisionCatchUp + st.Recovery.DecisionReason = "durable_ack_stalled_within_wal" + r.Commands = append(r.Commands, StartCatchUp{ + ReplicaID: st.Identity.ReplicaID, + Epoch: st.Identity.Epoch, + EndpointVersion: st.Identity.EndpointVersion, + FromLSN: st.Recovery.DurableAckR + 1, + FrontierHint: fact.PrimaryH, + TargetLSN: fact.PrimaryH, + }) + trace("command", "StartCatchUp (durable ack stalled)") + } + if lag == LagDecisionStartRebuild && canEmitRecoveryFromDurableAck(st) { + st.Recovery.R = st.Recovery.DurableAckR + st.Recovery.S = fact.PrimaryS + st.Recovery.H = fact.PrimaryH + st.Recovery.Decision = DecisionRebuild + st.Recovery.DecisionReason = "durable_ack_below_retention" + r.Commands = append(r.Commands, StartRebuild{ + ReplicaID: st.Identity.ReplicaID, + Epoch: st.Identity.Epoch, + EndpointVersion: st.Identity.EndpointVersion, + FrontierHint: fact.PrimaryH, + TargetLSN: fact.PrimaryH, + }) + trace("command", "StartRebuild (durable ack below retention)") + } +} + +func updateDurableAckLagState(st *ReplicaState, fact ReplicaProgressFact) { + previousKnown := st.Recovery.DurableAckKnown + previousR := st.Recovery.DurableAckR + previousH := st.Recovery.DurableAckPrimaryH + + currentR := previousR + if !previousKnown || fact.ReplicaR > currentR { + currentR = fact.ReplicaR + } + st.Recovery.DurableAckPrimaryH = fact.PrimaryH + + switch { + case !previousKnown: + st.Recovery.DurableAckStalledSample = 1 + case currentR > previousR: + st.Recovery.DurableAckStalledSample = 1 + case fact.PrimaryH > previousH: + st.Recovery.DurableAckStalledSample++ + } +} + +func durableAckLagDecision(st *ReplicaState, fact ReplicaProgressFact) LagDecision { + facts := make([]ReplicaProgressFact, 0, 3) + if st.Recovery.DurableAckStalledSample >= 3 { + facts = append(facts, + durableAckPolicyFact(st.Recovery.DurableAckR, fact.PrimaryS, subtractSaturating(fact.PrimaryH, 2)), + durableAckPolicyFact(st.Recovery.DurableAckR, fact.PrimaryS, subtractSaturating(fact.PrimaryH, 1)), + ) + } + facts = append(facts, durableAckPolicyFact(st.Recovery.DurableAckR, fact.PrimaryS, fact.PrimaryH)) + return EvaluateLagPolicy(LagPolicy{StalledSamples: 3}, facts) +} + +func canEmitRecoveryFromDurableAck(st *ReplicaState) bool { + return st.Identity.MemberPresent && + st.Reachability.Status == ProbeReachable && + !hasActiveSession(st) +} + +func subtractSaturating(v, delta uint64) uint64 { + if v < delta { + return 0 + } + return v - delta +} + +func durableAckPolicyFact(replicaR, primaryS, primaryH uint64) ReplicaProgressFact { + return ReplicaProgressFact{ + ReplicaR: replicaR, + ReplicaRKnown: true, + PrimaryS: primaryS, + PrimaryH: primaryH, + PrimaryBoundsKnown: true, + Source: ProgressFromDurableAck, + Confidence: ProgressLiveWire, + } } // decide runs the core bounded decision logic. diff --git a/seaweed-block/core/engine/durable_ack_test.go b/seaweed-block/core/engine/durable_ack_test.go index 17b20a19fa..ce39be7f1c 100644 --- a/seaweed-block/core/engine/durable_ack_test.go +++ b/seaweed-block/core/engine/durable_ack_test.go @@ -88,3 +88,110 @@ func TestDurableAckObserved_IsMonotonic(t *testing.T) { t.Fatalf("DurableAckR regressed to %d want 9", st.Recovery.DurableAckR) } } + +func TestDurableAckObserved_ProgressingLagDoesNotStartRecovery(t *testing.T) { + st := &ReplicaState{} + assignAndProbe(st) + + for _, ack := range []DurableAckObserved{ + durableAckEvent(10, 5, 20), + durableAckEvent(11, 5, 25), + durableAckEvent(12, 5, 30), + } { + r := Apply(st, ack) + assertNoCommand(t, r, "StartCatchUp") + assertNoCommand(t, r, "StartRebuild") + } + + if st.Recovery.LagDecision != LagDecisionKeepFeeding { + t.Fatalf("LagDecision=%s want %s", st.Recovery.LagDecision, LagDecisionKeepFeeding) + } +} + +func TestDurableAckObserved_StalledLagStartsCatchUp(t *testing.T) { + st := &ReplicaState{} + assignAndProbe(st) + + r1 := Apply(st, durableAckEvent(10, 5, 20)) + assertNoCommand(t, r1, "StartCatchUp") + r2 := Apply(st, durableAckEvent(10, 5, 25)) + assertNoCommand(t, r2, "StartCatchUp") + r3 := Apply(st, durableAckEvent(10, 5, 30)) + + cmd := findDurableAckStartCatchUp(t, r3.Commands) + if cmd.FromLSN != 11 { + t.Fatalf("FromLSN=%d want durable ack R+1=11", cmd.FromLSN) + } + if cmd.FrontierHint != 30 || cmd.TargetLSN != 30 { + t.Fatalf("frontier/target=(%d,%d), want (30,30)", + cmd.FrontierHint, cmd.TargetLSN) + } + if st.Recovery.Decision != DecisionCatchUp { + t.Fatalf("Decision=%s want %s", st.Recovery.Decision, DecisionCatchUp) + } + if st.Recovery.DecisionReason != "durable_ack_stalled_within_wal" { + t.Fatalf("DecisionReason=%q", st.Recovery.DecisionReason) + } +} + +func TestDurableAckObserved_BelowRetentionStartsRebuild(t *testing.T) { + st := &ReplicaState{} + assignAndProbe(st) + + r := Apply(st, durableAckEvent(4, 5, 30)) + + assertHasCommand(t, r, "StartRebuild") + if st.Recovery.Decision != DecisionRebuild { + t.Fatalf("Decision=%s want %s", st.Recovery.Decision, DecisionRebuild) + } + if st.Recovery.DecisionReason != "durable_ack_below_retention" { + t.Fatalf("DecisionReason=%q", st.Recovery.DecisionReason) + } +} + +func TestDurableAckObserved_DoesNotStartRecoveryWhileSessionActive(t *testing.T) { + st := &ReplicaState{} + assignAndProbe(st) + Apply(st, SessionPrepared{ + ReplicaID: "r1", + SessionID: 7, + Kind: SessionCatchUp, + FrontierHint: 30, + }) + Apply(st, SessionStarted{ReplicaID: "r1", SessionID: 7}) + + for _, ack := range []DurableAckObserved{ + durableAckEvent(10, 5, 20), + durableAckEvent(10, 5, 25), + durableAckEvent(10, 5, 30), + } { + r := Apply(st, ack) + assertNoCommand(t, r, "StartCatchUp") + assertNoCommand(t, r, "StartRebuild") + } + if st.Recovery.LagDecision != LagDecisionStartCatchUp { + t.Fatalf("LagDecision=%s want %s", st.Recovery.LagDecision, LagDecisionStartCatchUp) + } +} + +func durableAckEvent(durableR, primaryS, primaryH uint64) DurableAckObserved { + return DurableAckObserved{ + ReplicaID: "r1", + EndpointVersion: 1, + TransportEpoch: 1, + DurableLSN: durableR, + PrimaryTailLSN: primaryS, + PrimaryHeadLSN: primaryH, + } +} + +func findDurableAckStartCatchUp(t *testing.T, cmds []Command) StartCatchUp { + t.Helper() + for _, cmd := range cmds { + if c, ok := cmd.(StartCatchUp); ok { + return c + } + } + t.Fatalf("no StartCatchUp command in %v", cmds) + return StartCatchUp{} +} diff --git a/seaweed-block/core/engine/state.go b/seaweed-block/core/engine/state.go index 6ec18a3405..e174fc50d1 100644 --- a/seaweed-block/core/engine/state.go +++ b/seaweed-block/core/engine/state.go @@ -91,8 +91,15 @@ type RecoveryTruth struct { // DurableAckR is the latest replica durable-ack frontier observed // by the engine. It is progress telemetry for lag policy and WAL // release authority, not a recovery-class predicate by itself. - DurableAckR uint64 - DurableAckKnown bool + DurableAckR uint64 + DurableAckKnown bool + DurableAckPrimaryH uint64 + DurableAckStalledSample int + + // LagDecision records the latest coordinator interpretation of durable + // ack progress. This is policy state only; the raw recovery class still + // comes from R/S/H facts and ClassifyProgress. + LagDecision LagDecision // Attempts tracks how many StartCatchUp / StartRecovery commands // the engine has emitted for the current Decision. Engine