From 9759dc02d6dc51e804fa33fdb321c5ccdbd1beb0 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Fri, 20 Nov 2026 01:31:33 -0800 Subject: [PATCH] fix(storage): retain takeover cooling across retries and hydration fencing --- .../durable/takeover_read_ready_test.go | 27 +++++++++++++------ seaweed-block/core/storage/chunk_head_belt.go | 5 ++++ seaweed-block/core/storage/chunk_layer.go | 2 +- seaweed-block/core/storage/chunk_restore.go | 5 ++++ 4 files changed, 30 insertions(+), 9 deletions(-) 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 e2278f7b30..44f61a1b4f 100644 --- a/seaweed-block/core/frontend/durable/takeover_read_ready_test.go +++ b/seaweed-block/core/frontend/durable/takeover_read_ready_test.go @@ -95,14 +95,15 @@ func TestWALStoreChunk_TakeoverReadBeforeAttachIsRetryable(t *testing.T) { command := nvme.IOCommand{Opcode: 0x02, NSID: 1, SLBA: 33, NLB: 1} start := time.Now() for attempt := 0; attempt <= 5; attempt++ { + commandStart := time.Now() result := handler.Handle(context.Background(), command) + if elapsed := time.Since(commandStart); elapsed >= 5*time.Second { + t.Fatalf("guest command took %s", elapsed) + } if result.AsError() == nil { 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 } @@ -142,14 +143,24 @@ func TestWALStoreChunk_PendingReadHonoursEarlierDeadline(t *testing.T) { func TestWALStoreChunk_TakeoverWriteWaitsForAttach(t *testing.T) { _, backend, _ := pendingTakeoverRead(t, 2500*time.Millisecond) - start := time.Now() - n, err := backend.Write(context.Background(), 33*4096, block(0x74)) + var n int + var err error + for attempt := 0; attempt <= 5; attempt++ { + start := time.Now() + n, err = backend.Write(context.Background(), 33*4096, block(0x74)) + if elapsed := time.Since(start); elapsed >= 5*time.Second { + t.Fatalf("write command waited %s", elapsed) + } + if err == nil { + break + } + if n != 0 || !errors.Is(err, frontend.ErrBackendBusy) { + t.Fatalf("pending write lost retryability: n=%d err=%v", n, err) + } + } if err != nil || n != 4096 { t.Fatalf("pending write: n=%d err=%v", n, err) } - if elapsed := time.Since(start); elapsed >= 5*time.Second { - t.Fatalf("write waited %s", elapsed) - } got := make([]byte, 4096) if n, err := backend.Read(context.Background(), 33*4096, got); err != nil || n != 4096 || !bytes.Equal(got, block(0x74)) { t.Fatalf("attached write readback: n=%d err=%v", n, err) diff --git a/seaweed-block/core/storage/chunk_head_belt.go b/seaweed-block/core/storage/chunk_head_belt.go index c8e565097c..07cb375fc0 100644 --- a/seaweed-block/core/storage/chunk_head_belt.go +++ b/seaweed-block/core/storage/chunk_head_belt.go @@ -69,6 +69,11 @@ func (l *chunkLayerImpl) publicationOwnerChanged(cur chunkHead) bool { func (l *chunkLayerImpl) fencePublication(cur chunkHead) { l.stMu.Lock() l.fenced = true + if cur.Gen != 0 { + // Preserve the existing hydration identity invalidation. This is not + // settlement of our cut: remoteLSN and captured-map dirt stay unchanged. + l.head = copyHead(cur) + } l.stMu.Unlock() log.Printf("ALARM: chunk publisher fenced: local epoch=%d holder=%q, observed gen=%d epoch=%d holder=%q; keeping local data and intents", l.cfg.Epoch, l.cfg.Holder, cur.Gen, cur.Epoch, cur.Holder) l.abortCOW(errChunkLayerFenced) diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index a1f040ee87..2bf933ad6f 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -1175,7 +1175,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch keepPending() return chunkCutResult{}, fmt.Errorf("storage: capture CAS outcome unresolved (%v): %w: %w", casErr, errChunkCASOutcomeUnknown, gerr) } - if errors.Is(casErr, errChunkLayerFenced) || (!sameProposal(cur, next) && l.publicationOwnerChanged(cur)) { + if !sameProposal(cur, next) && l.publicationOwnerChanged(cur) { keepPending() // Close must keep these roots as well. l.fencePublication(cur) return chunkCutResult{}, errors.Join(errChunkLayerFenced, casErr) diff --git a/seaweed-block/core/storage/chunk_restore.go b/seaweed-block/core/storage/chunk_restore.go index b064fea133..ba5ce7c8e3 100644 --- a/seaweed-block/core/storage/chunk_restore.go +++ b/seaweed-block/core/storage/chunk_restore.go @@ -50,6 +50,11 @@ func ClaimChunkHead(ctx context.Context, remote ChunkRemote, cfg ChunkLayerConfi return head, nil } if head.Gen != 0 && head.Epoch == cfg.Epoch && head.Holder == cfg.Holder && !chunkHeadIsDetached(head) && head.DetachedBy == nil { + // A previous claim may have committed but timed out during cooling. + // An identical root alone does not prove that wait completed. + if err := confirmChunkTakeover(ctx, remote, head); err != nil { + return ChunkHead{}, err + } return head, allowed() // idempotent restart or settled prior claim } next := copyHead(head)