recovery: forward durable ack facts to engine

This commit is contained in:
pingqiu committed 2026-05-02 11:28:03 -07:00
1 parent 0575e24f77
commit 4fc11986c7
8 files changed
+172 -1

No files matched your search

+10
View File
@@ -97,6 +97,11 @@ func NewVolumeReplicaAdapter(exec CommandExecutor) *VolumeReplicaAdapter {
exec.SetOnSessionClose(func(result SessionCloseResult) {
a.OnSessionClose(result)
})
if durableAckExec, ok := exec.(DurableAckCallbackSetter); ok {
durableAckExec.SetOnDurableAck(func(result DurableAckResult) {
a.OnDurableAck(result)
})
}
// Wire the fence callback: executor → adapter.OnFenceComplete → engine.
exec.SetOnFenceComplete(func(result FenceResult) {
a.OnFenceComplete(result)
@@ -132,6 +137,11 @@ func (a *VolumeReplicaAdapter) OnSessionStart(result SessionStartResult) ApplyLo
return a.applyBatchAndExecute([]engine.Event{NormalizeSessionStart(result)}, "SessionStart")
}
// OnDurableAck processes non-terminal durable progress from the executor.
func (a *VolumeReplicaAdapter) OnDurableAck(result DurableAckResult) ApplyLog {
return a.applyBatchAndExecute([]engine.Event{NormalizeDurableAck(result)}, "DurableAck")
}
// OnFenceComplete processes a fence outcome from the transport.
// Releases the in-flight slot (keyed by lineage) and normalizes to
// FenceCompleted (success) or FenceFailed (failure) into the engine.
@@ -22,6 +22,7 @@ type mockExecutor struct {
nextSession atomic.Uint64
onSessionStart OnSessionStart
onSessionClose OnSessionClose // wired by adapter
onDurableAck OnDurableAck
onFenceComplete OnFenceComplete
autoStart bool
autoClose bool
@@ -78,6 +79,12 @@ func (m *mockExecutor) SetOnSessionClose(fn OnSessionClose) {
m.onSessionClose = fn
}
func (m *mockExecutor) SetOnDurableAck(fn OnDurableAck) {
m.mu.Lock()
defer m.mu.Unlock()
m.onDurableAck = fn
}
func (m *mockExecutor) SetOnFenceComplete(fn OnFenceComplete) {
m.mu.Lock()
defer m.mu.Unlock()
@@ -335,6 +342,40 @@ func TestC8_SameFactsSameDecision(t *testing.T) {
}
}
func TestAdapter_DurableAckCallback_ReachesEngineProgressFact(t *testing.T) {
exec := newMockExecutor()
a := NewVolumeReplicaAdapter(exec)
a.OnAssignment(AssignmentInfo{
VolumeID: "v1",
ReplicaID: "r1",
Epoch: 1,
EndpointVersion: 1,
DataAddr: "data",
CtrlAddr: "ctrl",
})
exec.mu.Lock()
cb := exec.onDurableAck
exec.mu.Unlock()
if cb == nil {
t.Fatal("NewVolumeReplicaAdapter did not wire optional durable ack callback")
}
cb(DurableAckResult{
ReplicaID: "r1",
SessionID: 7,
DurableLSN: 11,
PrimaryTailLSN: 5,
PrimaryHeadLSN: 20,
})
a.mu.Lock()
defer a.mu.Unlock()
if !a.state.Recovery.DurableAckKnown || a.state.Recovery.DurableAckR != 11 {
t.Fatalf("engine DurableAck=(%d known=%v), want (11 true)",
a.state.Recovery.DurableAckR, a.state.Recovery.DurableAckKnown)
}
}
func TestAdapter_StartCatchUp_UsesFrontierHintOverLegacyTarget(t *testing.T) {
exec := newMockExecutor()
exec.autoStart = false
+25
View File
@@ -123,6 +123,12 @@ type OnSessionClose func(SessionCloseResult)
// tied to actual execution start instead of command issuance alone.
type OnSessionStart func(SessionStartResult)
// OnDurableAck is the callback signature for non-terminal durable
// progress. It is intentionally separate from OnSessionClose: durable
// ack can drive lag policy / pin authority, but does not complete a
// recovery session by itself.
type OnDurableAck func(DurableAckResult)
// OnFenceComplete is the callback signature for FenceAtEpoch outcomes.
// Fires exactly once per Fence call. Success=true advances
// Reachability.FencedEpoch; Success=false leaves it unchanged and
@@ -138,3 +144,22 @@ type FenceResult struct {
Success bool
FailReason string
}
// DurableAckResult reports a replica durable frontier observed during an
// active recovery session.
type DurableAckResult struct {
ReplicaID string
SessionID uint64
EndpointVersion uint64
TransportEpoch uint64
DurableLSN uint64
PrimaryTailLSN uint64
PrimaryHeadLSN uint64
}
// DurableAckCallbackSetter is optionally implemented by executors that
// can report mid-session durable progress. CommandExecutor does not
// require this so existing mocks and muscles remain source-compatible.
type DurableAckCallbackSetter interface {
SetOnDurableAck(fn OnDurableAck)
}
+14
View File
@@ -178,6 +178,20 @@ func NormalizeSessionStart(r SessionStartResult) engine.Event {
}
}
// NormalizeDurableAck converts mid-session durable progress into an
// engine event. The event records progress only; engine policy decides
// later whether and how to use it for lag.
func NormalizeDurableAck(r DurableAckResult) engine.Event {
return engine.DurableAckObserved{
ReplicaID: r.ReplicaID,
EndpointVersion: r.EndpointVersion,
TransportEpoch: r.TransportEpoch,
DurableLSN: r.DurableLSN,
PrimaryTailLSN: r.PrimaryTailLSN,
PrimaryHeadLSN: r.PrimaryHeadLSN,
}
}
// NormalizeSessionPrepared creates a SessionPrepared event when the
// adapter has set up a recovery session in response to a Start* command.
func NormalizeSessionPrepared(replicaID string, sessionID uint64, kind engine.SessionKind, frontierHint uint64) engine.Event {
+20
View File
@@ -352,6 +352,16 @@ func TestE2E_PinFloorAdvancesIncrementally(t *testing.T) {
coord := NewPeerShipCoordinator()
sender := NewSenderWithBacklogRelay(primary, coord, primaryConn, "r1")
var ackMu sync.Mutex
var ackFrontiers []uint64
sender.SetOnDurableAck(func(replicaID ReplicaID, sessionID, acknowledgedLSN, primaryS, primaryH uint64) {
if replicaID != "r1" || sessionID != 21 {
t.Errorf("durable ack callback identity=(%s,%d), want (r1,21)", replicaID, sessionID)
}
ackMu.Lock()
ackFrontiers = append(ackFrontiers, acknowledgedLSN)
ackMu.Unlock()
})
// Small K so cadence triggers within the 50-LBA backlog.
receiver := NewReceiverWithCadence(replica, replicaConn, 8, 50*time.Millisecond)
@@ -381,6 +391,16 @@ func TestE2E_PinFloorAdvancesIncrementally(t *testing.T) {
if recvErr != nil {
t.Fatalf("receiver: %v", recvErr)
}
ackMu.Lock()
defer ackMu.Unlock()
if len(ackFrontiers) == 0 {
t.Fatal("sender durable ack callback did not fire")
}
for i := 1; i < len(ackFrontiers); i++ {
if ackFrontiers[i] < ackFrontiers[i-1] {
t.Fatalf("durable ack callbacks regressed: %v", ackFrontiers)
}
}
// After session: coord goes Idle (pinFloor reads 0). To inspect
// the LAST observed pinFloor before EndSession dropped it, use
@@ -50,6 +50,10 @@ type SessionStartCallback func(replicaID ReplicaID, sessionID uint64)
// On failure `err` is non-nil and `achievedLSN` is 0.
type SessionCloseCallback func(replicaID ReplicaID, sessionID uint64, achievedLSN uint64, err error)
// DurableAckCallback fires when a sender observes a receiver durable ack.
// It is progress telemetry; session close remains the terminal callback.
type BridgeDurableAckCallback func(replicaID ReplicaID, sessionID, acknowledgedLSN, primaryS, primaryH uint64)
// PrimaryBridge is the primary-side adapter. One bridge instance
// per primary daemon; it serializes session starts per replica via
// the embedded coordinator's INV-SINGLE-FLIGHT-PER-REPLICA check.
@@ -64,6 +68,7 @@ type PrimaryBridge struct {
onStart SessionStartCallback
onClose SessionCloseCallback
onAck BridgeDurableAckCallback
mu sync.Mutex
senders map[ReplicaID]*Sender
@@ -90,6 +95,14 @@ func NewPrimaryBridge(store storage.LogicalStorage, coord *PeerShipCoordinator,
}
}
// SetOnDurableAck installs an optional progress callback. Safe to call
// after construction; senders snapshot the bridge callback at ack time.
func (b *PrimaryBridge) SetOnDurableAck(fn BridgeDurableAckCallback) {
b.mu.Lock()
defer b.mu.Unlock()
b.onAck = fn
}
// StartRebuildSession is shaped like CommandExecutor.StartRebuild:
// non-blocking, returns immediately after spawning the session
// goroutine. After P2c-slice B-2 the session terminates on its own
@@ -167,6 +180,14 @@ func (b *PrimaryBridge) startRebuildSessionLocked(ctx context.Context, conn net.
} else {
sender = NewSenderWithSink(b.store, b.coord, conn, replicaID, sink)
}
sender.SetOnDurableAck(func(rid ReplicaID, sid, acknowledgedLSN, primaryS, primaryH uint64) {
b.mu.Lock()
cb := b.onAck
b.mu.Unlock()
if cb != nil {
cb(rid, sid, acknowledgedLSN, primaryS, primaryH)
}
})
b.senders[replicaID] = sender
b.mu.Unlock()
+18 -1
View File
@@ -44,6 +44,11 @@ type preBarrierSealer interface {
SealBeforeBarrier() error
}
// DurableAckCallback reports a receiver BaseBatchAck after it has passed
// the coordinator pin-floor update. It is optional telemetry for the
// semantic engine; pin safety still lives in PeerShipCoordinator.
type DurableAckCallback func(replicaID ReplicaID, sessionID, acknowledgedLSN, primaryS, primaryH uint64)
// Sender is the primary-side driver of one rebuild session. It runs
// the base lane, then delegates the WAL pump (and live-write traffic
// during the session) to its WalShipperSink, and finally runs the
@@ -111,6 +116,8 @@ type Sender struct {
// to avoid routing live writes into a sender that is registered for
// single-flight but whose WalShipper is not yet the active feeder.
liveReady atomic.Bool
onDurableAck DurableAckCallback
}
// ackOrResult is the channel payload from the reader goroutine to
@@ -205,6 +212,13 @@ func NewSenderWithSink(primaryStore storage.LogicalStorage, coordinator *PeerShi
return s
}
// SetOnDurableAck installs an optional progress callback fired whenever
// the receiver emits a valid durable BaseBatchAck. Existing callers may
// leave it nil; it is not part of the close predicate.
func (s *Sender) SetOnDurableAck(fn DurableAckCallback) {
s.onDurableAck = fn
}
// walItem is the per-entry record buffered by senderBacklogSink while
// streamBacklog is in flight. P2c-slice B-2 moved this into the sink:
// the buffer is a sink-internal artifact of the bridging path, not a
@@ -683,13 +697,16 @@ func (s *Sender) readerLoop() {
fmt.Errorf("ack sessionID=%d != active=%d", p.SessionID, s.sessionID))}
return
}
_, primaryS, _ := s.primaryStore.Boundaries()
_, primaryS, primaryH := s.primaryStore.Boundaries()
if updateErr := s.coordinator.SetPinFloor(s.replicaID, p.AcknowledgedLSN, primaryS); updateErr != nil {
// SetPinFloor returns *Failure already typed
// (FailurePinUnderRetention or wrapped); pass through.
s.barrierRespCh <- ackOrResult{err: updateErr}
return
}
if s.onDurableAck != nil {
s.onDurableAck(s.replicaID, s.sessionID, p.AcknowledgedLSN, primaryS, primaryH)
}
// continue reading
case frameBarrierResp:
achieved, decErr := decodeBarrierResp(payload)
+23
View File
@@ -13,6 +13,8 @@ import (
"github.com/seaweedfs/seaweed-block/core/storage"
)
var _ adapter.DurableAckCallbackSetter = (*BlockExecutor)(nil)
// BlockExecutor implements adapter.CommandExecutor using real TCP
// transport and a primary-side LogicalStorage. It is the "muscle"
// layer — it executes commands but never decides policy.
@@ -35,6 +37,7 @@ type BlockExecutor struct {
mu sync.Mutex
onSessionStart adapter.OnSessionStart
onSessionClose adapter.OnSessionClose
onDurableAck adapter.OnDurableAck
onFenceComplete adapter.OnFenceComplete
sessions map[uint64]*activeSession
stepDelay time.Duration
@@ -254,6 +257,20 @@ func NewBlockExecutorWithDualLane(
cb(res)
},
)
bridge.SetOnDurableAck(func(rid recovery.ReplicaID, sid, acknowledgedLSN, primaryS, primaryH uint64) {
e.mu.Lock()
cb := e.onDurableAck
e.mu.Unlock()
if cb != nil {
cb(adapter.DurableAckResult{
ReplicaID: string(rid),
SessionID: sid,
DurableLSN: acknowledgedLSN,
PrimaryTailLSN: primaryS,
PrimaryHeadLSN: primaryH,
})
}
})
e.dualLane = &DualLaneConfig{
Bridge: bridge,
DialAddr: dualLaneAddr,
@@ -270,6 +287,12 @@ func (e *BlockExecutor) SetOnSessionStart(fn adapter.OnSessionStart) {
e.onSessionStart = fn
}
func (e *BlockExecutor) SetOnDurableAck(fn adapter.OnDurableAck) {
e.mu.Lock()
defer e.mu.Unlock()
e.onDurableAck = fn
}
func (e *BlockExecutor) SetStepDelay(d time.Duration) {
e.mu.Lock()
e.stepDelay = d