From e910449f70863234c3d735e002e3d9e4e24ee037 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 15 Nov 2026 23:04:18 -0800 Subject: [PATCH] storage: RED controls for the #155 review (detection only) codex01's review of f8d43653 (V:/share/rdma/codex01-block155-review- 20261116T0644Z/REVIEW.md) and claude05's ruling (A), 06:47Z. Against the reviewed production code (its replay cutoff and positional smartwal fail-closed; smartwal warning accessors stubbed so the tests build): - a damaged STALE record in a released range drops retained, synced LSN 3 (TestWALStoreRecoverStaleReleasedGarbageKeepsRetainedData); - a torn smartwal slot accepted by one recovery becomes wal_corrupt after a frontier jump and a new sync (TestSmartWALTornSlotSurvivesAFrontierJump); - a smartwal mid-history hole is an outage instead of a warning (TestDurableStartup_Issue51_SmartWALHoleIsAWarningNotAnOutage); - plus codex01's advisory: a healthy primary stays healthy with a warning (TestStatusServer_StatusCarriesRecoveryWarnings..., which passes either way: the plumbing never forced health). Co-Authored-By: Claude Opus 5.5 --- .../durable_startup_slotcrc_test.go | 31 +++-- .../core/host/volume/status_warnings_test.go | 17 ++- .../smartwal/smartwal_hole_detection_test.go | 106 ++++++++++++++++++ seaweed-block/core/storage/smartwal/store.go | 6 + .../storage/walstore_midhistory_hole_test.go | 78 ++++++++----- 5 files changed, 201 insertions(+), 37 deletions(-) create mode 100644 seaweed-block/core/storage/smartwal/smartwal_hole_detection_test.go diff --git a/seaweed-block/cmd/blockvolume/durable_startup_slotcrc_test.go b/seaweed-block/cmd/blockvolume/durable_startup_slotcrc_test.go index df0760aea1..8c99a5b2cf 100644 --- a/seaweed-block/cmd/blockvolume/durable_startup_slotcrc_test.go +++ b/seaweed-block/cmd/blockvolume/durable_startup_slotcrc_test.go @@ -106,16 +106,33 @@ func TestDurableStartup_Issue51_CorruptNewerSlotIsWALCorrupt(t *testing.T) { } } -// claude05's ruling (1) for smartwal: a corrupted committed record with a -// later valid record after it (a mid-history hole) fails closed: -// wal_corrupt, readiness blocked. -func TestDurableStartup_Issue51_SmartWALMidHistoryHoleIsWALCorrupt(t *testing.T) { +// claude05's ruling (A), 06:47Z: detection only for smartwal too. A +// corrupted committed slot with later valid records starts as before +// (ready), and the durable status carries the counted warning. +func TestDurableStartup_Issue51_SmartWALHoleIsAWarningNotAnOutage(t *testing.T) { cfg := durable.ProviderConfig{Impl: durable.ImplSmartWAL, StorageRoot: t.TempDir(), BlockSize: 4096, NumBlocks: 16} path := writeSmartWAL(t, cfg, []uint32{0, 1, 2, 3}) corruptSlotCRC(t, path, 1) // LSN 2; LSN 3 and 4 stay valid - outcome, h := startAfterCorruption(t, cfg) - if outcome != durableStartupFaulted || len(h.reported) != 1 || h.reported[0] != volume.UnhealthyWALCorrupt || len(h.blocked) == 0 { - t.Fatalf("mid-history hole: outcome=%v reported=%v blocked=%v; want faulted, wal_corrupt, readiness blocked", outcome, h.reported, h.blocked) + dp, err := durable.NewDurableProvider(cfg, healthyView{}) + if err != nil { + t.Fatal(err) + } + defer dp.Close() + h := &spyHost{} + outcome, evidence, err := openAndRecoverDurable(dp, h, "v1", 5*time.Second) + if outcome != durableStartupReady || len(h.reported) != 0 { + t.Fatalf("smartwal hole: outcome=%v reported=%v evidence=%q err=%v; want ready", outcome, h.reported, evidence, err) + } + warned := false + for _, st := range dp.DurableStatuses() { + for _, w := range st.Warnings { + if strings.HasPrefix(w, storage.WarningWALHoleSuspected+":") && st.HolesSuspected == 1 { + warned = true + } + } + } + if !warned { + t.Fatalf("durable statuses %+v; want the counted %s warning", dp.DurableStatuses(), storage.WarningWALHoleSuspected) } } diff --git a/seaweed-block/core/host/volume/status_warnings_test.go b/seaweed-block/core/host/volume/status_warnings_test.go index 7cf43c77a2..879ab1826c 100644 --- a/seaweed-block/core/host/volume/status_warnings_test.go +++ b/seaweed-block/core/host/volume/status_warnings_test.go @@ -6,13 +6,25 @@ import ( "net/http" "testing" + "github.com/seaweedfs/seaweed-block/core/engine" "github.com/seaweedfs/seaweed-block/core/frontend/durable" ) // Issue #51 (walstore interim): the durable store's typed recovery warning // is visible on /status and does not change Healthy or readiness. func TestStatusServer_StatusCarriesRecoveryWarningsWithoutChangingHealth(t *testing.T) { - s := NewStatusServer(NewAdapterProjectionView(stubProjector{}, "v1", "r1", nil)) + t.Run("not-ready replica", func(t *testing.T) { + statusWarningsCase(t, NewAdapterProjectionView(stubProjector{}, "v1", "r1", nil), false) + }) + t.Run("healthy primary", func(t *testing.T) { + // codex01's advisory: a healthy primary stays healthy with a warning. + p := stubProjector{p: engine.ReplicaProjection{Mode: engine.ModeHealthy, Epoch: 1, EndpointVersion: 1}} + statusWarningsCase(t, NewAdapterProjectionView(p, "v1", "r1", stubProbe{yes: false}), true) + }) +} + +func statusWarningsCase(t *testing.T, view *AdapterProjectionView, wantHealthy bool) { + s := NewStatusServer(view) addr, err := s.Start("127.0.0.1:0") if err != nil { t.Fatal(err) @@ -32,6 +44,9 @@ func TestStatusServer_StatusCarriesRecoveryWarningsWithoutChangingHealth(t *test return p } before := get() + if before.Healthy != wantHealthy || before.FrontendPrimaryReady != wantHealthy { + t.Fatalf("before the warning: healthy=%t ready=%t; want %t", before.Healthy, before.FrontendPrimaryReady, wantHealthy) + } s.SetDurableStatusSource(stubDurableStatusSource{vols: []durable.VolumeStatus{{ VolumeID: "v1", Impl: "walstore", Warnings: []string{"wal_hole_suspected: WAL entry at offset=4134 fails its check"}, HolesSuspected: 1, }}}) diff --git a/seaweed-block/core/storage/smartwal/smartwal_hole_detection_test.go b/seaweed-block/core/storage/smartwal/smartwal_hole_detection_test.go new file mode 100644 index 0000000000..74463615f4 --- /dev/null +++ b/seaweed-block/core/storage/smartwal/smartwal_hole_detection_test.go @@ -0,0 +1,106 @@ +package smartwal + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "github.com/seaweedfs/seaweed-block/core/storage" +) + +func flipSlotCRC(t *testing.T, path string, lsn uint64) { + t.Helper() + _, records, err := InspectRecords(path) + if err != nil { + t.Fatal(err) + } + for _, r := range records { + if r.LSN != lsn { + continue + } + f, err := os.OpenFile(path, os.O_RDWR, 0) + if err != nil { + t.Fatal(err) + } + defer f.Close() + b := make([]byte, 1) + if _, err := f.ReadAt(b, r.Offset+31); err != nil { + t.Fatal(err) + } + b[0] ^= 0xFF + if _, err := f.WriteAt(b, r.Offset+31); err != nil { + t.Fatal(err) + } + return + } + t.Fatalf("no record LSN %d", lsn) +} + +func warnsHole(s *Store) bool { + for _, w := range s.RecoveryWarnings() { + if strings.HasPrefix(w, storage.WarningWALHoleSuspected+":") { + return true + } + } + return false +} + +// codex01's r1 P1, under claude05's 06:47Z ruling (detection only): a torn +// slot accepted by one recovery stays a recoverable warning after a +// frontier jump and a new synced write; nothing infers its generation from +// its position in the ring. +func TestSmartWALTornSlotSurvivesAFrontierJump(t *testing.T) { + path := filepath.Join(t.TempDir(), "s.sw") + s, err := CreateStore(path, 16, 4096) + if err != nil { + t.Fatal(err) + } + for i, lba := range []uint32{0, 1} { // LSN 1, 2 + data := make([]byte, 4096) + data[0] = byte(0xE0 + i) + if _, err := s.Write(lba, data); err != nil { + t.Fatal(err) + } + } + if _, err := s.Sync(); err != nil { + t.Fatal(err) + } + if err := s.Close(); err != nil { + t.Fatal(err) + } + flipSlotCRC(t, path, 2) // LSN 2 torn + + s, err = OpenStore(path) + if err != nil { + t.Fatal(err) + } + if _, err := s.Recover(); err != nil { + t.Fatalf("first recovery: %v; want the torn tail accepted", err) + } + firstWarned, firstHoles := warnsHole(s), s.HolesSuspected() + s.AdvanceFrontier(4) + data := make([]byte, 4096) + data[0] = 0xEF + if _, err := s.Write(2, data); err != nil { // LSN 5 + t.Fatal(err) + } + if _, err := s.Sync(); err != nil { + t.Fatal(err) + } + if err := s.Close(); err != nil { + t.Fatal(err) + } + + s, err = OpenStore(path) + if err != nil { + t.Fatal(err) + } + defer s.Close() + if _, err := s.Recover(); err != nil { + t.Fatalf("second recovery after the frontier jump: %v; want recovered (the old torn slot is not corruption)", err) + } + if !firstWarned || firstHoles != 1 || !warnsHole(s) { + t.Fatalf("warnings: first %t (%d), second %v; want the counted %s both times", firstWarned, firstHoles, s.RecoveryWarnings(), storage.WarningWALHoleSuspected) + } +} diff --git a/seaweed-block/core/storage/smartwal/store.go b/seaweed-block/core/storage/smartwal/store.go index e94b150285..8913f4d38b 100644 --- a/seaweed-block/core/storage/smartwal/store.go +++ b/seaweed-block/core/storage/smartwal/store.go @@ -528,3 +528,9 @@ func (s *Store) SyncCount() uint64 { return s.syncs.Load() } // Compile-time assertion: Store satisfies the LogicalStorage contract. var _ storage.LogicalStorage = (*Store)(nil) + +// RecoveryWarnings: RED stub (the reviewed head reports none). +func (s *Store) RecoveryWarnings() []string { return nil } + +// HolesSuspected: RED stub. +func (s *Store) HolesSuspected() uint64 { return 0 } diff --git a/seaweed-block/core/storage/walstore_midhistory_hole_test.go b/seaweed-block/core/storage/walstore_midhistory_hole_test.go index da21ca4f24..b5b37fc802 100644 --- a/seaweed-block/core/storage/walstore_midhistory_hole_test.go +++ b/seaweed-block/core/storage/walstore_midhistory_hole_test.go @@ -6,11 +6,10 @@ import ( "testing" ) -// Issue #51, walstore interim (claude05's ruling 2026-11-16 06:14Z): a -// plain power loss must never become an outage, so a WAL hole followed by -// valid records does not fail recovery. It is detected, logged, counted and -// reported as the typed warning wal_hole_suspected, and nothing after the -// hole is replayed in any range. +// Issue #51, walstore (claude05's ruling 2026-11-16 06:47Z): pure +// detection. A WAL hole followed by valid records is logged, counted and +// reported as the typed warning wal_hole_suspected; nothing about what is +// replayed, where replay stops, or whether the volume opens changes. // writeSyncedEntries writes one block to each of lbas (in order), syncs, // and returns the store with auto-flush off (nothing checkpointed) and @@ -80,8 +79,8 @@ func holeWarning(s *WALStore) bool { return false } -// (a) A corrupted mid-history synced record: recovery succeeds to the -// prefix before it, warns (typed, counted), and replays nothing after it. +// (a) A corrupted mid-history synced record: recovery opens as before +// (this range stops at it), and warns (typed, counted). func TestWALStoreRecoverWarnsOnMidHistoryHole(t *testing.T) { s, path, offs := writeSyncedEntries(t, []uint32{0, 1, 2, 3}) flipDataByte(t, s, offs[1]) @@ -132,10 +131,13 @@ func TestWALStoreRecoverKeepsATornTail(t *testing.T) { } } -// (c) No replay past a hole across wrapped ranges: the older record at the -// high end of the WAL is corrupted; the newer batch that wrapped to offset -// 0 must not be replayed. -func TestWALStoreRecoverNoReplayPastAHoleAcrossWrappedRanges(t *testing.T) { +// (c) A hole across wrapped ranges (the older record at the high end +// corrupted, the newer batch wrapped to offset 0) is no outage. Detection +// here is per range: when the newer entries were scanned first there is +// nothing "later" left to find, and telling generations apart across +// ranges (like not replaying past such a hole) is the post-M4 watermark +// work. +func TestWALStoreRecoverWrappedHoleIsNoOutage(t *testing.T) { const ( blockSize = 4096 walSize = 24 * 1024 @@ -154,35 +156,53 @@ func TestWALStoreRecoverNoReplayPastAHoleAcrossWrappedRanges(t *testing.T) { if err := s.flusher.flushOnce(); err != nil { t.Fatal(err) } - if _, err := s.Write(20, makeBlock(blockSize, 0xA4)); err != nil { // high end + if _, err := s.Write(20, makeBlock(blockSize, 0xA4)); err != nil { t.Fatal(err) } s.enableMultiBlockRecordsForTest(true) - if _, err := s.WriteBatch(24, [][]byte{makeBlock(blockSize, 0xB5), makeBlock(blockSize, 0xB6)}); err != nil { // wraps to 0 + if _, err := s.WriteBatch(24, [][]byte{makeBlock(blockSize, 0xB5), makeBlock(blockSize, 0xB6)}); err != nil { t.Fatal(err) } if _, err := s.Sync(); err != nil { t.Fatal(err) } offs := walOffsetsByLBA(s) - if offs[24] != 0 || offs[20] == 0 { - t.Fatalf("offsets high=%d low=%d; want the batch wrapped to 0", offs[20], offs[24]) - } flipDataByte(t, s, offs[20]) r, _, err := reopenAfterCrash(t, s, path) if err != nil { - t.Fatalf("Recover: %v; want no outage", err) - } - if !holeWarning(r) { - t.Fatalf("warnings %v; want %s", r.RecoveryWarnings(), WarningWALHoleSuspected) - } - for _, lba := range []uint32{24, 25} { - got, err := r.Read(lba) - if err != nil { - t.Fatal(err) - } - if got[0] == 0xB5 || got[0] == 0xB6 { - t.Fatalf("LBA %d replayed from past the hole (byte %02x)", lba, got[0]) - } + t.Fatalf("Recover = %v; want no outage", err) } + _ = r +} + +// codex01's r1 P1: a damaged STALE record in a released range (below the +// checkpoint, outside the retained window) must not cost retained data. +// Layout: checkpoint 2, persisted tail at S, head at 3S; LSN 1 (released) +// at 0 damaged, LSN 2 checkpointed at S, LSN 3 synced and unflushed at 2S. +// LSN 3 must be replayed with its exact bytes. +func TestWALStoreRecoverStaleReleasedGarbageKeepsRetainedData(t *testing.T) { + const blockSize = 4096 + s, path, offs := writeSyncedEntries(t, []uint32{0, 1, 2}) + S := uint64(walEntryHeaderSize + blockSize) + if offs[0] != 0 || offs[1] != S || offs[2] != 2*S { + t.Fatalf("offsets %v; want 0, S, 2S", offs) + } + sb := *s.sb + sb.WALCheckpointLSN, sb.WALTail, sb.WALHead = 2, S, 3*S + buf := newSimpleByteBuf() + if _, err := sb.writeTo(buf); err != nil { + t.Fatal(err) + } + if _, err := s.fd.WriteAt(buf.bytes(), 0); err != nil { + t.Fatal(err) + } + if err := s.fd.Sync(); err != nil { + t.Fatal(err) + } + flipDataByte(t, s, offs[0]) // the released LSN 1 + r, _, err := reopenAfterCrash(t, s, path) + if err != nil { + t.Fatalf("Recover: %v", err) + } + assertRecoveryHardeningBlock(t, r, 2, makeBlock(blockSize, 0xB2)) // LSN 3, exact bytes }