From 3dfb612a807c8282001db9d959ec57c1947b5813 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 15 Nov 2026 12:31:49 -0800 Subject: [PATCH] storage: Recover holds the capture barrier shared, like a write Recover republishes the dirty map but did not take captureMu, so it could run inside a quiesced cut: after the cut's Sync fixed H and before its copy, a same-open Recover could publish a newer complete record (H+1) and the cut would copy those bytes into a HEAD labelled H. Recover now holds captureMu shared (order captureMu -> lifecycleMu -> mu), as Write, WriteBatch, ApplyEntry and WriteExtentDirect do; nothing calls Recover under captureMu. Control: a real partial batch pwrite leaves a complete record at H+1; the cut parks right after its barrier Sync fixed H and starts a same-open Recover, which waits for the barrier; HEAD at H holds no post-H bytes and the next cut publishes the recovered bytes. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/core/storage/chunk_layer.go | 4 ++ .../core/storage/chunk_layer_precopy_test.go | 72 +++++++++++++++++++ seaweed-block/core/storage/walstore.go | 10 ++- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index a16f8fb488..d88e0a35fb 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -106,6 +106,7 @@ type chunkLayerImpl struct { testAfterAdmitHook func() // right after a capture is admitted testAfterStageHook func(round int) // after each pre-copy staging round testAfterStageReadHook func(idx uint64) // after a staging read, before its result is kept + testAfterBarrierSyncHook func(h uint64) // under the capture barrier, right after Sync fixed H syncDirFn func(dir string) error // directory fsync after a rename; syncDir in production // Absence: chunks whose local bytes are not authoritative (fresh host, @@ -685,6 +686,9 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch barrier := time.Now() h, err := l.s.Sync() st.SyncNs = time.Since(barrier).Nanoseconds() + if hook := l.testAfterBarrierSyncHook; hook != nil && err == nil { + hook(h) + } if err != nil { l.s.captureMu.Unlock() st.BarrierPauseNs = time.Since(barrier).Nanoseconds() diff --git a/seaweed-block/core/storage/chunk_layer_precopy_test.go b/seaweed-block/core/storage/chunk_layer_precopy_test.go index 615df7ec48..b5ac900287 100644 --- a/seaweed-block/core/storage/chunk_layer_precopy_test.go +++ b/seaweed-block/core/storage/chunk_layer_precopy_test.go @@ -12,6 +12,7 @@ import ( "io" "strings" "testing" + "time" ) // precopyLayer attaches a fresh layer with the given barrier bound and @@ -349,3 +350,74 @@ func TestChunkPrecopy_RecoverAfterStagingInvalidatesTheStage(t *testing.T) { } assertNoIntentsLeft(t, f) } + +// partialBatchPrefix leaves a complete, CRC-valid record for want at the +// next LSN plus a torn second record through a real partial pwrite; the +// failed batch publishes neither (same-open Recover would). +func partialBatchPrefix(t *testing.T, s *WALStore, want []byte) { + t.Helper() + limit := walEntryHeaderSize + ctBlockSize + walEntryHeaderSize + 17 + s.wal.batchWriteAt = func(data []byte, off int64) (int, error) { + if len(data) <= limit { + return 0, errors.New("fixture needs two whole records") + } + n, err := s.fd.WriteAt(data[:limit], off) + if err != nil { + return n, err + } + return n, io.ErrShortWrite + } + _, err := s.WriteBatch(0, [][]byte{want, makeBlock(ctBlockSize, 0xb8)}) + s.wal.batchWriteAt = nil + if !errors.Is(err, io.ErrShortWrite) { + t.Fatalf("partial batch: %v", err) + } +} + +// A cut parked right after its barrier Sync fixed H: a same-open Recover +// that would republish a newer complete record (H+1) waits for the +// barrier, so the cut's HEAD at H never holds post-H bytes; the recovered +// bytes are published by the next cut. +func TestChunkCut_RecoverWaitsForTheCaptureBarrier(t *testing.T) { + s, f, l, w := precopyLayer(t, 0, 2) // no bound: the barrier copies all + newer := makeBlock(ctBlockSize, 0xa7) + partialBatchPrefix(t, s, newer) + recovered := make(chan error, 1) + parked := false + l.testAfterBarrierSyncHook = func(h uint64) { + parked = true + go func() { _, err := s.Recover(); recovered <- err }() + select { + case err := <-recovered: + t.Errorf("Recover ran inside the capture barrier after Sync fixed H=%d (err %v)", h, err) + case <-time.After(300 * time.Millisecond): // held: it waits for the barrier + } + } + r, err := l.Cut(ctTimeout(t)) + if err != nil { + t.Fatal(err) + } + if !parked { + t.Fatal("the cut never reached the post-Sync hook") + } + assertEveryHeadIsPrefixImage(t, f, w) // HEAD at H is the image at H: no 0xa7 + if got := f.imageOf(t, r.Head)[:ctBlockSize]; bytes.Equal(got, newer) { + t.Fatalf("HEAD at H=%d holds the post-H recovered bytes", r.H) + } + select { + case err := <-recovered: + if err != nil { + t.Fatalf("Recover after the barrier: %v", err) + } + case <-time.After(ctLocalBound): + t.Fatal("Recover never ran after the barrier was released") + } + l.testAfterBarrierSyncHook = nil + r2, err := l.Cut(ctTimeout(t)) + if err != nil { + t.Fatal(err) + } + if got := f.imageOf(t, r2.Head)[:ctBlockSize]; !bytes.Equal(got, newer) || r2.H <= r.H { + t.Fatalf("next cut H=%d (prev %d): LBA 0 %#x..., want the recovered %#x...", r2.H, r.H, got[0], newer[0]) + } +} diff --git a/seaweed-block/core/storage/walstore.go b/seaweed-block/core/storage/walstore.go index 3f01bc111f..f94863e6db 100644 --- a/seaweed-block/core/storage/walstore.go +++ b/seaweed-block/core/storage/walstore.go @@ -67,8 +67,8 @@ type WALStore struct { lifecycleMu sync.RWMutex mu sync.RWMutex // captureMu is the chunk layer's capture write barrier: Write, - // WriteBatch, ApplyEntry and WriteExtentDirect hold it shared (before - // lifecycleMu); a quiesced cut holds it exclusively. Order: + // WriteBatch, ApplyEntry, WriteExtentDirect and Recover hold it shared + // (before lifecycleMu); a quiesced cut holds it exclusively. Order: // captureMu -> lifecycleMu -> mu. captureMu sync.RWMutex // chunk is the attached chunk layer's hooks; nil when none. @@ -431,6 +431,12 @@ func (s *WALStore) FlusherInstrumentation() FlusherInstrumentationStatus { // Idempotent: calling Recover twice on the same on-disk state yields // identical results. func (s *WALStore) Recover() (uint64, error) { + // Recover republishes the dirty map like a write: it holds the capture + // barrier shared, so it can never publish bytes between a quiesced + // cut's Sync (which fixes H) and that cut's copy. + // Order: captureMu -> lifecycleMu -> mu. + s.captureMu.RLock() + defer s.captureMu.RUnlock() s.lifecycleMu.RLock() defer s.lifecycleMu.RUnlock() s.mu.Lock()