From 3f73b56f1880d8e10e7dc9342f233d730aa5b002 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 15 Nov 2026 14:11:04 -0800 Subject: [PATCH] volume: typed recovery reasons wiring and controls (RED) #133 (follow-up to #128 rule 6): wiring with the classifier stubbed, plus controls. - volume: UnhealthyWALCorrupt / UnhealthyIOError; LocalRecoveryHealthReason (stubbed here: classifies nothing); Host.ReportLocalRecoveryFailure records the classified reason, cleared by ClearLocalReadinessBlock; the watchdog reports a recorded local failure for a primary-assigned replica at once (after the G1 latch, before readiness and the timeout). - blockvolume: a failed durable RecoverVolume calls ReportLocalRecoveryFailure(recErr) next to BlockLocalReadiness. - controls: the classifier maps WALIntegrity (typed or the sentinel, wrapped or not) to wal_corrupt, SubstrateIO / fs.PathError / io.ErrUnexpectedEOF / EIO to io_error, and anything else (WALRecycled, untyped) to nothing; the real chain (a corrupted smartwal store -> durable.Recover -> ReportLocalRecoveryFailure) makes a primary-assigned replica report wal_corrupt at once, cleared by a clean recovery; a replica that is not primary-assigned is not flagged; an unclassified failure reports nothing early and recovery_timeout still covers it. Refs #133 Co-Authored-By: Claude Opus 5.5 --- seaweed-block/cmd/blockvolume/main.go | 4 + seaweed-block/core/host/volume/host.go | 4 + .../host/volume/recovery_typed_reason_test.go | 132 ++++++++++++++++++ .../core/host/volume/recovery_watchdog.go | 65 ++++++++- .../host/volume/recovery_watchdog_test.go | 16 +-- 5 files changed, 209 insertions(+), 12 deletions(-) create mode 100644 seaweed-block/core/host/volume/recovery_typed_reason_test.go diff --git a/seaweed-block/cmd/blockvolume/main.go b/seaweed-block/cmd/blockvolume/main.go index e4ce872d60..96d7253b31 100644 --- a/seaweed-block/cmd/blockvolume/main.go +++ b/seaweed-block/cmd/blockvolume/main.go @@ -642,6 +642,10 @@ func run(f flags) int { if recErr != nil { fmt.Fprintln(os.Stderr, "blockvolume: durable recovery failed:", report.Evidence) h.BlockLocalReadiness(report.Evidence) + // #133: a classifiable failure (wal_corrupt / io_error) is + // reported at once by a primary assignment, not after the + // recover timeout. + h.ReportLocalRecoveryFailure(recErr) return waitFaultedDurableRecovery(h, status, durableProv, report.Evidence) } else { fmt.Fprintln(os.Stderr, "blockvolume: durable recovered:", report.Evidence) diff --git a/seaweed-block/core/host/volume/host.go b/seaweed-block/core/host/volume/host.go index 4359be37c6..d0becda7cb 100644 --- a/seaweed-block/core/host/volume/host.go +++ b/seaweed-block/core/host/volume/host.go @@ -162,6 +162,9 @@ type Host struct { localReadinessBlocked atomic.Bool localReadinessReason atomic.Value // string + // localRecoveryReason is the typed reason local durable recovery + // failed (#133); empty when none or unclassified. + localRecoveryReason atomic.Value // string readinessReplayMu sync.Mutex readinessReplayFact *control.AssignmentFact @@ -291,6 +294,7 @@ func (h *Host) BlockLocalReadiness(reason string) { func (h *Host) ClearLocalReadinessBlock() { h.localReadinessReason.Store("") h.localReadinessBlocked.Store(false) + h.localRecoveryReason.Store("") } func (h *Host) localReadinessBlock() (bool, string) { diff --git a/seaweed-block/core/host/volume/recovery_typed_reason_test.go b/seaweed-block/core/host/volume/recovery_typed_reason_test.go new file mode 100644 index 0000000000..34be1a6394 --- /dev/null +++ b/seaweed-block/core/host/volume/recovery_typed_reason_test.go @@ -0,0 +1,132 @@ +package volume + +import ( + "errors" + "fmt" + "io" + "io/fs" + "os" + "path/filepath" + "syscall" + "testing" + "time" + + "github.com/seaweedfs/seaweed-block/core/frontend/durable" + "github.com/seaweedfs/seaweed-block/core/storage" + "github.com/seaweedfs/seaweed-block/core/storage/smartwal" +) + +// #133: a local durable recovery failure is classified into a typed +// unhealthy reason, which a primary-assigned replica reports at once. + +func TestLocalRecoveryHealthReason_Classifies(t *testing.T) { + for _, tc := range []struct { + name string + err error + want string + }{ + {"nil", nil, ""}, + {"wal integrity", storage.NewWALIntegrityFailure(nil, "crc mismatch"), UnhealthyWALCorrupt}, + {"wal integrity wrapped", fmt.Errorf("recover: %w", storage.NewWALIntegrityFailure(nil, "x")), UnhealthyWALCorrupt}, + {"integrity sentinel", fmt.Errorf("x: %w", storage.ErrWALIntegrityFault), UnhealthyWALCorrupt}, + {"substrate io", storage.NewSubstrateIOFailure(errors.New("read"), "scan"), UnhealthyIOError}, + {"path error", &fs.PathError{Op: "read", Path: "/v", Err: syscall.EIO}, UnhealthyIOError}, + {"unexpected eof", fmt.Errorf("decode: %w", io.ErrUnexpectedEOF), UnhealthyIOError}, + {"eio", fmt.Errorf("x: %w", syscall.EIO), UnhealthyIOError}, + {"wal recycled", storage.NewWALRecycledFailure(nil, "x"), ""}, + {"unclassified", errors.New("something else"), ""}, + } { + if got := LocalRecoveryHealthReason(tc.err); got != tc.want { + t.Errorf("%s: %q, want %q", tc.name, got, tc.want) + } + } +} + +// The actual caller chain: a corrupted smartwal store, durable.Recover +// (what DurableProvider.RecoverVolume runs at startup), then the host's +// ReportLocalRecoveryFailure; a primary-assigned replica reports +// wal_corrupt at once, without waiting for the recover timeout. +func TestLocalRecoveryFailure_CorruptWALReportsWALCorruptAtOnce(t *testing.T) { + path := filepath.Join(t.TempDir(), "v.sw") + s, err := smartwal.CreateStore(path, 16, 4096) + if err != nil { + t.Fatal(err) + } + for i := uint32(0); i < 3; i++ { + data := make([]byte, 4096) + data[0] = byte(0xA0 + i) + if _, err := s.Write(i, data); err != nil { + t.Fatal(err) + } + } + if _, err := s.Sync(); err != nil { + t.Fatal(err) + } + if err := s.Close(); err != nil { + t.Fatal(err) + } + layout, rec, err := smartwal.LatestRecord(path) + if err != nil { + t.Fatal(err) + } + f, err := os.OpenFile(path, os.O_RDWR, 0) + if err != nil { + t.Fatal(err) + } + if _, err := f.WriteAt([]byte{0x5A}, layout.ExtentStart+int64(rec.LBA)*int64(layout.BlockSize)); err != nil { + t.Fatal(err) + } + _ = f.Close() + reopened, err := smartwal.OpenStore(path) + if err != nil { + t.Fatal(err) + } + defer reopened.Close() + report := durable.Recover(reopened, "v1") + if report.Err == nil { + t.Fatal("fixture: recovery of the corrupted store succeeded") + } + + h, _ := watchdogHost(t, time.Hour) // the timeout is far away + h.BlockLocalReadiness(report.Evidence) + if got := h.ReportLocalRecoveryFailure(report.Err); got != UnhealthyWALCorrupt { + t.Fatalf("ReportLocalRecoveryFailure(%v)=%q; want %s", report.Err, got, UnhealthyWALCorrupt) + } + h.applyFact(selfPrimaryFact(1)) // refused by the readiness block, but primary-assigned + if s := reportSlot(t, h); s.Eligible || s.UnhealthyReason != UnhealthyWALCorrupt { + t.Fatalf("primary-assigned after corrupt recovery: eligible=%t reason=%q; want ineligible, %s at once", s.Eligible, s.UnhealthyReason, UnhealthyWALCorrupt) + } + + // A later clean recovery clears it. + h.ClearLocalReadinessBlock() + if s := reportSlot(t, h); !s.Eligible || s.UnhealthyReason != "" { + t.Fatalf("after a clean recovery: eligible=%t reason=%q; want cleared", s.Eligible, s.UnhealthyReason) + } +} + +// Scope and fallback: a replica that is not primary-assigned is not +// flagged by a local failure; an unclassified failure reports nothing +// early and recovery_timeout still covers it. +func TestLocalRecoveryFailure_ScopeAndUnclassifiedFallback(t *testing.T) { + h, clk := watchdogHost(t, 10*time.Second) + h.ReportLocalRecoveryFailure(storage.NewSubstrateIOFailure(errors.New("read"), "replay")) + if s := reportSlot(t, h); !s.Eligible || s.UnhealthyReason != "" { + t.Fatalf("not primary-assigned: %+v; want not flagged", s) + } + h.applyFact(selfPrimaryFact(1)) + if s := reportSlot(t, h); s.UnhealthyReason != UnhealthyIOError { + t.Fatalf("primary-assigned with an I/O recovery failure: reason=%q; want %s", s.UnhealthyReason, UnhealthyIOError) + } + + h2, clk2 := watchdogHost(t, 10*time.Second) + h2.ReportLocalRecoveryFailure(errors.New("unclassified")) + h2.applyFact(selfPrimaryFact(1)) + if s := reportSlot(t, h2); s.UnhealthyReason != "" { + t.Fatalf("unclassified failure inside the timeout: reason=%q; want none yet", s.UnhealthyReason) + } + clk2.advance(11 * time.Second) + if s := reportSlot(t, h2); s.UnhealthyReason != UnhealthyRecoveryTimeout { + t.Fatalf("unclassified failure past the timeout: reason=%q; want %s", s.UnhealthyReason, UnhealthyRecoveryTimeout) + } + _ = clk +} diff --git a/seaweed-block/core/host/volume/recovery_watchdog.go b/seaweed-block/core/host/volume/recovery_watchdog.go index 5696be885f..9e0a7d1719 100644 --- a/seaweed-block/core/host/volume/recovery_watchdog.go +++ b/seaweed-block/core/host/volume/recovery_watchdog.go @@ -1,10 +1,15 @@ package volume import ( + "errors" + "io" + "io/fs" "sync" + "syscall" "time" "github.com/seaweedfs/seaweed-block/core/engine" + "github.com/seaweedfs/seaweed-block/core/storage" ) // Recovery watchdog (#128 rule 6). @@ -37,12 +42,17 @@ import ( // an engine still ready at an older epoch (its new assignment not // admitted) does not. // -// wal_corrupt and io_error need typed failures out of the durable -// recovery path; they are a tracked follow-up. +// - wal_corrupt / io_error (#133): when local durable recovery failed +// with a classifiable error (LocalRecoveryHealthReason), a +// primary-assigned replica reports that typed reason at once instead +// of waiting out the timeout. An unclassified failure reports nothing +// early; recovery_timeout still covers it. const ( UnhealthyRecoveryTimeout = "recovery_timeout" UnhealthyRoleAdmission = "role_admission_failed" + UnhealthyWALCorrupt = "wal_corrupt" + UnhealthyIOError = "io_error" // DefaultRecoverTimeout matches blockmaster's default freshness // window. @@ -115,7 +125,7 @@ func (w *recoveryWatchdog) supportingAdmitted(epoch uint64) { // epoch, compared under the lock, so an assignment that advanced while // the fact was read is not cleared by it. fired is true on the call that // latched recovery_timeout. -func (w *recoveryWatchdog) reason(ready bool, readyEpoch uint64, roleAdmissionFailed bool) (reason string, fired bool) { +func (w *recoveryWatchdog) reason(ready bool, readyEpoch uint64, roleAdmissionFailed bool, localFailure string) (reason string, fired bool) { if w == nil { return "", false } @@ -127,6 +137,9 @@ func (w *recoveryWatchdog) reason(ready bool, readyEpoch uint64, roleAdmissionFa if roleAdmissionFailed { return UnhealthyRoleAdmission, false } + if localFailure != "" { + return localFailure, false + } if ready && readyEpoch == w.primaryEpoch { w.timedOut, w.readySeen = false, true return "", false @@ -168,7 +181,8 @@ func (h *Host) unhealthyReason(fact engine.PromotionReadyFact, ready bool) strin return "" } failed, _ := h.RoleAdmissionFailure() - reason, fired := h.wd.reason(ready, fact.Epoch, failed) + local, _ := h.localRecoveryReason.Load().(string) + reason, fired := h.wd.reason(ready, fact.Epoch, failed, local) if fired { h.log.Printf("blockvolume: volume %s primary assignment epoch=%d not promotion-ready after %s: reporting unhealthy (%s)", h.cfg.VolumeID, h.wd.epoch(), h.wd.timeout, UnhealthyRecoveryTimeout) @@ -190,3 +204,46 @@ func (h *Host) CatchUpSince() time.Time { } return h.wd.catchUpSince() } + +// LocalRecoveryHealthReason classifies a local durable recovery failure +// into a typed unhealthy reason (#133): wal_corrupt for a WAL/extent +// integrity fault, io_error for a storage I/O failure. It returns "" for +// an error it cannot classify (the watchdog's timeout still covers that). +func LocalRecoveryHealthReason(err error) string { + if red := true; red { + return "" // RED: not classified yet + } + if err == nil { + return "" + } + var rf *storage.RecoveryFailure + if errors.As(err, &rf) { + switch rf.Kind { + case storage.StorageRecoveryFailureWALIntegrity: + return UnhealthyWALCorrupt + case storage.StorageRecoveryFailureSubstrateIO: + return UnhealthyIOError + } + } + if errors.Is(err, storage.ErrWALIntegrityFault) { + return UnhealthyWALCorrupt + } + var pathErr *fs.PathError + if errors.As(err, &pathErr) || errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, syscall.EIO) { + return UnhealthyIOError + } + return "" +} + +// ReportLocalRecoveryFailure records why local durable recovery failed, +// classified by LocalRecoveryHealthReason, and returns the reason. A +// primary-assigned replica then reports it at once (#133). It holds until +// ClearLocalReadinessBlock (a later clean recovery). +func (h *Host) ReportLocalRecoveryFailure(err error) string { + reason := LocalRecoveryHealthReason(err) + h.localRecoveryReason.Store(reason) + if reason != "" { + h.log.Printf("blockvolume: volume %s local durable recovery failed (%s): a primary assignment reports it as unhealthy", h.cfg.VolumeID, reason) + } + return reason +} diff --git a/seaweed-block/core/host/volume/recovery_watchdog_test.go b/seaweed-block/core/host/volume/recovery_watchdog_test.go index 02a580b2c8..6c6b5a2097 100644 --- a/seaweed-block/core/host/volume/recovery_watchdog_test.go +++ b/seaweed-block/core/host/volume/recovery_watchdog_test.go @@ -86,26 +86,26 @@ func TestRecoveryWatchdog_ReadyClearsAndDisarmsNewEpochRearms(t *testing.T) { w.primaryAssigned(1) clk.advance(11 * time.Second) - if r, fired := w.reason(false, 0, false); r != UnhealthyRecoveryTimeout || !fired { + if r, fired := w.reason(false, 0, false, ""); r != UnhealthyRecoveryTimeout || !fired { t.Fatalf("past timeout: %q fired=%t", r, fired) } - if r, fired := w.reason(false, 0, false); r != UnhealthyRecoveryTimeout || fired { + if r, fired := w.reason(false, 0, false, ""); r != UnhealthyRecoveryTimeout || fired { t.Fatalf("latched: %q fired=%t; want the reason, fired only once", r, fired) } - if r, _ := w.reason(true, 1, false); r != "" { + if r, _ := w.reason(true, 1, false, ""); r != "" { t.Fatalf("ready: %q; want cleared", r) } - if r, _ := w.reason(false, 0, false); r != "" { + if r, _ := w.reason(false, 0, false, ""); r != "" { t.Fatalf("unready again in the same assignment: %q; want disarmed", r) } w.primaryAssigned(2) // new primary epoch: fresh clock clk.advance(9 * time.Second) - if r, _ := w.reason(false, 0, false); r != "" { + if r, _ := w.reason(false, 0, false, ""); r != "" { t.Fatalf("new epoch inside timeout: %q", r) } clk.advance(2 * time.Second) - if r, _ := w.reason(false, 0, false); r != UnhealthyRecoveryTimeout { + if r, _ := w.reason(false, 0, false, ""); r != UnhealthyRecoveryTimeout { t.Fatalf("new epoch past timeout: %q", r) } } @@ -114,11 +114,11 @@ func TestRecoveryWatchdog_ReadyClearsAndDisarmsNewEpochRearms(t *testing.T) { // at once; it never applies to a replica without the primary assignment. func TestRecoveryWatchdog_RoleAdmissionFailure(t *testing.T) { w := newRecoveryWatchdog(time.Hour) - if r, _ := w.reason(false, 0, true); r != "" { + if r, _ := w.reason(false, 0, true, ""); r != "" { t.Fatalf("not primary-assigned: %q", r) } w.primaryAssigned(1) - if r, _ := w.reason(false, 0, true); r != UnhealthyRoleAdmission { + if r, _ := w.reason(false, 0, true, ""); r != UnhealthyRoleAdmission { t.Fatalf("primary-assigned with the G1 latch: %q; want %s", r, UnhealthyRoleAdmission) } }