diff --git a/seaweed-block/core/engine/apply.go b/seaweed-block/core/engine/apply.go index f0ef93008f..6177ece1ec 100644 --- a/seaweed-block/core/engine/apply.go +++ b/seaweed-block/core/engine/apply.go @@ -177,6 +177,7 @@ func applyAssignment(st *ReplicaState, e AssignmentObserved, r *ApplyResult, tra st.Identity.EndpointVersion = e.EndpointVersion st.Identity.DataAddr = e.DataAddr st.Identity.CtrlAddr = e.CtrlAddr + st.Identity.PrimaryStartLSN = e.PrimaryStartLSN st.Identity.MemberPresent = true trace("identity_updated", "member_present=true") @@ -491,6 +492,29 @@ func decide(st *ReplicaState, r *ApplyResult, trace func(string, string)) { return } + // Divergent tail (OSS-SPEC section 8 rule 7): until the replica has + // accepted this epoch (a fence or a completed session raises + // FencedEpoch), everything it holds was written at an older epoch, and + // whatever lies above this primary's start position was never + // acknowledged. R above the start is such a tail: rebuild, pinned. It is + // never fenced into healthy because R >= H, and its R is not coverage. + if st.Identity.PrimaryStartLSN > 0 && fact.ReplicaRKnown && fact.ReplicaR > st.Identity.PrimaryStartLSN && + st.Reachability.FencedEpoch < st.Identity.Epoch && !hasActiveSession(st) { + st.Recovery.Decision = DecisionRebuild + st.Recovery.DecisionReason = "divergent_tail" + st.Recovery.Attempts = 0 + st.Recovery.RebuildPinned = true + trace("decision", "rebuild (divergent tail above the primary's start)") + r.Commands = append(r.Commands, StartRebuild{ + ReplicaID: st.Identity.ReplicaID, + Epoch: st.Identity.Epoch, + EndpointVersion: st.Identity.EndpointVersion, + FrontierHint: H, + TargetLSN: H, + }) + return + } + class := ClassifyProgress(fact) if class == DecisionUnknown && !fact.PrimaryBoundsKnown { st.Recovery.Decision = DecisionUnknown diff --git a/seaweed-block/core/engine/divergent_tail_test.go b/seaweed-block/core/engine/divergent_tail_test.go new file mode 100644 index 0000000000..03cf025c3a --- /dev/null +++ b/seaweed-block/core/engine/divergent_tail_test.go @@ -0,0 +1,71 @@ +package engine + +import "testing" + +// A replica that has not yet accepted the current epoch holds only entries +// written at older epochs. Above the new primary's start position those were +// never acknowledged (an acknowledged write is on the promotion target), so a +// replica reporting R above that start holds a divergent tail: it is rebuilt, +// never fenced into healthy because R >= H, and never counted as caught up. +func TestDivergentTailBeforeTheEpochIsAcceptedIsRebuilt(t *testing.T) { + st := &ReplicaState{} + Apply(st, AssignmentObserved{ + VolumeID: "vol1", ReplicaID: "r1", + Epoch: 2, EndpointVersion: 1, + DataAddr: "127.0.0.1:9333", CtrlAddr: "127.0.0.1:9334", + PrimaryStartLSN: 90, // the new primary's durable LSN when it took the role at epoch 2 + }) + Apply(st, ProbeSucceeded{ReplicaID: "r1", EndpointVersion: 1, TransportEpoch: 2}) + r := Apply(st, RecoveryFactsObserved{ReplicaID: "r1", R: 100, S: 10, H: 95}) + + assertHasCommand(t, r, "StartRebuild") + assertNoCommand(t, r, "FenceAtEpoch") + assertNoCommand(t, r, "PublishHealthy") + if st.Recovery.Decision != DecisionRebuild || st.Recovery.DecisionReason != "divergent_tail" || !st.Recovery.RebuildPinned { + t.Fatalf("decision %s (%s), pinned %t; want rebuild (divergent_tail), pinned", st.Recovery.Decision, st.Recovery.DecisionReason, st.Recovery.RebuildPinned) + } + // A later probe with R >= H does not downgrade it. + r = Apply(st, RecoveryFactsObserved{ReplicaID: "r1", R: 100, S: 10, H: 100}) + assertNoCommand(t, r, "FenceAtEpoch") + assertNoCommand(t, r, "PublishHealthy") +} + +// The controls: at or below the start position the classification is the +// ordinary one, and once the replica has accepted the epoch its R above the +// start came from this primary. +func TestDivergentTailControls(t *testing.T) { + for _, c := range []struct { + name string + r, h uint64 + fenced bool + want string // a command the decision must issue + without string // a command it must not + }{ + {"R equal to the start, caught up", 90, 90, false, "FenceAtEpoch", "StartRebuild"}, + {"R below the start, catch-up", 80, 95, false, "StartCatchUp", "StartRebuild"}, + {"epoch accepted, R above the start from this primary", 100, 100, true, "PublishHealthy", "StartRebuild"}, + } { + t.Run(c.name, func(t *testing.T) { + st := &ReplicaState{} + Apply(st, AssignmentObserved{VolumeID: "vol1", ReplicaID: "r1", Epoch: 2, EndpointVersion: 1, DataAddr: "a", CtrlAddr: "b", PrimaryStartLSN: 90}) + Apply(st, ProbeSucceeded{ReplicaID: "r1", EndpointVersion: 1, TransportEpoch: 2}) + if c.fenced { + Apply(st, RecoveryFactsObserved{ReplicaID: "r1", R: 90, S: 10, H: 90}) + Apply(st, FenceCompleted{ReplicaID: "r1", Epoch: 2, EndpointVersion: 1}) + } + r := Apply(st, RecoveryFactsObserved{ReplicaID: "r1", R: c.r, S: 10, H: c.h}) + assertHasCommand(t, r, c.want) + assertNoCommand(t, r, c.without) + }) + } +} + +// Without a start position (a primary that was never promoted, or a host that +// does not supply one) the classification is unchanged. +func TestDivergentTailNeedsAStartPosition(t *testing.T) { + st := &ReplicaState{} + assignAndProbe(st) + r := Apply(st, RecoveryFactsObserved{ReplicaID: "r1", R: 100, S: 10, H: 95}) + assertHasCommand(t, r, "FenceAtEpoch") + assertNoCommand(t, r, "StartRebuild") +} diff --git a/seaweed-block/core/engine/events.go b/seaweed-block/core/engine/events.go index f711ff389f..058654bce5 100644 --- a/seaweed-block/core/engine/events.go +++ b/seaweed-block/core/engine/events.go @@ -16,6 +16,10 @@ type AssignmentObserved struct { EndpointVersion uint64 DataAddr string CtrlAddr string + // PrimaryStartLSN is this primary's durable LSN when it took the primary + // role at Epoch; 0 = not known. Entries a replica holds above it from an + // older epoch were never acknowledged (a divergent tail). + PrimaryStartLSN uint64 } func (AssignmentObserved) eventKind() string { return "AssignmentObserved" } diff --git a/seaweed-block/core/engine/state.go b/seaweed-block/core/engine/state.go index c92f15734c..64c2c58478 100644 --- a/seaweed-block/core/engine/state.go +++ b/seaweed-block/core/engine/state.go @@ -29,6 +29,7 @@ type IdentityTruth struct { DataAddr string CtrlAddr string MemberPresent bool + PrimaryStartLSN uint64 // see AssignmentObserved.PrimaryStartLSN } // ProbeStatus represents the current reachability state.