From dc0e45f370abc6c8e12cad3b06b2f30bf67571ea Mon Sep 17 00:00:00 2001 From: pingqiu Date: Thu, 26 Nov 2026 16:42:20 -0800 Subject: [PATCH] test(storage): a replayed write is not drained before the chunk layer attaches (red) A synced write into an absent chunk, a crash, a reopen: the flusher drains the replayed write to the extent before the chunk layer attaches, so the block is not marked local and the next fetch of its chunk installs the snapshot's block over it (reads 0x89, want 0xE9). Adds the owner's hold call (HoldExtentDrainUntilChunkLayer, a no-op here) and the test. Co-Authored-By: Claude Opus 5.5 --- .../chunk_replay_before_attach_test.go | 67 +++++++++++++++++++ seaweed-block/core/storage/walstore.go | 7 ++ 2 files changed, 74 insertions(+) create mode 100644 seaweed-block/core/storage/chunk_replay_before_attach_test.go diff --git a/seaweed-block/core/storage/chunk_replay_before_attach_test.go b/seaweed-block/core/storage/chunk_replay_before_attach_test.go new file mode 100644 index 0000000000..6480b75426 --- /dev/null +++ b/seaweed-block/core/storage/chunk_replay_before_attach_test.go @@ -0,0 +1,67 @@ +package storage + +import ( + "bytes" + "testing" +) + +// A synced guest write into an absent chunk, then a crash: on reopen the +// replayed write must not reach the extent before the chunk layer attaches, +// or it would not be marked local and the next fetch of its chunk would +// install the snapshot's block over it. The store's owner holds the drain +// (as the durable provider does, before Recover); a flush before attach +// drains nothing, and after attach the block reads back as written and is +// drained as local. +func TestReplayedWriteIsNotDrainedBeforeTheChunkLayerAttaches(t *testing.T) { + s, path, crash := crashableStore(t) + f := newFakeChunkRemote() + cfg := ctConfig(1) + layer := mustAttach(t, s, f, cfg) + for lba := uint32(8); lba < 12; lba++ { + if _, err := s.Write(lba, makeBlock(ctBlockSize, byte(0x80+lba))); err != nil { + t.Fatal(err) + } + } + mustSyncFlush(t, s) + if _, err := layer.Capture(ctTimeout(t)); err != nil { + t.Fatal(err) + } + if err := layer.EvictChunk(2); err != nil { + t.Fatalf("evict: %v", err) + } + newer := makeBlock(ctBlockSize, 0xE9) + if _, err := s.Write(9, newer); err != nil { + t.Fatal(err) + } + if _, err := s.Sync(); err != nil { + t.Fatal(err) + } + crash() + + // Reopen as the provider does: hold the drain, then recover. + s2, err := OpenWALStore(path) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { closeBounded(t, s2) }) + s2.DisableAutoFlushForRecoveryTest() + s2.HoldExtentDrainUntilChunkLayer() + if _, err := s2.Recover(); err != nil { + t.Fatalf("local recovery: %v", err) + } + if err := s2.flusher.flushOnce(); err != nil { // a flusher tick before attach + t.Fatal(err) + } + mustAttach(t, s2, f, cfg) + if got, err := s2.Read(9); err != nil || !bytes.Equal(got, newer) { + t.Fatalf("after restart LBA 9 = %#x (err %v); want the synced write %#x", got[0], err, newer[0]) + } + // After attach the drain runs, marks the block local, and a later read + // still returns the write (no fetch installs over it). + if err := s2.flusher.flushOnce(); err != nil { + t.Fatal(err) + } + if got, err := s2.Read(9); err != nil || !bytes.Equal(got, newer) { + t.Fatalf("after the drain LBA 9 = %#x (err %v); want the synced write %#x", got[0], err, newer[0]) + } +} diff --git a/seaweed-block/core/storage/walstore.go b/seaweed-block/core/storage/walstore.go index ca937ba73f..231011552d 100644 --- a/seaweed-block/core/storage/walstore.go +++ b/seaweed-block/core/storage/walstore.go @@ -700,6 +700,13 @@ func (s *WALStore) DisableAutoFlushForRecoveryTest() { } } +// HoldExtentDrainUntilChunkLayer is called by the owner of a store opened +// for chunk use, before Recover: no dirty block is drained to the extent +// until a chunk layer is attached. A block drained earlier would not be +// marked local, and a later fetch of its absent chunk would install the +// remote block over the acknowledged write. +func (s *WALStore) HoldExtentDrainUntilChunkLayer() {} + func (s *WALStore) readFromExtent(lba uint32) ([]byte, error) { return s.readFromExtentContext(context.Background(), lba) }