From 028351bcdc9538ddb0edcbbdbabecdbd54cc7e65 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 24 Nov 2026 21:29:32 -0800 Subject: [PATCH] volume: the per-peer engine gets this primary's frontier at the add as its start position With the degraded-peer probe loop on, a peer the seed barrier degraded for a divergent tail would otherwise be probed, classified caught up (R >= H) and fenced back into healthy. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/cmd/blockvolume/main.go | 1 + seaweed-block/core/adapter/normalize.go | 2 + .../peer_adapter_divergent_tail_test.go | 40 +++++++++++++++++++ .../core/host/volume/peer_adapter_registry.go | 23 +++++++++++ 4 files changed, 66 insertions(+) create mode 100644 seaweed-block/core/host/volume/peer_adapter_divergent_tail_test.go diff --git a/seaweed-block/cmd/blockvolume/main.go b/seaweed-block/cmd/blockvolume/main.go index ec467d4a86..2e87505e49 100644 --- a/seaweed-block/cmd/blockvolume/main.go +++ b/seaweed-block/cmd/blockvolume/main.go @@ -979,6 +979,7 @@ func runVolume(f flags, exits *processExit) int { // ProductionProbeFn routes per-peer via this registry so // engine.checkReplicaID accepts the events. peerRegistry := volume.NewPeerAdapterRegistry(f.volumeID, f.replicaID) + peerRegistry.SetFrontier(store.Sync) if err := replVolume.ConfigurePeerLifecycleHook(peerRegistry.OnPeerAdded, peerRegistry.OnPeerRemoved); err != nil { fmt.Fprintln(os.Stderr, "blockvolume: peer lifecycle hook:", err) _ = replVolume.Close() diff --git a/seaweed-block/core/adapter/normalize.go b/seaweed-block/core/adapter/normalize.go index 8871e7159c..3247b733c4 100644 --- a/seaweed-block/core/adapter/normalize.go +++ b/seaweed-block/core/adapter/normalize.go @@ -33,6 +33,7 @@ type AssignmentInfo struct { EndpointVersion uint64 DataAddr string CtrlAddr string + PrimaryStartLSN uint64 // see engine.AssignmentObserved.PrimaryStartLSN; 0 = not known } // ProbeResult is the outcome of a transport probe. @@ -110,6 +111,7 @@ func NormalizeAssignment(a AssignmentInfo) engine.Event { EndpointVersion: a.EndpointVersion, DataAddr: a.DataAddr, CtrlAddr: a.CtrlAddr, + PrimaryStartLSN: a.PrimaryStartLSN, } } diff --git a/seaweed-block/core/host/volume/peer_adapter_divergent_tail_test.go b/seaweed-block/core/host/volume/peer_adapter_divergent_tail_test.go new file mode 100644 index 0000000000..4e9da7ac71 --- /dev/null +++ b/seaweed-block/core/host/volume/peer_adapter_divergent_tail_test.go @@ -0,0 +1,40 @@ +package volume + +import ( + "errors" + "strings" + "testing" + + "github.com/seaweedfs/seaweed-block/core/adapter" +) + +// The registry hands each added peer's engine this primary's frontier at the +// add as its start position, so a probe that finds the peer above it pins a +// rebuild instead of fencing it into healthy on length. +func TestPeerAdapterRegistry_DivergentTailAboveTheFrontierAtAddIsRebuilt(t *testing.T) { + for _, c := range []struct { + name string + frontier func() (uint64, error) + want string + }{ + {"frontier known", func() (uint64, error) { return 90, nil }, "StartRebuild"}, + {"frontier unknown", func() (uint64, error) { return 0, errors.New("sync failed") }, "FenceAtEpoch"}, + } { + t.Run(c.name, func(t *testing.T) { + reg := NewPeerAdapterRegistry("vol1", "r1") + reg.SetFrontier(c.frontier) + reg.OnPeerAdded(newPeerForRegistryTest(t, "r2", 2, 1)) + adpt := reg.AdapterFor("r2") + if adpt == nil { + t.Fatal("no adapter for r2") + } + adpt.OnProbeResult(adapter.ProbeResult{ + ReplicaID: "r2", Success: true, EndpointVersion: 1, TransportEpoch: 2, + ReplicaFlushedLSN: 100, PrimaryTailLSN: 10, PrimaryHeadLSN: 95, + }) + if log := strings.Join(adpt.CommandLog(), " "); !strings.Contains(log, c.want) { + t.Fatalf("commands %q; want %s", log, c.want) + } + }) + } +} diff --git a/seaweed-block/core/host/volume/peer_adapter_registry.go b/seaweed-block/core/host/volume/peer_adapter_registry.go index 9d300f4e1e..467b525e17 100644 --- a/seaweed-block/core/host/volume/peer_adapter_registry.go +++ b/seaweed-block/core/host/volume/peer_adapter_registry.go @@ -30,11 +30,25 @@ type PeerAdapterRegistry struct { volumeID string selfReplicaID string // sanity guard: never register the host's own slot + // frontier is this primary's local durable frontier (nil = not known). + // A peer added here has received nothing from this primary at its + // lineage, so the frontier at the add is the start position above + // which anything the peer holds is a divergent old-epoch tail. + frontier func() (uint64, error) + mu sync.Mutex adapters map[string]*adapter.VolumeReplicaAdapter // keyed by ReplicaID closed bool } +// SetFrontier installs the local durable frontier read at each peer add +// (the store's Sync). Call before peers are added. +func (r *PeerAdapterRegistry) SetFrontier(fn func() (uint64, error)) { + if r != nil { + r.frontier = fn + } +} + // NewPeerAdapterRegistry constructs an empty registry for the given // volume. selfReplicaID is the host's own slot — used as a sanity // guard so we never accidentally register the host's own slot @@ -91,6 +105,14 @@ func (r *PeerAdapterRegistry) OnPeerAdded(peer *replication.ReplicaPeer) { // apply.go: "Engine not yet initialized — accept"), but having // AssignmentObserved fire first matches the production master- // driven path semantics: identity comes BEFORE probe. + var start uint64 + if r.frontier != nil { + if lsn, err := r.frontier(); err == nil { + start = lsn + } else { + log.Printf("PeerAdapterRegistry: peer=%s frontier unknown (%v): no divergent-tail check", target.ReplicaID, err) + } + } adpt.OnAssignment(adapter.AssignmentInfo{ VolumeID: r.volumeID, ReplicaID: target.ReplicaID, @@ -98,6 +120,7 @@ func (r *PeerAdapterRegistry) OnPeerAdded(peer *replication.ReplicaPeer) { EndpointVersion: target.EndpointVersion, DataAddr: target.DataAddr, CtrlAddr: target.ControlAddr, + PrimaryStartLSN: start, }) r.adapters[target.ReplicaID] = adpt