From 6db8fbd8cf2f29307c07f30fcc9129d08c93a78a Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 25 Nov 2026 17:12:07 -0800 Subject: [PATCH] test(master): RF2 manual failover of an unreachable primary decides on the fencing round (red) Co-Authored-By: Claude Opus 5.5 --- .../host/master/manual_failover_fence_test.go | 123 +++++++++++++++++- 1 file changed, 118 insertions(+), 5 deletions(-) diff --git a/seaweed-block/core/host/master/manual_failover_fence_test.go b/seaweed-block/core/host/master/manual_failover_fence_test.go index 4e26bd8129..65f7822222 100644 --- a/seaweed-block/core/host/master/manual_failover_fence_test.go +++ b/seaweed-block/core/host/master/manual_failover_fence_test.go @@ -37,9 +37,13 @@ func rf3LineOnR1(t *testing.T) (*Host, authority.AuthorityBasis) { } func failoverTo(h *Host, current authority.AuthorityBasis, target string) error { + return failoverVolumeTo(h, "vol-rf3", current, target) +} + +func failoverVolumeTo(h *Host, volumeID string, current authority.AuthorityBasis, target string) error { addr := map[string][2]string{"r2": {"127.0.0.1:19201", "127.0.0.1:19202"}, "r3": {"127.0.0.1:19301", "127.0.0.1:19302"}}[target] _, err := h.FailbackAuthorityRuntime().ExecuteFailback(context.Background(), authority.FailbackRuntimeRequest{ - VolumeID: "vol-rf3", ReplicaID: target, TargetDataAddr: addr[0], TargetCtrlAddr: addr[1], + VolumeID: volumeID, ReplicaID: target, TargetDataAddr: addr[0], TargetCtrlAddr: addr[1], ExpectedCurrentReplicaID: current.ReplicaID, ExpectedCurrentEpoch: current.Epoch, AckEligible: true, FrontendFencedBeforeFailback: true, DurableFrontierCovered: true, NoCrossVolumeIdentityChange: true, }) @@ -147,12 +151,121 @@ func TestManualFailoverRF3EvidenceIsTheFencingRounds(t *testing.T) { } } -// An RF2 sync_quorum failover stays a planned move (its target is in every -// acknowledgement set): no fencing round, basis Planned. -func TestManualFailoverRF2SyncQuorumStaysPlanned(t *testing.T) { +// rf2LineOnR1 is a master whose RF2 volume's line is on r1, bound by a +// product tick under the given ack profile. +func rf2LineOnR1(t *testing.T, profile string) (*Host, authority.AuthorityBasis) { + t.Helper() h := newTestMasterWithControllerConfig(t, t.TempDir(), authority.TopologyControllerConfig{ExpectedSlotsPerVolume: 2}) - defer closeTestMaster(t, h) + h.SetPromotionEvidenceProvider(staticPromotionProbe{result: PromotionProbeResult{ + AckProfile: profile, + Candidates: []PromotionCandidateEvidence{{ReplicaID: "r2", Ready: true, DurableLSN: 1}}, + }}) seedRF2Placement(t, h) + ingestRF2Observation(t, h, true, true) + if _, err := h.RunLifecycleProductTick(); err != nil { + t.Fatalf("initial product tick: %v", err) + } + line := waitAuthorityLine(t, h.Publisher(), "vol-rf2") + if line.ReplicaID != "r1" { + t.Fatalf("initial line=%+v want r1", line) + } + return h, line +} + +// The operator's failover of an RF2 volume whose primary is unreachable (its +// host powered off: no fresh heartbeat) is decided on the fencing round too, +// one path with RF3 (Fence model Open/Answer/Decide; the count is a +// parameter: RF2 needs the one survivor's answer). The survivor must answer +// at floor e+1, on a valid table, ready; the line carries basis Fenced at +// e+1. Anything less publishes nothing and leaves the fence pending. +func TestManualFailoverRF2UnreachablePrimaryDecidesOnTheFencingRound(t *testing.T) { + for _, profile := range []string{"sync_quorum", "sync_all"} { + for _, c := range []struct { + name string + survivor func(e uint64) PromotionCandidateEvidence + fail bool + promoted bool + }{ + {"the survivor fenced at e+1, ready", func(e uint64) PromotionCandidateEvidence { + return PromotionCandidateEvidence{ReplicaID: "r2", Ready: true, DurableLSN: 5, Floor: e + 1, TableValid: true} + }, false, true}, + {"the survivor answered at a higher floor (superseded)", func(e uint64) PromotionCandidateEvidence { + return PromotionCandidateEvidence{ReplicaID: "r2", Ready: true, DurableLSN: 5, Floor: e + 2, TableValid: true} + }, false, false}, + {"the survivor is not ready", func(e uint64) PromotionCandidateEvidence { + return PromotionCandidateEvidence{ReplicaID: "r2", DurableLSN: 5, Floor: e + 1, TableValid: true} + }, false, false}, + {"the survivor's table is invalid", func(e uint64) PromotionCandidateEvidence { + return PromotionCandidateEvidence{ReplicaID: "r2", Ready: true, DurableLSN: 5, Floor: e + 1, TableInvalid: true} + }, false, false}, + {"the fencing round failed", nil, true, false}, + } { + t.Run(profile+"/"+c.name, func(t *testing.T) { + h, line := rf2LineOnR1(t, profile) + defer closeTestMaster(t, h) + ingestRF2Observation(t, h, false, true) // r1's host is gone + p := &fenceProbe{plain: PromotionProbeResult{AckProfile: profile, SyncAckKnown: true, Candidates: []PromotionCandidateEvidence{{ReplicaID: "r2", Ready: true, DurableLSN: 5}}}} + if c.fail { + p.fenced = func(authority.AuthorityBasis, []PromotionCandidateEvidence) (PromotionProbeResult, error) { + return PromotionProbeResult{}, errors.New("no stream") + } + } else { + p.fenced = fencedAs(c.survivor(line.Epoch)) + } + h.SetPromotionEvidenceProvider(ackedFenceProbe{p}) + + if c.promoted { + ev, err := h.plannedMoveEvidence("vol-rf2", "r2") + if err != nil || ev.Basis != adapter.BasisFenced || ev.FencedEpoch != line.Epoch+1 || ev.DurableLSN != 5 { + t.Fatalf("evidence %+v err=%v; want basis fenced at %d, end 5", ev, err, line.Epoch+1) + } + } + err := failoverVolumeTo(h, "vol-rf2", line, "r2") + if len(p.survivors) == 0 || p.survivors[len(p.survivors)-1] != "r2" { + t.Fatalf("fenced %v; want the survivor r2 fenced before any decision", p.survivors) + } + after, _ := h.Publisher().VolumeAuthorityLine("vol-rf2") + if !c.promoted { + if err == nil || after != line { + t.Fatalf("err=%v line=%+v; want refused, line %+v unchanged", err, after, line) + } + if !h.pendingFence("vol-rf2", line.Epoch) { + t.Fatal("a refused fenced failover left no pending fence") + } + return + } + if err != nil || after.ReplicaID != "r2" || after.Epoch != line.Epoch+1 { + t.Fatalf("err=%v line=%+v; want r2 at %d", err, after, line.Epoch+1) + } + }) + } + } +} + +// best_effort acknowledges a write without any replica, so no round can +// prove the survivor holds every acknowledged write: the operator's failover +// of an unreachable best_effort primary is refused (never basis Fenced), +// and no fencing round runs. +func TestManualFailoverRF2BestEffortIsNeverFenced(t *testing.T) { + h, line := rf2LineOnR1(t, "best_effort") + defer closeTestMaster(t, h) + ingestRF2Observation(t, h, false, true) + p := &fenceProbe{ + plain: PromotionProbeResult{AckProfile: "best_effort", Candidates: []PromotionCandidateEvidence{{ReplicaID: "r2", Ready: true, DurableLSN: 5}}}, + fenced: fencedAs(PromotionCandidateEvidence{ReplicaID: "r2", Ready: true, DurableLSN: 5, Floor: line.Epoch + 1, TableValid: true}), + } + h.SetPromotionEvidenceProvider(ackedFenceProbe{p}) + if _, err := h.plannedMoveEvidence("vol-rf2", "r2"); !errors.Is(err, ErrPlannedMoveBestEffort) || len(p.survivors) != 0 { + t.Fatalf("err=%v fenced=%v; want ErrPlannedMoveBestEffort and no fencing round", err, p.survivors) + } +} + +// An RF2 sync_quorum failover from a reachable primary stays a planned move +// (its target is in every acknowledgement set): no fencing round, basis +// Planned. +func TestManualFailoverRF2ReachablePrimaryStaysPlanned(t *testing.T) { + h, _ := rf2LineOnR1(t, "sync_quorum") + defer closeTestMaster(t, h) p := &fenceProbe{ plain: PromotionProbeResult{AckProfile: "sync_quorum", SyncAckKnown: true, Candidates: []PromotionCandidateEvidence{{ReplicaID: "r2", Ready: true, DurableLSN: 5}}}, fenced: fencedAs(),