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 55f0065316..f462c415e4 100644 --- a/seaweed-block/core/host/volume/workspace_first_heartbeat_test.go +++ b/seaweed-block/core/host/volume/workspace_first_heartbeat_test.go @@ -15,16 +15,23 @@ import ( // orderServer is a master's two holder-facing services on one listener: it // records the order in which a holder's heartbeats and lease renewals // arrive, and holds the FIRST heartbeat open until released (a slow first -// report, as after a master restart or a congested link). +// report, as after a master restart or a congested link). entered closes +// when that first heartbeat is inside the handler: from then on the +// heartbeat is in flight and not yet answered. type orderServer struct { control.UnimplementedObservationServiceServer control.UnimplementedWorkspaceServiceServer mu sync.Mutex events []string heartbeat int + entered chan struct{} release chan struct{} } +func newOrderServer() *orderServer { + return &orderServer{entered: make(chan struct{}), release: make(chan struct{})} +} + func (s *orderServer) record(e string) { s.mu.Lock() s.events = append(s.events, e) @@ -37,6 +44,7 @@ func (s *orderServer) ReportHeartbeat(ctx context.Context, r *control.HeartbeatR first := s.heartbeat == 1 s.mu.Unlock() if first { + close(s.entered) select { case <-s.release: case <-ctx.Done(): @@ -63,13 +71,22 @@ func (s *orderServer) snapshot() []string { return append([]string(nil), s.events...) } -// Identity check (1) of the v1.1.2 bound: a v1.1 holder's first lease -// renewal on start follows its first heartbeat, which names its incarnation. -// Until then the master holds only its predecessor's report for the volume, -// and a renewal would be judged on that report. The first heartbeat is held -// open; no renewal may arrive before it, and one follows once it is answered. -func TestAHolderRenewsOnlyAfterItsFirstHeartbeatWithItsIncarnation(t *testing.T) { - s := &orderServer{release: make(chan struct{})} +func (s *orderServer) renewed() bool { + for _, e := range s.snapshot() { + if e == "renewal" { + return true + } + } + return false +} + +// firstHeartbeatRig starts an already-assigned holder (epoch 3, so the lease +// 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. +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") if err != nil { t.Fatal(err) @@ -79,33 +96,48 @@ func TestAHolderRenewsOnlyAfterItsFirstHeartbeatWithItsIncarnation(t *testing.T) control.RegisterWorkspaceServiceServer(srv, s) go srv.Serve(ln) t.Cleanup(srv.Stop) - - // An already-assigned volume (epoch 3): the lease runtime may start at once. - h := workspaceRuntimeHost(t, ln.Addr().String(), true) + h = workspaceRuntimeHost(t, ln.Addr().String(), true) // its cleanup closes the host (joins the heartbeat loop) ctx, cancel := context.WithCancel(context.Background()) - t.Cleanup(cancel) + ch := make(chan struct{}) h.Start() + started = time.Now() go func() { + defer close(ch) _ = h.RunWorkspaceLease(ctx, func(context.Context, *control.WorkspaceIdentity) (uint64, error) { return 0, nil }) }() - - // Give the lease runtime every chance to renew while the first heartbeat - // is still held. - time.Sleep(500 * time.Millisecond) - for _, e := range s.snapshot() { - if e == "renewal" { - t.Fatalf("the holder renewed its lease before its first heartbeat was answered: %v", s.snapshot()) + 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") } + }) + return h, started, ch +} + +// 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 +// the volume, and a renewal would be judged on that report. The window is +// witnessed open: the first heartbeat is inside the master's handler, held +// there, while the lease runtime has every chance to renew; once it is +// answered, a renewal follows. +func TestAHolderRenewsOnlyAfterItsFirstHeartbeatWithItsIncarnation(t *testing.T) { + s := newOrderServer() + h, _, _ := firstHeartbeatRig(t, s) + select { + case <-s.entered: + case <-time.After(5 * time.Second): + t.Fatal("the holder's first heartbeat never reached the master") + } + // The heartbeat is in flight and unanswered: the window is open. + time.Sleep(500 * time.Millisecond) + if s.renewed() { + t.Fatalf("the holder renewed its lease while its first heartbeat was in flight: %v", s.snapshot()) } close(s.release) - runtimeAwait(t, "a renewal after the first heartbeat", func() bool { - for _, e := range s.snapshot() { - if e == "renewal" { - return true - } - } - return false - }) + runtimeAwait(t, "a renewal after the first heartbeat", s.renewed) events := s.snapshot() if len(events) == 0 || events[0] == "renewal" { t.Fatalf("first event %v, want the first heartbeat", events) @@ -116,44 +148,35 @@ func TestAHolderRenewsOnlyAfterItsFirstHeartbeatWithItsIncarnation(t *testing.T) } // 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. +// not serve: the lease runtime waits the configured bound (one lease +// validity; shortened here), no less, then installs its guard fenced and +// returns. func TestAHolderWhoseHeartbeatIsNeverAnsweredFencesRatherThanServes(t *testing.T) { + const bound = 300 * time.Millisecond previous := firstHeartbeatWait - firstHeartbeatWait = 300 * time.Millisecond + firstHeartbeatWait = bound 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) + s := newOrderServer() // never released + h, started, 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") } - 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()) - } + if waited := time.Since(started); waited < bound { + t.Fatalf("the lease runtime fenced after %s, before its %s bound", waited, bound) + } + if s.renewed() { + 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 guard == nil || !strings.Contains(guard.FenceCause(), "no heartbeat of this process answered within "+bound.String()) { + t.Fatalf("the guard was not installed fenced with the unanswered-heartbeat cause and its bound: %v", guard) } if h.ProjectionView().Projection().Healthy { t.Fatal("a holder with no answered heartbeat serves")