Files
seaweedfs/seaweed-block/core/authority/controller.go
T
10b949183c seaweed-block: a promotion gate's replace decision logs what the controller did with it (#13428)
* fix(master): a promotion gate's replace decision logs what the controller did with it

A replace decided by the promotion gate went to the topology controller,
which can drop it on six paths without a word. These are: a stale basis,
superseded slot facts, a boot-grace hold, no evidence on the target, an
ask that is already desired, and an unsupported topology. When a volume
then did not fail over, nothing on the master's log said why.

The controller now records, per volume, what its last intake did: the ask
it enqueued, or why it enqueued none (LastOutcome). After each tick's
submit, the master logs every replace decision with that outcome, or with
why the decisions were not submitted. Every publication is logged too.

The creation-gap process test derives its failover bound from its own
flags: one loop to the tick that ends creation, plus the freshness window
to the loss, plus one loop to the tick that reads it. It waits twice that.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* seaweed-block: a replace decision is reported with its own submission's outcome, and on a binding-read failure

Review round 1:
- The tick reads the controller's outcome for each volume inside the
  intake that carried it (SubmitObservedStateOutcomes, under the
  controller's lock): the background observation host shares the
  controller and may submit before a later read.
- A workspace binding read that fails after the probe reports the tick's
  replace decisions as not submitted, with the error, before returning.
- The production-tick test waits for the publication line (bounded): the
  publisher logs it from its observer after the line is visible.
- A replace decision is identified by a flag set where it is decided, not
  by its display text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* seaweed-block: ledger row for the replace-decision report

Review round 2: Part C rule 4. INV-PROMOTE-DECISION-REPORTED-001 names the
regression tests this change adds (an intake after the submission, the
binding-read failure after the probe) and their enforcing functions.
Diagnostics only: no state machine or model change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* seaweed-block: split the replace-decision ledger row under the statement limit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* seaweed-block: gateNoteMu also guards gateReplaces

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-12-05 14:37:46 -08:00

831 lines
30 KiB
Go

package authority
import (
"context"
"fmt"
"sync"
"time"
"github.com/seaweedfs/seaweed-block/core/adapter"
)
// TopologyController is the multi-volume, cluster-scoped directive
// for the full P14 close target. It converts cluster snapshots into
// bounded desired AssignmentAsk values, then feeds those asks to the
// publisher through the existing Directive seam.
//
// The controller owns:
// - accepted topology validation for the richer P14 set
// - cluster-scoped placement / failover / rebalance decisions
// - convergence state ("desired per volume" until observed current)
//
// It does NOT mint Epoch / EndpointVersion and does not call the
// adapter directly. Publisher remains the sole minter and the
// adapter remains the only consumer ingress.
type TopologyController struct {
config TopologyControllerConfig
reader AuthorityBasisReader
queue *assignmentQueue
mu sync.Mutex
desired map[string]DesiredAssignment
unsupported map[string]UnsupportedEvidence
convergenceStuck map[string]*ConvergenceStuckEvidence
bootGrace map[string]*primaryBootGrace // volume/replica -> current incarnation's grace
outcome map[string]string // volume -> what the last intake did with it (LastOutcome); kept like bootGrace
now func() time.Time
}
func NewTopologyController(config TopologyControllerConfig, reader AuthorityBasisReader) *TopologyController {
return &TopologyController{
config: config.withDefaults(),
reader: reader,
queue: newAssignmentQueue(),
desired: make(map[string]DesiredAssignment),
unsupported: make(map[string]UnsupportedEvidence),
convergenceStuck: make(map[string]*ConvergenceStuckEvidence),
bootGrace: make(map[string]*primaryBootGrace),
outcome: make(map[string]string),
now: time.Now,
}
}
// LastOutcome says what the last snapshot intake that carried the volume
// as supported did with it: the ask it enqueued, or why it enqueued none
// ("" before any). The master logs it beside a promotion gate's replace
// decision, so a decision the controller did not act on says why.
func (c *TopologyController) LastOutcome(volumeID string) string {
c.mu.Lock()
defer c.mu.Unlock()
return c.outcome[volumeID]
}
// SetNowForTest overrides the controller's clock source. Tests
// use this to drive RetryWindow / Stuck transitions deterministically.
// Production code MUST NOT call this.
func (c *TopologyController) SetNowForTest(now func() time.Time) {
c.mu.Lock()
defer c.mu.Unlock()
c.now = now
}
// ExpectedSlotsPerVolume is the one replica-slot count every volume the
// controller decides must have (validateVolumeTopology).
func (c *TopologyController) ExpectedSlotsPerVolume() int { return c.config.ExpectedSlotsPerVolume }
// SubmitClusterSnapshot is the cluster-scoped entry point for
// topology, health, and convergence observations. Rejects empty
// snapshots to preserve the "supported truth only" invariant on
// this path.
//
// For the observation-fed intake that also carries pending /
// unsupported transitions, see SubmitObservedState.
func (c *TopologyController) SubmitClusterSnapshot(snap ClusterSnapshot) error {
if len(snap.Volumes) == 0 {
return fmt.Errorf("topology: cluster snapshot has no volumes")
}
serverIndex := indexServers(snap.Servers)
volumes := make([]VolumeTopologySnapshot, len(snap.Volumes))
copy(volumes, snap.Volumes)
sortVolumeSnapshots(volumes)
seenVolumes := make(map[string]struct{}, len(volumes))
for _, v := range volumes {
if _, ok := seenVolumes[v.VolumeID]; ok {
return fmt.Errorf("topology: duplicate volume snapshot %s", v.VolumeID)
}
seenVolumes[v.VolumeID] = struct{}{}
if err := validateVolumeTopology(v, c.config.ExpectedSlotsPerVolume); err != nil {
return err
}
}
c.mu.Lock()
defer c.mu.Unlock()
currentLoad := c.currentServerLoad(volumes)
projectedLoad := cloneLoad(currentLoad)
for _, vol := range volumes {
currentLine, hasLine := c.reader.VolumeAuthorityLine(vol.VolumeID)
c.processSupportedVolumeLocked(vol, snap, serverIndex, currentLine, hasLine, currentLoad, projectedLoad)
}
return nil
}
// SubmitObservedState is the observation-institution intake. It
// accepts one synthesized ClusterSnapshot together with the
// per-volume supportability report for the same build. This is
// the intake the observation host calls every rebuild, including
// when snap.Volumes is empty but report carries transitions.
//
// Contract:
// 1. snap.Volumes carries ONLY supported volumes. They drive the
// decision table the same way SubmitClusterSnapshot does.
// 2. For every volume in report.Pending or report.Unsupported,
// any previously-desired assignment is cleared, and the
// per-volume unsupported evidence is updated (Unsupported
// only — Pending does NOT produce evidence; it records that
// action is paused).
// 3. An entirely-empty intake (no Volumes, no Pending,
// no Unsupported) is a no-op rather than an error — a single
// tick with nothing new is not a contract violation.
//
// This is the intake fix for the architect finding that
// supportability transitions could not propagate to the
// controller. Suppressing submission on empty Volumes left old
// desired state live; SubmitObservedState always processes the
// report.
func (c *TopologyController) SubmitObservedState(snap ClusterSnapshot, report SupportabilityReport) error {
return c.submitObservedState(snap, report, nil)
}
// SubmitObservedStateOutcomes is SubmitObservedState that also returns what
// this intake did with each volume it carried (supported, pending or
// unsupported), read while the intake still holds the controller's lock: a
// later intake (the background observation host shares the controller)
// cannot change it. A volume this intake did not carry has no entry.
func (c *TopologyController) SubmitObservedStateOutcomes(snap ClusterSnapshot, report SupportabilityReport) (map[string]string, error) {
out := map[string]string{}
err := c.submitObservedState(snap, report, out)
return out, err
}
func (c *TopologyController) submitObservedState(snap ClusterSnapshot, report SupportabilityReport, outcomes map[string]string) error {
// Validate non-empty supported snapshots the same way
// SubmitClusterSnapshot does. Empty supported + empty report
// is allowed (no-op).
if len(snap.Volumes) == 0 && len(report.Pending) == 0 && len(report.Unsupported) == 0 {
return nil
}
// Pre-validate supported volumes (if any).
serverIndex := indexServers(snap.Servers)
volumes := make([]VolumeTopologySnapshot, len(snap.Volumes))
copy(volumes, snap.Volumes)
sortVolumeSnapshots(volumes)
seen := make(map[string]struct{}, len(volumes))
for _, v := range volumes {
if _, ok := seen[v.VolumeID]; ok {
return fmt.Errorf("topology: duplicate volume snapshot %s", v.VolumeID)
}
seen[v.VolumeID] = struct{}{}
if err := validateVolumeTopology(v, c.config.ExpectedSlotsPerVolume); err != nil {
return err
}
}
c.mu.Lock()
defer c.mu.Unlock()
// Process pending / unsupported transitions first. Clearing
// desired state for these volumes before decision logic runs
// means any NEWLY-supported volume in snap.Volumes gets fresh
// decision treatment, while any transitioned-away volume is
// no longer held as "desired" by the controller.
for vid := range report.Pending {
c.clearDesiredLocked(vid)
// Pending does not produce unsupported evidence. Any
// stale unsupported record from a prior build is cleared,
// because pending is a transitional state, not a fault.
delete(c.unsupported, vid)
c.outcome[vid] = "no ask: supportability pending"
}
for vid, ev := range report.Unsupported {
c.clearDesiredLocked(vid)
// Record per-volume unsupported evidence. The joined
// reasons string preserves the observation-layer reason
// vocabulary in the controller's existing per-volume
// evidence shape. The Basis is left at zero — the
// observation layer already captured the offending basis
// in its own VolumeUnsupportedEvidence; the controller's
// evidence exists to surface "this volume is not acted
// on" at the cluster-scope level.
c.unsupported[vid] = UnsupportedEvidence{
SnapshotRevision: ev.SnapshotRevision,
CollectedAt: ev.EvaluatedAt,
Basis: AuthorityBasis{},
Reason: joinReasons(ev.Reasons),
}
c.outcome[vid] = "no ask: unsupported: " + joinReasons(ev.Reasons)
}
// Process supported volumes via the existing decision path.
// A volume appearing here means observation cleared all the
// supportability rules; any prior unsupported evidence for
// this volume is dropped.
for _, v := range volumes {
delete(c.unsupported, v.VolumeID)
}
currentLoad := c.currentServerLoad(volumes)
projectedLoad := cloneLoad(currentLoad)
for _, vol := range volumes {
currentLine, hasLine := c.reader.VolumeAuthorityLine(vol.VolumeID)
c.processSupportedVolumeLocked(vol, snap, serverIndex, currentLine, hasLine, currentLoad, projectedLoad)
}
if outcomes != nil {
for vid := range report.Pending {
outcomes[vid] = c.outcome[vid]
}
for vid := range report.Unsupported {
outcomes[vid] = c.outcome[vid]
}
for _, vol := range volumes {
outcomes[vol.VolumeID] = c.outcome[vol.VolumeID]
}
}
return nil
}
// processSupportedVolumeLocked is the shared supported-volume
// convergence loop used by both SubmitClusterSnapshot and
// SubmitObservedState. Per-iteration order (§5.2):
//
// 1. confirmDesiredLocked — if confirmation contract is
// satisfied, clear desired + stuck evidence (Active→None).
// 2. supersedeDesiredLocked — §9 case 1/2: drop desired if
// publisher itself has moved past the proposed basis to a
// different replica, or observation and publisher agree on a
// different outcome. Case 3 ("decision produces different
// ask") is covered by the same-ask dedupe further below.
// 3. Topology-honesty checks (clear desired on out-of-topology
// authority from either side).
// 4. evaluateStuckLocked — if desired still outstanding, mark
// Stuck when DesiredAt + RetryWindow has elapsed. Runs
// before the stale-basis continue so publish-but-not-
// observed scenarios can surface Stuck evidence.
// 5. Stale-basis continue — keep desired, no decide this tick.
// 6. decideVolume + same-ask dedupe + enqueue. On enqueue,
// DesiredAt is anchored (or re-anchored) to c.now().
func (c *TopologyController) processSupportedVolumeLocked(
vol VolumeTopologySnapshot,
snap ClusterSnapshot,
serverIndex map[string]ServerObservation,
currentLine AuthorityBasis,
hasLine bool,
currentLoad map[string]int,
projectedLoad map[string]int,
) {
// A volume appearing in the supported set means observation
// cleared every supportability rule for it. Any prior
// unsupported evidence for this volume must drop — otherwise
// LastUnsupported() would keep reporting a stale fault after
// the volume is healthy. (Regression from the pre-refactor
// S4 behavior; stuck evidence has its own lifecycle and is
// NOT cleared here — §10 clearing rule 4.)
delete(c.unsupported, vol.VolumeID)
// A snapshot carrying a superseded slot fact is stale (#128): it
// records nothing and decides nothing, below.
staleFacts := c.supersededFactsLocked(vol)
if !staleFacts {
c.observeIncarnationsLocked(vol, serverIndex)
}
c.confirmDesiredLocked(vol, currentLine, hasLine)
if c.supersedeDesiredLocked(vol, currentLine, hasLine) {
// fall through — decideVolume may emit a new ask this tick.
}
if vol.Authority.Assigned && !volumeIncludesReplica(vol, vol.Authority.ReplicaID) {
c.recordUnsupportedLocked(vol.VolumeID, snap, vol.Authority, "snapshot authority outside accepted topology: "+vol.Authority.ReplicaID)
c.clearDesiredLocked(vol.VolumeID)
c.outcome[vol.VolumeID] = "no ask: snapshot authority outside accepted topology"
return
}
if hasLine && !volumeIncludesReplica(vol, currentLine.ReplicaID) {
c.recordUnsupportedLocked(vol.VolumeID, snap, currentLine, "publisher authority on replica outside accepted topology: "+currentLine.ReplicaID)
c.clearDesiredLocked(vol.VolumeID)
c.outcome[vol.VolumeID] = "no ask: publisher authority outside accepted topology"
return
}
// Evaluate stuck state for any still-outstanding desired. This
// must run before the stale-basis continue so "publish-but-
// not-observed" — which shows up here as a stale basis — can
// surface ConvergenceStuckEvidence after the retry window.
c.evaluateStuckLocked(vol.VolumeID)
// Stale snapshot: do not emit from a stale basis, but keep any
// existing desired (publish-until-observed convergence record).
if !basisMatchesLine(vol.Authority, currentLine, hasLine) {
c.outcome[vol.VolumeID] = fmt.Sprintf("no ask: stale snapshot basis %s@%d, line %s@%d", vol.Authority.ReplicaID, vol.Authority.Epoch, currentLine.ReplicaID, currentLine.Epoch)
return
}
// Stale slot facts: likewise keep any existing desired and emit
// nothing from this snapshot.
if staleFacts {
c.outcome[vol.VolumeID] = "no ask: superseded slot facts"
return
}
ask, reason, emit := c.decideVolume(vol, serverIndex, currentLine, hasLine, currentLoad, projectedLoad)
if !emit {
c.clearDesiredLocked(vol.VolumeID)
c.outcome[vol.VolumeID] = "no ask: " + reason
return
}
// Same-ask dedupe: if the identical ask is already the current
// desired, do not re-enqueue. Without this guard, repeated
// identical supported feeds would mint a new Reassign epoch
// each iteration — authority churn.
if prev, ok := c.desired[vol.VolumeID]; ok && sameAsk(prev.Ask, ask) {
c.outcome[vol.VolumeID] = fmt.Sprintf("already desired since %s: %s %s (%s)", prev.DesiredAt.UTC().Format(time.RFC3339Nano), ask.Intent, ask.ReplicaID, prev.Reason)
return
}
// A different ask (or a new desired from None) replaces any
// prior stuck evidence — the old stuck record no longer
// describes the currently-outstanding desired (§10 clearing
// rule 2).
c.clearConvergenceStuckLocked(vol.VolumeID)
desired := DesiredAssignment{
Ask: ask,
Reason: reason,
Revision: snap.CollectedRevision,
ProposedBasis: currentLine,
DesiredAt: c.now(),
}
if !hasLine {
desired.ProposedBasis = AuthorityBasis{}
}
c.desired[vol.VolumeID] = desired
c.queue.enqueue(ask)
c.outcome[vol.VolumeID] = fmt.Sprintf("enqueued %s %s (%s)", ask.Intent, ask.ReplicaID, reason)
applyProjectedMove(projectedLoad, vol, currentLine, hasLine, ask)
}
// joinReasons returns a stable comma-joined reason string from
// the observation-layer vocabulary. Used to carry the observation
// reason set into the controller's per-volume evidence slot.
func joinReasons(reasons []string) string {
if len(reasons) == 0 {
return ""
}
out := reasons[0]
for i := 1; i < len(reasons); i++ {
out += "," + reasons[i]
}
return out
}
// Next satisfies Directive by draining the per-volume latest-wins
// assignment queue.
func (c *TopologyController) Next(ctx context.Context) (AssignmentAsk, error) {
return c.queue.next(ctx)
}
// SubmitAssignmentAsk appends a controller-verified ask to the publisher
// directive queue. It is intentionally still an ask-level seam: Publisher
// remains the only epoch / endpoint-version author.
func (c *TopologyController) SubmitAssignmentAsk(ask AssignmentAsk) error {
if err := validateAsk(ask); err != nil {
return err
}
c.queue.enqueue(ask)
return nil
}
// LastUnsupported returns the most recent unsupported evidence for
// one volume, if any.
func (c *TopologyController) LastUnsupported(volumeID string) (UnsupportedEvidence, bool) {
c.mu.Lock()
defer c.mu.Unlock()
ev, ok := c.unsupported[volumeID]
return ev, ok
}
// LastConvergenceStuck returns the per-volume
// ConvergenceStuckEvidence if the desired assignment for this
// volume is currently Stuck. Returns (_, false) when no desired
// is outstanding, when a desired is outstanding but still within
// the retry window, or when the desired was confirmed /
// superseded / cleared.
//
// Returned value is a copy: mutating it does not affect
// controller state.
func (c *TopologyController) LastConvergenceStuck(volumeID string) (ConvergenceStuckEvidence, bool) {
c.mu.Lock()
defer c.mu.Unlock()
ev, ok := c.convergenceStuck[volumeID]
if !ok {
return ConvergenceStuckEvidence{}, false
}
return *ev, true
}
// DesiredFor returns a copy of the current DesiredAssignment for
// one volume, if any. Test-surface accessor; production code
// should observe convergence outcomes through the publisher
// stream and snapshot feeds, not by reading desired state.
func (c *TopologyController) DesiredFor(volumeID string) (DesiredAssignment, bool) {
c.mu.Lock()
defer c.mu.Unlock()
d, ok := c.desired[volumeID]
return d, ok
}
func (c *TopologyController) clearDesiredLocked(volumeID string) {
_, hadDesired := c.desired[volumeID]
delete(c.desired, volumeID)
if hadDesired {
c.queue.discard(volumeID)
}
// §10 clearing rule 1 / 2 / 3: whenever the desired clears
// (confirmed, superseded, supportability-driven), the stuck
// evidence clears with it. Supportability regaining does NOT
// invoke this path — that is handled by the supported-volume
// loop (§10 clearing rule 4).
c.clearConvergenceStuckLocked(volumeID)
}
func (c *TopologyController) recordUnsupportedLocked(volumeID string, snap ClusterSnapshot, basis AuthorityBasis, reason string) {
c.unsupported[volumeID] = UnsupportedEvidence{
SnapshotRevision: snap.CollectedRevision,
CollectedAt: snap.CollectedAt,
Basis: basis,
Reason: reason,
}
}
func (c *TopologyController) confirmDesiredLocked(vol VolumeTopologySnapshot, currentLine AuthorityBasis, hasLine bool) {
desired, ok := c.desired[vol.VolumeID]
if !ok {
return
}
if desiredObserved(desired, vol, currentLine, hasLine) {
// Route through clearDesiredLocked so §10 clearing rule 1
// (stuck evidence clears with the desired) applies to
// late confirmations that happen AFTER the volume had
// already transitioned into Stuck. Deleting the desired
// entry directly here would leave c.convergenceStuck[vid]
// populated, reporting a volume as still stuck after
// convergence had actually completed.
c.clearDesiredLocked(vol.VolumeID)
}
}
func desiredObserved(desired DesiredAssignment, vol VolumeTopologySnapshot, currentLine AuthorityBasis, hasLine bool) bool {
if !hasLine || !vol.Authority.Assigned {
return false
}
if vol.Authority.ReplicaID != currentLine.ReplicaID ||
vol.Authority.Epoch != currentLine.Epoch ||
vol.Authority.EndpointVersion != currentLine.EndpointVersion {
return false
}
if currentLine.ReplicaID != desired.Ask.ReplicaID {
return false
}
switch desired.Ask.Intent {
case IntentRefreshEndpoint:
return vol.Authority.DataAddr == desired.Ask.DataAddr &&
vol.Authority.CtrlAddr == desired.Ask.CtrlAddr &&
(desired.Ask.Incarnation == "" || currentLine.Incarnation == desired.Ask.Incarnation)
case IntentBind, IntentReassign:
return true
default:
return false
}
}
func (c *TopologyController) currentServerLoad(volumes []VolumeTopologySnapshot) map[string]int {
load := make(map[string]int)
for _, vol := range volumes {
line, ok := c.reader.VolumeAuthorityLine(vol.VolumeID)
if !ok {
continue
}
slot, ok := candidateByReplica(vol, line.ReplicaID)
if !ok {
continue
}
load[slot.ServerID]++
}
return load
}
func (c *TopologyController) decideVolume(
vol VolumeTopologySnapshot,
serverIndex map[string]ServerObservation,
currentLine AuthorityBasis,
hasLine bool,
currentLoad map[string]int,
projectedLoad map[string]int,
) (AssignmentAsk, string, bool) {
if !hasLine {
cands := placeableCandidates(vol, serverIndex)
if len(cands) == 0 {
return AssignmentAsk{}, "no placeable candidate for the first line", false
}
best := choosePlacementCandidate(cands, projectedLoad)
return AssignmentAsk{
VolumeID: vol.VolumeID,
ReplicaID: best.ReplicaID,
DataAddr: best.DataAddr,
CtrlAddr: best.CtrlAddr,
Intent: IntentBind,
Incarnation: best.Incarnation,
}, "initial placement", true
}
current, ok := candidateByReplica(vol, currentLine.ReplicaID)
if !ok {
return AssignmentAsk{}, "the line's replica is not in the snapshot", false
}
if candidateAcceptable(current, serverIndex) {
if endpointStale(current, currentLine) {
return refreshAsk(vol.VolumeID, current, currentLine), "endpoint refresh", true
}
if target, ok := chooseRebalanceCandidate(vol, current, serverIndex, currentLoad, projectedLoad, c.config.RebalanceSkew); ok {
// A primary change goes out only with the promotion gate's
// evidence for its target (one decider): none, no rebalance. The
// current primary is alive, so it is a planned move: the same
// guard as a failback (the target in every acknowledgement set).
if target.PromotionEvidence.Kind != adapter.PrimaryKindPromotion || !target.PlannedMoveAllowed {
return AssignmentAsk{}, "the current primary is acceptable; rebalance target " + target.ReplicaID + " has no planned-move evidence", false
}
return AssignmentAsk{
VolumeID: vol.VolumeID,
ReplicaID: target.ReplicaID,
DataAddr: target.DataAddr,
CtrlAddr: target.CtrlAddr,
Intent: IntentReassign,
Evidence: plannedEvidence(target.PromotionEvidence),
Incarnation: target.Incarnation,
}, "rebalance", true
}
return AssignmentAsk{}, "the current primary is acceptable", false
}
// A restarted current primary that is placeable but not ready yet
// keeps its authority through its boot grace; only its endpoint
// may be refreshed.
if hold, superseded := c.primaryBootGraceLocked(vol.VolumeID, current, candidatePlaceable(current, serverIndex)); hold {
if !superseded && endpointStale(current, currentLine) {
return refreshAsk(vol.VolumeID, current, currentLine), "endpoint refresh (primary boot grace)", true
}
if superseded {
return AssignmentAsk{}, "the current primary holds its boot grace (by its newer recorded fact)", false
}
return AssignmentAsk{}, "the current primary holds its boot grace", false
}
why := "no acceptable candidate besides the current primary"
other := acceptableCandidatesExcept(vol, serverIndex, current.ReplicaID)
if len(other) > 0 {
best := chooseFailoverCandidate(other, projectedLoad)
// A failover goes out only with the promotion gate's evidence for
// its target (one decider): no evidence, no promotion.
if best.PromotionEvidence.Kind == adapter.PrimaryKindPromotion {
return AssignmentAsk{
VolumeID: vol.VolumeID,
ReplicaID: best.ReplicaID,
DataAddr: best.DataAddr,
CtrlAddr: best.CtrlAddr,
Intent: IntentReassign,
Evidence: best.PromotionEvidence,
Incarnation: best.Incarnation,
}, "failover", true
}
why = "failover target " + best.ReplicaID + " has no promotion evidence"
}
// No promotion-ready peer exists. Keep the current authority endpoint fresh
// when the current replica is still reachable/eligible; this preserves
// authority continuity without treating that replica as a failover candidate.
if candidatePlaceable(current, serverIndex) && endpointStale(current, currentLine) {
return refreshAsk(vol.VolumeID, current, currentLine), "endpoint refresh", true
}
return AssignmentAsk{}, why, false
}
// endpointStale reports whether the current primary's line needs a new
// endpoint version: its addresses changed, or the process reporting it is
// not the one the line was issued for (a restarted primary, compared for
// equality only; the order stays the endpoint version).
func endpointStale(current ReplicaCandidate, line AuthorityBasis) bool {
return current.DataAddr != line.DataAddr || current.CtrlAddr != line.CtrlAddr ||
current.Incarnation != "" && current.Incarnation != line.Incarnation
}
// refreshAsk re-issues the current primary's line at its reported
// addresses for its reported incarnation, only while the volume's line is
// still line (the one the refresh was decided against).
func refreshAsk(volumeID string, current ReplicaCandidate, line AuthorityBasis) AssignmentAsk {
return AssignmentAsk{
VolumeID: volumeID,
ReplicaID: current.ReplicaID,
DataAddr: current.DataAddr,
CtrlAddr: current.CtrlAddr,
Intent: IntentRefreshEndpoint,
Incarnation: current.Incarnation,
ExpectCurrent: &ExpectedLine{ReplicaID: line.ReplicaID, Epoch: line.Epoch, DataAddr: line.DataAddr, CtrlAddr: line.CtrlAddr},
}
}
func volumeIncludesReplica(vol VolumeTopologySnapshot, replicaID string) bool {
for _, slot := range vol.Slots {
if slot.ReplicaID == replicaID {
return true
}
}
return false
}
func candidateByReplica(vol VolumeTopologySnapshot, replicaID string) (ReplicaCandidate, bool) {
for _, slot := range vol.Slots {
if slot.ReplicaID == replicaID {
return slot, true
}
}
return ReplicaCandidate{}, false
}
func candidateAcceptable(slot ReplicaCandidate, servers map[string]ServerObservation) bool {
server, ok := servers[slot.ServerID]
if !ok {
return false
}
return slot.Reachable &&
slot.ReadyForPrimary &&
slot.Eligible &&
!slot.Withdrawn &&
server.Reachable &&
server.Eligible
}
func candidatePlaceable(slot ReplicaCandidate, servers map[string]ServerObservation) bool {
server, ok := servers[slot.ServerID]
if !ok {
return false
}
return slot.Reachable &&
slot.Eligible &&
!slot.Withdrawn &&
server.Reachable &&
server.Eligible
}
func placeableCandidates(vol VolumeTopologySnapshot, servers map[string]ServerObservation) []ReplicaCandidate {
var out []ReplicaCandidate
for _, slot := range vol.Slots {
if candidatePlaceable(slot, servers) {
out = append(out, slot)
}
}
return out
}
func acceptableCandidates(vol VolumeTopologySnapshot, servers map[string]ServerObservation) []ReplicaCandidate {
var out []ReplicaCandidate
for _, slot := range vol.Slots {
if candidateAcceptable(slot, servers) {
out = append(out, slot)
}
}
return out
}
func acceptableCandidatesExcept(vol VolumeTopologySnapshot, servers map[string]ServerObservation, excludeReplica string) []ReplicaCandidate {
var out []ReplicaCandidate
for _, slot := range vol.Slots {
if slot.ReplicaID == excludeReplica {
continue
}
if candidateAcceptable(slot, servers) {
out = append(out, slot)
}
}
return out
}
func choosePlacementCandidate(cands []ReplicaCandidate, load map[string]int) ReplicaCandidate {
best := cands[0]
for _, cand := range cands[1:] {
if load[cand.ServerID] < load[best.ServerID] {
best = cand
continue
}
if load[cand.ServerID] == load[best.ServerID] && cand.EvidenceScore > best.EvidenceScore {
best = cand
continue
}
if load[cand.ServerID] == load[best.ServerID] && cand.EvidenceScore == best.EvidenceScore && cand.ReplicaID < best.ReplicaID {
best = cand
}
}
return best
}
func chooseFailoverCandidate(cands []ReplicaCandidate, load map[string]int) ReplicaCandidate {
best := cands[0]
for _, cand := range cands[1:] {
if cand.EvidenceScore > best.EvidenceScore {
best = cand
continue
}
if cand.EvidenceScore == best.EvidenceScore && load[cand.ServerID] < load[best.ServerID] {
best = cand
continue
}
if cand.EvidenceScore == best.EvidenceScore && load[cand.ServerID] == load[best.ServerID] && cand.ReplicaID < best.ReplicaID {
best = cand
}
}
return best
}
func chooseRebalanceCandidate(
vol VolumeTopologySnapshot,
current ReplicaCandidate,
servers map[string]ServerObservation,
currentLoad map[string]int,
projectedLoad map[string]int,
skew int,
) (ReplicaCandidate, bool) {
lightest := currentLoad[current.ServerID]
for _, slot := range vol.Slots {
if !candidateAcceptable(slot, servers) {
continue
}
if currentLoad[slot.ServerID] < lightest {
lightest = currentLoad[slot.ServerID]
}
}
if currentLoad[current.ServerID]-lightest <= skew {
return ReplicaCandidate{}, false
}
best := ReplicaCandidate{}
ok := false
for _, slot := range vol.Slots {
if slot.ReplicaID == current.ReplicaID || !candidateAcceptable(slot, servers) {
continue
}
if !ok ||
projectedLoad[slot.ServerID] < projectedLoad[best.ServerID] ||
(projectedLoad[slot.ServerID] == projectedLoad[best.ServerID] && slot.EvidenceScore > best.EvidenceScore) ||
(projectedLoad[slot.ServerID] == projectedLoad[best.ServerID] && slot.EvidenceScore == best.EvidenceScore && slot.ReplicaID < best.ReplicaID) {
best = slot
ok = true
}
}
return best, ok
}
func cloneLoad(src map[string]int) map[string]int {
dst := make(map[string]int, len(src))
for k, v := range src {
dst[k] = v
}
return dst
}
func applyProjectedMove(projected map[string]int, vol VolumeTopologySnapshot, currentLine AuthorityBasis, hasLine bool, ask AssignmentAsk) {
target, ok := candidateByReplica(vol, ask.ReplicaID)
if !ok {
return
}
switch ask.Intent {
case IntentBind:
projected[target.ServerID]++
case IntentReassign:
if hasLine {
if current, ok := candidateByReplica(vol, currentLine.ReplicaID); ok && projected[current.ServerID] > 0 {
projected[current.ServerID]--
}
}
projected[target.ServerID]++
}
}
func sameAsk(a, b AssignmentAsk) bool {
return a.VolumeID == b.VolumeID &&
a.ReplicaID == b.ReplicaID &&
a.DataAddr == b.DataAddr &&
a.CtrlAddr == b.CtrlAddr &&
a.Intent == b.Intent &&
a.Incarnation == b.Incarnation &&
sameExpectedLine(a.ExpectCurrent, b.ExpectCurrent)
}
// sameExpectedLine: two asks conditional on different lines are different
// asks (one refused because the line moved is asked again against the new
// line, not suppressed as a duplicate).
func sameExpectedLine(a, b *ExpectedLine) bool {
if a == nil || b == nil {
return a == b
}
return *a == *b
}
// plannedEvidence is a rebalance's evidence: the gate's probe of the target,
// a planned move after the planned-move guard (PlannedMoveAllowed).
func plannedEvidence(ev adapter.PrimaryEvidence) adapter.PrimaryEvidence {
ev.FencedEpoch, ev.Basis = 0, adapter.BasisPlanned
return ev
}