seaweed-block: the wait for the first answered heartbeat is bounded; past it the holder fences

Ruling: the first renewal waits for the first answered heartbeat at most
one lease validity (the longest validity a reply may carry); past that the
lease runtime installs its guard fenced, so a holder whose heartbeat is
never answered neither renews nor serves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-05 15:19:27 -08:00
1 parent 53bdf7442f
commit 2dae56beeb
2 files changed
+68 -1

No files matched your search

@@ -3,6 +3,7 @@ package volume
import (
"context"
"net"
"strings"
"sync"
"testing"
"time"
@@ -113,3 +114,48 @@ func TestAHolderRenewsOnlyAfterItsFirstHeartbeatWithItsIncarnation(t *testing.T)
t.Fatalf("the first heartbeat %q does not name this process's incarnation %q", events[0], h.incarnation)
}
}
// A holder whose first heartbeat is never answered does not renew and does
// not serve: once the wait passes one lease validity, the lease runtime
// installs its guard fenced (the safe direction) and returns.
func TestAHolderWhoseHeartbeatIsNeverAnsweredFencesRatherThanServes(t *testing.T) {
previous := firstHeartbeatWait
firstHeartbeatWait = 300 * time.Millisecond
t.Cleanup(func() { firstHeartbeatWait = previous })
s := &orderServer{release: make(chan struct{})} // never released
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
srv := grpc.NewServer()
control.RegisterObservationServiceServer(srv, s)
control.RegisterWorkspaceServiceServer(srv, s)
go srv.Serve(ln)
t.Cleanup(srv.Stop)
h := workspaceRuntimeHost(t, ln.Addr().String(), true)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
h.Start()
done := make(chan error, 1)
go func() {
done <- h.RunWorkspaceLease(ctx, func(context.Context, *control.WorkspaceIdentity) (uint64, error) { return 0, nil })
}()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("the lease runtime kept waiting past the bound instead of fencing")
}
for _, e := range s.snapshot() {
if e == "renewal" {
t.Fatalf("a holder with no answered heartbeat renewed: %v", s.snapshot())
}
}
guard := h.view.workspaceGuard.Load()
if guard == nil || !strings.Contains(guard.FenceCause(), "no heartbeat of this process answered") {
t.Fatalf("the guard was not installed fenced with the unanswered-heartbeat cause: %v", guard)
}
if h.ProjectionView().Projection().Healthy {
t.Fatal("a holder with no answered heartbeat serves")
}
}
@@ -26,12 +26,25 @@ func (h *Host) runWorkspaceLease(ctx context.Context, stop func(context.Context,
}
tick := time.NewTicker(50 * time.Millisecond)
defer tick.Stop()
var heartbeatWaitStart time.Time
for {
if err := ctx.Err(); err != nil {
return err
}
p := h.view.Projection()
if p.Epoch > 0 && h.firstHeartbeatAnswered() {
// The first renewal waits for this process's first answered heartbeat,
// at most one lease validity: past that the guard is installed fenced
// (no renewal, no serving) rather than waiting for ever.
unanswered := ""
if p.Epoch > 0 && !h.firstHeartbeatAnswered() {
if heartbeatWaitStart.IsZero() {
heartbeatWaitStart = time.Now()
}
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)
}
}
if p.Epoch > 0 && (unanswered != "" || h.firstHeartbeatAnswered()) {
id := &control.WorkspaceIdentity{VolumeId: h.cfg.VolumeID, ReplicaId: h.cfg.ReplicaID, ServerId: h.cfg.ServerID, Epoch: p.Epoch}
guard, err := NewWorkspaceLeaseGuard(h.view, control.NewWorkspaceServiceClient(h.conn), id, func(c context.Context) (uint64, error) { return stop(c, id) }, nil)
if err != nil {
@@ -52,6 +65,9 @@ func (h *Host) runWorkspaceLease(ctx context.Context, stop func(context.Context,
if ctx.Err() != nil {
guard.fenceLocked("shutdown requested (context cancelled)")
}
if unanswered != "" {
guard.fenceLocked(unanswered)
}
guard.stopPending = h.workspaceStopPending
h.view.workspaceGuard.Store(guard)
h.workspaceLeaseMu.Unlock()
@@ -65,6 +81,11 @@ func (h *Host) runWorkspaceLease(ctx context.Context, stop func(context.Context,
}
}
// firstHeartbeatWait bounds the wait for the first answered heartbeat: the
// longest lease validity a renewal reply may carry (Refresh refuses more).
// Tests shorten it.
var firstHeartbeatWait = 30 * time.Second
// 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