diff --git a/seaweed-block/core/authority/primary_boot_grace.go b/seaweed-block/core/authority/primary_boot_grace.go index e01a81f574..06d020132e 100644 --- a/seaweed-block/core/authority/primary_boot_grace.go +++ b/seaweed-block/core/authority/primary_boot_grace.go @@ -9,9 +9,9 @@ import "time" // primary would otherwise fail over the moment a ready peer exists. // The controller keeps the CURRENT primary through one boot grace: // -// 1. the grace starts when the controller first sees a new process +// 1. the grace starts when the controller first records a new process // incarnation for the replica, and lasts at most PrimaryBootGrace -// (the master wires it to its freshness window); +// (the master caps it at its freshness window); // 2. it covers only "not ready yet": the replica must stay placeable // (reachable, eligible, not withdrawn, on a reachable eligible // server). Any explicit unhealthy report fails over at once; @@ -23,42 +23,58 @@ import "time" // (PrimaryBootFlapRestarts incarnations within // PrimaryBootFlapWindow). // -// A replica that reports no incarnation (an older volume binary) gets -// no grace. All state is guarded by TopologyController.mu. +// Ordering: a fact changes the history only if its ObservedAt is +// strictly newer than the last fact recorded for that replica, so a +// delayed or superseded snapshot can never count a restart, reset a +// grace, or end one. A slot without an incarnation (an older volume +// binary) or without an observation time gets no grace. All state is +// guarded by TopologyController.mu. type primaryBootGrace struct { - incarnation string - graceStart time.Time - ended bool // reported ready, expired, or never granted - starts []time.Time // incarnation starts within the flap window + incarnation string + lastObservedAt time.Time // newest fact recorded for this replica + graceStart time.Time // controller clock when the incarnation was first recorded + ended bool // reported ready, expired, or never granted + starts []time.Time // incarnation starts within the flap window } func bootGraceKey(volumeID, replicaID string) string { return volumeID + "/" + replicaID } -// observeIncarnationsLocked records each slot's incarnation. It is the -// only writer of bootGrace and runs once per supported snapshot. +// observeIncarnationsLocked records each slot's boot fact. func (c *TopologyController) observeIncarnationsLocked(vol VolumeTopologySnapshot) { - now := c.now() for _, slot := range vol.Slots { - if slot.Incarnation == "" { - continue - } - key := bootGraceKey(vol.VolumeID, slot.ReplicaID) - g := c.bootGrace[key] - if g == nil || g.incarnation != slot.Incarnation { - g = c.startIncarnation(g, slot.Incarnation, now) - c.bootGrace[key] = g - } - if slot.ReadyForPrimary { - g.ended = true - } + c.recordBootFactLocked(vol.VolumeID, slot) } } -// startIncarnation returns the grace record for a newly seen -// incarnation: its start is counted, and the grace is refused when -// that makes the replica a flapper. -func (c *TopologyController) startIncarnation(prev *primaryBootGrace, incarnation string, now time.Time) *primaryBootGrace { +// recordBootFactLocked is the only writer of bootGrace. +func (c *TopologyController) recordBootFactLocked(volumeID string, slot ReplicaCandidate) { + if slot.Incarnation == "" || slot.ObservedAt.IsZero() { + return + } + key := bootGraceKey(volumeID, slot.ReplicaID) + g := c.bootGrace[key] + switch { + case g == nil: + g = c.startIncarnation(nil, slot.Incarnation) + c.bootGrace[key] = g + case !slot.ObservedAt.After(g.lastObservedAt): + return // not newer than what is recorded: never changes history + case g.incarnation != slot.Incarnation: + g = c.startIncarnation(g, slot.Incarnation) + c.bootGrace[key] = g + } + g.lastObservedAt = slot.ObservedAt + if slot.ReadyForPrimary { + g.ended = true + } +} + +// startIncarnation returns the grace record for a newly recorded +// incarnation: its start is counted, and the grace is refused when that +// makes the replica a flapper. +func (c *TopologyController) startIncarnation(prev *primaryBootGrace, incarnation string) *primaryBootGrace { + now := c.now() var starts []time.Time if prev != nil { for _, t := range prev.starts { @@ -93,23 +109,20 @@ func (c *TopologyController) inPrimaryBootGraceLocked(volumeID string, current R return true } -// PrimaryInBootGrace reports whether a direct reassignment away from -// the current primary must wait: the slot is, or on this observation -// would become, inside its boot grace. It does not record anything. -// The master's promotion gate calls it before a direct reassign; the -// gate holds no controller-visible lock while calling. -func (c *TopologyController) PrimaryInBootGrace(volumeID string, current ReplicaCandidate) bool { +// PrimaryBootGraceHolds records the current primary's fresh slot fact +// (ordered, as above) and reports whether a reassignment away from it +// must wait: the slot is placeable by its own report and inside its +// boot grace. The caller passes the replica's own fresh heartbeat slot +// with its server's reachability and eligibility folded in, never a +// slot synthesized from placement. The master's promotion gates call it +// while holding no lock the controller takes. +func (c *TopologyController) PrimaryBootGraceHolds(volumeID string, current ReplicaCandidate) bool { c.mu.Lock() defer c.mu.Unlock() - if current.Incarnation == "" || current.ReadyForPrimary { + if !current.Reachable || !current.Eligible || current.Withdrawn { return false } - g := c.bootGrace[bootGraceKey(volumeID, current.ReplicaID)] - if g == nil || g.incarnation != current.Incarnation { - // Not seen yet: the grace this observation would start. - next := c.startIncarnation(g, current.Incarnation, c.now()) - return !next.ended - } + c.recordBootFactLocked(volumeID, current) return c.inPrimaryBootGraceLocked(volumeID, current) } @@ -119,3 +132,40 @@ func (c *TopologyController) PrimaryBootGrace() time.Duration { defer c.mu.Unlock() return c.config.PrimaryBootGrace } + +// FreshSlotCandidate returns the replica's own fresh heartbeat slot for +// the volume, with the reporting server's reachability and eligibility +// folded in. It reports false when no fresh slot exists, or when more +// than one fresh server claims the replica. +func (ss storeSnapshot) FreshSlotCandidate(volumeID, replicaID string) (ReplicaCandidate, bool) { + var out ReplicaCandidate + found := false + for id, obs := range ss.observations { + if ss.serverFreshness[id] != ServerFresh { + continue + } + for _, s := range obs.Slots { + if s.VolumeID != volumeID || s.ReplicaID != replicaID || slotExpired(s, obs, ss.evaluatedAt) { + continue + } + if found { + return ReplicaCandidate{}, false + } + found = true + out = ReplicaCandidate{ + ReplicaID: s.ReplicaID, + ServerID: obs.ServerID, + DataAddr: s.DataAddr, + CtrlAddr: s.CtrlAddr, + Reachable: s.Reachable && obs.Server.Reachable, + ReadyForPrimary: s.ReadyForPrimary, + Eligible: s.Eligible && obs.Server.Eligible, + Withdrawn: s.Withdrawn, + EvidenceScore: s.EvidenceScore, + Incarnation: s.Incarnation, + ObservedAt: slotObservedAt(s, obs), + } + } + } + return out, found +} diff --git a/seaweed-block/core/host/master/host.go b/seaweed-block/core/host/master/host.go index c1494ca35a..d09412acd4 100644 --- a/seaweed-block/core/host/master/host.go +++ b/seaweed-block/core/host/master/host.go @@ -179,10 +179,14 @@ func New(cfg Config) (*Host, error) { holder := &publisherHolder{} ctrlCfg := cfg.ControllerConfig - if ctrlCfg.PrimaryBootGrace == 0 && cfg.Freshness.FreshnessWindow > 0 { + if w := cfg.Freshness.FreshnessWindow; w > 0 && (ctrlCfg.PrimaryBootGrace == 0 || ctrlCfg.PrimaryBootGrace > w) { // A restarted primary may stay not-ready for at most the - // window in which its observations are trusted (#128). - ctrlCfg.PrimaryBootGrace = cfg.Freshness.FreshnessWindow + // window in which its observations are trusted (#128); a + // larger configured grace is capped, never widened. + if ctrlCfg.PrimaryBootGrace > w { + lg.Printf("blockmaster: primary boot grace %s capped at freshness window %s", ctrlCfg.PrimaryBootGrace, w) + } + ctrlCfg.PrimaryBootGrace = w } ctrl := authority.NewTopologyController(ctrlCfg, holder) diff --git a/seaweed-block/core/host/master/product_loop.go b/seaweed-block/core/host/master/product_loop.go index bdb5ab0fd1..9b830cf4f8 100644 --- a/seaweed-block/core/host/master/product_loop.go +++ b/seaweed-block/core/host/master/product_loop.go @@ -183,7 +183,7 @@ func (h *Host) applyPromotionEvidenceGate(snap *authority.ClusterSnapshot) (int, slot.ReadyForPrimary = false } } - if enabledCandidate && result.CurrentKnown && !result.Current.Ready && primaryBootGraceHolds(h.ctrl, vol.VolumeID, vol.Slots[currentIdx]) { + if enabledCandidate && result.CurrentKnown && !result.Current.Ready && h.primaryBootGraceHolds(vol.VolumeID, withServerFacts(vol.Slots[currentIdx], serverIndex)) { h.log.Printf("blockmaster: promotion gate volume=%s current=%s not ready but in boot grace; no direct reassign", vol.VolumeID, vol.Authority.ReplicaID) } else if enabledCandidate && result.CurrentKnown && !result.Current.Ready { current := &vol.Slots[currentIdx] @@ -259,6 +259,13 @@ func (h *Host) applyUnsupportedPromotionEvidenceGate(placements []lifecycle.Plac } } if enabledCandidate && (!result.CurrentKnown || !result.Current.Ready) { + // Unsupported does not mean the current primary is unhealthy: + // a different slot may be stale. Judge the current primary + // from its own fresh heartbeat slot, never from placement. + if cur, ok := h.freshCurrentSlot(vol.VolumeID, current.ReplicaID); ok && h.primaryBootGraceHolds(vol.VolumeID, cur) { + h.log.Printf("blockmaster: promotion gate unsupported volume=%s current=%s not ready but in boot grace; no direct reassign", vol.VolumeID, current.ReplicaID) + continue + } if target, ok := chooseAllowedPromotionTarget(vol, result, allowed); ok { h.log.Printf("blockmaster: promotion gate unsupported volume=%s direct_reassign=%s current=%s", vol.VolumeID, target.ReplicaID, current.ReplicaID) directAsks = append(directAsks, authority.AssignmentAsk{ @@ -565,13 +572,31 @@ func (h *Host) assignmentAskAlreadyCurrent(volumeID, replicaID, dataAddr, ctrlAd line.CtrlAddr == ctrlAddr } -// primaryBootGraceHolds reports whether the current primary's slot is -// placeable by its own report and inside its boot grace (#128): a -// restarted primary that is merely not ready yet. An explicit unhealthy +// primaryBootGraceHolds reports whether the current primary is a +// restarted primary that is merely not ready yet: placeable by its own +// fresh report and inside its boot grace (#128). An explicit unhealthy // report (unreachable, ineligible, withdrawn) never holds. -func primaryBootGraceHolds(ctrl *authority.TopologyController, volumeID string, current authority.ReplicaCandidate) bool { - if ctrl == nil || !current.Reachable || !current.Eligible || current.Withdrawn { +func (h *Host) primaryBootGraceHolds(volumeID string, current authority.ReplicaCandidate) bool { + if h.ctrl == nil { return false } - return ctrl.PrimaryInBootGrace(volumeID, current) + return h.ctrl.PrimaryBootGraceHolds(volumeID, current) +} + +// freshCurrentSlot returns the replica's own fresh heartbeat slot from +// the observation store (server facts folded in). +func (h *Host) freshCurrentSlot(volumeID, replicaID string) (authority.ReplicaCandidate, bool) { + if h.obs == nil { + return authority.ReplicaCandidate{}, false + } + return h.obs.Store().Snapshot().FreshSlotCandidate(volumeID, replicaID) +} + +// withServerFacts folds the reporting server's reachability and +// eligibility into a slot; a server absent from the index is neither. +func withServerFacts(slot authority.ReplicaCandidate, servers map[string]authority.ServerObservation) authority.ReplicaCandidate { + srv, ok := servers[slot.ServerID] + slot.Reachable = slot.Reachable && ok && srv.Reachable + slot.Eligible = slot.Eligible && ok && srv.Eligible + return slot }