master: the line-wake test waits on a settled observation host, not a sleep

ObservationHost.BuildsForTest reports the builds completed and whether a
wake is buffered or a build is running. The test settles the host before
the heartbeat, requires the heartbeat's build to be a new one, settles
again after the controller's publication, and then requires the line in a
build after that point (or in the settled last build, which can only have
come after the publication).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-01 13:41:47 -08:00
1 parent 1cb5e0e124
commit 2caed63f04
2 files changed
+68 -14

No files matched your search

@@ -3,6 +3,7 @@ package authority
import (
"context"
"sync"
"sync/atomic"
"time"
)
@@ -57,8 +58,12 @@ type ObservationHost struct {
sink ControllerSink
reader AuthorityBasisReader
mu sync.Mutex
lastBuild *BuildResult
mu sync.Mutex
lastBuild *BuildResult
// builds counts completed rebuilds and building is set while one runs
// (BuildsForTest).
builds atomic.Uint64
building atomic.Bool
sinkErrors []error
wake chan struct{}
@@ -118,6 +123,13 @@ func NewObservationHost(cfg ObservationHostConfig) *ObservationHost {
return h
}
// BuildsForTest reports the rebuilds completed and whether the host is
// settled (no wake buffered, no rebuild running), so a test can order a
// change after every earlier build. Production code MUST NOT call this.
func (h *ObservationHost) BuildsForTest() (builds uint64, settled bool) {
return h.builds.Load(), len(h.wake) == 0 && !h.building.Load()
}
// Wake asks for a rebuild: a change a build reads that no heartbeat
// carries (a published authority line, a topology change). Non-blocking;
// wakes coalesce.
@@ -281,6 +293,11 @@ func (h *ObservationHost) run(ctx context.Context) {
// AND no report entries is a genuine no-op (e.g. start-up
// before any ingest) and is skipped.
func (h *ObservationHost) rebuildOnce() {
h.building.Store(true)
defer func() {
h.builds.Add(1)
h.building.Store(false)
}()
snap := h.store.Snapshot()
result := BuildSnapshot(snap, h.topology, h.reader)
@@ -38,28 +38,47 @@ func TestMasterRebuildsObservationOnAPublishedLine(t *testing.T) {
}
h.Start()
defer closeTestMaster(t, h)
waitBuild := func(what string, ok func(authority.BuildResult) bool) authority.BuildResult {
// settle waits until no wake is buffered and no rebuild runs, twice in a
// row with the same count (a deadline timer could fire in between), and
// returns the count.
settle := func(what string) uint64 {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for time.Now().Before(deadline) {
if b, have := h.obs.LastBuild(); have && ok(b) {
return b
n1, ok1 := h.obs.BuildsForTest()
time.Sleep(50 * time.Millisecond)
n2, ok2 := h.obs.BuildsForTest()
if ok1 && ok2 && n1 == n2 && n1 > 0 {
return n2
}
}
t.Fatalf("%s: the observation host did not settle", what)
return 0
}
waitAfter := func(what string, after uint64, ok func(authority.BuildResult) bool) uint64 {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for time.Now().Before(deadline) {
n, _ := h.obs.BuildsForTest()
if b, have := h.obs.LastBuild(); have && n > after && ok(b) {
return n
}
time.Sleep(10 * time.Millisecond)
}
b, _ := h.obs.LastBuild()
t.Fatalf("%s: last build %+v", what, b.Snapshot.Volumes)
return authority.BuildResult{}
t.Fatalf("%s: no build after #%d satisfied it; last build %+v", what, after, b.Snapshot.Volumes)
return 0
}
waitBuild("the initial build", func(authority.BuildResult) bool { return true })
time.Sleep(300 * time.Millisecond) // the grace's deadline fires and its rebuild drains
// The initial build and the 1 ms grace's rebuild, drained.
before := settle("startup")
if err := h.obs.IngestHeartbeat(authority.HeartbeatMessage{
ServerID: "node-a", SentAt: time.Now().UTC(), Reachable: true, Eligible: true,
Slots: []authority.HeartbeatSlot{{VolumeID: "v1", ReplicaID: "r1", DataAddr: "127.0.0.1:9201", CtrlAddr: "127.0.0.1:9101", Reachable: true, ReadyForPrimary: true, Eligible: true}},
}); err != nil {
t.Fatal(err)
}
waitBuild("the heartbeat's build (v1 supported)", func(b authority.BuildResult) bool {
// The heartbeat's build: a new build, v1 supported.
waitAfter("the heartbeat's build", before, func(b authority.BuildResult) bool {
for _, v := range b.Snapshot.Volumes {
if v.VolumeID == "v1" {
return true
@@ -67,11 +86,29 @@ func TestMasterRebuildsObservationOnAPublishedLine(t *testing.T) {
}
return false
})
waitBuild("the build after the controller's publication", func(b authority.BuildResult) bool {
line, ok := h.Publisher().VolumeAuthorityLine("v1")
if !ok || !line.Assigned {
return false
// The controller binds v1; its publication is the only wake left.
for wait := time.Now().Add(5 * time.Second); ; time.Sleep(10 * time.Millisecond) {
if line, ok := h.Publisher().VolumeAuthorityLine("v1"); ok && line.Assigned {
break
}
if time.Now().After(wait) {
t.Fatal("the controller did not bind v1")
}
}
line, _ := h.Publisher().VolumeAuthorityLine("v1")
// Settled again: any build the heartbeat's wake caused has run. If the
// last build already carries the line, it came after the publication,
// so from the hook (the heartbeat's single wake was consumed by a build
// that saw no line); otherwise a later build must bring it.
settled := settle("after the publication")
if b, have := h.obs.LastBuild(); have {
for _, v := range b.Snapshot.Volumes {
if v.VolumeID == "v1" && v.Authority.Assigned && v.Authority.Epoch == line.Epoch {
return
}
}
}
waitAfter("the build after the publication", settled, func(b authority.BuildResult) bool {
for _, v := range b.Snapshot.Volumes {
if v.VolumeID == "v1" && v.Authority.Assigned && v.Authority.Epoch == line.Epoch {
return true