diff --git a/seaweed-block/core/adapter/normalize.go b/seaweed-block/core/adapter/normalize.go index ceb8a5e5fe..0199bd0147 100644 --- a/seaweed-block/core/adapter/normalize.go +++ b/seaweed-block/core/adapter/normalize.go @@ -45,6 +45,32 @@ type ProbeResult struct { 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 diff --git a/seaweed-block/core/adapter/progress_fact_test.go b/seaweed-block/core/adapter/progress_fact_test.go new file mode 100644 index 0000000000..5d079bf4b4 --- /dev/null +++ b/seaweed-block/core/adapter/progress_fact_test.go @@ -0,0 +1,79 @@ +package adapter + +import ( + "testing" + + "github.com/seaweedfs/seaweed-block/core/engine" +) + +func TestProbeResult_ProgressFact_SuccessCarriesRSH(t *testing.T) { + probe := ProbeResult{ + ReplicaID: "r1", + Success: true, + EndpointVersion: 7, + TransportEpoch: 9, + ReplicaFlushedLSN: 11, + PrimaryTailLSN: 5, + PrimaryHeadLSN: 20, + } + + fact := probe.ProgressFact() + if fact.ReplicaID != probe.ReplicaID { + t.Fatalf("ReplicaID=%q want %q", fact.ReplicaID, probe.ReplicaID) + } + if fact.EndpointVersion != probe.EndpointVersion { + t.Fatalf("EndpointVersion=%d want %d", fact.EndpointVersion, probe.EndpointVersion) + } + if fact.Source != engine.ProgressFromProbe { + t.Fatalf("Source=%s want %s", fact.Source, engine.ProgressFromProbe) + } + if fact.Confidence != engine.ProgressLiveWire { + t.Fatalf("Confidence=%s want %s", fact.Confidence, engine.ProgressLiveWire) + } + if !fact.ReplicaRKnown || !fact.PrimaryBoundsKnown { + t.Fatalf("fact should carry known R/S/H: %+v", fact) + } + if fact.ReplicaR != probe.ReplicaFlushedLSN || + fact.PrimaryS != probe.PrimaryTailLSN || + fact.PrimaryH != probe.PrimaryHeadLSN { + t.Fatalf("fact R/S/H=(%d,%d,%d), want (%d,%d,%d)", + fact.ReplicaR, fact.PrimaryS, fact.PrimaryH, + probe.ReplicaFlushedLSN, probe.PrimaryTailLSN, probe.PrimaryHeadLSN) + } +} + +func TestProbeResult_ProgressFact_FailureCarriesMissingFact(t *testing.T) { + probe := ProbeResult{ + ReplicaID: "r1", + Success: false, + EndpointVersion: 7, + FailReason: "dial refused", + } + + fact := probe.ProgressFact() + if fact.Source != engine.ProgressFromProbe { + t.Fatalf("Source=%s want %s", fact.Source, engine.ProgressFromProbe) + } + if fact.Confidence != engine.ProgressMissing { + t.Fatalf("Confidence=%s want %s", fact.Confidence, engine.ProgressMissing) + } + if fact.ReplicaRKnown || fact.PrimaryBoundsKnown { + t.Fatalf("failed probe must not carry trusted R/S/H: %+v", fact) + } +} + +func TestProbeResult_ProgressFact_AllZeroRemainsNoBoundaries(t *testing.T) { + probe := ProbeResult{ + ReplicaID: "r1", + Success: true, + EndpointVersion: 1, + } + + fact := probe.ProgressFact() + if fact.ReplicaRKnown || fact.PrimaryBoundsKnown { + t.Fatalf("all-zero probe remains no-boundaries for engine compatibility: %+v", fact) + } + if got := engine.ClassifyProgress(fact); got != engine.DecisionUnknown { + t.Fatalf("ClassifyProgress(all-zero probe)=%s want %s", got, engine.DecisionUnknown) + } +} diff --git a/seaweed-block/core/engine/apply.go b/seaweed-block/core/engine/apply.go index fedce9b7fe..69f721f0b5 100644 --- a/seaweed-block/core/engine/apply.go +++ b/seaweed-block/core/engine/apply.go @@ -314,7 +314,14 @@ func decide(st *ReplicaState, r *ApplyResult, trace func(string, string)) { return } - R, S, H := st.Recovery.R, st.Recovery.S, st.Recovery.H + fact := RecoveryFactsObserved{ + ReplicaID: st.Identity.ReplicaID, + EndpointVersion: st.Identity.EndpointVersion, + R: st.Recovery.R, + S: st.Recovery.S, + H: st.Recovery.H, + }.ProgressFact() + H := fact.PrimaryH // Sticky rebuild (INV-REPL-REBUILD-DECISION-STICKY): once a // catch-up failure has escalated to rebuild (WALRecycled or @@ -340,15 +347,16 @@ func decide(st *ReplicaState, r *ApplyResult, trace func(string, string)) { return } - if R == 0 && S == 0 && H == 0 { + class := ClassifyProgress(fact) + if class == DecisionUnknown && !fact.PrimaryBoundsKnown { st.Recovery.Decision = DecisionUnknown st.Recovery.DecisionReason = "no_boundaries" trace("decision", "unknown (no R/S/H)") return } - switch { - case R >= H: + switch class { + case DecisionNone: st.Recovery.Decision = DecisionNone st.Recovery.DecisionReason = "caught_up" trace("decision", "none (R >= H)") @@ -375,7 +383,7 @@ func decide(st *ReplicaState, r *ApplyResult, trace func(string, string)) { } } - case R >= S && R < H: + case DecisionCatchUp: st.Recovery.Decision = DecisionCatchUp st.Recovery.DecisionReason = "gap_within_wal" trace("decision", "catch_up (R >= S, R < H)") @@ -391,14 +399,14 @@ func decide(st *ReplicaState, r *ApplyResult, trace func(string, string)) { ReplicaID: st.Identity.ReplicaID, Epoch: st.Identity.Epoch, EndpointVersion: st.Identity.EndpointVersion, - FromLSN: R + 1, + FromLSN: fact.ReplicaR + 1, FrontierHint: H, TargetLSN: H, }) trace("command", "StartCatchUp") } - case R < S: + case DecisionRebuild: st.Recovery.Decision = DecisionRebuild st.Recovery.DecisionReason = "gap_beyond_wal" trace("decision", "rebuild (R < S)") diff --git a/seaweed-block/core/engine/progress_fact.go b/seaweed-block/core/engine/progress_fact.go index 3c6c0c8a42..b3fd553832 100644 --- a/seaweed-block/core/engine/progress_fact.go +++ b/seaweed-block/core/engine/progress_fact.go @@ -48,6 +48,25 @@ type ReplicaProgressFact struct { Confidence ProgressConfidence } +// ProgressFact converts the legacy engine recovery-facts event into the +// canonical fact seam. The historical all-zero event means "no boundaries +// observed" in engine tests, so it remains unknown here; a future explicit +// empty-volume fact should set PrimaryBoundsKnown through a different path. +func (e RecoveryFactsObserved) ProgressFact() ReplicaProgressFact { + boundsKnown := !(e.R == 0 && e.S == 0 && e.H == 0) + return ReplicaProgressFact{ + ReplicaID: e.ReplicaID, + EndpointVersion: e.EndpointVersion, + ReplicaR: e.R, + ReplicaRKnown: boundsKnown, + PrimaryS: e.S, + PrimaryH: e.H, + PrimaryBoundsKnown: boundsKnown, + Source: ProgressFromProbe, + Confidence: ProgressLiveWire, + } +} + // ClassifyProgress maps a unified progress fact to the engine's recovery // class. Numeric R/S/H semantics are source-independent; source authority // is handled by helpers such as CanAdvanceRecyclePin. diff --git a/seaweed-block/core/engine/progress_fact_test.go b/seaweed-block/core/engine/progress_fact_test.go index 3e1b923efa..6d58612df7 100644 --- a/seaweed-block/core/engine/progress_fact_test.go +++ b/seaweed-block/core/engine/progress_fact_test.go @@ -138,3 +138,32 @@ func TestReplicaProgressFact_HasNoTargetLSNField(t *testing.T) { t.Fatal("ReplicaProgressFact must not carry TargetLSN; use PrimaryH/frontier facts instead") } } + +func TestRecoveryFactsObserved_ProgressFact_PreservesEngineNoBoundariesCompatibility(t *testing.T) { + event := RecoveryFactsObserved{ReplicaID: "r1"} + fact := event.ProgressFact() + if fact.ReplicaRKnown || fact.PrimaryBoundsKnown { + t.Fatalf("all-zero RecoveryFactsObserved must remain no-boundaries for engine compatibility: %+v", fact) + } + if got := ClassifyProgress(fact); got != DecisionUnknown { + t.Fatalf("ClassifyProgress(all-zero event)=%s want %s", got, DecisionUnknown) + } +} + +func TestRecoveryFactsObserved_ProgressFact_ClassifiesWithCanonicalFact(t *testing.T) { + event := RecoveryFactsObserved{ + ReplicaID: "r1", + EndpointVersion: 2, + R: 5, + S: 5, + H: 9, + } + fact := event.ProgressFact() + if fact.Source != ProgressFromProbe || fact.Confidence != ProgressLiveWire { + t.Fatalf("source/confidence=(%s,%s), want (%s,%s)", + fact.Source, fact.Confidence, ProgressFromProbe, ProgressLiveWire) + } + if got := ClassifyProgress(fact); got != DecisionCatchUp { + t.Fatalf("ClassifyProgress()=%s want %s", got, DecisionCatchUp) + } +}