control, authority: report a per-process incarnation; boot-grace controller tests (RED)

blockvolume reports a random incarnation, new on every process start, in
each heartbeat slot (proto field 12, regenerated with the checked-in
generator versions). It flows through the wire, observation and snapshot
layers to ReplicaCandidate.Incarnation as a raw fact. The controller
config gains PrimaryBootGrace and the flap bounds, unused here.

The new controller tests pin the #128 boot-grace contract and fail
against today's rule (a not-ready current primary with a ready peer
fails over at once): restart -> endpoint refresh; no ask inside the
grace; ready ends the grace; a flapper gets none. The explicit-unhealthy
and no-incarnation cases pass on both sides as controls.

Refs #128

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-15 11:10:32 -08:00
1 parent 6daca22e2f
commit fc10f148ff
9 files changed
+749 -526

No files matched your search

@@ -54,6 +54,9 @@ type SlotFact struct {
Eligible bool
Withdrawn bool
EvidenceScore uint64
// Incarnation identifies the reporting volume process (new on
// every process start); raw fact, empty from older binaries.
Incarnation string
// ObservedAt/ExpiresAt are store-owned per-slot freshness
// markers. They let one Kubernetes node run multiple
// blockvolume processes that each report a different slot
@@ -56,6 +56,7 @@ type HeartbeatSlot struct {
Eligible bool
Withdrawn bool
EvidenceScore uint64
Incarnation string
// LocalRoleClaim is the V2 "I think I am" role flag, mapped
// one-to-one into the V3 Observation shape without
// reinterpretation. Two servers each sending LocalRolePrimary
@@ -93,6 +94,7 @@ func HeartbeatToObservation(msg HeartbeatMessage) (Observation, error) {
Eligible: s.Eligible,
Withdrawn: s.Withdrawn,
EvidenceScore: s.EvidenceScore,
Incarnation: s.Incarnation,
LocalRoleClaim: s.LocalRoleClaim,
})
}
@@ -0,0 +1,163 @@
package authority
import (
"testing"
"time"
)
// Primary boot grace (#128): a restarted current primary that is placeable
// but not ready yet keeps its authority for one bounded grace.
type bootGraceRig struct {
t *testing.T
ctrl *TopologyController
now time.Time
rev uint64
}
const testBootGrace = 30 * time.Second
func newBootGraceRig(t *testing.T) *bootGraceRig {
reader := newFakeBasisReader()
reader.Set("v1", "r1", AuthorityBasis{
Assigned: true, ReplicaID: "r1", Epoch: 1, EndpointVersion: 1,
DataAddr: "d1-old", CtrlAddr: "c1-old",
})
g := &bootGraceRig{t: t, now: time.Unix(1000, 0)}
g.ctrl = NewTopologyController(TopologyControllerConfig{PrimaryBootGrace: testBootGrace}, reader)
g.ctrl.SetNowForTest(func() time.Time { return g.now })
return g
}
// r1 is the current primary; r2 and r3 are ready peers.
func (g *bootGraceRig) submit(r1 ReplicaCandidate) {
g.t.Helper()
g.rev++
r2 := candidate("r2", "s2", "d2", "c2", 20)
r2.Incarnation = "r2-a"
r3 := candidate("r3", "s3", "d3", "c3", 10)
r3.Incarnation = "r3-a"
snap := ClusterSnapshot{
CollectedRevision: g.rev,
Servers: []ServerObservation{
{ServerID: "s1", Reachable: true, Eligible: true},
{ServerID: "s2", Reachable: true, Eligible: true},
{ServerID: "s3", Reachable: true, Eligible: true},
},
Volumes: []VolumeTopologySnapshot{clusterVolume("v1",
AuthorityBasis{Assigned: true, ReplicaID: "r1", Epoch: 1, EndpointVersion: 1, DataAddr: "d1-old", CtrlAddr: "c1-old"},
r1, r2, r3)},
}
if err := g.ctrl.SubmitClusterSnapshot(snap); err != nil {
g.t.Fatalf("SubmitClusterSnapshot: %v", err)
}
}
func r1Slot(incarnation, dataAddr, ctrlAddr string, ready bool) ReplicaCandidate {
c := candidate("r1", "s1", dataAddr, ctrlAddr, 30)
c.Incarnation = incarnation
c.ReadyForPrimary = ready
return c
}
// The running primary, ready at its old endpoint: nothing to do.
func (g *bootGraceRig) steadyPrimary() {
g.t.Helper()
g.submit(r1Slot("r1-a", "d1-old", "c1-old", true))
expectNoNext(g.t, g.ctrl)
}
func expectFailoverToR2(t *testing.T, ctrl *TopologyController, why string) {
t.Helper()
ask := nextAskOrFail(t, ctrl)
if ask.Intent != IntentReassign || ask.ReplicaID != "r2" {
t.Fatalf("%s: want failover to r2, got %+v", why, ask)
}
}
// RED today: a restart with new addresses fails over at once. Within the
// grace the primary keeps its authority and only its endpoint refreshes.
func TestPrimaryBootGrace_RestartWithReadyPeerRefreshesEndpoint(t *testing.T) {
g := newBootGraceRig(t)
g.steadyPrimary()
g.now = g.now.Add(time.Second)
g.submit(r1Slot("r1-b", "d1-new", "c1-new", false))
ask := nextAskOrFail(t, g.ctrl)
if ask.Intent != IntentRefreshEndpoint || ask.ReplicaID != "r1" || ask.DataAddr != "d1-new" || ask.CtrlAddr != "c1-new" {
t.Fatalf("restarted primary inside its boot grace: want endpoint refresh on r1, got %+v", ask)
}
}
// Same endpoint, not ready: no ask during the grace; still unready after
// it expires -> failover to the ready peer.
func TestPrimaryBootGrace_StaysUnreadyPastGraceFailsOver(t *testing.T) {
g := newBootGraceRig(t)
g.steadyPrimary()
g.submit(r1Slot("r1-b", "d1-old", "c1-old", false))
expectNoNext(t, g.ctrl)
g.now = g.now.Add(testBootGrace - time.Second)
g.submit(r1Slot("r1-b", "d1-old", "c1-old", false))
expectNoNext(t, g.ctrl)
g.now = g.now.Add(2 * time.Second)
g.submit(r1Slot("r1-b", "d1-old", "c1-old", false))
expectFailoverToR2(t, g.ctrl, "unready past the boot grace")
}
// An explicit unhealthy report gets no grace: immediate failover.
func TestPrimaryBootGrace_ExplicitUnhealthyFailsOverAtOnce(t *testing.T) {
for _, tc := range []struct {
name string
mut func(*ReplicaCandidate)
}{
{"ineligible", func(c *ReplicaCandidate) { c.Eligible = false }},
{"unreachable", func(c *ReplicaCandidate) { c.Reachable = false }},
{"withdrawn", func(c *ReplicaCandidate) { c.Withdrawn = true }},
} {
t.Run(tc.name, func(t *testing.T) {
g := newBootGraceRig(t)
g.steadyPrimary()
s := r1Slot("r1-b", "d1-old", "c1-old", false)
tc.mut(&s)
g.submit(s)
expectFailoverToR2(t, g.ctrl, tc.name+" restarted primary")
})
}
}
// Ready ends the grace for good: the same incarnation going unready
// again fails over at once.
func TestPrimaryBootGrace_ReadyEndsGrace(t *testing.T) {
g := newBootGraceRig(t)
g.steadyPrimary()
g.submit(r1Slot("r1-b", "d1-old", "c1-old", false))
expectNoNext(t, g.ctrl)
g.now = g.now.Add(time.Second)
g.submit(r1Slot("r1-b", "d1-old", "c1-old", true))
expectNoNext(t, g.ctrl)
g.now = g.now.Add(time.Second)
g.submit(r1Slot("r1-b", "d1-old", "c1-old", false))
expectFailoverToR2(t, g.ctrl, "unready again after ready in the same incarnation")
}
// A flapping primary (3 process starts within the flap window) gets no
// grace.
func TestPrimaryBootGrace_FlappingPrimaryGetsNoGrace(t *testing.T) {
g := newBootGraceRig(t)
g.steadyPrimary() // start 1
g.now = g.now.Add(time.Second)
g.submit(r1Slot("r1-b", "d1-old", "c1-old", false)) // start 2: grace
expectNoNext(t, g.ctrl)
g.now = g.now.Add(time.Second)
g.submit(r1Slot("r1-b", "d1-old", "c1-old", true))
g.now = g.now.Add(time.Second)
g.submit(r1Slot("r1-c", "d1-old", "c1-old", false)) // start 3: flapping
expectFailoverToR2(t, g.ctrl, "third restart within the flap window")
}
// A primary that reports no incarnation (older binary) keeps today's
// rule: not ready with a ready peer fails over.
func TestPrimaryBootGrace_NoIncarnationNoGrace(t *testing.T) {
g := newBootGraceRig(t)
g.submit(r1Slot("", "d1-old", "c1-old", false))
expectFailoverToR2(t, g.ctrl, "no incarnation")
}
@@ -447,6 +447,7 @@ func assembleVolumeTopology(
Eligible: f.slot.Eligible,
Withdrawn: f.slot.Withdrawn,
EvidenceScore: f.slot.EvidenceScore,
Incarnation: f.slot.Incarnation,
})
}
+20
View File
@@ -29,6 +29,9 @@ type ReplicaCandidate struct {
Eligible bool
Withdrawn bool
EvidenceScore uint64
// Incarnation identifies the reporting volume process; it changes
// on every process start. Empty from older volume binaries.
Incarnation string
}
// VolumeTopologySnapshot is the complete point-in-time topology and
@@ -64,6 +67,14 @@ type TopologyControllerConfig struct {
ExpectedSlotsPerVolume int
RebalanceSkew int
RetryWindow time.Duration
// PrimaryBootGrace bounds how long a restarted current primary is
// kept while it reports not-ready (see primary_boot_grace.go).
// The master sets it to its observation freshness window.
PrimaryBootGrace time.Duration
// PrimaryBootFlapRestarts process starts within
// PrimaryBootFlapWindow mark a replica as flapping: no grace.
PrimaryBootFlapRestarts int
PrimaryBootFlapWindow time.Duration
}
func (c TopologyControllerConfig) withDefaults() TopologyControllerConfig {
@@ -76,6 +87,15 @@ func (c TopologyControllerConfig) withDefaults() TopologyControllerConfig {
if c.RetryWindow == 0 {
c.RetryWindow = 5 * time.Second
}
if c.PrimaryBootGrace == 0 {
c.PrimaryBootGrace = 10 * time.Second // the observation store's default freshness window
}
if c.PrimaryBootFlapRestarts == 0 {
c.PrimaryBootFlapRestarts = 3
}
if c.PrimaryBootFlapWindow == 0 {
c.PrimaryBootFlapWindow = 10 * time.Minute
}
return c
}
@@ -659,6 +659,7 @@ func heartbeatFromWire(r *control.HeartbeatReport) authority.HeartbeatMessage {
Eligible: s.Eligible,
Withdrawn: s.Withdrawn,
EvidenceScore: s.EvidenceScore,
Incarnation: s.Incarnation,
LocalRoleClaim: authority.LocalRoleClaim(s.LocalRoleClaim),
})
}
+16
View File
@@ -2,6 +2,8 @@ package volume
import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"log"
@@ -91,6 +93,9 @@ type replicaSetUpdater interface {
// Host is the composed volume-side block product daemon.
type Host struct {
cfg Config
// incarnation is new on every process start and reported in every
// heartbeat, so the master can tell a restart from a long silence.
incarnation string
log *log.Logger
exec *noopExecutor
t1exec *HealthyPathExecutor
@@ -233,6 +238,7 @@ func New(cfg Config) (*Host, error) {
h := &Host{
cfg: cfg,
incarnation: newIncarnation(),
log: lg,
exec: noopExec,
t1exec: t1Exec,
@@ -583,11 +589,21 @@ func (h *Host) buildReport() *control.HeartbeatReport {
Reachable: true,
ReadyForPrimary: h.readyForPrimaryFact(),
Eligible: true,
Incarnation: h.incarnation,
},
},
}
}
// newIncarnation returns a random process incarnation id.
func newIncarnation() string {
var b [8]byte
if _, err := rand.Read(b[:]); err != nil {
return fmt.Sprintf("t%x", time.Now().UnixNano())
}
return hex.EncodeToString(b[:])
}
// readyForPrimaryFact is the volume-side promotion-readiness fact used by the
// authority controller for failover/rebalance candidates. A live process or
// Kubernetes Ready pod is not enough; the adapter must expose an engine
File diff suppressed because it is too large. Load diff
@@ -122,6 +122,10 @@ message HeartbeatSlot {
uint64 evidence_score = 9;
uint32 local_role_claim = 10; // authority.LocalRoleClaim enum value
repeated FrontendTarget frontends = 11;
// incarnation identifies the reporting volume process; it is new on
// every process start. Raw observation fact: the controller uses it
// only to bound a restarted primary's boot grace.
string incarnation = 12;
}
// FrontendTarget — read-only frontend publication fact reported by