From 8dcf498997795fbeebf5b32e3924af2bc708657d Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 15 Nov 2026 11:34:43 -0800 Subject: [PATCH] authority, master: boot-grace review controls (RED) Controls for the review of #130, on 6c00734 with declarations only (ReplicaCandidate.ObservedAt copied by the snapshot builder; a PrimaryBootGrace() accessor): - full product tick on the unsupported-volume path: the third server's facts age out (StaleObservation) while the current primary reports a fresh, placeable, restarted, not-ready slot. Today the gate reassigns directly inside the grace; - an ineligible restarted primary still fails over at once (control); - a configured grace above the freshness window is capped, and an unready primary fails over at the capped limit; - an out-of-order snapshot carrying the previous incarnation's older fact, and an equal-timestamp fact, must not change boot history. The controller rig now advances its clock after the steady fact, so every later fact is strictly newer. Refs #128 Co-Authored-By: Claude Opus 5.5 --- .../core/authority/primary_boot_grace.go | 7 + .../core/authority/primary_boot_grace_test.go | 53 ++++- .../core/authority/snapshot_builder.go | 1 + seaweed-block/core/authority/topology.go | 4 + .../master/primary_boot_grace_gate_test.go | 212 ++++++++++++++++-- 5 files changed, 254 insertions(+), 23 deletions(-) diff --git a/seaweed-block/core/authority/primary_boot_grace.go b/seaweed-block/core/authority/primary_boot_grace.go index c8c268df14..e01a81f574 100644 --- a/seaweed-block/core/authority/primary_boot_grace.go +++ b/seaweed-block/core/authority/primary_boot_grace.go @@ -112,3 +112,10 @@ func (c *TopologyController) PrimaryInBootGrace(volumeID string, current Replica } return c.inPrimaryBootGraceLocked(volumeID, current) } + +// PrimaryBootGrace returns the effective boot-grace bound. +func (c *TopologyController) PrimaryBootGrace() time.Duration { + c.mu.Lock() + defer c.mu.Unlock() + return c.config.PrimaryBootGrace +} diff --git a/seaweed-block/core/authority/primary_boot_grace_test.go b/seaweed-block/core/authority/primary_boot_grace_test.go index 86fdd68ce8..bf7e2bfefa 100644 --- a/seaweed-block/core/authority/primary_boot_grace_test.go +++ b/seaweed-block/core/authority/primary_boot_grace_test.go @@ -29,10 +29,14 @@ func newBootGraceRig(t *testing.T) *bootGraceRig { return g } -// r1 is the current primary; r2 and r3 are ready peers. +// r1 is the current primary; r2 and r3 are ready peers. A slot without +// an observation time is stamped with the rig's clock. func (g *bootGraceRig) submit(r1 ReplicaCandidate) { g.t.Helper() g.rev++ + if r1.ObservedAt.IsZero() { + r1.ObservedAt = g.now + } r2 := candidate("r2", "s2", "d2", "c2", 20) r2.Incarnation = "r2-a" r3 := candidate("r3", "s3", "d3", "c3", 10) @@ -65,6 +69,7 @@ func (g *bootGraceRig) steadyPrimary() { g.t.Helper() g.submit(r1Slot("r1-a", "d1-old", "c1-old", true)) expectNoNext(g.t, g.ctrl) + g.now = g.now.Add(time.Second) // later facts are strictly newer } func expectFailoverToR2(t *testing.T, ctrl *TopologyController, why string) { @@ -161,3 +166,49 @@ func TestPrimaryBootGrace_NoIncarnationNoGrace(t *testing.T) { g.submit(r1Slot("", "d1-old", "c1-old", false)) expectFailoverToR2(t, g.ctrl, "no incarnation") } + +// A delayed snapshot carrying the previous incarnation's older fact +// arrives after the restart: it must neither count a process start nor +// reset or end the restarted primary's grace (review of #130). +func TestPrimaryBootGrace_OutOfOrderSnapshotDoesNotTouchHistory(t *testing.T) { + g := newBootGraceRig(t) + t1 := g.now + g.steadyPrimary() // A, observed at t1 + g.now = g.now.Add(time.Second) + g.submit(r1Slot("r1-b", "d1-old", "c1-old", false)) // B restarted, observed at t1+1s + expectNoNext(t, g.ctrl) + + delayedA := r1Slot("r1-a", "d1-old", "c1-old", true) + delayedA.ObservedAt = t1 // older than B's fact + g.now = g.now.Add(time.Second) + g.submit(delayedA) + expectNoNext(t, g.ctrl) // A is ready in this stale view: nothing to do + + for i := 0; i < 2; i++ { + g.now = g.now.Add(time.Second) + g.submit(r1Slot("r1-b", "d1-old", "c1-old", false)) + expectNoNext(t, g.ctrl) // B keeps its grace: no extra starts, not flapping + } + g.now = g.now.Add(testBootGrace) + g.submit(r1Slot("r1-b", "d1-old", "c1-old", false)) + expectFailoverToR2(t, g.ctrl, "B unready past its own grace") +} + +// Two facts with the same observation time: the second never changes +// history (no new start, no ready latch). +func TestPrimaryBootGrace_EqualTimestampFactIgnored(t *testing.T) { + g := newBootGraceRig(t) + g.steadyPrimary() + g.now = g.now.Add(time.Second) + b := r1Slot("r1-b", "d1-old", "c1-old", false) + g.submit(b) + expectNoNext(t, g.ctrl) + sameTimeReady := b + sameTimeReady.ObservedAt = g.now + sameTimeReady.ReadyForPrimary = true + g.submit(sameTimeReady) + expectNoNext(t, g.ctrl) + g.now = g.now.Add(time.Second) + g.submit(r1Slot("r1-b", "d1-old", "c1-old", false)) + expectNoNext(t, g.ctrl) // the equal-time ready fact did not end the grace +} diff --git a/seaweed-block/core/authority/snapshot_builder.go b/seaweed-block/core/authority/snapshot_builder.go index e8f126d68c..f54d6ef540 100644 --- a/seaweed-block/core/authority/snapshot_builder.go +++ b/seaweed-block/core/authority/snapshot_builder.go @@ -448,6 +448,7 @@ func assembleVolumeTopology( Withdrawn: f.slot.Withdrawn, EvidenceScore: f.slot.EvidenceScore, Incarnation: f.slot.Incarnation, + ObservedAt: f.observedAt, }) } diff --git a/seaweed-block/core/authority/topology.go b/seaweed-block/core/authority/topology.go index a8eeee2299..712accf052 100644 --- a/seaweed-block/core/authority/topology.go +++ b/seaweed-block/core/authority/topology.go @@ -32,6 +32,10 @@ type ReplicaCandidate struct { // Incarnation identifies the reporting volume process; it changes // on every process start. Empty from older volume binaries. Incarnation string + // ObservedAt is the observation time of the slot fact this + // candidate was built from (zero when synthesized). It orders boot + // facts: an older fact never changes boot-grace history. + ObservedAt time.Time } // VolumeTopologySnapshot is the complete point-in-time topology and diff --git a/seaweed-block/core/host/master/primary_boot_grace_gate_test.go b/seaweed-block/core/host/master/primary_boot_grace_gate_test.go index b297c1ec10..502ffeb492 100644 --- a/seaweed-block/core/host/master/primary_boot_grace_gate_test.go +++ b/seaweed-block/core/host/master/primary_boot_grace_gate_test.go @@ -7,30 +7,198 @@ import ( "github.com/seaweedfs/seaweed-block/core/authority" ) -// The promotion gate's direct reassign waits only for a restarted primary -// that is placeable by its own report and inside its boot grace (#128). -func TestPrimaryBootGraceHolds_OnlyForPlaceableRestartedPrimary(t *testing.T) { - ctrl := authority.NewTopologyController(authority.TopologyControllerConfig{PrimaryBootGrace: 30 * time.Second}, nil) - restarted := authority.ReplicaCandidate{ - ReplicaID: "r1", ServerID: "s1", Reachable: true, Eligible: true, Incarnation: "r1-b", - } - if !primaryBootGraceHolds(ctrl, "v1", restarted) { - t.Fatal("restarted, placeable, not-ready primary: want the boot grace to hold") - } - for name, mut := range map[string]func(*authority.ReplicaCandidate){ - "ready": func(c *authority.ReplicaCandidate) { c.ReadyForPrimary = true }, - "unreachable": func(c *authority.ReplicaCandidate) { c.Reachable = false }, - "ineligible": func(c *authority.ReplicaCandidate) { c.Eligible = false }, - "withdrawn": func(c *authority.ReplicaCandidate) { c.Withdrawn = true }, - "no incarnation": func(c *authority.ReplicaCandidate) { c.Incarnation = "" }, +// #128 boot grace through the full lifecycle product tick, on the +// unsupported-volume promotion path (review of #130): the third +// server's observation has gone stale, so the volume is unsupported, +// but the current primary's own fresh heartbeat says it is a restarted, +// placeable, not-yet-ready process. + +type rf3Slot struct { + skip bool // the server sends nothing (its last fact ages out) + incarnation string + reachable bool + eligible bool + ready bool +} + +func ingestRF3Incarnations(t *testing.T, h *Host, r1, r2, r3 rf3Slot) { + t.Helper() + now := time.Now().UTC() + for i, s := range []struct { + server, replica string + slot rf3Slot + }{ + {"node-a", "r1", r1}, + {"node-b", "r2", r2}, + {"node-c", "r3", r3}, } { - c := restarted - mut(&c) - if primaryBootGraceHolds(ctrl, "v1", c) { - t.Errorf("%s: the boot grace must not hold", name) + if s.slot.skip { + continue + } + if err := h.ObservationHost().Ingest(authority.Observation{ + ServerID: s.server, + ObservedAt: now, + Server: authority.ServerFact{Reachable: true, Eligible: true}, + Slots: []authority.SlotFact{{ + VolumeID: "vol-rf3", + ReplicaID: s.replica, + DataAddr: "127.0.0.1:19" + string(rune('1'+i)) + "01", + CtrlAddr: "127.0.0.1:19" + string(rune('1'+i)) + "02", + Reachable: s.slot.reachable, + ReadyForPrimary: s.slot.ready, + Eligible: s.slot.eligible, + EvidenceScore: uint64(30 - i), + Incarnation: s.slot.incarnation, + }}, + }); err != nil { + t.Fatalf("ingest %s: %v", s.server, err) } } - if primaryBootGraceHolds(nil, "v1", restarted) { - t.Error("nil controller: must not hold") +} + +func readySlot(inc string) rf3Slot { + return rf3Slot{incarnation: inc, reachable: true, eligible: true, ready: true} +} + +var ( + silent = rf3Slot{skip: true} + restarting = rf3Slot{incarnation: "r1-b", reachable: true, eligible: true} +) + +const testFreshness = time.Second + +// newBootGraceMaster starts a master (freshness window testFreshness +// unless cfg says otherwise, bootstrap pending grace 1 ms) whose +// promotion probe says the current primary r1 is not ready and r2 is a +// ready survivor, and publishes r1 from an all-ready first observation. +// It then lets node-c's facts age out, so the volume is unsupported. +func newBootGraceMaster(t *testing.T, cfg Config) *Host { + t.Helper() + cfg.AuthorityStoreDir = t.TempDir() + cfg.LifecycleStoreDir = t.TempDir() + cfg.Listen = "127.0.0.1:0" + cfg.ControllerConfig.ExpectedSlotsPerVolume = 3 + if cfg.Freshness.FreshnessWindow == 0 { + cfg.Freshness.FreshnessWindow = testFreshness + } + cfg.Freshness.PendingGrace = time.Millisecond + h, err := New(cfg) + if err != nil { + t.Fatalf("master.New: %v", err) + } + h.Start() + h.SetPromotionEvidenceProvider(staticPromotionProbe{result: PromotionProbeResult{ + AckProfile: "sync-quorum", + CurrentKnown: true, + Current: PromotionCandidateEvidence{ReplicaID: "r1", Ready: false}, + Candidates: []PromotionCandidateEvidence{{ReplicaID: "r2", Ready: true, DurableLSN: 70}}, + }}) + seedRF3PlacementForServersWithAddrs(t, h, "vol-rf3", "node-a", "node-b", "node-c") + ingestRF3Incarnations(t, h, readySlot("r1-a"), readySlot("r2-a"), readySlot("r3-a")) + if _, err := h.RunLifecycleProductTick(); err != nil { + t.Fatalf("initial product tick: %v", err) + } + if line := waitAuthorityLine(t, h.Publisher(), "vol-rf3"); line.ReplicaID != "r1" { + t.Fatalf("initial line=%+v want r1", line) + } + time.Sleep(cfg.Freshness.FreshnessWindow + 100*time.Millisecond) // node-c goes stale + return h +} + +// unsupportedTick ingests fresh r1/r2 facts (node-c stays silent) and +// runs one product tick, requiring that the unsupported-volume gate +// probed. +func unsupportedTick(t *testing.T, h *Host, r1 rf3Slot) { + t.Helper() + ingestRF3Incarnations(t, h, r1, readySlot("r2-a"), silent) + topo, ok := acceptedTopologyFromPlacementIntents(h.Lifecycle().Placements.ListPlacements()) + if !ok { + t.Fatal("no accepted topology from placements") + } + build := authority.BuildSnapshot(h.obs.Store().Snapshot(), topo, h.Publisher()) + if _, unsupported := build.Report.Unsupported["vol-rf3"]; !unsupported { + t.Fatalf("vol-rf3 is not unsupported (report %+v); the test needs the unsupported-volume path", build.Report) + } + res, err := h.RunLifecycleProductTick() + if err != nil { + t.Fatalf("product tick: %v", err) + } + if res.PromotionProbes != 1 { + t.Fatalf("product tick %+v: want one promotion probe", res) } } + +func expectLineStaysR1(t *testing.T, h *Host, why string) { + t.Helper() + time.Sleep(150 * time.Millisecond) + line, ok := h.Publisher().VolumeAuthorityLine("vol-rf3") + if !ok || line.ReplicaID != "r1" || line.Epoch != 1 { + t.Fatalf("%s: line=%+v want r1@1 unchanged", why, line) + } +} + +func expectFailoverToR2(t *testing.T, h *Host, why string) { + t.Helper() + if line := waitAuthorityReplica(t, h.Publisher(), "vol-rf3", "r2"); line.Epoch != 2 { + t.Fatalf("%s: line=%+v want r2@2", why, line) + } +} + +// Inside the grace: no direct reassign. Still unready after it: +// failover to the ready survivor. +func TestBootGrace_UnsupportedPathHonorsGraceThenFailsOver(t *testing.T) { + h := newBootGraceMaster(t, Config{ControllerConfig: authority.TopologyControllerConfig{PrimaryBootGrace: 600 * time.Millisecond}}) + defer closeTestMaster(t, h) + + unsupportedTick(t, h, restarting) + expectLineStaysR1(t, h, "restarted primary inside its boot grace, third server stale") + + time.Sleep(600 * time.Millisecond) + unsupportedTick(t, h, restarting) + expectFailoverToR2(t, h, "unready past its boot grace") +} + +// An explicit unhealthy current primary gets no grace on this path. +func TestBootGrace_UnsupportedPathUnhealthyCurrentFailsOverAtOnce(t *testing.T) { + h := newBootGraceMaster(t, Config{}) + defer closeTestMaster(t, h) + + ineligible := restarting + ineligible.eligible = false + unsupportedTick(t, h, ineligible) + expectFailoverToR2(t, h, "ineligible restarted primary") +} + +// A configured grace above the freshness window is capped at it, and a +// genuinely unready primary fails over at that limit. +func TestBootGrace_CappedAtFreshnessWindow(t *testing.T) { + for _, tc := range []struct { + configured, want time.Duration + }{ + {0, 2 * time.Second}, + {5 * time.Minute, 2 * time.Second}, + {time.Second, time.Second}, + } { + h, err := New(Config{ + AuthorityStoreDir: t.TempDir(), + LifecycleStoreDir: t.TempDir(), + Listen: "127.0.0.1:0", + Freshness: authority.FreshnessConfig{FreshnessWindow: 2 * time.Second}, + ControllerConfig: authority.TopologyControllerConfig{PrimaryBootGrace: tc.configured}, + }) + if err != nil { + t.Fatalf("master.New: %v", err) + } + if got := h.Controller().PrimaryBootGrace(); got != tc.want { + t.Errorf("configured %s with a 2s freshness window: grace %s, want %s", tc.configured, got, tc.want) + } + } + + h := newBootGraceMaster(t, Config{ControllerConfig: authority.TopologyControllerConfig{PrimaryBootGrace: 5 * time.Minute}}) + defer closeTestMaster(t, h) + unsupportedTick(t, h, restarting) + expectLineStaysR1(t, h, "inside the capped grace") + time.Sleep(testFreshness) + unsupportedTick(t, h, restarting) + expectFailoverToR2(t, h, "unready past the capped grace (the 1s window, not 5m)") +}