From a0ad707ecb25caaef6957a4b3bfd5a47a03b2d74 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 25 Nov 2026 18:21:00 -0800 Subject: [PATCH] fix(authority): the operator's expected line binds the failover through publication The runtime checked --expect-current once, then ran the gate and published without it: a move landing in between let the stale request move a line the operator never saw. The reassign now carries the expected line and the publisher mints only while the volume's line is exactly it (under its lock); the gate decides on that same line (reachability and the fencing round are the expected line's, never one read later). Co-Authored-By: Claude Opus 5.5 --- seaweed-block/core/authority/authority.go | 6 +++ seaweed-block/core/authority/directive.go | 15 +++++++ .../core/authority/failback_runtime.go | 9 ++++- .../core/authority/failback_runtime_test.go | 39 +++++++++++++++++-- seaweed-block/core/authority/policy.go | 5 +++ .../authority/promotion_basis_census_test.go | 2 +- seaweed-block/core/host/master/host.go | 2 +- .../core/host/master/planned_move.go | 26 +++++++------ .../host/master/planned_move_paths_test.go | 9 +++++ .../ops/failback_authority_runtime_adapter.go | 2 +- ...failback_authority_runtime_adapter_test.go | 2 +- 11 files changed, 95 insertions(+), 22 deletions(-) diff --git a/seaweed-block/core/authority/authority.go b/seaweed-block/core/authority/authority.go index 6b56109a4e..d0104a8c49 100644 --- a/seaweed-block/core/authority/authority.go +++ b/seaweed-block/core/authority/authority.go @@ -586,6 +586,12 @@ func (p *Publisher) applyWithWorkspaceMove(ask AssignmentAsk, grant *workspaceGr p.mu.Unlock() return ErrReassignNotBound } + if exp := ask.ExpectCurrent; exp != nil { + if cur, ok := p.volumeLineLocked(ask.VolumeID); !ok || cur.ReplicaID != exp.ReplicaID || cur.Epoch != exp.Epoch { + p.mu.Unlock() + return fmt.Errorf("%w: volume %s expected %s@%d, the line is %s@%d", ErrExpectedLineMoved, ask.VolumeID, exp.ReplicaID, exp.Epoch, cur.ReplicaID, cur.Epoch) + } + } next = adapter.AssignmentInfo{ VolumeID: ask.VolumeID, ReplicaID: ask.ReplicaID, diff --git a/seaweed-block/core/authority/directive.go b/seaweed-block/core/authority/directive.go index d68f029ebd..8b15b4eb30 100644 --- a/seaweed-block/core/authority/directive.go +++ b/seaweed-block/core/authority/directive.go @@ -74,8 +74,23 @@ type AssignmentAsk struct { // target (a bind is always a bootstrap); see adapter.PrimaryEvidence. It // is a probe fact about the target, never an authority fact. Evidence adapter.PrimaryEvidence + // ExpectCurrent, when set, makes a reassign conditional on the volume's + // line being exactly this replica at this epoch when the publisher mints + // (under its lock): an operator's move never lands on a line it did not + // see. + ExpectCurrent *ExpectedLine } +// ExpectedLine is the line a conditional reassign was decided against. +type ExpectedLine struct { + ReplicaID string + Epoch uint64 +} + +// ErrExpectedLineMoved: a conditional reassign found the volume's line no +// longer the one it was decided against; the publisher mints nothing. +var ErrExpectedLineMoved = errors.New("authority: the volume's line moved since the move was decided") + // ErrReassignWithoutKind: a primary change was asked without a kind (no // promotion-gate evidence, not a forced move); the publisher mints no line. var ErrReassignWithoutKind = errors.New("authority: a reassign needs a primary kind (promotion evidence or a forced move)") diff --git a/seaweed-block/core/authority/failback_runtime.go b/seaweed-block/core/authority/failback_runtime.go index 600a682d12..c55e868283 100644 --- a/seaweed-block/core/authority/failback_runtime.go +++ b/seaweed-block/core/authority/failback_runtime.go @@ -40,7 +40,9 @@ type FailbackAuthorityRuntime struct { // Gate is the promotion gate's decision for this planned move: the // target's probed evidence, or why the move is refused. A failback is a // primary change: without a gate it publishes nothing. - Gate func(volumeID, targetReplicaID string) (adapter.PrimaryEvidence, error) + // It decides on current, the line the request was checked against (the + // expected line), never on a line read later. + Gate func(volumeID, targetReplicaID string, current AuthorityBasis) (adapter.PrimaryEvidence, error) // TargetEndpoint is the master's own record of the target replica's data // and control addresses (its heartbeat, else its placement). The line is // published at the addresses the master knows, never the caller's: a @@ -87,7 +89,7 @@ func (r FailbackAuthorityRuntime) ExecuteFailback(ctx context.Context, req Failb return FailbackRuntimeResult{}, fmt.Errorf("%w: %s is at data=%s ctrl=%s, the request names data=%s ctrl=%s", ErrFailbackTargetEndpoint, req.ReplicaID, data, ctrl, req.TargetDataAddr, req.TargetCtrlAddr) } - evidence, err := r.Gate(req.VolumeID, req.ReplicaID) + evidence, err := r.Gate(req.VolumeID, req.ReplicaID, before) if err != nil { return FailbackRuntimeResult{}, fmt.Errorf("authority failback runtime: %w", err) } @@ -98,6 +100,9 @@ func (r FailbackAuthorityRuntime) ExecuteFailback(ctx context.Context, req Failb CtrlAddr: req.TargetCtrlAddr, Intent: IntentReassign, Evidence: evidence, + // Conditional on the expected line at the mint, not only at the + // check above: a move that landed in between refuses this one. + ExpectCurrent: &ExpectedLine{ReplicaID: req.ExpectedCurrentReplicaID, Epoch: req.ExpectedCurrentEpoch}, }); err != nil { return FailbackRuntimeResult{}, err } diff --git a/seaweed-block/core/authority/failback_runtime_test.go b/seaweed-block/core/authority/failback_runtime_test.go index fe492ccbd0..5a3eaf8ebf 100644 --- a/seaweed-block/core/authority/failback_runtime_test.go +++ b/seaweed-block/core/authority/failback_runtime_test.go @@ -90,7 +90,7 @@ func validAuthorityFailbackRequest() FailbackRuntimeRequest { // allowGate stands in for the master's planned-move decision: the target is // allowed with the probe round's evidence. -func allowGate(volumeID, target string) (adapter.PrimaryEvidence, error) { +func allowGate(volumeID, target string, _ AuthorityBasis) (adapter.PrimaryEvidence, error) { return adapter.PrimaryEvidence{Kind: adapter.PrimaryKindPromotion, DurableLSN: 1, Basis: adapter.BasisPlanned}, nil } @@ -101,7 +101,7 @@ func TestFailbackAuthorityRuntime_PublishesNothingWithoutTheGatesEvidence(t *tes for name, rt := range map[string]func(*Publisher) FailbackAuthorityRuntime{ "no gate": func(p *Publisher) FailbackAuthorityRuntime { return FailbackAuthorityRuntime{Publisher: p} }, "gate refuses": func(p *Publisher) FailbackAuthorityRuntime { - return FailbackAuthorityRuntime{Publisher: p, TargetEndpoint: r1Endpoint, Gate: func(string, string) (adapter.PrimaryEvidence, error) { + return FailbackAuthorityRuntime{Publisher: p, TargetEndpoint: r1Endpoint, Gate: func(string, string, AuthorityBasis) (adapter.PrimaryEvidence, error) { return adapter.PrimaryEvidence{}, errors.New("not caught up") }} }, @@ -155,9 +155,9 @@ func TestFailbackAuthorityRuntime_RefusesAddressesThatAreNotTheMastersRecord(t * req := validAuthorityFailbackRequest() req.TargetDataAddr, req.TargetCtrlAddr = c.data, c.ctrl gated := false - rt := FailbackAuthorityRuntime{Publisher: pub, TargetEndpoint: c.endpoint, Gate: func(v, r string) (adapter.PrimaryEvidence, error) { + rt := FailbackAuthorityRuntime{Publisher: pub, TargetEndpoint: c.endpoint, Gate: func(v, r string, cur AuthorityBasis) (adapter.PrimaryEvidence, error) { gated = true - return allowGate(v, r) + return allowGate(v, r, cur) }} if _, err := rt.ExecuteFailback(context.Background(), req); !errors.Is(err, ErrFailbackTargetEndpoint) { t.Fatalf("err=%v; want ErrFailbackTargetEndpoint", err) @@ -171,3 +171,34 @@ func TestFailbackAuthorityRuntime_RefusesAddressesThatAreNotTheMastersRecord(t * }) } } + +// The operator's expected line binds the move through publication, not only +// at the first check: a move that lands after the check and before the mint +// (here: inside the gate) makes the stale request publish nothing, and the +// line is the intervening one. The gate decides on the expected line. +func TestFailbackAuthorityRuntime_AnInterveningMoveRefusesTheStaleRequest(t *testing.T) { + pub := NewPublisher(NewStaticDirective(nil)) + if err := pub.apply(AssignmentAsk{VolumeID: "v1", ReplicaID: "r2", DataAddr: "data-r2", CtrlAddr: "ctrl-r2", Intent: IntentBind}); err != nil { + t.Fatal(err) + } + var decidedOn AuthorityBasis + rt := FailbackAuthorityRuntime{Publisher: pub, TargetEndpoint: r1Endpoint, Gate: func(v, r string, cur AuthorityBasis) (adapter.PrimaryEvidence, error) { + decidedOn = cur + // Another move lands now: r3 at epoch 2. + if err := pub.apply(AssignmentAsk{VolumeID: "v1", ReplicaID: "r3", DataAddr: "data-r3", CtrlAddr: "ctrl-r3", Intent: IntentReassign, + Evidence: adapter.PrimaryEvidence{Kind: adapter.PrimaryKindPromotion, DurableLSN: 1, Basis: adapter.BasisPlanned}}); err != nil { + t.Fatalf("intervening move: %v", err) + } + return allowGate(v, r, cur) + }} + _, err := rt.ExecuteFailback(context.Background(), validAuthorityFailbackRequest()) + if !errors.Is(err, ErrExpectedLineMoved) { + t.Fatalf("err=%v; want ErrExpectedLineMoved", err) + } + if decidedOn.ReplicaID != "r2" || decidedOn.Epoch != 1 { + t.Fatalf("the gate decided on %+v; want the expected line r2@1", decidedOn) + } + if line, _ := pub.VolumeAuthorityLine("v1"); line.ReplicaID != "r3" || line.Epoch != 2 { + t.Fatalf("line=%+v; want the intervening r3@2, not the stale request's r1", line) + } +} diff --git a/seaweed-block/core/authority/policy.go b/seaweed-block/core/authority/policy.go index 12d19a465d..d64e3a29cd 100644 --- a/seaweed-block/core/authority/policy.go +++ b/seaweed-block/core/authority/policy.go @@ -88,6 +88,11 @@ func (p *Publisher) LastAuthorityBasis(volumeID, replicaID string) (AuthorityBas func (p *Publisher) VolumeAuthorityLine(volumeID string) (AuthorityBasis, bool) { p.mu.Lock() defer p.mu.Unlock() + return p.volumeLineLocked(volumeID) +} + +// volumeLineLocked is VolumeAuthorityLine with p.mu held. +func (p *Publisher) volumeLineLocked(volumeID string) (AuthorityBasis, bool) { var best AuthorityBasis any := false for k, st := range p.state { diff --git a/seaweed-block/core/authority/promotion_basis_census_test.go b/seaweed-block/core/authority/promotion_basis_census_test.go index 7b8e1b4812..9dc4fd1134 100644 --- a/seaweed-block/core/authority/promotion_basis_census_test.go +++ b/seaweed-block/core/authority/promotion_basis_census_test.go @@ -26,7 +26,7 @@ func TestPromotionBasisCensus(t *testing.T) { want := []string{ "core/authority/controller.go plannedEvidence BasisPlanned", "core/authority/workspace.go MoveWorkspace BasisPlanned", - "core/host/master/planned_move.go plannedMoveEvidence BasisPlanned", + "core/host/master/planned_move.go plannedMoveEvidenceAt BasisPlanned", "core/host/master/product_loop.go healAfterFailedFence BasisHeal", "core/host/master/product_loop.go promotionEvidenceFor BasisFenced", } diff --git a/seaweed-block/core/host/master/host.go b/seaweed-block/core/host/master/host.go index ce0f8f4494..0d6fd3031b 100644 --- a/seaweed-block/core/host/master/host.go +++ b/seaweed-block/core/host/master/host.go @@ -453,7 +453,7 @@ func (h *Host) Publisher() *authority.Publisher { // the live Publisher. Callers still need an explicit policy gate before using // it; constructing the runtime does not execute failback. func (h *Host) FailbackAuthorityRuntime() authority.FailbackAuthorityRuntime { - return authority.FailbackAuthorityRuntime{Publisher: h.Publisher(), Gate: h.plannedMoveEvidence, TargetEndpoint: h.replicaEndpoint} + return authority.FailbackAuthorityRuntime{Publisher: h.Publisher(), Gate: h.plannedMoveEvidenceAt, TargetEndpoint: h.replicaEndpoint} } // replicaEndpoint is the master's own record of a replica's addresses: its diff --git a/seaweed-block/core/host/master/planned_move.go b/seaweed-block/core/host/master/planned_move.go index 88469beda2..b10239640e 100644 --- a/seaweed-block/core/host/master/planned_move.go +++ b/seaweed-block/core/host/master/planned_move.go @@ -48,10 +48,14 @@ type ackProfiled interface{ AckProfile() string } func (p *roundEvidenceProvider) AckProfile() string { return p.ackProfile } -// plannedMoveEvidence is the promotion gate's decision for an operator's -// planned move to target: the guard, then a probe round of the target, which -// must answer ready; its evidence goes on the line (kind PROMOTION). -func (h *Host) plannedMoveEvidence(volumeID, target string) (adapter.PrimaryEvidence, error) { +// plannedMoveEvidenceAt is the promotion gate's decision for an operator's +// move to target: the guard, then a probe round of the target, which must +// answer ready (or, for an unreachable primary, the fencing round); its +// evidence goes on the line (kind PROMOTION). It decides the move against current, the line the +// operator's request was checked against (its expected line): the +// reachability judged and the fencing round opened are that line's, never a +// line read later. The publisher then mints only while the line is still it. +func (h *Host) plannedMoveEvidenceAt(volumeID, target string, current authority.AuthorityBasis) (adapter.PrimaryEvidence, error) { h.promotionMu.RLock() prober := h.promotionProber h.promotionMu.RUnlock() @@ -80,8 +84,8 @@ func (h *Host) plannedMoveEvidence(volumeID, target string) (adapter.PrimaryEvid // move stays a planned move under its guard. if err := plannedMoveAllowed(profile, len(placement.Slots)); err != nil && !errors.Is(err, ErrPlannedMoveNeedsSeal) { return adapter.PrimaryEvidence{}, err - } else if h.primaryUnreachable(volumeID) { - return h.fencedMoveEvidence(volumeID, target, profile, placement.Slots) + } else if h.primaryUnreachable(volumeID, current) { + return h.fencedMoveEvidence(volumeID, target, profile, placement.Slots, current) } else if err != nil { return adapter.PrimaryEvidence{}, err } @@ -101,9 +105,8 @@ func (h *Host) plannedMoveEvidence(volumeID, target string) (adapter.PrimaryEvid // primaryUnreachable: the line's primary has no fresh, reachable heartbeat // slot. A volume without a line has no primary to lose. -func (h *Host) primaryUnreachable(volumeID string) bool { - current, ok := h.Publisher().VolumeAuthorityLine(volumeID) - if !ok { +func (h *Host) primaryUnreachable(volumeID string, current authority.AuthorityBasis) bool { + if !current.Assigned { return false } slot, fresh := h.freshCurrentSlot(volumeID, current.ReplicaID) @@ -118,9 +121,8 @@ func (h *Host) primaryUnreachable(volumeID string) bool { // fenced end and is ready). A round that does not allow the target publishes // nothing and leaves the fence pending, so a primary that answers again is // continued at e+1 (healAfterFailedFence) by the product loop. -func (h *Host) fencedMoveEvidence(volumeID, target, profile string, slots []lifecycle.PlacementSlotIntent) (adapter.PrimaryEvidence, error) { - current, ok := h.Publisher().VolumeAuthorityLine(volumeID) - if !ok { +func (h *Host) fencedMoveEvidence(volumeID, target, profile string, slots []lifecycle.PlacementSlotIntent, current authority.AuthorityBasis) (adapter.PrimaryEvidence, error) { + if !current.Assigned { return adapter.PrimaryEvidence{}, fmt.Errorf("fenced move: volume %s has no authority line", volumeID) } h.promotionMu.RLock() diff --git a/seaweed-block/core/host/master/planned_move_paths_test.go b/seaweed-block/core/host/master/planned_move_paths_test.go index 86bc59771a..4a26790840 100644 --- a/seaweed-block/core/host/master/planned_move_paths_test.go +++ b/seaweed-block/core/host/master/planned_move_paths_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/seaweedfs/seaweed-block/core/adapter" "github.com/seaweedfs/seaweed-block/core/authority" ) @@ -74,3 +75,11 @@ func TestPlannedMoveOneGuardForFailbackAndRebalance(t *testing.T) { } } } + +// plannedMoveEvidence is plannedMoveEvidenceAt against the volume's line as +// it is now (the tests' shorthand; the failback runtime passes the line its +// request was checked against). +func (h *Host) plannedMoveEvidence(volumeID, target string) (adapter.PrimaryEvidence, error) { + current, _ := h.Publisher().VolumeAuthorityLine(volumeID) + return h.plannedMoveEvidenceAt(volumeID, target, current) +} diff --git a/seaweed-block/core/ops/failback_authority_runtime_adapter.go b/seaweed-block/core/ops/failback_authority_runtime_adapter.go index fe20618cf1..89bd7b53c2 100644 --- a/seaweed-block/core/ops/failback_authority_runtime_adapter.go +++ b/seaweed-block/core/ops/failback_authority_runtime_adapter.go @@ -17,7 +17,7 @@ type AuthorityFailbackRuntime struct { // NewAuthorityFailbackRuntime adapts the authority failback runtime; gate is // the promotion gate's planned-move decision and endpoint the master's record // of the target's addresses (nil either: every failback refuses). -func NewAuthorityFailbackRuntime(publisher *authority.Publisher, gate func(volumeID, targetReplicaID string) (adapter.PrimaryEvidence, error), endpoint func(volumeID, targetReplicaID string) (string, string, bool)) *AuthorityFailbackRuntime { +func NewAuthorityFailbackRuntime(publisher *authority.Publisher, gate func(volumeID, targetReplicaID string, current authority.AuthorityBasis) (adapter.PrimaryEvidence, error), endpoint func(volumeID, targetReplicaID string) (string, string, bool)) *AuthorityFailbackRuntime { return &AuthorityFailbackRuntime{ Runtime: authority.FailbackAuthorityRuntime{Publisher: publisher, Gate: gate, TargetEndpoint: endpoint}, } diff --git a/seaweed-block/core/ops/failback_authority_runtime_adapter_test.go b/seaweed-block/core/ops/failback_authority_runtime_adapter_test.go index 54c45deaa7..e7f928772a 100644 --- a/seaweed-block/core/ops/failback_authority_runtime_adapter_test.go +++ b/seaweed-block/core/ops/failback_authority_runtime_adapter_test.go @@ -147,7 +147,7 @@ func seededFailbackPublisher(t *testing.T) *authority.Publisher { } // opsAllowGate stands in for the master's planned-move decision (allowed). -func opsAllowGate(string, string) (adapter.PrimaryEvidence, error) { +func opsAllowGate(string, string, authority.AuthorityBasis) (adapter.PrimaryEvidence, error) { return adapter.PrimaryEvidence{Kind: adapter.PrimaryKindPromotion, DurableLSN: 1, Basis: adapter.BasisPlanned}, nil }