From 15336d7485d0694db9ffcb95b0797058c84bff8f Mon Sep 17 00:00:00 2001 From: pingqiu Date: Fri, 20 Nov 2026 06:25:08 -0800 Subject: [PATCH 1/4] test(durable): restore and detach-freeze tests no longer race wall-clock bounds The restore tests ran under a fixed 4-10 s test context; under load the whole test outran it and a HEAD read or claim failed with a deadline. They now use a hang guard derived from the test binary's own deadline. The detach-freeze test held the next CAS, which a running chunk loop could take instead of the detach, and gave the settlement 75 ms. It now holds only the detach CAS, uses a 2 s freeze bound (the settlement keeps 500 ms), and checks the abort against the deadlines the detach traced: the cut deadline at three quarters of the bound, the abort at or after it, the return within the bound. Refs #300 Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/durable/hang_guard_test.go | 25 +++++++++++ .../provider_chunk_detach_abort_test.go | 43 ++++++++++++++----- .../durable/provider_restore_schedule_test.go | 6 +-- .../durable/provider_seed_linux_test.go | 7 +-- .../durable/provider_seed_order_test.go | 7 +-- .../provider_ship_rebase_order_test.go | 7 +-- .../durable/provider_zero_bootstrap_test.go | 4 +- 7 files changed, 66 insertions(+), 33 deletions(-) create mode 100644 seaweed-block/core/frontend/durable/hang_guard_test.go diff --git a/seaweed-block/core/frontend/durable/hang_guard_test.go b/seaweed-block/core/frontend/durable/hang_guard_test.go new file mode 100644 index 0000000000..3cd19adbd0 --- /dev/null +++ b/seaweed-block/core/frontend/durable/hang_guard_test.go @@ -0,0 +1,25 @@ +package durable_test + +import ( + "context" + "testing" + "time" +) + +// 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 +// 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) + t.Cleanup(cancel) + return ctx +} 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 4584662a70..f348e094c7 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 @@ -25,7 +25,7 @@ type faultRemote struct { *storage.MemChunkRemote mu sync.Mutex putErr error // returned by every Put while set - casMode string // "", "precondition", "lost" (not applied, error), "applied-lost" (applied, error), "hold" + casMode string // "", "precondition", "lost" (not applied, error), "applied-lost" (applied, error), "hold", "hold-detach" getHeadErr error // returned by every GetHead while set getHeadFail int // the next N GetHead calls fail getHeadHold bool // every GetHead blocks until its caller gives up @@ -71,14 +71,19 @@ func (r *faultRemote) GetHead(ctx context.Context) (storage.ChunkHead, error) { func (r *faultRemote) CASHead(ctx context.Context, expected storage.ChunkHeadVersion, next storage.ChunkHead) (uint64, error) { r.mu.Lock() mode := r.casMode - r.casMode = "" // one fault, on the next CAS only + switch { + case mode == "hold-detach" && next.State != storage.ChunkHeadDetached: + mode = "" // only the detach CAS is held; others (a running chunk loop) pass + default: + r.casMode = "" // one fault, on the next CAS only + } r.mu.Unlock() switch mode { case "precondition": return 0, storage.ErrChunkHeadPrecondition case "lost": return 0, errInjected - case "hold": // held until the caller gives up: not applied + case "hold", "hold-detach": // held until the caller gives up: not applied <-ctx.Done() return 0, ctx.Err() case "delayed": @@ -224,10 +229,14 @@ func TestWALStoreChunk_UndecidableDetachStaysStopped(t *testing.T) { } } -// The freeze is bounded by DetachFreezeTimeout: a CAS that hangs ends there, -// the outcome is settled (HEAD still at base: cancelled) and serving resumes, -// all well inside bound + settle. +// The freeze is bounded by DetachFreezeTimeout: a detach CAS that hangs ends at +// the cut's deadline (three quarters of the bound), the outcome is settled +// (HEAD still at base: cancelled) inside the bound, and serving resumes. The +// test asserts against the deadlines the detach itself traced, so a loaded +// host cannot turn the order into a timing failure; the settlement keeps a +// quarter of a 2 s bound for its HEAD read and cancel. func TestWALStoreChunk_DetachFreezeIsBoundedThenResumes(t *testing.T) { + const freeze = 2 * time.Second remote := &faultRemote{MemChunkRemote: storage.NewMemChunkRemote()} p, err := durable.NewDurableProvider(durable.ProviderConfig{ Impl: durable.ImplWALStoreChunk, @@ -235,7 +244,7 @@ func TestWALStoreChunk_DetachFreezeIsBoundedThenResumes(t *testing.T) { BlockSize: 4096, NumBlocks: pcBlocks, ChunkBlocks: pcChunkBlocks, - DetachFreezeTimeout: 300 * time.Millisecond, + DetachFreezeTimeout: freeze, ChunkRemoteFor: func(string) (storage.ChunkRemote, error) { return remote, nil }, }, v1View()) if err != nil { @@ -256,16 +265,28 @@ func TestWALStoreChunk_DetachFreezeIsBoundedThenResumes(t *testing.T) { if _, err := b.Write(context.Background(), 8192, block(2)); err != nil { t.Fatal(err) } - remote.set(func(r *faultRemote) { r.casMode = "hold" }) + 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") - took := time.Since(start) + 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. - if took < 225*time.Millisecond || took > 300*time.Millisecond+200*time.Millisecond { - t.Fatalf("detach with a hung CAS took %v; want the cut ended at 3/4 of the 300 ms freeze bound and the total within it", took) + 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) + } + // 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)) + } + if late := end.Sub(settle); late > 500*time.Millisecond { + t.Fatalf("detach returned %v after the freeze bound", late) } assertServing(t, p, b, remote) } diff --git a/seaweed-block/core/frontend/durable/provider_restore_schedule_test.go b/seaweed-block/core/frontend/durable/provider_restore_schedule_test.go index 6499084fc8..21d2df090e 100644 --- a/seaweed-block/core/frontend/durable/provider_restore_schedule_test.go +++ b/seaweed-block/core/frontend/durable/provider_restore_schedule_test.go @@ -25,8 +25,7 @@ func awaitRestoreCondition(t *testing.T, ctx context.Context, what string, ready } func TestRestoreCheckpointBeforeAssignmentPreservesRemoteBytes(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() + ctx := hangGuard(t) remote := storage.NewMemChunkRemote() view := newStubView(frontend.Projection{VolumeID: "v1", ReplicaID: "r1"}) p := chunkProvider(t, t.TempDir(), remote, view, 0) @@ -80,8 +79,7 @@ func (r *publishAfterAbsentRead) GetHead(ctx context.Context) (storage.ChunkHead } func TestRestoreHEADAppearsBetweenClaimAndAttach(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() + ctx := hangGuard(t) remote := storage.NewMemChunkRemote() data := bytes.Repeat([]byte{0xA7}, pcChunkBlocks*4096) wrapped := &publishAfterAbsentRead{ChunkRemote: remote, beforeReturn: func() { diff --git a/seaweed-block/core/frontend/durable/provider_seed_linux_test.go b/seaweed-block/core/frontend/durable/provider_seed_linux_test.go index 65c5007223..9d59f9d211 100644 --- a/seaweed-block/core/frontend/durable/provider_seed_linux_test.go +++ b/seaweed-block/core/frontend/durable/provider_seed_linux_test.go @@ -13,7 +13,6 @@ import ( "sync" "syscall" "testing" - "time" "github.com/seaweedfs/seaweed-block/core/frontend" "github.com/seaweedfs/seaweed-block/core/frontend/durable" @@ -87,8 +86,7 @@ func failSeedFD(t *testing.T, root string) func() { } func TestRestoreFailedSeedSyncRetryCaptureAndReopen(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() + ctx := hangGuard(t) root := filepath.Join(t.TempDir(), "provider") remote := storage.NewMemChunkRemote() data := bytes.Repeat([]byte{0xA7}, 4*4096) @@ -191,8 +189,7 @@ func TestRestoreFailedSeedSyncRetryCaptureAndReopen(t *testing.T) { } func TestRestoreFailedSeedNeverTakesOwnership(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() + ctx := hangGuard(t) root := filepath.Join(t.TempDir(), "provider") remote := storage.NewMemChunkRemote() if _, err := remote.CASHead(ctx, storage.ChunkHeadVersion{}, storage.ChunkHead{Epoch: 1, Holder: "old", RemoteLSN: 7}); err != nil { diff --git a/seaweed-block/core/frontend/durable/provider_seed_order_test.go b/seaweed-block/core/frontend/durable/provider_seed_order_test.go index ce1c06c932..ccb1f2e85f 100644 --- a/seaweed-block/core/frontend/durable/provider_seed_order_test.go +++ b/seaweed-block/core/frontend/durable/provider_seed_order_test.go @@ -6,7 +6,6 @@ import ( "errors" "sync/atomic" "testing" - "time" "github.com/seaweedfs/seaweed-block/core/frontend" "github.com/seaweedfs/seaweed-block/core/frontend/durable" @@ -24,8 +23,7 @@ func (r *takeoverOrderRemote) CASHead(ctx context.Context, v storage.ChunkHeadVe } func TestRestoreSeedPrecedesTakeoverCAS(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() + ctx := hangGuard(t) remote := storage.NewMemChunkRemote() if _, err := remote.CASHead(ctx, storage.ChunkHeadVersion{}, storage.ChunkHead{Epoch: 1, Holder: "old", RemoteLSN: 7}); err != nil { t.Fatal(err) @@ -81,8 +79,7 @@ func TestRestoreSeedPrecedesTakeoverCAS(t *testing.T) { // A restored restart under the same assignment must reactivate a detached // image before exposing I/O, not treat it as an already-settled active claim. func TestRestoreEqualOwnerDetachedHEADIsReactivated(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() + ctx := hangGuard(t) remote := storage.NewMemChunkRemote() makeProvider := func() *durable.DurableProvider { p, err := durable.NewDurableProvider(durable.ProviderConfig{Impl: durable.ImplWALStoreChunk, StorageRoot: t.TempDir(), NumBlocks: 64, BlockSize: 4096, ChunkBlocks: 4, ChunkHolder: "node-a/r1", diff --git a/seaweed-block/core/frontend/durable/provider_ship_rebase_order_test.go b/seaweed-block/core/frontend/durable/provider_ship_rebase_order_test.go index 4178a6df28..0b74463ecc 100644 --- a/seaweed-block/core/frontend/durable/provider_ship_rebase_order_test.go +++ b/seaweed-block/core/frontend/durable/provider_ship_rebase_order_test.go @@ -11,7 +11,6 @@ import ( "sync" "sync/atomic" "testing" - "time" "github.com/seaweedfs/seaweed-block/core/frontend" "github.com/seaweedfs/seaweed-block/core/frontend/durable" @@ -49,8 +48,7 @@ func (o *countingRebaser) seen() []uint64 { // before the takeover CAS. Returns the recovery error. func restoreWithObserver(t *testing.T, obs *countingRebaser, atCAS func(p *durable.DurableProvider)) (storage.ChunkRemote, *durable.DurableProvider, *atomic.Int32, error) { t.Helper() - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - t.Cleanup(cancel) + ctx := hangGuard(t) remote := storage.NewMemChunkRemote() if _, err := remote.CASHead(ctx, storage.ChunkHeadVersion{}, storage.ChunkHead{Epoch: 1, Holder: "old", RemoteLSN: 7}); err != nil { t.Fatal(err) @@ -104,8 +102,7 @@ func TestRestoreRefusedShipRebaseKeepsHeadAndIOClosed(t *testing.T) { if err == nil { t.Fatal("restore succeeded although the observer refused the post-seed ship-LSN rebase") } - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() + ctx := hangGuard(t) h, herr := remote.GetHead(ctx) if herr != nil || calls.Load() != 0 || h.Epoch != 1 || h.Holder != "old" { t.Fatalf("HEAD claimed despite a refused ship rebase: CAS calls=%d head=%+v err=%v; want 0 calls, epoch 1 holder old", calls.Load(), h, herr) diff --git a/seaweed-block/core/frontend/durable/provider_zero_bootstrap_test.go b/seaweed-block/core/frontend/durable/provider_zero_bootstrap_test.go index 78b5ea33e7..28ac62a9da 100644 --- a/seaweed-block/core/frontend/durable/provider_zero_bootstrap_test.go +++ b/seaweed-block/core/frontend/durable/provider_zero_bootstrap_test.go @@ -10,7 +10,6 @@ import ( "github.com/seaweedfs/seaweed-block/core/storage" "sync" "testing" - "time" ) type bootstrapBlobs struct { @@ -36,8 +35,7 @@ func (b *bootstrapBlobs) Get(_ context.Context, id string) ([]byte, error) { } func TestRestoreEnsureVolumeZeroHEADPreservesCheckpointedBootstrap(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second) - defer cancel() + ctx := hangGuard(t) filer := chunkstore.NewMemFiler() catalog, err := chunkstore.NewCatalog(filer, "/bootstrap") if err != nil { From bce9b4f99e77277f1faae35db29f3f2da46c4478 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Fri, 20 Nov 2026 07:04:32 -0800 Subject: [PATCH 2/4] test(master): the revocation ACK test no longer races a 1 s bound The ACK in TestWorkspaceBindingRemoteReadDoesNotBlockAck ran under a fixed 1 s deadline that also covered a durable persist, so a loaded host could fail it with nothing blocked. It now runs under the test's own context: the held remote call is released only after the ACK returns, so an ACK that waited for it still fails at that deadline. Refs #300 Co-Authored-By: Claude Opus 5.5 --- .../core/host/master/workspace_binding_review_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 7a1fabe73e..a05b189517 100644 --- a/seaweed-block/core/host/master/workspace_binding_review_test.go +++ b/seaweed-block/core/host/master/workspace_binding_review_test.go @@ -282,9 +282,11 @@ func TestWorkspaceBindingRemoteReadDoesNotBlockAck(t *testing.T) { case <-ctx.Done(): t.Fatal("remote call not reached", ctx.Err()) } - ackCtx, cancel := context.WithTimeout(ctx, time.Second) - _, e := client.AckWorkspaceRevoked(ackCtx, &control.WorkspaceRevocationAck{Identity: id, HeadGen: 9}) - cancel() + // The held remote call is released only after the ACK returns, so an + // ACK that waited for it would run into the test's own deadline. No + // 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: From 5aab45c9ddaa585ccb31735c6b60d8f54841cf3d Mon Sep 17 00:00:00 2001 From: pingqiu Date: Fri, 20 Nov 2026 07:18:16 -0800 Subject: [PATCH 3/4] test: witness the held detach CAS; bounded ACK join; proportional hang guard DetachFreeze now witnesses the held detach CAS itself: it was held, ran under exactly the cut deadline, and was released by that deadline expiring before the detach returned. The budget is checked from its one origin (the settlement deadline a quarter of the bound after the cut deadline), not against a start time sampled outside DetachVolume. The revocation ACK test registers, before either worker launches, an idempotent release and a join bounded on its own, so a blocked ACK that used up the fixture context still releases and joins the held operation. hangGuard keeps a tenth of the remaining budget (at most 30 s) before the binary's deadline, also for short or nearly expired budgets, and an expired budget gives an expired guard; unit-tested. Refs #300 Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/durable/hang_guard_test.go | 57 ++++++++++++++++--- .../provider_chunk_detach_abort_test.go | 47 +++++++++++---- .../master/workspace_binding_review_test.go | 37 ++++++++++-- 3 files changed, 117 insertions(+), 24 deletions(-) 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) From da8b29cd47e46ceb18c71eb3105d447373729900 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Fri, 20 Nov 2026 08:57:52 -0800 Subject: [PATCH 4/4] test: join only a launched ACK worker; composable held-CAS witness The revocation ACK test records whether its worker was launched: every exit still releases the held call, but only a launched worker is joined, so a setup failure before the launch no longer waits 10 s for a sender that does not exist or reports a join failure that did not happen. The detach-freeze witness checks the held CAS's context deadline only when the context carries one, and in every case that the CAS ended no earlier than the cut deadline, so it also holds if the cut bounds the CAS by a cancelling timer instead of a deadline. Refs #300 Co-Authored-By: Claude Opus 5.5 --- .../provider_chunk_detach_abort_test.go | 7 +++++-- .../master/workspace_binding_review_test.go | 21 +++++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) 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 297bd7715b..c6c9c6636c 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 @@ -303,8 +303,11 @@ func TestWALStoreChunk_DetachFreezeIsBoundedThenResumes(t *testing.T) { 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) + // The cut may bound the CAS by a context deadline or by a timer that + // cancels it: a deadline, when there is one, must be the cut's exactly; + // either way the CAS must end no earlier than the cut deadline. + if held.hasDeadline && !held.deadline.Equal(cut) { + t.Fatalf("held detach CAS deadline %v; want the cut deadline %v", held.deadline, cut) } if held.ended.Before(cut) { t.Fatalf("held detach CAS ended %v before its deadline", cut.Sub(held.ended)) 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 ce77965901..bae394f195 100644 --- a/seaweed-block/core/host/master/workspace_binding_review_test.go +++ b/seaweed-block/core/host/master/workspace_binding_review_test.go @@ -266,11 +266,20 @@ func TestWorkspaceBindingRemoteReadDoesNotBlockAck(t *testing.T) { 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. + // held call, and a worker that was launched is joined under its own + // bound, not under the fixture context a blocked ACK has already + // used up. A setup failure before the launch joins nothing. + launched := false + launch := func(worker func() error) { + launched = true + go func() { done <- worker() }() + } var joinOnce sync.Once var joined error join := func() error { + if !launched { + return nil + } joinOnce.Do(func() { select { case joined = <-done: @@ -288,10 +297,10 @@ func TestWorkspaceBindingRemoteReadDoesNotBlockAck(t *testing.T) { }) if mode == "head" { reviewLegacyBind(t, h, c, ctx, "probe") - go func() { + launch(func() error { _, e := client.MoveWorkspace(ctx, &control.WorkspaceMove{RequestId: "held", VolumeId: "probe", ExpectedReplicaId: "r1", ExpectedEpoch: 1, TargetReplicaId: "r1", TargetServerId: "node-b", TargetDataAddr: "127.0.0.1:9200", TargetCtrlAddr: "127.0.0.1:9100", Kind: "clean"}) - done <- e - }() + return e + }) } else { if _, e := h.Lifecycle().Volumes.CreateVolume(lifecycle.VolumeSpec{VolumeID: "vol-rf3", SizeBytes: 1 << 20, ReplicationFactor: 3}); e != nil { t.Fatal(e) @@ -304,7 +313,7 @@ func TestWorkspaceBindingRemoteReadDoesNotBlockAck(t *testing.T) { waitAuthorityLine(t, h.Publisher(), "vol-rf3") ingestRF3Observation(t, h, false, true, true) h.SetPromotionEvidenceProvider(reviewHeldPromotion{arrived, release}) - go func() { _, e := h.RunLifecycleProductTick(); done <- e }() + launch(func() error { _, e := h.RunLifecycleProductTick(); return e }) } select { case <-arrived: