From ccc3ca6cb321adcc09c1bc2aef71f6066dc9d15e Mon Sep 17 00:00:00 2001 From: pingqiu Date: Fri, 1 May 2026 23:24:55 -0700 Subject: [PATCH] transport: route live WAL through single feed ingress --- seaweed-block/core/replication/peer.go | 27 +++++-------- seaweed-block/core/replication/peer_test.go | 17 ++++---- seaweed-block/core/transport/executor.go | 25 ------------ seaweed-block/core/transport/ship_sender.go | 39 +++++++++++++++---- .../core/transport/ship_sender_test.go | 31 +++++++++++++++ 5 files changed, 79 insertions(+), 60 deletions(-) diff --git a/seaweed-block/core/replication/peer.go b/seaweed-block/core/replication/peer.go index c3f753b75a..cfec1b04a6 100644 --- a/seaweed-block/core/replication/peer.go +++ b/seaweed-block/core/replication/peer.go @@ -519,30 +519,23 @@ func (p *ReplicaPeer) ShipEntry(ctx context.Context, lineage transport.RecoveryL peerLineage := p.lineage targetID := p.target.ReplicaID executor := p.executor - if p.state == ReplicaDegraded { - p.mu.Unlock() - if handled, err := executor.TryPushLiveWrite(targetID, lba, lsn, data); handled { - if err != nil { - return err - } - log.Printf("replication: ship session-lane peer=%s lba=%d lsn=%d (peer degraded, active recovery session)", - targetID, lba, lsn) - return nil - } - log.Printf("replication: ship gate-degraded peer=%s lba=%d lsn=%d (peer degraded)", - targetID, lba, lsn) - return fmt.Errorf("replication: ShipEntry: peer %s degraded", targetID) - } + allowSteady := p.state == ReplicaHealthy p.mu.Unlock() - if err := p.executor.Ship(p.target.ReplicaID, peerLineage, lba, lsn, data); err != nil { + disposition, err := executor.FeedLiveWrite(targetID, peerLineage, allowSteady, lba, lsn, data) + if err != nil { log.Printf("replication: ship FAILED peer=%s addr=%s lba=%d lsn=%d: %v", - p.target.ReplicaID, p.target.DataAddr, lba, lsn, err) + targetID, p.target.DataAddr, lba, lsn, err) p.Invalidate(fmt.Sprintf("ship error: %v", err)) return err } + if disposition == transport.LiveWriteRetained { + log.Printf("replication: ship retained peer=%s lba=%d lsn=%d (state disallows steady; recovery will replay)", + targetID, lba, lsn) + return nil + } log.Printf("replication: ship ok peer=%s lba=%d lsn=%d", - p.target.ReplicaID, lba, lsn) + targetID, lba, lsn) return nil } diff --git a/seaweed-block/core/replication/peer_test.go b/seaweed-block/core/replication/peer_test.go index bec7c569e4..e881d6a46f 100644 --- a/seaweed-block/core/replication/peer_test.go +++ b/seaweed-block/core/replication/peer_test.go @@ -88,7 +88,8 @@ func TestReplicaPeer_ShipEntry_Happy(t *testing.T) { // TestReplicaPeer_ShipEntry_ConnFailure_MarksDegraded — the forward- // carry CARRY-1 pin test from T4a-2. On ship error, peer must mark // Degraded + (implicitly) Invalidate. Subsequent ShipEntry on the -// same peer returns error without touching the wire (T4a state gate). +// same peer is retained without touching the wire (health state is not +// the WAL egress owner; future recovery will replay from primary WAL). // // This closes catalogue §3.2.1 C4 (no-hard-stop / error-return // handoff) from PARTIAL to DONE. @@ -135,16 +136,12 @@ func TestReplicaPeer_ShipEntry_ConnFailure_MarksDegraded(t *testing.T) { t.Fatalf("expected ReplicaDegraded after ship failure, got %s", peer.State()) } - // Subsequent ShipEntry is rejected at the peer without touching - // the wire (peer-state gate). This is the "no-hard-stop" shape: - // failure degrades the peer, and upstream callers see a cheap - // error instead of a real dial attempt every time. + // Subsequent ShipEntry is retained without touching the wire. This + // is the single-egress shape: Degraded remains a health fact, not a + // second WAL-routing decision. err = peer.ShipEntry(context.Background(), transport.RecoveryLineage{}, 1, 2, data) - if err == nil { - t.Fatal("Degraded peer should reject subsequent ShipEntry, got nil") - } - if !strings.Contains(err.Error(), "degraded") { - t.Fatalf("expected 'degraded' error, got: %v", err) + if err != nil { + t.Fatalf("Degraded peer live write should be retained for recovery replay, got error: %v", err) } } diff --git a/seaweed-block/core/transport/executor.go b/seaweed-block/core/transport/executor.go index 95b1254de8..52f2e271dd 100644 --- a/seaweed-block/core/transport/executor.go +++ b/seaweed-block/core/transport/executor.go @@ -872,31 +872,6 @@ func (e *BlockExecutor) DualLanePrimaryBridge() (*recovery.PrimaryBridge, recove return e.dualLane.Bridge, e.dualLane.ReplicaID, true } -// TryPushLiveWrite routes a live WAL entry through an active dual-lane -// recovery session if one exists for replicaID. Returns (handled=true) -// whenever the entry was claimed by that session path, even if the -// session sink returned an error. Legacy-mode executors, or dual-lane -// executors with no active session for replicaID, return handled=false. -// -// This is the peer-layer counterpart to Ship's internal active-session -// gate: callers that would otherwise reject on coarse peer state can -// still respect the single-emitter rule and offer writes to the active -// recovery session before falling back to steady shipping. -func (e *BlockExecutor) TryPushLiveWrite(replicaID string, lba uint32, lsn uint64, data []byte) (handled bool, err error) { - if e.dualLane == nil { - return false, nil - } - rid := recovery.ReplicaID(replicaID) - if !e.dualLane.Bridge.HasActiveSession(rid) { - return false, nil - } - err = e.dualLane.Bridge.PushLiveWrite(rid, lba, lsn, data) - if errors.Is(err, ErrSinkSealed) { - return false, nil - } - return true, err -} - // Registry lifecycle note (architect P1 review #4): walShippers // entries are never torn down today. For replica churn, lineage // invalidation, or executor reuse across distinct peer diff --git a/seaweed-block/core/transport/ship_sender.go b/seaweed-block/core/transport/ship_sender.go index 313a16d334..61690138ed 100644 --- a/seaweed-block/core/transport/ship_sender.go +++ b/seaweed-block/core/transport/ship_sender.go @@ -10,6 +10,13 @@ import ( "github.com/seaweedfs/seaweed-block/core/recovery" ) +type LiveWriteDisposition int + +const ( + LiveWriteEmitted LiveWriteDisposition = iota + LiveWriteRetained +) + // shipWriteDeadline bounds Ship's TCP write so a dead replica can't // wedge the caller on the ~120s TCP retransmission timeout. // Matches V2 WALShipper.Ship (wal_shipper.go:240) verbatim. @@ -59,6 +66,17 @@ const shipDialTimeout = 3 * time.Second // epoch return nil without writing anything to the wire. // - 3s write deadline (wal_shipper.go:240): byte-identical bound. func (e *BlockExecutor) Ship(replicaID string, lineage RecoveryLineage, lba uint32, lsn uint64, data []byte) error { + _, err := e.FeedLiveWrite(replicaID, lineage, true, lba, lsn, data) + return err +} + +// FeedLiveWrite is the single primary-side ingress for one committed WAL +// fact toward a peer. It owns the route decision between an active +// recovery session and the legacy steady sender. Health state is a hint: +// when allowSteady is false and no recovery session claims the write, the +// WAL fact is retained in the primary WAL for future recovery instead of +// being emitted on the legacy steady connection. +func (e *BlockExecutor) FeedLiveWrite(replicaID string, lineage RecoveryLineage, allowSteady bool, lba uint32, lsn uint64, data []byte) (LiveWriteDisposition, error) { // Global single-emitter gate: an active recovery session owns the // peer's live WAL egress. Ship must not fall through to the legacy // steady MsgShip path because that path mutates the resident @@ -68,10 +86,10 @@ func (e *BlockExecutor) Ship(replicaID string, lineage RecoveryLineage, lba uint if e.dualLane != nil && e.dualLane.Bridge.HasActiveSession(recovery.ReplicaID(replicaID)) { err := e.dualLane.Bridge.PushLiveWrite(recovery.ReplicaID(replicaID), lba, lsn, data) if err == nil { - return nil + return LiveWriteEmitted, nil } if !errors.Is(err, ErrSinkSealed) { - return err + return LiveWriteRetained, err } // The bridge map can briefly outlive sink.EndSession. Once // ErrSinkSealed is visible, RecoverySink has already restored @@ -79,12 +97,17 @@ func (e *BlockExecutor) Ship(replicaID string, lineage RecoveryLineage, lba uint // Ship path instead of treating a closing session as having // claimed this live write. } + if !allowSteady { + log.Printf("transport: live write retained replica=%s lsn=%d (steady disallowed; recovery will replay)", + replicaID, lsn) + return LiveWriteRetained, nil + } e.mu.Lock() session, ok := e.sessions[lineage.SessionID] if !ok || session == nil { e.mu.Unlock() - return fmt.Errorf("transport: ship: no session for replica=%s sessionID=%d", + return LiveWriteRetained, fmt.Errorf("transport: ship: no session for replica=%s sessionID=%d", replicaID, lineage.SessionID) } // Epoch-== fence. V2 wal_shipper.go:217-221 silent drop. @@ -96,7 +119,7 @@ func (e *BlockExecutor) Ship(replicaID string, lineage RecoveryLineage, lba uint e.mu.Unlock() log.Printf("transport: ship: dropping LSN=%d replica=%s stale epoch %d (session epoch %d)", lsn, replicaID, lineage.Epoch, sessionEpoch) - return nil + return LiveWriteRetained, nil } conn := session.conn e.mu.Unlock() @@ -110,7 +133,7 @@ func (e *BlockExecutor) Ship(replicaID string, lineage RecoveryLineage, lba uint if err != nil { log.Printf("transport: ship lazy-dial FAILED replica=%s addr=%s: %v", replicaID, e.replicaAddr, err) - return fmt.Errorf("transport: ship: dial replica=%s sessionID=%d: %w", + return LiveWriteRetained, fmt.Errorf("transport: ship: dial replica=%s sessionID=%d: %w", replicaID, lineage.SessionID, err) } log.Printf("transport: ship lazy-dial ok replica=%s addr=%s", @@ -125,7 +148,7 @@ func (e *BlockExecutor) Ship(replicaID string, lineage RecoveryLineage, lba uint case !stillActive || current != session: e.mu.Unlock() _ = dialed.Close() - return fmt.Errorf("transport: ship: session %d invalidated during dial", + return LiveWriteRetained, fmt.Errorf("transport: ship: session %d invalidated during dial", lineage.SessionID) case session.conn != nil: conn = session.conn @@ -155,10 +178,10 @@ func (e *BlockExecutor) Ship(replicaID string, lineage RecoveryLineage, lba uint s := e.WalShipperFor(replicaID) e.updateWalShipperEmitContext(replicaID, conn, lineage, EmitProfileSteadyMsgShip) if err := s.NotifyAppend(lba, lsn, data); err != nil { - return fmt.Errorf("transport: ship: write failed replica=%s lsn=%d: %w", + return LiveWriteRetained, fmt.Errorf("transport: ship: write failed replica=%s lsn=%d: %w", replicaID, lsn, err) } - return nil + return LiveWriteEmitted, nil } // HasSession reports whether a session with the given SessionID is diff --git a/seaweed-block/core/transport/ship_sender_test.go b/seaweed-block/core/transport/ship_sender_test.go index c1538749ef..166d5a8a3a 100644 --- a/seaweed-block/core/transport/ship_sender_test.go +++ b/seaweed-block/core/transport/ship_sender_test.go @@ -608,6 +608,37 @@ func TestExecutor_Ship_ActiveDualLaneSession_RoutesThroughSingleSessionEmitter(t close(sink.release) } +func TestExecutor_FeedLiveWrite_SteadyDisallowed_RetainsWithoutDial(t *testing.T) { + primary := storage.NewBlockStore(8, 4096) + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + deadAddr := ln.Addr().String() + _ = ln.Close() + + exec := NewBlockExecutor(primary, deadAddr) + lineage := shipTestLineage() + if err := exec.registerShipSessionForTest(lineage); err != nil { + t.Fatalf("registerShipSessionForTest: %v", err) + } + + data := make([]byte, 4096) + disposition, err := exec.FeedLiveWrite("r1", lineage, false, 1, 1, data) + if err != nil { + t.Fatalf("FeedLiveWrite with steady disallowed should retain, got error: %v", err) + } + if disposition != LiveWriteRetained { + t.Fatalf("disposition=%v want LiveWriteRetained", disposition) + } + exec.mu.Lock() + conn := exec.sessions[lineage.SessionID].conn + exec.mu.Unlock() + if conn != nil { + t.Fatal("FeedLiveWrite with steady disallowed unexpectedly dialed/attached conn") + } +} + // TestExecutor_Ship_ActiveSessionSealed_FallsBackToSteadyEmitter pins // the close-window behavior between RecoverySink.EndSession and bridge // sender-map deletion. ErrSinkSealed means the session sink is no longer