diff --git a/seaweed-block/core/adapter/adapter.go b/seaweed-block/core/adapter/adapter.go index 04508f71b7..53df0afcdc 100644 --- a/seaweed-block/core/adapter/adapter.go +++ b/seaweed-block/core/adapter/adapter.go @@ -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. diff --git a/seaweed-block/core/adapter/adapter_test.go b/seaweed-block/core/adapter/adapter_test.go index 6187eea6c9..50192a970d 100644 --- a/seaweed-block/core/adapter/adapter_test.go +++ b/seaweed-block/core/adapter/adapter_test.go @@ -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 diff --git a/seaweed-block/core/adapter/executor.go b/seaweed-block/core/adapter/executor.go index e9bdad7cb8..8807399726 100644 --- a/seaweed-block/core/adapter/executor.go +++ b/seaweed-block/core/adapter/executor.go @@ -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) +} diff --git a/seaweed-block/core/adapter/normalize.go b/seaweed-block/core/adapter/normalize.go index 0199bd0147..56dd9a5a78 100644 --- a/seaweed-block/core/adapter/normalize.go +++ b/seaweed-block/core/adapter/normalize.go @@ -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 { diff --git a/seaweed-block/core/recovery/e2e_test.go b/seaweed-block/core/recovery/e2e_test.go index 9f8e197aec..cbfaa37f65 100644 --- a/seaweed-block/core/recovery/e2e_test.go +++ b/seaweed-block/core/recovery/e2e_test.go @@ -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 diff --git a/seaweed-block/core/recovery/integration_stub.go b/seaweed-block/core/recovery/integration_stub.go index 3fd9840497..21d195cf11 100644 --- a/seaweed-block/core/recovery/integration_stub.go +++ b/seaweed-block/core/recovery/integration_stub.go @@ -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() diff --git a/seaweed-block/core/recovery/sender.go b/seaweed-block/core/recovery/sender.go index 2cb13d1838..035edaee1d 100644 --- a/seaweed-block/core/recovery/sender.go +++ b/seaweed-block/core/recovery/sender.go @@ -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) diff --git a/seaweed-block/core/transport/executor.go b/seaweed-block/core/transport/executor.go index 7c87dfaf7b..b07b83c32b 100644 --- a/seaweed-block/core/transport/executor.go +++ b/seaweed-block/core/transport/executor.go @@ -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