diff --git a/seaweed-block/core/recovery/receiver.go b/seaweed-block/core/recovery/receiver.go index b699680ef9..17a0812e69 100644 --- a/seaweed-block/core/recovery/receiver.go +++ b/seaweed-block/core/recovery/receiver.go @@ -35,6 +35,12 @@ type Receiver struct { // Populated after frameSessionStart. session *RebuildSession sessionID uint64 + // recvFromLSN/appliedWalLSN: wire-level LSN monotonicity gate (§IV.0 T4, + // v3-recovery-unified-wal-stream-kickoff.md §5.1). After SessionStart, + // appliedWalLSN == FromLSN until the first WAL frame advances it; only + // lsn == appliedWalLSN+1 is accepted. + recvFromLSN uint64 + appliedWalLSN uint64 // BaseBatchAck cadence config + state (per docs/recovery-pin-floor-wire.md §3). cadenceK uint32 @@ -62,6 +68,37 @@ func NewReceiverWithCadence(store storage.LogicalStorage, conn io.ReadWriter, k } } +// checkMonotonic enforces contiguous recover-wire LSNs after the session FromLSN +// watermark (§IV.0 T4; kickoff §5.1). Only used when replica substrate reports +// RecoveryModeWALReplay — BlockStore/smartwal convergence scans synthesize LSN. +func (r *Receiver) checkMonotonic(lsn uint64) error { + expected := r.appliedWalLSN + 1 + if lsn == expected { + return nil + } + if lsn > expected { + return newFailure(FailureContract, PhaseRecvDispatch, + fmt.Errorf("WAL gap: got LSN=%d, expected=%d (appliedWalLSN=%d)", lsn, expected, r.appliedWalLSN)) + } + if lsn == r.appliedWalLSN { + return newFailure(FailureProtocol, PhaseRecvDispatch, + fmt.Errorf("WAL exact-duplicate on wire: LSN=%d (appliedWalLSN=%d)", lsn, r.appliedWalLSN)) + } + return newFailure(FailureProtocol, PhaseRecvDispatch, + fmt.Errorf("WAL backward: got LSN=%d < appliedWalLSN=%d", lsn, r.appliedWalLSN)) +} + +func (r *Receiver) enforceWalWireMonotonic(lsn uint64) error { + switch r.store.RecoveryMode() { + case storage.RecoveryModeWALReplay: + return r.checkMonotonic(lsn) + default: + // State convergence: ScanLBAs may attach the same frontier LSN to many + // RecoveryEntry payloads (storage/store.go §ScanLBAs, smartwal semantics). + return nil + } +} + // Session returns the active session (nil if SessionStart not yet // received). Test/diagnostic accessor. func (r *Receiver) Session() *RebuildSession { return r.session } @@ -100,6 +137,8 @@ func (r *Receiver) Run() (achievedLSN uint64, err error) { } r.sessionID = s.SessionID r.session = NewRebuildSession(r.store, s.TargetLSN) + r.recvFromLSN = s.FromLSN + r.appliedWalLSN = s.FromLSN case frameBaseBlock: if r.session == nil { @@ -133,10 +172,16 @@ func (r *Receiver) Run() (achievedLSN uint64, err error) { if decErr != nil { return 0, newFailure(FailureProtocol, PhaseRecvDispatch, decErr) } + if err := r.enforceWalWireMonotonic(lsn); err != nil { + return 0, err + } if applyErr := r.session.ApplyWALEntry(kind, lba, data, lsn); applyErr != nil { return 0, newFailure(FailureSubstrate, PhaseRecvApply, fmt.Errorf("apply wal kind=%s lba=%d lsn=%d: %w", kind, lba, lsn, applyErr)) } + if lsn > r.appliedWalLSN { + r.appliedWalLSN = lsn + } case frameBaseDone: if r.session == nil { diff --git a/seaweed-block/core/recovery/receiver_monotonic_test.go b/seaweed-block/core/recovery/receiver_monotonic_test.go new file mode 100644 index 0000000000..04eeac5ac0 --- /dev/null +++ b/seaweed-block/core/recovery/receiver_monotonic_test.go @@ -0,0 +1,55 @@ +package recovery + +import ( + "testing" +) + +func TestReceiver_checkMonotonic_T4(t *testing.T) { + r := &Receiver{recvFromLSN: 10, appliedWalLSN: 10} + + if err := r.checkMonotonic(11); err != nil { + t.Fatalf("first forward: %v", err) + } + r.appliedWalLSN = 11 + + if err := r.checkMonotonic(11); err == nil { + t.Fatal("exact duplicate: want error") + } else { + f := AsFailure(err) + if f == nil || f.Kind != FailureProtocol { + t.Fatalf("duplicate: got %v", err) + } + } + + r.appliedWalLSN = 20 + if err := r.checkMonotonic(22); err == nil { + t.Fatal("gap: want error") + } else { + f := AsFailure(err) + if f == nil || f.Kind != FailureContract { + t.Fatalf("gap: got %v", err) + } + } + + r.appliedWalLSN = 30 + if err := r.checkMonotonic(29); err == nil { + t.Fatal("backward: want error") + } else { + f := AsFailure(err) + if f == nil || f.Kind != FailureProtocol { + t.Fatalf("backward: got %v", err) + } + } +} + +func TestReceiver_checkMonotonic_boundaryDup(t *testing.T) { + r := &Receiver{recvFromLSN: 5, appliedWalLSN: 5} + err := r.checkMonotonic(5) + if err == nil { + t.Fatal("lsn==fromLSN before any apply: want Protocol failure") + } + f := AsFailure(err) + if f == nil || f.Kind != FailureProtocol { + t.Fatalf("want FailureProtocol, got %v", err) + } +}