From 67998e5df66adc7a90e26d4776ced18d2aeac9f4 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 15 Nov 2026 22:49:45 -0800 Subject: [PATCH 1/4] storage: RED control: walstore recovery replays an older version over a newer one claude05's ruling (B), 2026-11-16 06:47Z. On main, recovery applies entries to the dirty map in physical range-scan order and putAt replaces unconditionally. When the persisted head/tail predate a wrap, the range scanned first can hold the newer version of an LBA and a later range the older one: the older version wins, silently. TestWALStoreRecoverLastWriterWinsAcrossScanOrder: LBA 20 written at the high end (LSN 4), then rewritten by a batch that wraps to offset 0 (LSN 5); after recovery LBA 20 reads the LSN 4 bytes. Co-Authored-By: Claude Opus 5.5 --- .../storage/walstore_recovery_lww_test.go | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 seaweed-block/core/storage/walstore_recovery_lww_test.go diff --git a/seaweed-block/core/storage/walstore_recovery_lww_test.go b/seaweed-block/core/storage/walstore_recovery_lww_test.go new file mode 100644 index 0000000000..1f319e574a --- /dev/null +++ b/seaweed-block/core/storage/walstore_recovery_lww_test.go @@ -0,0 +1,64 @@ +package storage + +import ( + "path/filepath" + "testing" +) + +// Recovery is last-writer-wins by LSN (claude05's ruling (B), 2026-11-16 +// 06:47Z). Two valid versions of one LBA survive in the WAL; the persisted +// head/tail predate the wrap, so the range scanned first holds the newer +// version (wrapped to offset 0) and the range scanned later the older one. +// Recovery must still serve the newer bytes. +func TestWALStoreRecoverLastWriterWinsAcrossScanOrder(t *testing.T) { + const ( + blockSize = 4096 + walSize = 24 * 1024 + ) + path := filepath.Join(t.TempDir(), "store.bin") + s := createWALStoreWithWALSizeForTest(t, path, 64, blockSize, walSize) + s.DisableAutoFlushForRecoveryTest() + for lba := uint32(0); lba < 3; lba++ { + if _, err := s.Write(lba, makeBlock(blockSize, byte(0x10+lba))); err != nil { + t.Fatal(err) + } + } + if _, err := s.Sync(); err != nil { + t.Fatal(err) + } + if err := s.flusher.flushOnce(); err != nil { // checkpoint 3: head/tail persisted here + t.Fatal(err) + } + older := makeBlock(blockSize, 0xA4) + if _, err := s.Write(20, older); err != nil { // LSN 4, at the high end + t.Fatal(err) + } + newer := makeBlock(blockSize, 0xB5) + s.enableMultiBlockRecordsForTest(true) + if _, err := s.WriteBatch(20, [][]byte{newer, makeBlock(blockSize, 0xB6)}); err != nil { // LSN 5-6, wraps to 0 + t.Fatal(err) + } + if _, err := s.Sync(); err != nil { + t.Fatal(err) + } + if e := recoverySnapshotEntriesByLBA(s.dm.snapshot())[20]; e.WALOffset != 0 || e.LSN != 5 { + t.Fatalf("newest LBA 20 entry at offset %d LSN %d; want the wrapped batch at 0, LSN 5", e.WALOffset, e.LSN) + } + crashRecoveryHardeningStore(t, s) + r, err := OpenWALStore(path) + if err != nil { + t.Fatal(err) + } + r.DisableAutoFlushForRecoveryTest() + t.Cleanup(func() { _ = r.Close() }) + if _, err := r.Recover(); err != nil { + t.Fatal(err) + } + got, err := r.Read(20) + if err != nil { + t.Fatal(err) + } + if got[0] != newer[0] { + t.Fatalf("LBA 20 after recovery = %02x; want the newer version %02x (LSN 5), not the older %02x (LSN 4)", got[0], newer[0], older[0]) + } +} From ca42d7681226ef7907829e8bff065ec33b12529b Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 15 Nov 2026 22:55:13 -0800 Subject: [PATCH 2/4] storage: walstore recovery is last-writer-wins by LSN claude05's ruling (B), 2026-11-16 06:47Z. Recovery replay now uses dirtyMap.putAtIfNewer: an entry replaces an LBA's dirty-map entry only when its LSN is higher. Range scan order is physical, and a persisted head/tail can predate a wrap, so a newer version can be scanned before an older one. This changes only the order in which the replayed entries take effect, never which entries are replayed; the runtime write paths keep putAt. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/core/storage/dirty_map.go | 22 ++++++++++++++++++++++ seaweed-block/core/storage/recovery.go | 6 +++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/seaweed-block/core/storage/dirty_map.go b/seaweed-block/core/storage/dirty_map.go index 89a6ff72b6..99f9723598 100644 --- a/seaweed-block/core/storage/dirty_map.go +++ b/seaweed-block/core/storage/dirty_map.go @@ -50,6 +50,28 @@ func (d *dirtyMap) put(lba, walOffset, lsn uint64, length uint32) { d.putAt(lba, walOffset, 0, lsn, length) } +// putAtIfNewer is putAt for recovery replay: last writer wins by LSN, so +// an older version of an LBA scanned after a newer one (range scan order +// is physical, not LSN order) never replaces it. +func (d *dirtyMap) putAtIfNewer( + lba, walOffset uint64, + dataOffset uint32, + lsn uint64, + length uint32, +) { + s := d.shard(lba) + s.mu.Lock() + if cur, ok := s.m[lba]; !ok || cur.lsn < lsn { + s.m[lba] = dirtyEntry{ + walOffset: walOffset, + dataOffset: dataOffset, + lsn: lsn, + length: length, + } + } + s.mu.Unlock() +} + func (d *dirtyMap) putAt( lba, walOffset uint64, dataOffset uint32, diff --git a/seaweed-block/core/storage/recovery.go b/seaweed-block/core/storage/recovery.go index 3b0f6d12e1..43ca18107c 100644 --- a/seaweed-block/core/storage/recovery.go +++ b/seaweed-block/core/storage/recovery.go @@ -165,7 +165,7 @@ func recoverWAL(fd *os.File, sb *superblock, dm *dirtyMap) (recoveryResult, erro case walEntryWrite: blocks := entry.Length / sb.BlockSize for i := uint32(0); i < blocks; i++ { - dm.putAt( + dm.putAtIfNewer( entry.LBA+uint64(i), pos, i*sb.BlockSize, entry.LSN, sb.BlockSize, ) @@ -178,7 +178,7 @@ func recoverWAL(fd *os.File, sb *superblock, dm *dirtyMap) (recoveryResult, erro if lsn <= checkpointLSN { continue } - dm.putAt( + dm.putAtIfNewer( entry.LBA+uint64(i), pos, i*sb.BlockSize, lsn, sb.BlockSize, ) @@ -190,7 +190,7 @@ func recoverWAL(fd *os.File, sb *superblock, dm *dirtyMap) (recoveryResult, erro blocks = 1 } for i := uint32(0); i < blocks; i++ { - dm.putAt( + dm.putAtIfNewer( entry.LBA+uint64(i), pos, i*sb.BlockSize, entry.LSN, sb.BlockSize, ) From 980de2b94a24ceb712527c1f11bd791aecdff7ce Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 16 Nov 2026 00:09:55 -0800 Subject: [PATCH 3/4] storage: guard: recovery keeps synced entries above an unpersisted frontier jump claude05's condition (1) for any prefix-cut recovery rule (2026-11-16 07:33Z). A rebuild installs BASE by WriteExtentDirect (no LSN) and applies WAL-lane entries with their real LSNs by ApplyEntry; AdvanceFrontier runs only at MarkBaseComplete and is in memory until a Sync. So a store can hold synced LSN 101-102 with a persisted checkpoint of 0 and no LSN 1..100. Recovery keeps them; a rule replaying only the contiguous LSN prefix above the checkpoint would keep none. This pins that, so no such rule lands. Co-Authored-By: Claude Opus 5.5 --- .../walstore_recovery_frontier_gap_test.go | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 seaweed-block/core/storage/walstore_recovery_frontier_gap_test.go diff --git a/seaweed-block/core/storage/walstore_recovery_frontier_gap_test.go b/seaweed-block/core/storage/walstore_recovery_frontier_gap_test.go new file mode 100644 index 0000000000..ff0af66903 --- /dev/null +++ b/seaweed-block/core/storage/walstore_recovery_frontier_gap_test.go @@ -0,0 +1,65 @@ +package storage + +import ( + "path/filepath" + "testing" +) + +// claude05's condition (1) for any prefix-cut recovery rule (2026-11-16 +// 07:33Z): an intentional LSN gap must not drop synced entries above it. A +// rebuild installs BASE through WriteExtentDirect (no LSN) and applies +// WAL-lane entries with their real LSNs through ApplyEntry; the frontier +// jump (AdvanceFrontier at MarkBaseComplete) is not persisted before them. +// So a replica can hold a synced LSN 101 with a persisted checkpoint of 0 and +// no LSN 1..100 anywhere. Recovery must keep it (a rule that replays only the +// contiguous LSN prefix above the checkpoint keeps nothing here). +func TestWALStoreRecoverKeepsSyncedEntriesAboveAnUnpersistedFrontierJump(t *testing.T) { + const blockSize = 4096 + path := filepath.Join(t.TempDir(), "store.bin") + s := createWALStoreWithWALSizeForTest(t, path, 64, blockSize, 24*1024) + s.DisableAutoFlushForRecoveryTest() + if err := s.WriteExtentDirect(0, makeBlock(blockSize, 0xBA)); err != nil { // BASE, no LSN + t.Fatal(err) + } + live := makeBlock(blockSize, 0x65) + if err := s.ApplyEntry(3, live, 101); err != nil { // session-live entry above the BASE hint + t.Fatal(err) + } + if err := s.ApplyEntry(4, makeBlock(blockSize, 0x66), 102); err != nil { + t.Fatal(err) + } + synced, err := s.Sync() + if err != nil { + t.Fatal(err) + } + if synced < 102 { + t.Fatalf("Sync frontier %d; want 102 (the applied entries are synced)", synced) + } + crashRecoveryHardeningStore(t, s) + if sb := diskSuperblockForGapTest(t, path); sb.WALCheckpointLSN != 0 { + t.Fatalf("persisted checkpoint %d; want 0 (the frontier jump is not persisted)", sb.WALCheckpointLSN) + } + r, err := OpenWALStore(path) + if err != nil { + t.Fatal(err) + } + r.DisableAutoFlushForRecoveryTest() + t.Cleanup(func() { _ = r.Close() }) + if _, err := r.Recover(); err != nil { + t.Fatalf("Recover: %v", err) + } + assertRecoveryHardeningBlock(t, r, 3, live) + assertRecoveryHardeningBlock(t, r, 4, makeBlock(blockSize, 0x66)) +} + +func diskSuperblockForGapTest(t *testing.T, path string) superblock { + t.Helper() + r, err := OpenWALStore(path) + if err != nil { + t.Fatal(err) + } + sb := *r.sb + r.committer.Stop() + _ = r.fd.Close() + return sb +} From 4ffa3c9a08caa4f1f1a3aa3404c8031730a1367c Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 16 Nov 2026 00:17:08 -0800 Subject: [PATCH 4/4] storage: frontier-gap guard reads the superblock without opening a store codex01's AMEND on 980de2b9 (tests-only P2): the helper opened a full WALStore and only stopped its committer and closed its fd, leaving the flusher and ticker alive. Read the superblock with a scoped file read (readSuperblock), and register the original store's cleanup right after open so a Fatal before the crash step still closes it. Co-Authored-By: Claude Opus 5.5 --- .../walstore_recovery_frontier_gap_test.go | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/seaweed-block/core/storage/walstore_recovery_frontier_gap_test.go b/seaweed-block/core/storage/walstore_recovery_frontier_gap_test.go index ff0af66903..c89d538294 100644 --- a/seaweed-block/core/storage/walstore_recovery_frontier_gap_test.go +++ b/seaweed-block/core/storage/walstore_recovery_frontier_gap_test.go @@ -1,6 +1,7 @@ package storage import ( + "os" "path/filepath" "testing" ) @@ -18,6 +19,12 @@ func TestWALStoreRecoverKeepsSyncedEntriesAboveAnUnpersistedFrontierJump(t *test path := filepath.Join(t.TempDir(), "store.bin") s := createWALStoreWithWALSizeForTest(t, path, 64, blockSize, 24*1024) s.DisableAutoFlushForRecoveryTest() + crashed := false + t.Cleanup(func() { + if !crashed { + _ = s.Close() + } + }) if err := s.WriteExtentDirect(0, makeBlock(blockSize, 0xBA)); err != nil { // BASE, no LSN t.Fatal(err) } @@ -36,6 +43,7 @@ func TestWALStoreRecoverKeepsSyncedEntriesAboveAnUnpersistedFrontierJump(t *test t.Fatalf("Sync frontier %d; want 102 (the applied entries are synced)", synced) } crashRecoveryHardeningStore(t, s) + crashed = true if sb := diskSuperblockForGapTest(t, path); sb.WALCheckpointLSN != 0 { t.Fatalf("persisted checkpoint %d; want 0 (the frontier jump is not persisted)", sb.WALCheckpointLSN) } @@ -52,14 +60,18 @@ func TestWALStoreRecoverKeepsSyncedEntriesAboveAnUnpersistedFrontierJump(t *test assertRecoveryHardeningBlock(t, r, 4, makeBlock(blockSize, 0x66)) } +// diskSuperblockForGapTest reads the on-disk superblock with a plain file +// read: no store, no flusher or committer. func diskSuperblockForGapTest(t *testing.T, path string) superblock { t.Helper() - r, err := OpenWALStore(path) + f, err := os.Open(path) + if err != nil { + t.Fatal(err) + } + defer f.Close() + sb, err := readSuperblock(f) if err != nil { t.Fatal(err) } - sb := *r.sb - r.committer.Stop() - _ = r.fd.Close() return sb }