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 0f79da6784..b15bd02e74 100644 --- a/seaweed-block/core/storage/recovery.go +++ b/seaweed-block/core/storage/recovery.go @@ -179,7 +179,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, ) @@ -192,7 +192,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, ) @@ -204,7 +204,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, ) 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..c89d538294 --- /dev/null +++ b/seaweed-block/core/storage/walstore_recovery_frontier_gap_test.go @@ -0,0 +1,77 @@ +package storage + +import ( + "os" + "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() + 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) + } + 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) + 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) + } + 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)) +} + +// 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() + f, err := os.Open(path) + if err != nil { + t.Fatal(err) + } + defer f.Close() + sb, err := readSuperblock(f) + if err != nil { + t.Fatal(err) + } + return sb +} 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]) + } +}