From fdee737e2fb4f01682cb00701267da089dc0cd14 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 25 Nov 2026 04:00:09 -0800 Subject: [PATCH] wip: fencing round tests (gate, heal, round ask, mint) Co-Authored-By: Claude Opus 5.5 --- .../authority/bind_volume_genesis_test.go | 21 ++ .../core/host/master/fence_gate_test.go | 192 ++++++++++++++++++ 2 files changed, 213 insertions(+) create mode 100644 seaweed-block/core/host/master/fence_gate_test.go diff --git a/seaweed-block/core/authority/bind_volume_genesis_test.go b/seaweed-block/core/authority/bind_volume_genesis_test.go index 22df2459b4..b00da638dd 100644 --- a/seaweed-block/core/authority/bind_volume_genesis_test.go +++ b/seaweed-block/core/authority/bind_volume_genesis_test.go @@ -124,3 +124,24 @@ func TestPublisher_ReassignWithoutAKindIsRefused(t *testing.T) { t.Fatalf("a reassign with a kind: %v", err) } } + +// A decision made on a fencing round at e+1 is minted only while the line +// is still at e: once the line moved, it is refused and nothing changes. +func TestPublisher_AFencedDecisionIsMintedOnlyAtItsFencedEpoch(t *testing.T) { + pub := NewPublisher(NewStaticDirective(nil)) + if err := pub.apply(bindAsk("v1", "r1")); err != nil { + t.Fatal(err) + } + ask := reassignAsk("v1", "r2") + ask.Evidence.FencedEpoch = 3 // fenced for a line at 2; the line is at 1 + if err := pub.apply(ask); !errors.Is(err, ErrFenceSuperseded) { + t.Fatalf("a decision fenced at 3 on a line at 1: %v; want ErrFenceSuperseded", err) + } + if line, _ := pub.VolumeAuthorityLine("v1"); line.ReplicaID != "r1" || line.Epoch != 1 { + t.Fatalf("the line moved: %+v", line) + } + ask.Evidence.FencedEpoch = 2 + if err := pub.apply(ask); err != nil { + t.Fatalf("a decision fenced at the next epoch: %v", err) + } +} diff --git a/seaweed-block/core/host/master/fence_gate_test.go b/seaweed-block/core/host/master/fence_gate_test.go new file mode 100644 index 0000000000..1aaf088658 --- /dev/null +++ b/seaweed-block/core/host/master/fence_gate_test.go @@ -0,0 +1,192 @@ +package master + +import ( + "errors" + "testing" + "time" + + "github.com/seaweedfs/seaweed-block/core/adapter" + "github.com/seaweedfs/seaweed-block/core/authority" + "github.com/seaweedfs/seaweed-block/core/rpc/control" + "github.com/seaweedfs/seaweed-block/core/storage" +) + +// fenceProbe answers the plain round with plain, the fencing round with +// fenced (given the survivors), and a heal round (no candidates) with heal. +type fenceProbe struct { + plain PromotionProbeResult + fenced func(current authority.AuthorityBasis, survivors []PromotionCandidateEvidence) (PromotionProbeResult, error) + heal PromotionProbeResult + survivors []string +} + +func (p *fenceProbe) ProbePromotionCandidates(_ string, _ authority.AuthorityBasis, candidates []authority.ReplicaCandidate) (PromotionProbeResult, error) { + if candidates == nil { + return p.heal, nil + } + return p.plain, nil +} + +func (p *fenceProbe) FencePromotionCandidates(_ string, current authority.AuthorityBasis, survivors []PromotionCandidateEvidence) (PromotionProbeResult, error) { + for _, s := range survivors { + p.survivors = append(p.survivors, s.ReplicaID) + } + return p.fenced(current, survivors) +} + +// fencedAs answers the fencing round with these candidates (their floors as +// given). +func fencedAs(cs ...PromotionCandidateEvidence) func(authority.AuthorityBasis, []PromotionCandidateEvidence) (PromotionProbeResult, error) { + return func(current authority.AuthorityBasis, _ []PromotionCandidateEvidence) (PromotionProbeResult, error) { + return PromotionProbeResult{FencedEpoch: current.Epoch + 1, Candidates: cs}, nil + } +} + +// rf3DeadPrimary is an RF3 snapshot whose primary r1 (line at epoch 1) is +// not acceptable: the gate is to replace it. +func rf3DeadPrimary() authority.ClusterSnapshot { + slot := func(r, s string, up bool) authority.ReplicaCandidate { + return authority.ReplicaCandidate{ReplicaID: r, ServerID: s, DataAddr: "d-" + r, CtrlAddr: "c-" + r, Reachable: up, ReadyForPrimary: up, Eligible: true} + } + return authority.ClusterSnapshot{ + Servers: []authority.ServerObservation{{ServerID: "s1", Reachable: true, Eligible: true}, {ServerID: "s2", Reachable: true, Eligible: true}, {ServerID: "s3", Reachable: true, Eligible: true}}, + Volumes: []authority.VolumeTopologySnapshot{{ + VolumeID: "vol-rf3", + Authority: authority.AuthorityBasis{Assigned: true, ReplicaID: "r1", Epoch: 1, EndpointVersion: 1, DataAddr: "d-r1", CtrlAddr: "c-r1"}, + Slots: []authority.ReplicaCandidate{slot("r1", "s1", false), slot("r2", "s2", true), slot("r3", "s3", true)}, + }}, + } +} + +func allowedSlots(snap authority.ClusterSnapshot) map[string]adapter.PrimaryEvidence { + out := map[string]adapter.PrimaryEvidence{} + for _, s := range snap.Volumes[0].Slots[1:] { + if s.ReadyForPrimary { + out[s.ReplicaID] = s.PromotionEvidence + } + } + return out +} + +// The decision to replace a primary is read from the fencing round: the +// survivors must answer at floor e+1 (N-K+1 of them under sync_quorum RF3), +// the target is the highest FENCED end, and its evidence names the fenced +// epoch. Anything less publishes nothing. +func TestPromotionGate_DecidesOnTheFencingRound(t *testing.T) { + plain := PromotionProbeResult{AckProfile: "sync_quorum", SyncAckKnown: true, Candidates: []PromotionCandidateEvidence{ + {ReplicaID: "r2", Ready: true, DurableLSN: 10}, + {ReplicaID: "r3", Ready: true, DurableLSN: 9}, + }} + for _, c := range []struct { + name string + fenced func(authority.AuthorityBasis, []PromotionCandidateEvidence) (PromotionProbeResult, error) + want map[string]uint64 // allowed replica -> its evidence LSN + }{ + {"both fenced: the highest fenced end, not the plain one", + fencedAs(PromotionCandidateEvidence{ReplicaID: "r2", Ready: true, DurableLSN: 10, Floor: 2}, PromotionCandidateEvidence{ReplicaID: "r3", Ready: true, DurableLSN: 12, Floor: 2}), + map[string]uint64{"r3": 12}}, + {"one fenced, one silent: too few", + fencedAs(PromotionCandidateEvidence{ReplicaID: "r2", Ready: true, DurableLSN: 10, Floor: 2}), + map[string]uint64{}}, + {"one answered at a higher floor (superseded): not counted", + fencedAs(PromotionCandidateEvidence{ReplicaID: "r2", Ready: true, DurableLSN: 10, Floor: 2}, PromotionCandidateEvidence{ReplicaID: "r3", Ready: true, DurableLSN: 12, Floor: 3}), + map[string]uint64{}}, + {"one answered without the fence (another store: its floor did not move)", + fencedAs(PromotionCandidateEvidence{ReplicaID: "r2", Ready: true, DurableLSN: 10, Floor: 2}, PromotionCandidateEvidence{ReplicaID: "r3", Ready: true, DurableLSN: 12, Floor: 1}), + map[string]uint64{}}, + {"the fencing round failed", + func(authority.AuthorityBasis, []PromotionCandidateEvidence) (PromotionProbeResult, error) { + return PromotionProbeResult{}, errors.New("no stream") + }, + map[string]uint64{}}, + } { + t.Run(c.name, func(t *testing.T) { + h := newTestMasterWithControllerConfig(t, t.TempDir(), authority.TopologyControllerConfig{ExpectedSlotsPerVolume: 3}) + defer closeTestMaster(t, h) + p := &fenceProbe{plain: plain, fenced: c.fenced} + h.SetPromotionEvidenceProvider(p) + snap := rf3DeadPrimary() + h.applyPromotionEvidenceGate(&snap) + if len(p.survivors) != 2 { + t.Fatalf("fenced %v; want the plain round's ready survivors r2, r3", p.survivors) + } + got := allowedSlots(snap) + if len(got) != len(c.want) { + t.Fatalf("allowed %v; want %v", got, c.want) + } + for r, lsn := range c.want { + ev, ok := got[r] + if !ok || ev.DurableLSN != lsn || ev.FencedEpoch != 2 || ev.Kind != adapter.PrimaryKindPromotion { + t.Fatalf("%s evidence %+v; want PROMOTION at %d fenced at 2", r, ev, lsn) + } + } + }) + } +} + +// A fencing round that publishes nothing leaves survivors refusing the +// primary at e: the gate continues the line's own primary at e+1 when it +// answers ready (a PROMOTION with its own evidence), else the volume waits +// with a visible reason. +func TestPromotionGate_HealsAFailedFencingRoundOnTheCurrentPrimary(t *testing.T) { + store := storage.StoreID{7} + for _, ready := range []bool{true, false} { + t.Run(map[bool]string{true: "primary answers ready", false: "primary not ready: wait"}[ready], func(t *testing.T) { + h := newTestMasterWithControllerConfig(t, t.TempDir(), authority.TopologyControllerConfig{ExpectedSlotsPerVolume: 3}) + defer closeTestMaster(t, h) + p := &fenceProbe{ + plain: PromotionProbeResult{AckProfile: "sync_quorum", SyncAckKnown: true, CurrentKnown: true, Current: PromotionCandidateEvidence{ReplicaID: "r1"}, + Candidates: []PromotionCandidateEvidence{{ReplicaID: "r2", Ready: true, DurableLSN: 10}, {ReplicaID: "r3", Ready: true, DurableLSN: 10}}}, + fenced: fencedAs(PromotionCandidateEvidence{ReplicaID: "r2", Ready: true, DurableLSN: 10, Floor: 2}), // r3 silent + heal: PromotionProbeResult{CurrentKnown: true, Current: PromotionCandidateEvidence{ReplicaID: "r1", Ready: ready, DurableLSN: 11, + LastDataEpoch: 1, LastDataFirstLSN: 1, StoreID: store}}, + } + h.SetPromotionEvidenceProvider(p) + snap := rf3DeadPrimary() + snap.Volumes[0].Slots[0].Reachable, snap.Volumes[0].Slots[0].ReadyForPrimary = true, true + _, _, asks := h.applyPromotionEvidenceGate(&snap) + if got := allowedSlots(snap); len(got) != 0 { + t.Fatalf("a survivor was allowed after a failed fencing round: %v", got) + } + if !ready { + if len(asks) != 0 { + t.Fatalf("asks %+v; want none (the volume waits)", asks) + } + return + } + if len(asks) != 1 { + t.Fatalf("asks %+v; want the heal", asks) + } + a := asks[0] + if a.ReplicaID != "r1" || a.Intent != authority.IntentReassign || a.Evidence.Kind != adapter.PrimaryKindPromotion || + a.Evidence.DurableLSN != 11 || a.Evidence.StoreID != store || a.Evidence.FencedEpoch != 2 { + t.Fatalf("heal ask %+v; want r1 continued at 2 on its own evidence", a) + } + }) + } +} + +// The round sends a fencing ask with the fenced epoch and the probed store, +// and reads each answer's floor. +func TestProbeRound_FencingAskCarriesTheEpochAndStore(t *testing.T) { + hub := newProbeHub() + p := &roundEvidenceProvider{hub: hub, serverOf: func(_, r string) string { return "s2" }, wait: 300 * time.Millisecond, attested: attestedAs} + var asked *control.ProbeRound + roundNode(t, hub, "s2", func(r *control.ProbeRound) *control.ProbeAnswer { + asked = r + a := readyAnswer("s2")(r) + a.Floor = r.FenceEpoch + return a + }) + res, err := p.FencePromotionCandidates("v1", authority.AuthorityBasis{ReplicaID: "r1", Epoch: 4}, []PromotionCandidateEvidence{{ReplicaID: "r2", StoreID: storeOf("r2")}}) + if err != nil { + t.Fatal(err) + } + want := storeOf("r2") + if asked == nil || asked.FenceEpoch != 5 || string(asked.ExpectedStoreId) != string(want[:]) { + t.Fatalf("fencing ask %+v; want fence epoch 5 on r2's store", asked) + } + if res.FencedEpoch != 5 || len(res.Candidates) != 1 || res.Candidates[0].Floor != 5 || !res.Candidates[0].Ready { + t.Fatalf("fencing result %+v", res) + } +}