From f12c8f73582e9dd6e3e593877553ff6e3458cb65 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Thu, 19 Nov 2026 07:08:58 -0800 Subject: [PATCH] test(durable): witness pending read before drain and close --- .../frontend/durable/read_ready_wait_test.go | 106 ++++++++++-------- .../core/frontend/durable/storage_adapter.go | 5 + 2 files changed, 66 insertions(+), 45 deletions(-) diff --git a/seaweed-block/core/frontend/durable/read_ready_wait_test.go b/seaweed-block/core/frontend/durable/read_ready_wait_test.go index e9296bca88..36b4b41bc1 100644 --- a/seaweed-block/core/frontend/durable/read_ready_wait_test.go +++ b/seaweed-block/core/frontend/durable/read_ready_wait_test.go @@ -3,6 +3,7 @@ package durable import ( "context" "errors" + "sync" "testing" "time" @@ -10,51 +11,66 @@ import ( ) func TestReadReadinessWaitDoesNotHoldAdmissionAndCloseWakesIt(t *testing.T) { - // No storage is needed: this read must never pass the operational gate. - b := &StorageBackend{waitReadReady: true} - b.opEvidence.Store("attachment pending") - done := make(chan error, 1) - joined := make(chan struct{}) - t.Cleanup(func() { - _ = b.Close() - select { - case <-joined: - case <-time.After(time.Second): - t.Error("pending read did not settle after Close") + for _, drain := range []bool{false, true} { + name := "close" + if drain { + name = "drain-then-close" } - }) - go func() { - defer close(joined) - _, err := b.Read(context.Background(), 0, make([]byte, 4096)) - done <- err - }() - deadline := time.Now().Add(time.Second) - for { - b.mu.Lock() - registered := b.readyChanged != nil - b.mu.Unlock() - if registered { - break - } - if time.Now().After(deadline) { - t.Fatal("read did not register a readiness notification") - } - time.Sleep(time.Millisecond) - } - ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) - defer cancel() - if err := b.StopIO(ctx, "attachment still pending"); err != nil { - t.Fatalf("waiting read held I/O admission: %v", err) - } - if err := b.Close(); err != nil { - t.Fatal(err) - } - select { - case err := <-done: - if !errors.Is(err, frontend.ErrBackendClosed) { - t.Fatalf("Close wake error=%v", err) - } - case <-time.After(time.Second): - t.Fatal("Close did not wake registered read") + t.Run(name, func(t *testing.T) { + // No storage is needed: this read must never pass the operational gate. + b := &StorageBackend{waitReadReady: true} + b.opEvidence.Store("attachment pending") + registered := make(chan struct{}, 1) + resume := make(chan struct{}) + var once sync.Once + release := func() { once.Do(func() { close(resume) }) } + b.testBeforeReadReadyWait = func() { + select { + case registered <- struct{}{}: + default: + } + <-resume + } + done := make(chan error, 1) + joined := make(chan struct{}) + t.Cleanup(func() { + release() + _ = b.Close() + select { + case <-joined: + case <-time.After(5 * time.Second): + t.Error("pending read did not settle after release and Close") + } + }) + go func() { + defer close(joined) + _, err := b.Read(context.Background(), 0, make([]byte, 4096)) + done <- err + }() + select { + case <-registered: + case <-time.After(time.Second): + t.Fatal("read did not register a readiness notification") + } + if drain { + ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) + defer cancel() + if err := b.StopIO(ctx, "attachment still pending"); err != nil { + t.Fatalf("waiting read held I/O admission: %v", err) + } + } + if err := b.Close(); err != nil { + t.Fatal(err) + } + release() + select { + case err := <-done: + if !errors.Is(err, frontend.ErrBackendClosed) { + t.Fatalf("Close wake error=%v", err) + } + case <-time.After(time.Second): + t.Fatal("Close did not wake registered read") + } + }) } } diff --git a/seaweed-block/core/frontend/durable/storage_adapter.go b/seaweed-block/core/frontend/durable/storage_adapter.go index 84c754d49f..5737aebd1a 100644 --- a/seaweed-block/core/frontend/durable/storage_adapter.go +++ b/seaweed-block/core/frontend/durable/storage_adapter.go @@ -149,6 +149,8 @@ type StorageBackend struct { // testBeforeIOLock, test-only, runs in admit after the readiness check, // right before the I/O lock (an arrival handshake for queued I/O). testBeforeIOLock func() + // Runs outside locks after a read registers its readiness notification. + testBeforeReadReadyWait func() storage storage.LogicalStorage view frontend.ProjectionView @@ -596,6 +598,9 @@ func (b *StorageBackend) awaitReadAdmission(ctx context.Context) (func(), error) if !errors.Is(err, frontend.ErrNotReady) || terminal || b.operational.Load() { return release, err } + if hook := b.testBeforeReadReadyWait; hook != nil { + hook() + } select { case <-changed: case <-ctx.Done():