mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
authority: a counter handshake orders the master's line-wake test
The observation host counts the rebuilds it takes (in the step that takes the wake) and the rebuilds it finishes, and records when its armed deadline timer fires, all under its lock; BuildsForTest returns them with whether a wake is buffered. The master test settles on started == done with no buffered wake and no timer due, records N, ingests one heartbeat (build N+1, v1 supported and unbound), and after the controller's publication requires build N+2 to carry the line: only the hook can take it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2caed63f04
commit
db7f6530a3
2 files changed
+66
-53
No files matched your search
@@ -3,7 +3,6 @@ package authority
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -60,11 +59,12 @@ type ObservationHost struct {
|
||||
|
||||
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
|
||||
// buildsStarted counts rebuilds the loop took (in the step that takes
|
||||
// their wake), buildsDone the rebuilds finished, timerAt when the armed deadline
|
||||
// timer fires (zero: none); all under mu (BuildsForTest).
|
||||
buildsStarted, buildsDone uint64
|
||||
timerAt time.Time
|
||||
sinkErrors []error
|
||||
|
||||
wake chan struct{}
|
||||
stop chan struct{}
|
||||
@@ -123,11 +123,22 @@ 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()
|
||||
// BuildsForTest reports, under one lock, the rebuilds the loop took and
|
||||
// finished, whether a wake is buffered, and when the armed deadline timer
|
||||
// fires (zero: none), so a test can order a change after every earlier
|
||||
// build. Production code MUST NOT call this.
|
||||
func (h *ObservationHost) BuildsForTest() (started, done uint64, wakeBuffered bool, timerAt time.Time) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
return h.buildsStarted, h.buildsDone, len(h.wake) > 0, h.timerAt
|
||||
}
|
||||
|
||||
// takeBuild counts a rebuild the loop takes, in the step that takes it.
|
||||
func (h *ObservationHost) takeBuild() {
|
||||
h.mu.Lock()
|
||||
h.buildsStarted++
|
||||
h.timerAt = time.Time{}
|
||||
h.mu.Unlock()
|
||||
}
|
||||
|
||||
// Wake asks for a rebuild: a change a build reads that no heartbeat
|
||||
@@ -237,6 +248,7 @@ func (h *ObservationHost) run(ctx context.Context) {
|
||||
|
||||
// First build on start — so consumers see an initial empty
|
||||
// snapshot rather than waiting for the first ingest.
|
||||
h.takeBuild()
|
||||
h.rebuildOnce()
|
||||
|
||||
for {
|
||||
@@ -252,6 +264,9 @@ func (h *ObservationHost) run(ctx context.Context) {
|
||||
}
|
||||
timer = time.NewTimer(d)
|
||||
deadlineC = timer.C
|
||||
h.mu.Lock()
|
||||
h.timerAt = time.Now().Add(d)
|
||||
h.mu.Unlock()
|
||||
}
|
||||
stopTimer := func() {
|
||||
if timer != nil {
|
||||
@@ -267,10 +282,16 @@ func (h *ObservationHost) run(ctx context.Context) {
|
||||
return
|
||||
case <-h.wake:
|
||||
stopTimer()
|
||||
h.takeBuild()
|
||||
h.rebuildOnce()
|
||||
case <-deadlineC:
|
||||
if h.store.ExpireDue() {
|
||||
h.takeBuild()
|
||||
h.rebuildOnce()
|
||||
} else {
|
||||
h.mu.Lock()
|
||||
h.timerAt = time.Time{}
|
||||
h.mu.Unlock()
|
||||
}
|
||||
case <-gcC:
|
||||
stopTimer()
|
||||
@@ -293,10 +314,10 @@ 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)
|
||||
h.mu.Lock()
|
||||
h.buildsDone++
|
||||
h.mu.Unlock()
|
||||
}()
|
||||
snap := h.store.Snapshot()
|
||||
result := BuildSnapshot(snap, h.topology, h.reader)
|
||||
|
||||
@@ -38,47 +38,57 @@ func TestMasterRebuildsObservationOnAPublishedLine(t *testing.T) {
|
||||
}
|
||||
h.Start()
|
||||
defer closeTestMaster(t, h)
|
||||
// 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 waits until every build the loop took has finished, no wake is
|
||||
// buffered and no deadline timer fires within the test (the one-hour
|
||||
// expiry timer stays armed), all read under the host's lock; it returns
|
||||
// the builds done.
|
||||
settle := func(what string) uint64 {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
n1, ok1 := h.obs.BuildsForTest()
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
n2, ok2 := h.obs.BuildsForTest()
|
||||
if ok1 && ok2 && n1 == n2 && n1 > 0 {
|
||||
return n2
|
||||
started, done, buffered, timerAt := h.obs.BuildsForTest()
|
||||
if started == done && done > 0 && !buffered && (timerAt.IsZero() || timerAt.After(time.Now().Add(time.Minute))) {
|
||||
return done
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("%s: the observation host did not settle", what)
|
||||
return 0
|
||||
}
|
||||
waitAfter := func(what string, after uint64, ok func(authority.BuildResult) bool) uint64 {
|
||||
waitDone := func(what string, n uint64, ok func(authority.BuildResult) bool) {
|
||||
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
|
||||
_, done, _, _ := h.obs.BuildsForTest()
|
||||
if b, have := h.obs.LastBuild(); have && done >= n && ok(b) {
|
||||
return
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
b, _ := h.obs.LastBuild()
|
||||
t.Fatalf("%s: no build after #%d satisfied it; last build %+v", what, after, b.Snapshot.Volumes)
|
||||
return 0
|
||||
t.Fatalf("%s: no build #%d satisfied it; last build %+v", what, n, b.Snapshot.Volumes)
|
||||
}
|
||||
// The initial build and the 1 ms grace's rebuild, drained.
|
||||
before := settle("startup")
|
||||
carriesLine := func(epoch uint64) func(authority.BuildResult) bool {
|
||||
return func(b authority.BuildResult) bool {
|
||||
for _, v := range b.Snapshot.Volumes {
|
||||
if v.VolumeID == "v1" && v.Authority.Assigned && v.Authority.Epoch == epoch {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
// The initial build and the 1 ms grace's rebuild, finished.
|
||||
n := 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)
|
||||
}
|
||||
// The heartbeat's build: a new build, v1 supported.
|
||||
waitAfter("the heartbeat's build", before, func(b authority.BuildResult) bool {
|
||||
// The heartbeat's one build (#n+1) shows v1 supported and unbound: the
|
||||
// controller binds only after it.
|
||||
waitDone("the heartbeat's build", n+1, func(b authority.BuildResult) bool {
|
||||
for _, v := range b.Snapshot.Volumes {
|
||||
if v.VolumeID == "v1" {
|
||||
return true
|
||||
@@ -86,7 +96,6 @@ func TestMasterRebuildsObservationOnAPublishedLine(t *testing.T) {
|
||||
}
|
||||
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
|
||||
@@ -96,24 +105,7 @@ func TestMasterRebuildsObservationOnAPublishedLine(t *testing.T) {
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
// Every build after the heartbeat's was taken after the publication (no
|
||||
// other wake was left): the hook's. One must come and carry the line.
|
||||
waitDone("the hook's build", n+2, carriesLine(line.Epoch))
|
||||
}
|
||||
Reference in new issue
Block a user