diff --git a/seaweed-block/core/authority/controller.go b/seaweed-block/core/authority/controller.go index ebb8731837..3fd174be68 100644 --- a/seaweed-block/core/authority/controller.go +++ b/seaweed-block/core/authority/controller.go @@ -230,7 +230,7 @@ func (c *TopologyController) processSupportedVolumeLocked( // S4 behavior; stuck evidence has its own lifecycle and is // NOT cleared here — §10 clearing rule 4.) delete(c.unsupported, vol.VolumeID) - c.observeIncarnationsLocked(vol) + c.observeIncarnationsLocked(vol, serverIndex) c.confirmDesiredLocked(vol, currentLine, hasLine) @@ -497,8 +497,8 @@ func (c *TopologyController) decideVolume( // 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 candidatePlaceable(current, serverIndex) && c.inPrimaryBootGraceLocked(vol.VolumeID, current) { - if current.DataAddr != currentLine.DataAddr || current.CtrlAddr != currentLine.CtrlAddr { + if hold, superseded := c.primaryBootGraceLocked(vol.VolumeID, current, candidatePlaceable(current, serverIndex)); hold { + if !superseded && (current.DataAddr != currentLine.DataAddr || current.CtrlAddr != currentLine.CtrlAddr) { return AssignmentAsk{ VolumeID: vol.VolumeID, ReplicaID: current.ReplicaID, diff --git a/seaweed-block/core/authority/observation_types.go b/seaweed-block/core/authority/observation_types.go index 63951be9a3..772d38c69d 100644 --- a/seaweed-block/core/authority/observation_types.go +++ b/seaweed-block/core/authority/observation_types.go @@ -125,6 +125,12 @@ type FreshnessConfig struct { PendingGrace time.Duration } +// EffectiveFreshnessWindow is the window the observation store actually +// uses: the configured one, or its default when omitted or nonpositive. +func (c FreshnessConfig) EffectiveFreshnessWindow() time.Duration { + return c.withDefaults().FreshnessWindow +} + func (c FreshnessConfig) withDefaults() FreshnessConfig { out := c if out.FreshnessWindow <= 0 { diff --git a/seaweed-block/core/authority/primary_boot_grace.go b/seaweed-block/core/authority/primary_boot_grace.go index 06d020132e..f1cd498f68 100644 --- a/seaweed-block/core/authority/primary_boot_grace.go +++ b/seaweed-block/core/authority/primary_boot_grace.go @@ -26,13 +26,16 @@ import "time" // 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 +// grace, or end one. A superseded fact (older, or equal-time with a +// different incarnation) is not a decision input either: the decision +// follows the newest recorded fact, and its endpoint is never used. 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 lastObservedAt time.Time // newest fact recorded for this replica + lastPlaceable bool // whether that newest fact was placeable 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 @@ -41,14 +44,14 @@ type primaryBootGrace struct { func bootGraceKey(volumeID, replicaID string) string { return volumeID + "/" + replicaID } // observeIncarnationsLocked records each slot's boot fact. -func (c *TopologyController) observeIncarnationsLocked(vol VolumeTopologySnapshot) { +func (c *TopologyController) observeIncarnationsLocked(vol VolumeTopologySnapshot, servers map[string]ServerObservation) { for _, slot := range vol.Slots { - c.recordBootFactLocked(vol.VolumeID, slot) + c.recordBootFactLocked(vol.VolumeID, slot, candidatePlaceable(slot, servers)) } } // recordBootFactLocked is the only writer of bootGrace. -func (c *TopologyController) recordBootFactLocked(volumeID string, slot ReplicaCandidate) { +func (c *TopologyController) recordBootFactLocked(volumeID string, slot ReplicaCandidate, placeable bool) { if slot.Incarnation == "" || slot.ObservedAt.IsZero() { return } @@ -65,6 +68,7 @@ func (c *TopologyController) recordBootFactLocked(volumeID string, slot ReplicaC c.bootGrace[key] = g } g.lastObservedAt = slot.ObservedAt + g.lastPlaceable = placeable if slot.ReadyForPrimary { g.ended = true } @@ -92,14 +96,34 @@ func (c *TopologyController) startIncarnation(prev *primaryBootGrace, incarnatio } } -// inPrimaryBootGraceLocked reports whether the current primary's slot -// is inside its boot grace now. Expiry ends the grace for good. -func (c *TopologyController) inPrimaryBootGraceLocked(volumeID string, current ReplicaCandidate) bool { - if current.Incarnation == "" || current.ReadyForPrimary { - return false +// primaryBootGraceLocked decides whether the current primary, described +// by fact, holds its authority through a boot grace. hold is true when +// it does; superseded reports that fact is older than the newest +// recorded one (or equal-time with another incarnation), in which case +// the decision follows the recorded fact and the caller must not use +// fact's endpoint. +func (c *TopologyController) primaryBootGraceLocked(volumeID string, fact ReplicaCandidate, placeable bool) (hold, superseded bool) { + if fact.Incarnation == "" { + return false, false } - g := c.bootGrace[bootGraceKey(volumeID, current.ReplicaID)] - if g == nil || g.incarnation != current.Incarnation || g.ended { + g := c.bootGrace[bootGraceKey(volumeID, fact.ReplicaID)] + if g == nil { + return false, false + } + if !fact.ObservedAt.IsZero() && (fact.ObservedAt.Before(g.lastObservedAt) || + (fact.ObservedAt.Equal(g.lastObservedAt) && fact.Incarnation != g.incarnation)) { + return g.lastPlaceable && c.graceActiveLocked(g), true + } + if !placeable || fact.ReadyForPrimary || g.incarnation != fact.Incarnation { + return false, false + } + return c.graceActiveLocked(g), false +} + +// graceActiveLocked reports whether g's grace is running. Expiry ends +// it for good. +func (c *TopologyController) graceActiveLocked(g *primaryBootGrace) bool { + if g.ended { return false } if c.now().Sub(g.graceStart) >= c.config.PrimaryBootGrace { @@ -119,11 +143,26 @@ func (c *TopologyController) inPrimaryBootGraceLocked(volumeID string, current R func (c *TopologyController) PrimaryBootGraceHolds(volumeID string, current ReplicaCandidate) bool { c.mu.Lock() defer c.mu.Unlock() - if !current.Reachable || !current.Eligible || current.Withdrawn { - return false - } - c.recordBootFactLocked(volumeID, current) - return c.inPrimaryBootGraceLocked(volumeID, current) + placeable := ownPlaceable(current) + c.recordBootFactLocked(volumeID, current, placeable) + hold, _ := c.primaryBootGraceLocked(volumeID, current, placeable) + return hold +} + +// RecordPrimaryBootFact records the current primary's fresh slot fact +// (ordered, as above) whether or not a promotion is being considered, +// so a ready report ends the grace and every start counts toward flap +// history. Same caller contract as PrimaryBootGraceHolds. +func (c *TopologyController) RecordPrimaryBootFact(volumeID string, current ReplicaCandidate) { + c.mu.Lock() + defer c.mu.Unlock() + c.recordBootFactLocked(volumeID, current, ownPlaceable(current)) +} + +// ownPlaceable reports placeability from a slot that already carries +// its server's reachability and eligibility. +func ownPlaceable(slot ReplicaCandidate) bool { + return slot.Reachable && slot.Eligible && !slot.Withdrawn } // PrimaryBootGrace returns the effective boot-grace bound. diff --git a/seaweed-block/core/host/master/host.go b/seaweed-block/core/host/master/host.go index d09412acd4..06a2cc816d 100644 --- a/seaweed-block/core/host/master/host.go +++ b/seaweed-block/core/host/master/host.go @@ -179,10 +179,12 @@ func New(cfg Config) (*Host, error) { holder := &publisherHolder{} ctrlCfg := cfg.ControllerConfig - if w := cfg.Freshness.FreshnessWindow; w > 0 && (ctrlCfg.PrimaryBootGrace == 0 || ctrlCfg.PrimaryBootGrace > w) { + if w := cfg.Freshness.EffectiveFreshnessWindow(); 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); a - // larger configured grace is capped, never widened. + // window in which its observations are trusted (#128): the + // store's effective window, also when the config omits it. A + // larger configured grace is capped, never widened; an unset + // or nonpositive one takes the window. if ctrlCfg.PrimaryBootGrace > w { lg.Printf("blockmaster: primary boot grace %s capped at freshness window %s", ctrlCfg.PrimaryBootGrace, w) } diff --git a/seaweed-block/core/host/master/product_loop.go b/seaweed-block/core/host/master/product_loop.go index 9b830cf4f8..3262193f5b 100644 --- a/seaweed-block/core/host/master/product_loop.go +++ b/seaweed-block/core/host/master/product_loop.go @@ -226,6 +226,14 @@ func (h *Host) applyUnsupportedPromotionEvidenceGate(placements []lifecycle.Plac if !ok || !current.Assigned { continue } + // Record the current primary's own fresh slot fact on every + // pass, not only when a promotion is considered, so a ready + // report ends its boot grace and every start counts toward + // flap history (#128). + cur, curFresh := h.freshCurrentSlot(placement.VolumeID, current.ReplicaID) + if curFresh && h.ctrl != nil { + h.ctrl.RecordPrimaryBootFact(placement.VolumeID, cur) + } vol, ok := topologyFromUnsupportedPlacement(placement, current) if !ok { continue @@ -262,7 +270,7 @@ func (h *Host) applyUnsupportedPromotionEvidenceGate(placements []lifecycle.Plac // 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) { + if curFresh && 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 }