diff --git a/seaweed-block/core/frontend/durable/m5_sync_cancel_test.go b/seaweed-block/core/frontend/durable/m5_sync_cancel_test.go index d5e69590d7..1dd85e4a16 100644 --- a/seaweed-block/core/frontend/durable/m5_sync_cancel_test.go +++ b/seaweed-block/core/frontend/durable/m5_sync_cancel_test.go @@ -1,53 +1,72 @@ package durable_test import ( + "bytes" "context" - "errors" + "fmt" + "os" + "os/exec" "sync/atomic" "testing" "time" ) -type cancelSyncProxy struct { - entered chan struct{} +type stalledSyncProxy struct { + release chan struct{} + done chan struct{} exited atomic.Int32 - syncs atomic.Int32 } -func (p *cancelSyncProxy) Write(ctx context.Context, _ int64, _ []byte) (int, error) { - p.entered <- struct{}{} - <-ctx.Done() - p.exited.Add(1) - return 0, ctx.Err() +func (p *stalledSyncProxy) Write(_ context.Context, _ int64, data []byte) (int, error) { + return len(data), nil +} +func (p *stalledSyncProxy) Sync(context.Context) error { + // Match contextless LogicalStorage.Sync: cancellation cannot release it. + <-p.release + if p.exited.Add(1) == 8 { + close(p.done) + } + return nil } -func (p *cancelSyncProxy) Sync(context.Context) error { p.syncs.Add(1); return nil } -func TestConcurrentSyncProxyCancellationJoinsEveryWorker(t *testing.T) { - const workers = 8 - backend := &cancelSyncProxy{entered: make(chan struct{}, workers)} - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) - defer cancel() - result := make(chan error, 1) - go func() { _, err := runConcurrentSyncProxy(ctx, backend, workers, 100000); result <- err }() - started := time.NewTimer(time.Second) - defer started.Stop() - for i := 0; i < workers; i++ { +func TestConcurrentSyncProxyStallChild(t *testing.T) { + if os.Getenv("SYNC_PROXY_STALL_CHILD") != "1" { + t.Skip("subprocess failure control only") + } + backend := &stalledSyncProxy{release: make(chan struct{}), done: make(chan struct{})} + t.Cleanup(func() { + close(backend.release) select { - case <-backend.entered: - case <-started.C: - t.Fatal("workers not started") + case <-backend.done: + fmt.Println("STALL_CONTROL_CLEANUP all8 Sync workers released") + case <-time.After(time.Second): + t.Error("stalled workers did not settle during cleanup") } + }) + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + _, _ = runConcurrentSyncProxy(t, ctx, backend, 8, 100000) + t.Fatal("stalled real-style Sync unexpectedly completed") +} + +func TestConcurrentSyncProxyStallFailsWithinDeadline(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestConcurrentSyncProxyStallChild$", "-test.v") + cmd.Env = append(os.Environ(), "SYNC_PROXY_STALL_CHILD=1") + cmd.WaitDelay = time.Second + out, err := cmd.CombinedOutput() + if ctx.Err() != nil { + t.Fatalf("local wait bound failed; subprocess deadline reached: %v\n%s", ctx.Err(), out) } - cancel() - select { - case err := <-result: - if !errors.Is(err, context.Canceled) { - t.Fatal(err) + exit, ok := err.(*exec.ExitError) + if !ok || exit.ExitCode() != 1 { + t.Fatalf("expected deliberate test failure exit1: %v\n%s", err, out) + } + for _, marker := range []string{"timed out waiting for backend Sync workers", "goroutine ", "stalledSyncProxy).Sync", "STALL_CONTROL_CLEANUP all8 Sync workers released"} { + if !bytes.Contains(out, []byte(marker)) { + t.Fatalf("missing %q in bounded stall receipt\n%s", marker, out) } - case <-time.After(time.Second): - t.Fatal("cancellation did not join workers") - } - if backend.exited.Load() != workers || backend.syncs.Load() != 0 { - t.Fatalf("abandoned workers or sync after cancellation: exited=%d syncs=%d", backend.exited.Load(), backend.syncs.Load()) } + t.Logf("context-ignoring Sync produced bounded failure, stack dump and cleanup: %d receipt bytes", len(out)) } diff --git a/seaweed-block/core/frontend/durable/m5_sync_concurrent_bench_test.go b/seaweed-block/core/frontend/durable/m5_sync_concurrent_bench_test.go index 6a05ee6a78..5dd7dc59d8 100644 --- a/seaweed-block/core/frontend/durable/m5_sync_concurrent_bench_test.go +++ b/seaweed-block/core/frontend/durable/m5_sync_concurrent_bench_test.go @@ -9,6 +9,7 @@ import ( "github.com/seaweedfs/seaweed-block/core/frontend/durable" "github.com/seaweedfs/seaweed-block/core/storage" "os" + "runtime" "sync" "testing" "time" @@ -40,7 +41,7 @@ func BenchmarkConcurrentBackendSync(b *testing.B) { profile := backend.WriteProfile() workloadCtx, cancelWorkload := context.WithTimeout(context.Background(), 30*time.Second) b.ResetTimer() - elapsed, workloadErr := runConcurrentSyncProxy(workloadCtx, backend, qd, b.N) + elapsed, workloadErr := runConcurrentSyncProxy(b, workloadCtx, backend, qd, b.N) b.StopTimer() cancelWorkload() if workloadErr != nil { @@ -66,15 +67,16 @@ func BenchmarkConcurrentBackendSync(b *testing.B) { } } -// Local file I/O is not interruptible by context. Cancellation stops new work -// between operations; every started worker is joined after in-flight I/O settles. -// Do not wrap wg.Wait in a select that abandons workers on timeout. +// Local file I/O is not interruptible by context. Bound the completion wait; +// a stalled operation fails the benchmark with its goroutine stacks. The caller +// registers backend cleanup before starting this workload. type concurrentSyncProxyBackend interface { Write(context.Context, int64, []byte) (int, error) Sync(context.Context) error } -func runConcurrentSyncProxy(ctx context.Context, backend concurrentSyncProxyBackend, qd, iterations int) (time.Duration, error) { +func runConcurrentSyncProxy(tb testing.TB, ctx context.Context, backend concurrentSyncProxyBackend, qd, iterations int) (time.Duration, error) { + tb.Helper() ctx, cancel := context.WithCancel(ctx) defer cancel() start := make(chan struct{}) @@ -109,7 +111,15 @@ func runConcurrentSyncProxy(ctx context.Context, backend concurrentSyncProxyBack } started := time.Now() close(start) - wg.Wait() + done := make(chan struct{}) + go func() { wg.Wait(); close(done) }() + select { + case <-done: + case <-ctx.Done(): + dump := make([]byte, 1<<20) + n := runtime.Stack(dump, true) + tb.Fatalf("timed out waiting for backend Sync workers: %v\n%s", ctx.Err(), dump[:n]) + } elapsed := time.Since(started) close(errs) for err := range errs {