diff --git a/seaweed-block/core/frontend/durable/takeover_read_ready_test.go b/seaweed-block/core/frontend/durable/takeover_read_ready_test.go index aa70b911f7..7866a30a52 100644 --- a/seaweed-block/core/frontend/durable/takeover_read_ready_test.go +++ b/seaweed-block/core/frontend/durable/takeover_read_ready_test.go @@ -3,48 +3,57 @@ package durable_test import ( "bytes" "context" + "errors" "sync" "testing" "time" "github.com/seaweedfs/seaweed-block/core/frontend" + "github.com/seaweedfs/seaweed-block/core/frontend/durable" "github.com/seaweedfs/seaweed-block/core/frontend/nvme" "github.com/seaweedfs/seaweed-block/core/storage" ) -// Hold the return from a successful takeover CAS: an external observer can -// see the new owner while provider attachment is still in flight. +// External observers see the committed takeover HEAD while attachment is +// held in the provider-owned worker, without a separate test goroutine. type heldTakeoverClaim struct { storage.ChunkRemote - claimed chan struct{} - release chan struct{} - once sync.Once + claimed chan struct{} + release chan struct{} + once, releaseOnce sync.Once + holdFor time.Duration } +func (r *heldTakeoverClaim) unblock() { r.releaseOnce.Do(func() { close(r.release) }) } func (r *heldTakeoverClaim) CASHead(ctx context.Context, expected storage.ChunkHeadVersion, next storage.ChunkHead) (uint64, error) { gen, err := r.ChunkRemote.CASHead(ctx, expected, next) if err != nil || next.Epoch != 4 { return gen, err } r.once.Do(func() { close(r.claimed) }) + var expired <-chan time.Time + if r.holdFor > 0 { + timer := time.NewTimer(r.holdFor) + defer timer.Stop() + expired = timer.C + } select { case <-r.release: return gen, nil + case <-expired: + return gen, nil case <-ctx.Done(): return gen, ctx.Err() } } -// The frontend can connect after authority arrives but before the fresh -// host finishes attaching HEAD. That window must not become a read EIO. -func TestWALStoreChunk_TakeoverReadBeforeAttachIsRetryable(t *testing.T) { - remote := &heldTakeoverClaim{ChunkRemote: seededRemote(t), claimed: make(chan struct{}), release: make(chan struct{})} - var releaseOnce sync.Once - release := func() { releaseOnce.Do(func() { close(remote.release) }) } +func pendingTakeoverRead(t *testing.T, hold time.Duration) (*durable.DurableProvider, frontend.Backend, *nvme.IOHandler) { + t.Helper() + remote := &heldTakeoverClaim{ChunkRemote: seededRemote(t), claimed: make(chan struct{}), release: make(chan struct{}), holdFor: hold} view := newStubView(frontend.Projection{VolumeID: "v1", ReplicaID: "r1"}) p := chunkProvider(t, t.TempDir(), remote, view, 0) - // Release before provider cleanup joins the authority/attach worker. - defer release() + // Registered after provider cleanup, so release precedes its worker join. + t.Cleanup(remote.unblock) if _, err := p.EnsureStorage("v1"); err != nil { t.Fatal(err) } @@ -66,35 +75,57 @@ func TestWALStoreChunk_TakeoverReadBeforeAttachIsRetryable(t *testing.T) { t.Fatal(err) } handler := nvme.NewIOHandler(nvme.HandlerConfig{Backend: backend, BlockSize: 4096, VolumeSize: pcBlocks * 4096, NSID: 1}) + return p, backend, handler +} + +// Model Linux max_retries=5 and CRD=0: at most six attempts, no sleeps. +// A status-only fix exhausts them during attachment and becomes guest EIO. +func TestWALStoreChunk_TakeoverReadBeforeAttachIsRetryable(t *testing.T) { + _, _, handler := pendingTakeoverRead(t, 2500*time.Millisecond) command := nvme.IOCommand{Opcode: 0x02, NSID: 1, SLBA: 33, NLB: 1} start := time.Now() - result := handler.Handle(context.Background(), command) - elapsed := time.Since(start) - // Keep the actual deferred attach parked for the takeover race interval. - time.Sleep(2500 * time.Millisecond) - release() - if result.SCT != nvme.SCTGeneric || result.SC != nvme.SCNamespaceNotReady || len(result.Data) != 0 { - t.Fatalf("read before HEAD attach: SCT=%#x SC=%#x data=%d reason=%s; want retryable namespace not ready", result.SCT, result.SC, len(result.Data), result.Reason) - } - if elapsed >= 5*time.Second { - t.Fatalf("read before attach took %s", elapsed) - } - deadline := time.Now().Add(5 * time.Second) - for { - result = handler.Handle(context.Background(), command) + for attempt := 0; attempt <= 5; attempt++ { + result := handler.Handle(context.Background(), command) if result.AsError() == nil { - break + if !bytes.Equal(result.Data, block(0x33)) { + t.Fatal("guest read returned incorrect HEAD bytes") + } + if elapsed := time.Since(start); elapsed >= 5*time.Second { + t.Fatalf("guest read took %s", elapsed) + } + t.Logf("guest read succeeded on attempt%d after%s (max5 immediate retries)", attempt+1, time.Since(start)) + return } if result.SCT != nvme.SCTGeneric || result.SC != nvme.SCNamespaceNotReady { - t.Fatalf("read while attach settles: %v", result.AsError()) + t.Fatalf("guest read terminal status: %v", result.AsError()) } - if time.Now().After(deadline) { - t.Fatal("attach did not make the same read operational") - } - time.Sleep(10 * time.Millisecond) } - if !bytes.Equal(result.Data, block(0x33)) { - t.Fatal("first operational read did not return exact HEAD bytes") + t.Fatal("guest EIO: all six immediate attempts exhausted before chunk attach") +} + +func TestWALStoreChunk_PendingReadWaitIsBounded(t *testing.T) { + _, _, handler := pendingTakeoverRead(t, 0) + start := time.Now() + result := handler.Handle(context.Background(), nvme.IOCommand{Opcode: 0x02, NSID: 1, SLBA: 33, NLB: 1}) + elapsed := time.Since(start) + if result.SCT != nvme.SCTGeneric || result.SC != nvme.SCNamespaceNotReady || len(result.Data) != 0 { + t.Fatalf("pending read result: %+v", result) } - t.Logf("pre-attach read returned in %s; same read recovered exact HEAD bytes after 2.5s attach delay", elapsed) + if elapsed < 3500*time.Millisecond || elapsed >= 5*time.Second { + t.Fatalf("pending read wait=%s; want bounded readiness wait below5s", elapsed) + } +} + +func TestWALStoreChunk_PendingReadHonoursEarlierDeadline(t *testing.T) { + _, backend, _ := pendingTakeoverRead(t, 0) + ctx, cancel := context.WithTimeout(context.Background(), 40*time.Millisecond) + defer cancel() + start := time.Now() + n, err := backend.Read(ctx, 33*4096, make([]byte, 4096)) + if n != 0 || !errors.Is(err, frontend.ErrBackendBusy) || !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("deadline read: n=%d err=%v", n, err) + } + if time.Since(start) >= time.Second { + t.Fatal("read ignored earlier deadline") + } }