Files
seaweedfs/seaweed-block/core/replication/component/faults.go
T
pingqiuandClaude Opus 4.7 e8aec8b954 T4d-4 part A: RecoveryMode interface method + Stop lifecycle (BUG-005 non-repeat) + framework wrap fix
Per T4d mini-plan v0.4 §2.4 expanded scope. Part A of three: smaller
substrate cleanups + lifecycle hardening land first; Path B fold
(ReplicationVolume↔adapter wiring + WithEngineDrivenRecovery real)
follows as part B; Windows cleanup + L2 matrix as part C.

T4c §I row 6 closure (RecoveryMode interface method):
- core/storage/logical_storage.go — add RecoveryMode() to
  LogicalStorage interface (returns substrate's tier-1 sub-mode
  per memo §5.1 / §13.0a)
- core/storage/store.go BlockStore.RecoveryMode() →
  RecoveryModeStateConvergence (in-memory; ScanLBAs synthesizes)
- core/storage/walstore.go WALStore.RecoveryMode() →
  RecoveryModeWALReplay (V2-faithful per-LSN)
- core/storage/smartwal/store.go Store.RecoveryMode() →
  RecoveryModeStateConvergence (per-LBA dedup)
- core/transport/catchup_sender.go — replace duck-typed
  CheckpointLSN probe with e.primaryStore.RecoveryMode() interface
  call. Forwards cleanly through component framework wraps via
  embedded LogicalStorage.
- core/replication/component/faults.go godoc — replace stale
  "MASKED by wrap" limitation note with T4d-4 closure note;
  AssertSawRecoveryMode now works correctly under wraps

T4d-4 lifecycle (BUG-005 non-repeat):
- core/replication/volume.go — Stop() canonical entry point added
  (delegates to existing Close); semantic clarity over Close().
  Both methods are equivalent in T4d-4; Stop() is the named entry
  for "stop volume lifecycle activity" while Close() retained for
  backward compatibility. Future expansions (drain pending I/O,
  stop background goroutines) land in Stop().
- INV-REPL-LIFECYCLE-HANDLE-BORROWED-001 pinned by
  TestReplicationVolume_Stop_DoesNotCloseBorrowedStore: store is
  STILL usable after volume.Stop (substrate handle is borrowed,
  never closed by us — BUG-005 discipline)
- TestReplicationVolume_Stop_Idempotent: second + later Stop calls
  are no-ops returning nil
- TestReplicationVolume_Stop_TearsDownPeers: empty-set lifecycle pin

Tests added (5 new):
- TestReplicationVolume_Stop_Idempotent
- TestReplicationVolume_Stop_DoesNotCloseBorrowedStore
- TestReplicationVolume_Stop_TearsDownPeers
- TestStorageRecoveryMode_BlockStore_StateConvergence
- TestStorageRecoveryMode_Walstore_WALReplay
- TestStorageRecoveryMode_LogicalStorageInterface_HasMethod
  (compile-time fence pinning RecoveryMode is on the interface,
  not just on substrate impls)

NOT in part A (deferred to part B / C):
- ReplicationVolume↔adapter wiring (Path B fold, part B)
- WithEngineDrivenRecovery real binding (Path B fold, part B)
- Full L2 integration matrix (part C)
- Windows TempDir cleanup race (part C)
- Test rename per kickoff §5 (part C)
- sessionID coordination (T4c §I row 5; part C — minor)

22 packages green; 0 failures. Forward-carries: T4c-2 + T4d-1 +
T4d-2 + T4d-3 invariants all green; new RecoveryMode plumbing
preserves T4c-2 INV-REPL-RECOVERY-MODE-OBSERVABLE.

Refs: T4d mini-plan v0.4 §2.4 (architect-ratified expanded scope);
T4c §I row 6 (substrate RecoveryMode method); kickoff §2.1 (BUG-005
non-repeat lifecycle); G-1 sign 80036404c (G-1 v0.2 + planning
artifacts).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-25 22:40:04 -07:00

144 lines
4.8 KiB
Go

package component
import (
"fmt"
"sync/atomic"
"github.com/seaweedfs/seaweed-block/core/storage"
)
// Substrate-wrap helpers for fault injection at the executor layer.
// Use with Cluster.WithPrimaryStorageWrap(NewXxxWrap(...)).
//
// All wraps embed `storage.LogicalStorage` and override one or more
// methods. The embedded interface forwards everything else
// transparently — only the overridden behavior changes.
//
// T4d-4 (round-46): the T4c-2 wrap-pattern limitation around
// `CheckpointLSN` masking is RESOLVED. `LogicalStorage.RecoveryMode()`
// is now a first-class interface method, forwarded automatically by
// embedded `storage.LogicalStorage` in the wrap structs below. The
// catch-up sender consults `e.primaryStore.RecoveryMode()` (interface
// method) instead of the duck-typed `CheckpointLSN` probe.
// `AssertSawRecoveryMode` works correctly under wraps now. T4c §I
// row 6 closure.
// NewRecycledScanWrap returns a PrimaryStorageWrap that always
// returns ErrWALRecycled from ScanLBAs. Use to test the rebuild-
// escalation path without driving a real walstore checkpoint dance.
//
// Authoring:
//
// c := component.NewCluster(t, component.Walstore).
// WithPrimaryStorageWrap(component.NewRecycledScanWrap()).
// Start()
// result := c.CatchUpReplica(0)
// // result.FailReason contains "WAL recycled" → engine maps to Rebuild
func NewRecycledScanWrap() PrimaryStorageWrap {
return func(inner storage.LogicalStorage) storage.LogicalStorage {
return &recycledScanWrap{LogicalStorage: inner}
}
}
type recycledScanWrap struct{ storage.LogicalStorage }
func (r *recycledScanWrap) ScanLBAs(uint64, func(storage.RecoveryEntry) error) error {
return storage.ErrWALRecycled
}
// NewSeverDuringScanWrap returns a PrimaryStorageWrap that emits
// `afterNEntries` entries successfully, then returns the given error
// from ScanLBAs (defaults to a generic "stream error" if nil).
//
// Use to test mid-stream drop / partial-progress scenarios:
// - INV-REPL-CATCHUP-LASTSENT-MONOTONIC: lastSent reflects only
// the entries successfully emitted before the drop
// - INV-REPL-CATCHUP-DEADLINE-PER-CALL-SCOPE: the per-call
// deadline is cleared even on mid-stream failure
//
// Authoring:
//
// c := component.NewCluster(t, component.Walstore).
// WithPrimaryStorageWrap(component.NewSeverDuringScanWrap(5, nil)).
// Start()
// c.PrimaryWriteN(20)
// result := c.CatchUpReplica(0)
// // result.AchievedLSN reflects entries 1..5 (at most), not 1..20
func NewSeverDuringScanWrap(afterNEntries int, severErr error) PrimaryStorageWrap {
if severErr == nil {
severErr = fmt.Errorf("component: substrate severed mid-scan after %d entries", afterNEntries)
}
return func(inner storage.LogicalStorage) storage.LogicalStorage {
return &severDuringScanWrap{
LogicalStorage: inner,
afterN: int32(afterNEntries),
err: severErr,
}
}
}
type severDuringScanWrap struct {
storage.LogicalStorage
afterN int32
err error
}
func (s *severDuringScanWrap) ScanLBAs(fromLSN uint64, fn func(storage.RecoveryEntry) error) error {
var emitted atomic.Int32
intercept := func(e storage.RecoveryEntry) error {
if emitted.Load() >= s.afterN {
return s.err
}
if err := fn(e); err != nil {
return err
}
emitted.Add(1)
return nil
}
if err := s.LogicalStorage.ScanLBAs(fromLSN, intercept); err != nil {
return err
}
// If the substrate exhausted before the intercept fired the sever,
// don't fail closed — the caller asked to sever AT entry N+1, but
// the substrate had ≤ N entries. That's a "no fault to inject"
// case; return nil so scenarios are robust to substrate-emit
// counts smaller than the sever threshold.
return nil
}
// NewObservedScanWrap returns a wrap that counts ScanLBAs entries
// emitted and exposes the count via the returned counter pointer.
// Use for observability assertions ("did the sender emit N entries
// before the barrier?").
//
// Authoring:
//
// count := new(atomic.Int32)
// c := component.NewCluster(t, component.Walstore).
// WithPrimaryStorageWrap(component.NewObservedScanWrap(count)).
// Start()
// c.PrimaryWriteN(10)
// c.CatchUpReplica(0)
// if count.Load() != 10 { t.Errorf("emit count = %d, want 10", count.Load()) }
func NewObservedScanWrap(counter *atomic.Int32) PrimaryStorageWrap {
return func(inner storage.LogicalStorage) storage.LogicalStorage {
return &observedScanWrap{LogicalStorage: inner, counter: counter}
}
}
type observedScanWrap struct {
storage.LogicalStorage
counter *atomic.Int32
}
func (o *observedScanWrap) ScanLBAs(fromLSN uint64, fn func(storage.RecoveryEntry) error) error {
intercept := func(e storage.RecoveryEntry) error {
if err := fn(e); err != nil {
return err
}
o.counter.Add(1)
return nil
}
return o.LogicalStorage.ScanLBAs(fromLSN, intercept)
}