recovery: retain writes during registered pre-ready session

This commit is contained in:
pingqiu committed 2026-05-02 10:34:03 -07:00
1 parent 2c50e8cdc8
commit 36dd0231cb
3 files changed
+124 -9

No files matched your search

@@ -2,6 +2,7 @@ package recovery
import (
"context"
"errors"
"fmt"
"log"
"net"
@@ -68,6 +69,15 @@ type PrimaryBridge struct {
senders map[ReplicaID]*Sender
}
// ErrNoActiveSession means the bridge has no sender registered for the
// requested replica.
var ErrNoActiveSession = errors.New("recovery bridge: no active session")
// ErrSessionNotLiveReady means a session is registered and owns the
// peer's WAL route, but the sink has not entered its live feeder window
// yet. Callers must retain/replay the WAL fact, not fall back to steady.
var ErrSessionNotLiveReady = errors.New("recovery bridge: session not live-ready")
// NewPrimaryBridge constructs a bridge bound to the primary's
// substrate. Callbacks may be nil (fire-and-forget).
func NewPrimaryBridge(store storage.LogicalStorage, coord *PeerShipCoordinator, onStart SessionStartCallback, onClose SessionCloseCallback) *PrimaryBridge {
@@ -191,6 +201,18 @@ func (b *PrimaryBridge) HasActiveSession(replicaID ReplicaID) bool {
return ok && sender.LiveReady()
}
// HasRegisteredSession reports whether recovery currently owns this
// peer's WAL route, regardless of whether the sender is live-ready yet.
// This is the routing-state check; HasActiveSession is only the
// push-ready check. Transport must not use "not live-ready" as license
// to fall back to steady while a recovery session is registered.
func (b *PrimaryBridge) HasRegisteredSession(replicaID ReplicaID) bool {
b.mu.Lock()
defer b.mu.Unlock()
_, ok := b.senders[replicaID]
return ok
}
// PushLiveWrite forwards a primary-side WAL append to the active
// session for this peer (if any). Caller is the WAL shipper
// integration; they MUST have already consulted
@@ -204,10 +226,10 @@ func (b *PrimaryBridge) PushLiveWrite(replicaID ReplicaID, lba uint32, lsn uint6
sender, ok := b.senders[replicaID]
b.mu.Unlock()
if !ok {
return fmt.Errorf("recovery bridge: no active session for replica %q", replicaID)
return fmt.Errorf("%w for replica %q", ErrNoActiveSession, replicaID)
}
if !sender.LiveReady() {
return fmt.Errorf("recovery bridge: session for replica %q is not live-ready", replicaID)
return fmt.Errorf("%w for replica %q", ErrSessionNotLiveReady, replicaID)
}
return sender.PushLiveWrite(lba, lsn, data)
}
+11 -2
View File
@@ -83,12 +83,21 @@ func (e *BlockExecutor) FeedLiveWrite(replicaID string, lineage RecoveryLineage,
// WalShipper's emit context. During recovery, every live write must
// enter through the active session sink so the one monotonic cursor
// speaks through one wire profile.
if e.dualLane != nil && e.dualLane.Bridge.HasActiveSession(recovery.ReplicaID(replicaID)) {
if e.dualLane != nil && e.dualLane.Bridge.HasRegisteredSession(recovery.ReplicaID(replicaID)) {
err := e.dualLane.Bridge.PushLiveWrite(recovery.ReplicaID(replicaID), lba, lsn, data)
if err == nil {
return LiveWriteEmitted, nil
}
if !errors.Is(err, ErrSinkSealed) {
if errors.Is(err, recovery.ErrSessionNotLiveReady) {
log.Printf("transport: live write retained replica=%s lsn=%d (recovery session registered but not live-ready)",
replicaID, lsn)
return LiveWriteRetained, nil
}
if errors.Is(err, recovery.ErrNoActiveSession) {
// Bridge sender-map deletion raced with our registered-session
// snapshot. Recovery no longer owns this write; use the steady
// path below.
} else if !errors.Is(err, ErrSinkSealed) {
return LiveWriteRetained, err
}
// The bridge map can briefly outlive sink.EndSession. Once
@@ -469,11 +469,12 @@ func isTimeoutErr(err error) bool {
}
type recordingSessionSink struct {
started chan struct{}
release chan struct{}
ended chan struct{}
startErr error
notifyErr error
started chan struct{}
release chan struct{}
ended chan struct{}
startErr error
notifyErr error
startBlocksUntil <-chan struct{}
mu sync.Mutex
entries []struct {
@@ -494,6 +495,9 @@ func newRecordingSessionSink() *recordingSessionSink {
func (s *recordingSessionSink) StartSession(fromLSN uint64) error {
_ = fromLSN
close(s.started)
if s.startBlocksUntil != nil {
<-s.startBlocksUntil
}
return s.startErr
}
@@ -608,6 +612,86 @@ func TestExecutor_Ship_ActiveDualLaneSession_RoutesThroughSingleSessionEmitter(t
close(sink.release)
}
// TestExecutor_Ship_RegisteredButNotLiveReady_RetainsEvenWhenSteadyAllowed
// pins the single-state-machine boundary before the session sink is
// live-ready. Once the bridge has registered a recovery session, that
// session owns the peer's WAL route; "not live-ready yet" means retain
// for replay, not fall back to the old steady emitter just because the
// peer is still Healthy.
func TestExecutor_Ship_RegisteredButNotLiveReady_RetainsEvenWhenSteadyAllowed(t *testing.T) {
primary := storage.NewBlockStore(8, 4096)
coord := recovery.NewPeerShipCoordinator()
replicaID := "r1"
exec := NewBlockExecutorWithDualLane(primary, "127.0.0.1:1", "127.0.0.1:2", coord, recovery.ReplicaID(replicaID))
steadyClient, steadyServer := net.Pipe()
t.Cleanup(func() {
_ = steadyClient.Close()
_ = steadyServer.Close()
})
go func() {
_, _ = io.Copy(io.Discard, steadyServer)
}()
steadyLineage := RecoveryLineage{SessionID: 91, Epoch: 7, EndpointVersion: 3, TargetLSN: 100}
session, err := exec.registerSession(steadyLineage)
if err != nil {
t.Fatalf("register steady session: %v", err)
}
session.conn = steadyClient
shipper := exec.WalShipperFor(replicaID)
exec.updateWalShipperEmitContext(replicaID, steadyClient, steadyLineage, EmitProfileSteadyMsgShip)
sessionClient, sessionServer := net.Pipe()
t.Cleanup(func() {
_ = sessionClient.Close()
_ = sessionServer.Close()
})
go func() {
_, _ = io.Copy(io.Discard, sessionServer)
}()
releaseStart := make(chan struct{})
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
sink := newRecordingSessionSink()
sink.startBlocksUntil = releaseStart
if err := exec.dualLane.Bridge.StartRebuildSessionWithSink(
ctx, sessionClient, recovery.ReplicaID(replicaID), 77, 10, 10, sink,
); err != nil {
t.Fatalf("StartRebuildSessionWithSink: %v", err)
}
select {
case <-sink.started:
case <-time.After(2 * time.Second):
t.Fatal("session sink did not start")
}
if exec.dualLane.Bridge.HasActiveSession(recovery.ReplicaID(replicaID)) {
t.Fatal("bridge reports active before blocked StartSession returns")
}
data := make([]byte, 4096)
data[0] = 0x3c
disposition, err := exec.FeedLiveWrite(replicaID, steadyLineage, true, 3, 1, data)
if err != nil {
t.Fatalf("FeedLiveWrite during registered/not-ready session: %v", err)
}
if disposition != LiveWriteRetained {
t.Fatalf("disposition=%v want LiveWriteRetained", disposition)
}
if got := sink.Entries(); got != 0 {
t.Fatalf("not-ready session sink recorded %d entries; want 0", got)
}
if got := shipper.Cursor(); got != 0 {
t.Fatalf("steady shipper cursor=%d want 0; steady fallback emitted during recovery start window", got)
}
close(releaseStart)
cancel()
_ = sessionClient.Close()
_ = sessionServer.Close()
}
func TestExecutor_FeedLiveWrite_SteadyDisallowed_RetainsWithoutDial(t *testing.T) {
primary := storage.NewBlockStore(8, 4096)
ln, err := net.Listen("tcp", "127.0.0.1:0")