diff --git a/seaweed-block/core/host/volume/workspace_first_heartbeat_test.go b/seaweed-block/core/host/volume/workspace_first_heartbeat_test.go index f462c415e4..9317174daa 100644 --- a/seaweed-block/core/host/volume/workspace_first_heartbeat_test.go +++ b/seaweed-block/core/host/volume/workspace_first_heartbeat_test.go @@ -84,7 +84,8 @@ func (s *orderServer) renewed() bool { // runtime may start at once) against s: its heartbeat loop and its lease // runtime. started is when the lease runtime was started; done closes when it // returns (any number of readers). Cleanup cancels the runtime and joins it -// within a bound, before the host and the server close. +// within a bound, before the host and the server close; a failed join stops +// the test binary (joinOrFailStop), so nothing is torn down under it. func firstHeartbeatRig(t *testing.T, s *orderServer) (h *Host, started time.Time, done <-chan struct{}) { t.Helper() ln, err := net.Listen("tcp", "127.0.0.1:0") @@ -107,15 +108,41 @@ func firstHeartbeatRig(t *testing.T, s *orderServer) (h *Host, started time.Time }() t.Cleanup(func() { cancel() - select { - case <-ch: - case <-time.After(10 * time.Second): - t.Error("the lease runtime did not return within 10s of its cancellation") - } + joinOrFailStop(ch, 10*time.Second, func(msg string) { panic(msg) }) }) return h, started, ch } +// joinOrFailStop waits for done within bound. On timeout it calls failStop, +// which must not return (the rig's panics and stops the test binary): the +// cleanups after it would close the host and the server, and restore shared +// settings, under a lease runtime that is still running. +func joinOrFailStop(done <-chan struct{}, bound time.Duration, failStop func(string)) { + select { + case <-done: + case <-time.After(bound): + failStop("the lease runtime did not return within " + bound.String() + " of its cancellation; refusing teardown under a live worker") + } +} + +// The negative path of the rig's join: a worker that never returns reaches +// failStop with the cause, after the bound and not before. +func TestAFailedRuntimeJoinFailStopsBeforeTeardown(t *testing.T) { + never := make(chan struct{}) + start := time.Now() + var cause string + joinOrFailStop(never, 50*time.Millisecond, func(msg string) { cause = msg }) + if !strings.Contains(cause, "refusing teardown under a live worker") { + t.Fatalf("a worker that never returned did not reach failStop: %q", cause) + } + if time.Since(start) < 50*time.Millisecond { + t.Fatalf("failStop came before the %s bound", 50*time.Millisecond) + } + done := make(chan struct{}) + close(done) + joinOrFailStop(done, time.Second, func(msg string) { t.Fatalf("a returned worker reached failStop: %s", msg) }) +} + // Identity check (1) of the v1.1.2 bound: a v1.1 holder's first lease // renewal on start follows its first ANSWERED heartbeat, which names its // incarnation. Until then the master holds only its predecessor's report for @@ -149,27 +176,45 @@ func TestAHolderRenewsOnlyAfterItsFirstHeartbeatWithItsIncarnation(t *testing.T) // A holder whose first heartbeat is never answered does not renew and does // not serve: the lease runtime waits the configured bound (one lease -// validity; shortened here), no less, then installs its guard fenced and -// returns. +// validity; shortened here), no less and not much more, then installs its +// guard fenced and returns. The wait is measured from its own start (a test +// hook), to its return; the tolerance covers the runtime's 50 ms poll and +// the fenced guard's install and drain. func TestAHolderWhoseHeartbeatIsNeverAnsweredFencesRatherThanServes(t *testing.T) { - const bound = 300 * time.Millisecond - previous := firstHeartbeatWait + const bound, tolerance = 300 * time.Millisecond, 500 * time.Millisecond + previous, previousHook := firstHeartbeatWait, firstHeartbeatWaitStarted + waitStarted := make(chan time.Time, 1) firstHeartbeatWait = bound - t.Cleanup(func() { firstHeartbeatWait = previous }) + firstHeartbeatWaitStarted = func(at time.Time) { + select { + case waitStarted <- at: + default: + } + } + t.Cleanup(func() { firstHeartbeatWait, firstHeartbeatWaitStarted = previous, previousHook }) s := newOrderServer() // never released - h, started, done := firstHeartbeatRig(t, s) + h, _, done := firstHeartbeatRig(t, s) select { case <-s.entered: case <-time.After(5 * time.Second): t.Fatal("the holder's first heartbeat never reached the master") } + var start time.Time select { + case start = <-waitStarted: + case <-time.After(5 * time.Second): + t.Fatal("the lease runtime never started waiting for the first answered heartbeat") + } + select { // suite protection only; the bound is checked below case <-done: case <-time.After(5 * time.Second): t.Fatal("the lease runtime kept waiting past the bound instead of fencing") } - if waited := time.Since(started); waited < bound { + switch waited := time.Since(start); { + case waited < bound: t.Fatalf("the lease runtime fenced after %s, before its %s bound", waited, bound) + case waited > bound+tolerance: + t.Fatalf("the lease runtime fenced after %s, past its %s bound plus %s", waited, bound, tolerance) } if s.renewed() { t.Fatalf("a holder with no answered heartbeat renewed: %v", s.snapshot()) diff --git a/seaweed-block/core/host/volume/workspace_runtime.go b/seaweed-block/core/host/volume/workspace_runtime.go index 130b9ef277..58c72e0ff4 100644 --- a/seaweed-block/core/host/volume/workspace_runtime.go +++ b/seaweed-block/core/host/volume/workspace_runtime.go @@ -39,6 +39,9 @@ func (h *Host) runWorkspaceLease(ctx context.Context, stop func(context.Context, if p.Epoch > 0 && !h.firstHeartbeatAnswered() { if heartbeatWaitStart.IsZero() { heartbeatWaitStart = time.Now() + if hook := firstHeartbeatWaitStarted; hook != nil { + hook(heartbeatWaitStart) + } } if time.Since(heartbeatWaitStart) >= firstHeartbeatWait { unanswered = fmt.Sprintf("no heartbeat of this process answered within %s; a renewal would be judged on another process's report", firstHeartbeatWait) @@ -86,6 +89,10 @@ func (h *Host) runWorkspaceLease(ctx context.Context, stop func(context.Context, // Tests shorten it. var firstHeartbeatWait = 30 * time.Second +// firstHeartbeatWaitStarted, when set (tests only), is told when the lease +// runtime starts waiting for the first answered heartbeat. +var firstHeartbeatWaitStarted func(time.Time) + // firstHeartbeatAnswered reports whether the lease runtime may renew: the // master has answered this process's first heartbeat (with its incarnation), // so a renewal is judged on this process's own report, never on a