feat(recovery): §IV.0 T4 wire-level WAL LSN monotonicity gate

Adds Receiver.appliedWalLSN/recvFromLSN tracking and checkMonotonic /
enforceWalWireMonotonic per §IV.0 T4 (recover-semantics-adjustment-plan
§7 v3.28 substrate-mode carve-out).

Behavior:
- SessionStart seeds recvFromLSN=appliedWalLSN=FromLSN.
- Each frameWALEntry runs enforceWalWireMonotonic before ApplyWALEntry.
- RecoveryModeWALReplay: requires lsn == appliedWalLSN+1; gap returns
  FailureContract; exact-duplicate / backward returns FailureProtocol.
- StateConvergence (smartwal ScanLBAs): skipped — frontier LSN may
  legitimately repeat across many RecoveryEntry payloads.

Tests:
- TestReceiver_checkMonotonic_T4: forward / duplicate / gap / backward.
- TestReceiver_checkMonotonic_boundaryDup: lsn==FromLSN before any
  apply is a Protocol failure (not a no-op).

Authored by architect; submitted on user request.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 4.7 committed 2026-05-01 15:35:08 -07:00
1 parent 32b16004fa
commit 3ebc6f15c2
2 files changed
+100

No files matched your search

+45
View File
@@ -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 {
@@ -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)
}
}