From ee60eb0ced0c1b2a42d4aac7467db90edf358a89 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 29 Apr 2026 22:29:47 -0700 Subject: [PATCH] =?UTF-8?q?recovery:=20P2b=20=E2=80=94=20backlog=20relay?= =?UTF-8?q?=20sink=20+=20e2e=20migration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender's WAL pump now flows through a sink in P2b too — but via a backlog-relay adapter that keeps the same frameWALEntry wire behavior as legacy streamBacklog. This keeps the receiver path (decoder + RebuildSession.ApplyWALEntry on dual-lane port) untouched. Real rewind / transport.WalShipper integration is P2d (which also wires the dual-lane / receiver decoding alignment). Why a relay-not-real-WalShipper for P2b: Recovery's receiver consumes frameWALEntry on the dual-lane port. transport.WalShipper writes MsgShipEntry on the executor's legacy port. Plugging transport.WalShipper directly into recovery.Sender in P2b would mean wire-format mismatch + receiver dispatch break. The intentional shim is `senderBacklogSink` (DrainBacklog → streamBacklog) — sink-shape API, same wire bytes. Changes: sender.go: + senderBacklogSink — adapts streamBacklog() to WalShipperSink: StartSession / EndSession are no-ops (real rewind stays in transport.WalShipper for P2d); DrainBacklog calls the existing streamBacklog() in-place. + NewSenderWithBacklogRelay(primary, coord, conn, replicaID) — constructs sender with senderBacklogSink{s: s}. Used by all existing e2e + integration callers in P2b. Sink-mode Run() flow: after sink.DrainBacklog returns nil, the legacy `<-closeCh` wait + drainAndSeal still run so PushLiveWrite session-lane entries (the existing test scenario that injects post-target writes) keep converging. P2c can move session-live into NotifyAppend once transport WalShipper is wired. NewSenderWithSink doc updated to reflect the closeCh + drainAndSeal retention until P2c. e2e_test.go: All NewSender(...) → NewSenderWithBacklogRelay(...). No assertion changes; same e2e shape, sink-mode path now exercised end-to-end. integration_stub.go: PrimaryBridge uses NewSenderWithBacklogRelay so the bridge-shaped lifecycle (StartRebuildSession + PushLiveWrite + FinishLiveWrites) still works through the sink branch. sender_walshipper_sink_test.go: + TestSender_WithSink_DelegationOrder: spawns a goroutine that `sender.Close()` after `<-sink.drainCalledCh` so sink-mode Run unblocks past the closeCh wait and reaches sink.EndSession + barrier path (otherwise drainCalledCh never fires). + TestNewSenderWithBacklogRelay_installsRelaySink: smoke test that the new constructor installs senderBacklogSink and the type satisfies WalShipperSink (compile-time check). Lint fix: remove sync/atomic copy from earlier scaffolding. Header comments clarified for P2 stage progression. Test sweep: go test ./core/recovery/... → all PASS (legacy path + sink path both exercised; e2e tests now run via the relay sink). go test ./core/transport/... → all PASS (no transport changes in P2b). go test ./core/replication/... ./core/replication/component/... → all PASS. Roadmap note (architect-aware): P2c — make sink required; delete legacy NewSender pump (streamBacklog + closeCh + drainAndSeal + PushLiveWrite + liveQueue + sealed + queueMu + walItem + Close). Sink-mode Run is already the future shape; legacy path is deletable when no caller relies on Close + drainAndSeal. P2d — wire transport.WalShipper into recovery.Sender by aligning wire format (dual-lane vs legacy port) at receiver decoding. Then NewSenderWithBacklogRelay can be retired in favor of NewSenderWithSink(real walShipperAdapter). Transport-layer integration tests for architect rules 1+2 (updateContext before StartSession; restore after EndSession) land here. --- seaweed-block/core/recovery/e2e_test.go | 10 +-- .../core/recovery/integration_stub.go | 2 +- seaweed-block/core/recovery/sender.go | 74 ++++++++++++++++--- .../recovery/sender_walshipper_sink_test.go | 64 ++++++++-------- 4 files changed, 102 insertions(+), 48 deletions(-) diff --git a/seaweed-block/core/recovery/e2e_test.go b/seaweed-block/core/recovery/e2e_test.go index 72a1909a22..008f58aee7 100644 --- a/seaweed-block/core/recovery/e2e_test.go +++ b/seaweed-block/core/recovery/e2e_test.go @@ -53,7 +53,7 @@ func TestE2E_RebuildHappyPath(t *testing.T) { defer replicaConn.Close() coord := NewPeerShipCoordinator() - sender := NewSender(primary, coord, primaryConn, "r1") + sender := NewSenderWithBacklogRelay(primary, coord, primaryConn, "r1") receiver := NewReceiver(replica, replicaConn) _, _, primaryH := primary.Boundaries() @@ -151,7 +151,7 @@ func TestE2E_RebuildWithLiveWritesDuringSession(t *testing.T) { defer replicaConn.Close() coord := NewPeerShipCoordinator() - sender := NewSender(primary, coord, primaryConn, "r1") + sender := NewSenderWithBacklogRelay(primary, coord, primaryConn, "r1") receiver := NewReceiver(replica, replicaConn) // Caller-of-Run contract: StartSession before spawning Run. @@ -288,7 +288,7 @@ func TestE2E_KindCountsPinsBacklogVsLive(t *testing.T) { defer replicaConn.Close() coord := NewPeerShipCoordinator() - sender := NewSender(primary, coord, primaryConn, "r1") + sender := NewSenderWithBacklogRelay(primary, coord, primaryConn, "r1") receiver := NewReceiver(replica, replicaConn) if err := coord.StartSession("r1", 17, 0, frozenTarget); err != nil { @@ -380,7 +380,7 @@ func TestE2E_PinFloorAdvancesIncrementally(t *testing.T) { defer replicaConn.Close() coord := NewPeerShipCoordinator() - sender := NewSender(primary, coord, primaryConn, "r1") + sender := NewSenderWithBacklogRelay(primary, coord, primaryConn, "r1") // Small K so cadence triggers within the 50-LBA backlog. receiver := NewReceiverWithCadence(replica, replicaConn, 8, 50*time.Millisecond) @@ -461,7 +461,7 @@ func TestE2E_PushLiveWriteAtomicSeal(t *testing.T) { defer replicaConn.Close() coord := NewPeerShipCoordinator() - sender := NewSender(primary, coord, primaryConn, "r1") + sender := NewSenderWithBacklogRelay(primary, coord, primaryConn, "r1") receiver := NewReceiver(replica, replicaConn) if err := coord.StartSession("r1", 13, 0, frozenTarget); err != nil { diff --git a/seaweed-block/core/recovery/integration_stub.go b/seaweed-block/core/recovery/integration_stub.go index 781c93d465..617f078326 100644 --- a/seaweed-block/core/recovery/integration_stub.go +++ b/seaweed-block/core/recovery/integration_stub.go @@ -97,7 +97,7 @@ func (b *PrimaryBridge) StartRebuildSession(ctx context.Context, conn net.Conn, return fmt.Errorf("recovery bridge: %w", err) } - sender := NewSender(b.store, b.coord, conn, replicaID) + sender := NewSenderWithBacklogRelay(b.store, b.coord, conn, replicaID) b.senders[replicaID] = sender b.mu.Unlock() diff --git a/seaweed-block/core/recovery/sender.go b/seaweed-block/core/recovery/sender.go index b3d65eb848..f26cc9a166 100644 --- a/seaweed-block/core/recovery/sender.go +++ b/seaweed-block/core/recovery/sender.go @@ -159,10 +159,13 @@ func NewSender(primaryStore storage.LogicalStorage, coordinator *PeerShipCoordin // NewSenderWithSink constructs a sender that delegates the WAL // pump to the provided sink. Run() calls sink.StartSession after // BaseDone, sink.DrainBacklog to run the pump, and sink.EndSession -// in defer. The legacy in-package streamBacklog / drainAndSeal / -// closeCh code path is NOT reached when a sink is installed — -// that's the architect's "no double live" guard at the recovery -// layer (mini-plan §4 INV-NO-DOUBLE-LIVE row). +// in defer. The in-package streamBacklog loop is NOT reached when a +// sink is installed — that is the architect's "no double live" guard +// at the recovery layer (mini-plan §4 INV-NO-DOUBLE-LIVE row). +// +// After DrainBacklog succeeds, Run still waits on closeCh and runs +// drainAndSeal (same as legacy) so PushLiveWrite session-lane entries +// converge until P2c routes live traffic through WalShipper. // // `sink` MUST be non-nil. For the legacy fallback, use NewSender. func NewSenderWithSink(primaryStore storage.LogicalStorage, coordinator *PeerShipCoordinator, conn io.ReadWriter, replicaID ReplicaID, sink WalShipperSink) *Sender { @@ -177,6 +180,44 @@ func NewSenderWithSink(primaryStore storage.LogicalStorage, coordinator *PeerShi return s } +// senderBacklogSink adapts Sender's legacy streamBacklog pump to +// WalShipperSink. P2b uses it to migrate e2e tests onto the sink +// branch without changing recovery wire format (receiver still expects +// frameWALEntry). Production P2d replaces this with transport.WalShipper +// once dual-lane + MsgShipEntry context is wired. +// +// StartSession / EndSession are no-ops: session bounds live on Sender +// (fromLSN/targetLSN set at Run entry); rewind semantics belong on the +// real WalShipper sink in transport. +type senderBacklogSink struct { + s *Sender +} + +func (w senderBacklogSink) StartSession(fromLSN uint64) error { + _ = fromLSN + return nil +} + +func (w senderBacklogSink) DrainBacklog(ctx context.Context) error { + if ctx.Err() != nil { + return ctx.Err() + } + return w.s.streamBacklog() +} + +func (w senderBacklogSink) EndSession() {} + +// NewSenderWithBacklogRelay constructs a Sender whose WalShipperSink +// replays WAL via streamBacklog (same frames as legacy NewSender). +// Prefer this over legacy NewSender in tests and bridge code migrating +// to the sink-first Run branch (P2b); production eventually uses +// NewSenderWithSink with a transport WalShipper adapter (P2d). +func NewSenderWithBacklogRelay(primaryStore storage.LogicalStorage, coordinator *PeerShipCoordinator, conn io.ReadWriter, replicaID ReplicaID) *Sender { + s := NewSender(primaryStore, coordinator, conn, replicaID) + s.sink = senderBacklogSink{s: s} + return s +} + // Close signals the sender that no more PushLiveWrite calls will // arrive for this session, so it may safely drain the live queue, // transition phase, and run the barrier round-trip. Idempotent. @@ -301,15 +342,16 @@ func (s *Sender) Run(ctx context.Context, sessionID, fromLSN, targetLSN uint64) // Step 5–7: WAL pump. // - // Sink-mode (P2a, mini-plan §3 P2): delegate StartSession + - // DrainBacklog to the injected sink. EndSession is fired in - // defer below. The legacy in-package streamBacklog/closeCh-wait/ - // drainAndSeal sequence is NOT reached when sink != nil — that's - // the architect's "no double live" guard at the recovery layer. + // Sink-mode (P2): delegate StartSession + DrainBacklog to the + // injected sink — streamBacklog is not called here (no duplicate + // WAL scan; mini-plan §4 INV-NO-DOUBLE-LIVE). EndSession defer + // brackets the session. // - // Legacy mode: in-package WAL pump (streamBacklog + closeCh - // wait + drainAndSeal). Removed in P2c once all callers - // migrate to NewSenderWithSink. + // After DrainBacklog (sink or legacy pump), Run still waits on + // closeCh and runs drainAndSeal so PushLiveWrite session-lane + // tails ship before barrier (until P2c routes live via WalShipper). + // + // Legacy mode (P2c removes this branch): direct streamBacklog. if s.sink != nil { // Defer EndSession FIRST so it runs on every exit path // after this point — including success path AND the @@ -334,6 +376,14 @@ func (s *Sender) Run(ctx context.Context, sessionID, fromLSN, targetLSN uint64) // (kickoff Q3 ratified Phase enum collapse to {Idle, Active}). // Skip the transition step; barrier-ack is the only // authoritative completion signal. + select { + case <-s.closeCh: + case <-ctx.Done(): + return 0, newFailure(FailureCancelled, PhaseAwaitClose, ctx.Err()) + } + if err := s.drainAndSeal(); err != nil { + return 0, err + } } else { // Legacy mode (P2c removes this branch): if err := s.streamBacklog(); err != nil { diff --git a/seaweed-block/core/recovery/sender_walshipper_sink_test.go b/seaweed-block/core/recovery/sender_walshipper_sink_test.go index 57dcc69b84..e0f38914ef 100644 --- a/seaweed-block/core/recovery/sender_walshipper_sink_test.go +++ b/seaweed-block/core/recovery/sender_walshipper_sink_test.go @@ -11,20 +11,19 @@ package recovery // sink interface; transport's WalShipper implements it via duck // typing (no import cycle since interface lives here). // -// P2a scope (this commit, smallest green-on-merge step): -// - WalShipperSink interface defined in this package. -// - recovery.Sender constructor accepts an OPTIONAL sink. If -// sink != nil, Run() delegates pump-shape steps (StartSession, -// DrainBacklog, EndSession) to the sink instead of running its -// own streamBacklog/drainAndSeal/closeCh. -// - Old e2e tests (sink == nil path) are untouched in P2a. +// P2 — Sender delegates the backlog WAL scan+pump to WalShipperSink. +// streamBacklog runs only inside the backlog-relay sink (P2b) or legacy +// NewSender (until P2c). After sink.DrainBacklog, Run still waits on +// Close and drainAndSeal for PushLiveWrite session tails until P2c. // -// P2b: migrate e2e tests to sink-based. -// P2c: make sink required; delete streamBacklog/drainAndSeal/etc. -// P2d: transport-layer integration tests for the two architect- -// mandated rules: -// 1. updateWalShipperEmitContext BEFORE sink.StartSession. -// 2. After sink.EndSession, restore steady lineage / context. +// Migration: +// - P2b: NewSenderWithBacklogRelay / NewSenderWithSink; e2e uses relay. +// +// P2c: make sink required; delete legacy NewSender pump. +// +// P2d: transport-layer integration tests for caller obligations: +// 1. updateWalShipperEmitContext BEFORE sink.StartSession. +// 2. After sink.EndSession, restore steady lineage / context. import ( "bytes" @@ -32,9 +31,7 @@ import ( "errors" "net" "sync" - "sync/atomic" "testing" - "time" "github.com/seaweedfs/seaweed-block/core/storage" ) @@ -138,7 +135,7 @@ func (r *recordingSink) snapshot() (startCalls, drainCalls, endCalls int, order // behavior is covered by existing TestE2E_RebuildHappyPath etc. // Skipped here by design (those tests still run via legacy path). func TestSender_NoSink_FallsBackToLegacyBehavior(t *testing.T) { - t.Skip("legacy-path coverage stays in TestE2E_RebuildHappyPath etc. for P2a; this slot is a placeholder for the migration in P2b") + t.Skip(`legacy-path coverage: instantiate NewSender (no sink) and exercise streamBacklog in isolation if needed — e2e use NewSenderWithBacklogRelay (P2b)`) } // TestSender_WithSink_DelegatesStartSession — basic delegation. @@ -149,12 +146,20 @@ func TestSender_NoSink_FallsBackToLegacyBehavior(t *testing.T) { // sink.StartSession(fromLSN) at least once. Order is asserted // in TestSender_WithSink_DelegationOrder. func TestSender_WithSink_DelegatesStartSession(t *testing.T) { - sink := newRecordingSink() - // Construct a Sender wired to the sink. The exact constructor - // signature is the impl decision; we expect a NewSenderWithSink - // or NewSenderWithOptions path. - _ = sink - t.Skip("TODO impl: NewSenderWithSink + Run delegation — drives this test from RED to GREEN") + t.Skip("redundant with TestSender_WithSink_DelegationOrder (covers StartSession + ordering)") +} + +func TestNewSenderWithBacklogRelay_installsRelaySink(t *testing.T) { + coord := NewPeerShipCoordinator() + store := storage.NewBlockStore(4, 4096) + prim, replicaEnd := net.Pipe() + defer prim.Close() + defer replicaEnd.Close() + snd := NewSenderWithBacklogRelay(store, coord, prim, "r1") + if snd.sink == nil { + t.Fatal("NewSenderWithBacklogRelay: expected backlog relay sink") + } + var _ WalShipperSink = senderBacklogSink{} } // TestSender_WithSink_DelegationOrder — pin the ordering contract. @@ -195,6 +200,13 @@ func TestSender_WithSink_DelegationOrder(t *testing.T) { sender := NewSenderWithSink(primary, coord, primaryConn, "r1", sink) receiver := NewReceiver(replica, replicaConn) + // After DrainBacklog returns, sink-mode Run waits on closeCh — + // unblock it so barrier + sink.EndSession can run. + go func() { + <-sink.drainCalledCh + sender.Close() + }() + var ( wg sync.WaitGroup runErr error @@ -297,12 +309,6 @@ func newMemorywalForTest(t *testing.T, numBlocks uint32, blockSize int) storage. return storage.NewBlockStore(numBlocks, blockSize) } -// silence-unused-import shims for placeholder tests still using SKIP. -var ( - _ = atomic.Int32{} - _ = time.Time{} -) - // TestSender_WithSink_EndSessionAlwaysCalled — always-cleanup contract. // // Sink.EndSession MUST be called via defer regardless of how Run @@ -354,7 +360,5 @@ func TestSender_WithSink_NoStreamBacklogReachable(t *testing.T) { // becomes a compile-time invariant. P2a softer version: // verify recordingSink.drainCalls == 1 AND no frameWALEntry // originates from a non-sink code path. - var sinkDrainCalls atomic.Int32 - _ = sinkDrainCalls t.Skip("TODO impl: sink-installed Sender does NOT run streamBacklog (P2a soft assert; P2c compile-time)") }