diff --git a/seaweed-block/core/frontend/durable/hang_guard_test.go b/seaweed-block/core/frontend/durable/hang_guard_test.go index 3cd19adbd0..4c22824074 100644 --- a/seaweed-block/core/frontend/durable/hang_guard_test.go +++ b/seaweed-block/core/frontend/durable/hang_guard_test.go @@ -7,19 +7,58 @@ import ( ) // hangGuard returns a context that only guards against a hung test. It ends -// shortly before the test binary's own deadline (go test -timeout), not at a -// fixed wall-clock bound, so a loaded host slows these tests down instead of +// before the test binary's own deadline (go test -timeout), not at a fixed +// wall-clock bound, so a loaded host slows these tests down instead of // failing them: what they assert is the order and outcome of the operations. func hangGuard(t *testing.T) context.Context { t.Helper() deadline, ok := t.Deadline() - if !ok { - deadline = time.Now().Add(10 * time.Minute) - } - if margin := 30 * time.Second; time.Until(deadline) > 2*margin { - deadline = deadline.Add(-margin) - } - ctx, cancel := context.WithDeadline(context.Background(), deadline) + ctx, cancel := context.WithDeadline(context.Background(), hangGuardDeadline(time.Now(), deadline, ok)) t.Cleanup(cancel) return ctx } + +const ( + hangGuardNoDeadline = 10 * time.Minute // no -timeout: still bounded + hangGuardMaxMargin = 30 * time.Second +) + +// hangGuardDeadline leaves a tenth of the remaining budget (at most 30 s) +// before the binary's deadline, so the guard fires first and the test still +// has time to report and clean up, however short the budget. An expired +// budget gives an expired guard: the test fails at once instead of racing the +// binary's own timeout. +func hangGuardDeadline(now, deadline time.Time, ok bool) time.Time { + if !ok { + return now.Add(hangGuardNoDeadline) + } + remaining := deadline.Sub(now) + if remaining <= 0 { + return deadline + } + margin := remaining / 10 + if margin > hangGuardMaxMargin { + margin = hangGuardMaxMargin + } + return deadline.Add(-margin) +} + +func TestHangGuardDeadline(t *testing.T) { + now := time.Unix(1700000000, 0) + for _, c := range []struct { + name string + deadline time.Time + ok bool + want time.Time + }{ + {"no deadline", time.Time{}, false, now.Add(hangGuardNoDeadline)}, + {"long budget", now.Add(time.Hour), true, now.Add(time.Hour - hangGuardMaxMargin)}, + {"short budget", now.Add(20 * time.Second), true, now.Add(18 * time.Second)}, + {"nearly expired", now.Add(100 * time.Millisecond), true, now.Add(90 * time.Millisecond)}, + {"expired", now.Add(-time.Second), true, now.Add(-time.Second)}, + } { + if got := hangGuardDeadline(now, c.deadline, c.ok); !got.Equal(c.want) { + t.Errorf("%s: guard at %v, want %v", c.name, got.Sub(now), c.want.Sub(now)) + } + } +} diff --git a/seaweed-block/core/frontend/durable/provider_chunk_detach_abort_test.go b/seaweed-block/core/frontend/durable/provider_chunk_detach_abort_test.go index f348e094c7..297bd7715b 100644 --- a/seaweed-block/core/frontend/durable/provider_chunk_detach_abort_test.go +++ b/seaweed-block/core/frontend/durable/provider_chunk_detach_abort_test.go @@ -33,6 +33,16 @@ type faultRemote struct { // remote only when release closes; its own result goes to delayedErr. release chan struct{} delayedErr chan error + // held records the CAS held by "hold-detach": the deadline of the context + // it was given and when that context ended. + held heldCAS +} + +type heldCAS struct { + entered bool + deadline time.Time + hasDeadline bool + ended time.Time } var errInjected = errors.New("injected remote fault") @@ -83,9 +93,16 @@ func (r *faultRemote) CASHead(ctx context.Context, expected storage.ChunkHeadVer return 0, storage.ErrChunkHeadPrecondition case "lost": return 0, errInjected - case "hold", "hold-detach": // held until the caller gives up: not applied + case "hold": // held until the caller gives up: not applied <-ctx.Done() return 0, ctx.Err() + case "hold-detach": // the detach CAS, held until its caller gives up; witnessed + dl, ok := ctx.Deadline() + r.set(func(r *faultRemote) { r.held = heldCAS{entered: true, deadline: dl, hasDeadline: ok} }) + <-ctx.Done() + ended := time.Now() + r.set(func(r *faultRemote) { r.held.ended = ended }) + return 0, ctx.Err() case "delayed": r.mu.Lock() release, out := r.release, r.delayedErr @@ -268,22 +285,32 @@ func TestWALStoreChunk_DetachFreezeIsBoundedThenResumes(t *testing.T) { remote.set(func(r *faultRemote) { r.casMode = "hold-detach" }) deadlines := map[string]time.Time{} durable.SetTestDetachTrace(p, func(stage string, d time.Time) { deadlines[stage] = d }) - start := time.Now() _, _, err = p.DetachVolume(context.Background(), "v1") end := time.Now() if !errors.Is(err, durable.ErrDetachAborted) || !errors.Is(err, context.DeadlineExceeded) { t.Fatalf("detach with a hung CAS: %v; want an aborted detach at the freeze bound", err) } - // The cut gets three quarters of the bound, the settlement the rest. + // One budget from one origin: the cut ends at three quarters of it, the + // settlement at its end (exact, both derive from the same start). cut, settle := deadlines["cut"], deadlines["settle"] - if cutAt, settleAt := cut.Sub(start), settle.Sub(start); cutAt < freeze*3/4-50*time.Millisecond || cutAt > freeze*3/4+50*time.Millisecond || - settleAt < freeze-50*time.Millisecond || settleAt > freeze+50*time.Millisecond { - t.Fatalf("deadlines: cut %v, settle %v after the start; want three quarters of the %v freeze, then the bound", cutAt, settleAt, freeze) + if gap := settle.Sub(cut); gap != freeze/4 { + t.Fatalf("settlement deadline %v after the cut deadline; want a quarter of the %v bound", gap, freeze) } - // The abort happened at the cut's deadline, not before, and was settled - // inside the bound (scheduling slack only). - if end.Before(cut) { - t.Fatalf("detach aborted %v before its cut deadline", cut.Sub(end)) + // The held operation is the detach CAS itself: it ran under exactly the + // cut's deadline, and was released by that deadline expiring. + var held heldCAS + remote.set(func(r *faultRemote) { held = r.held }) + if !held.entered { + t.Fatal("the detach CAS was never held") + } + if !held.hasDeadline || !held.deadline.Equal(cut) { + t.Fatalf("held detach CAS deadline %v (set %v); want the cut deadline %v", held.deadline, held.hasDeadline, cut) + } + if held.ended.Before(cut) { + t.Fatalf("held detach CAS ended %v before its deadline", cut.Sub(held.ended)) + } + if end.Before(held.ended) { + t.Fatal("detach returned before its held CAS ended") } if late := end.Sub(settle); late > 500*time.Millisecond { t.Fatalf("detach returned %v after the freeze bound", late) diff --git a/seaweed-block/core/host/master/workspace_binding_review_test.go b/seaweed-block/core/host/master/workspace_binding_review_test.go index a05b189517..ce77965901 100644 --- a/seaweed-block/core/host/master/workspace_binding_review_test.go +++ b/seaweed-block/core/host/master/workspace_binding_review_test.go @@ -2,6 +2,8 @@ package master import ( "context" + "errors" + "sync" "testing" "time" @@ -234,6 +236,10 @@ func TestWorkspaceBindingChosenObservationMustMatchServerAndPair(t *testing.T) { } // The ACK must complete while the unrelated remote reader/prober remains held. +// errHeldNotJoined: the held remote operation's worker did not return within +// its own bound after being released. +var errHeldNotJoined = errors.New("held remote operation not joined") + func TestWorkspaceBindingRemoteReadDoesNotBlockAck(t *testing.T) { for _, mode := range []string{"head", "promotion"} { t.Run(mode, func(t *testing.T) { @@ -257,6 +263,29 @@ func TestWorkspaceBindingRemoteReadDoesNotBlockAck(t *testing.T) { t.Fatal(e) } done := make(chan error, 1) + var releaseOnce sync.Once + releaseHeld := func() { releaseOnce.Do(func() { close(release) }) } + // Registered before either worker launches: every exit releases the + // held call and joins its worker under its own bound, not under the + // fixture context a blocked ACK has already used up. + var joinOnce sync.Once + var joined error + join := func() error { + joinOnce.Do(func() { + select { + case joined = <-done: + case <-time.After(10 * time.Second): + joined = errHeldNotJoined + } + }) + return joined + } + t.Cleanup(func() { + releaseHeld() + if err := join(); errors.Is(err, errHeldNotJoined) { + t.Error(err) + } + }) if mode == "head" { reviewLegacyBind(t, h, c, ctx, "probe") go func() { @@ -287,11 +316,9 @@ func TestWorkspaceBindingRemoteReadDoesNotBlockAck(t *testing.T) { // separate 1 s bound: the ACK includes a durable persist, which a // loaded host can slow past that without anything being blocked. _, e := client.AckWorkspaceRevoked(ctx, &control.WorkspaceRevocationAck{Identity: id, HeadGen: 9}) - close(release) - select { - case <-done: - case <-ctx.Done(): - t.Fatal("remote operation did not finish", ctx.Err()) + releaseHeld() + if err := join(); errors.Is(err, errHeldNotJoined) { + t.Fatal(err) } if e != nil { t.Fatal("held remote operation blocked ACK", e)