fix(transport): unify replicaID in startRebuildDualLane (Phase 0 Fix #1)

INV-RID-UNIFIED-PER-SESSION: the engine arg replicaID is the single
source of truth for keying coord / WalShipper / sink / bridge for a
recovery session. Pre-fix, startRebuildDualLane built the RecoverySink
with the engine arg but called bridge.StartRebuildSessionWithSink with
the construction-time dl.ReplicaID. If the two diverged, the post-emit
hook keyed coord.RecordShipped under arg while the bridge keyed
coord.PinFloor under dl.ReplicaID — the keys never reconciled, no
error fired, watchdog timed out long after.

Fix:
  (a) Fail-closed assert at session entry: non-empty dl.ReplicaID must
      equal recovery.ReplicaID(arg). Empty stays permissive (legacy /
      test omission; arg flows through unchanged).
  (b) Thread argRID through to bridge.StartRebuildSessionWithSink so
      coord and sink key uniformly under arg from this point on.

Tests (core/transport/replicaid_unified_test.go):
  - TestPhase0Fix1_DriftRejected_FailsClosedAtEntry: non-empty drift
    returns error before any wire activity; coord.Phase Idle under
    both IDs; OnSessionStart/Close never fire.
  - TestPhase0Fix1_HappyPath_ArgEqualsConstructionID: aligned IDs,
    full rebuild, frontier converges, coord returns to Idle.
  - TestPhase0Fix1_EmptyConstructionRID_ArgFlowsThrough: empty
    construction RID bypasses guard; arg keys both bridge and sink;
    coord lifecycle observed under arg, never under empty key.

Refs: v3-recovery-execution-institution.md "Entity layering &
assembly contract" Phase 0; v3-recovery-wal-shipper-mini-plan §11.6.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 4.7 committed 2026-04-30 10:56:57 -07:00
1 parent 4f6d5c22e4
commit 11392baf41
2 files changed
+275 -1

No files matched your search

+28 -1
View File
@@ -7,6 +7,8 @@ import (
"net"
"sort"
"time"
"github.com/seaweedfs/seaweed-block/core/recovery"
)
// StartRebuild streams every base block from primary to replica when
@@ -146,6 +148,31 @@ func (e *BlockExecutor) doRebuild(replicaID string, session *activeSession, targ
// returns (P2c-slice B-2 lifecycle).
func (e *BlockExecutor) startRebuildDualLane(replicaID string, sessionID, epoch, endpointVersion, targetLSN uint64) error {
dl := e.dualLane
// Phase 0 Fix #1 — INV-RID-UNIFIED-PER-SESSION:
// the engine's arg `replicaID` is the SINGLE source of truth for keying
// coord / WalShipper / sink / bridge for this session. The
// construction-time `dl.ReplicaID` exists for the test accessor
// (DualLanePrimaryBridge) and any cluster-wiring sanity, but it is not
// re-keyed per session. If the two diverge:
//
// - sink.WriteMu()/post-emit hook keys under arg `replicaID`
// → WalShipper writeMu + RecordShipped advance under arg
// - bridge.StartRebuildSessionWithSink(... dl.ReplicaID ...) keys
// coord.PinFloor under dl.ReplicaID
// → PinFloor and shipCursor live under different IDs; the rebuild
// never reconciles, but no error fires until much later (frontier
// never converges, watchdog times out).
//
// Fix: (a) assert agreement at session entry — fail closed before we
// touch the wire — and (b) thread the arg through to the bridge so
// keying is uniform from this point on.
argRID := recovery.ReplicaID(replicaID)
if dl.ReplicaID != "" && dl.ReplicaID != argRID {
return fmt.Errorf("dual-lane replicaID drift: arg=%q dl=%q (must agree per INV-RID-UNIFIED-PER-SESSION)",
replicaID, string(dl.ReplicaID))
}
conn, err := net.DialTimeout("tcp", dl.DialAddr, 2*time.Second)
if err != nil {
return fmt.Errorf("dual-lane dial %s: %w", dl.DialAddr, err)
@@ -181,7 +208,7 @@ func (e *BlockExecutor) startRebuildDualLane(replicaID string, sessionID, epoch,
// caller wants partial rebuild from a specific LSN, the
// StartRebuild signature would need fromLSN added — out of scope.
if err := dl.Bridge.StartRebuildSessionWithSink(
context.Background(), conn, dl.ReplicaID, sessionID, 0, targetLSN, sink,
context.Background(), conn, argRID, sessionID, 0, targetLSN, sink,
); err != nil {
_ = conn.Close()
return fmt.Errorf("dual-lane start: %w", err)
@@ -0,0 +1,247 @@
package transport
// Phase 0 Fix #1 — INV-RID-UNIFIED-PER-SESSION test loop.
//
// Bug summary (pre-fix): startRebuildDualLane built the RecoverySink
// using the engine's arg `replicaID`, but called bridge.StartRebuild
// SessionWithSink with the construction-time `dl.ReplicaID`. If the
// two diverged, the post-emit hook (sink → coord.RecordShipped) keyed
// under arg while the bridge keyed coord.PinFloor under dl.ReplicaID.
// PinFloor and shipCursor advanced under different IDs; rebuild never
// reconciled, no error fired, watchdog eventually timed out.
//
// Post-fix: (1) the engine's arg `replicaID` is the single source of
// truth, threaded through to the bridge; (2) a fail-closed assert at
// session entry rejects mismatched (non-empty) construction IDs.
//
// Tests in this file:
//
// 1. TestPhase0Fix1_DriftRejected_FailsClosedAtEntry —
// construction RID != arg → StartRebuild returns drift error
// BEFORE any wire activity; no orphan coord state.
// 2. TestPhase0Fix1_HappyPath_ArgEqualsConstructionID —
// construction RID == arg, full rebuild completes; replica
// frontier converges; coord returns to Idle (the keying
// alignment is implicit: post-session phase observation under arg
// RID succeeds, which it could not if the bridge had keyed
// under a different ID).
// 3. TestPhase0Fix1_EmptyConstructionRID_ArgFlowsThrough —
// construction RID == "" (legacy/test omission). Assert is
// bypassed (empty is intentionally permissive), but post-fix the
// bridge call uses the engine arg, so coord and sink key
// uniformly under arg. Rebuild succeeds.
//
// Regression coverage: the existing dual-lane suite
// (TestDualLane_BlockExecutor_StartRebuild, TestC1_PostEmitHook_…,
// TestDualLane_LiveWritesDuringSession_AtomicSeal, etc.) all run with
// arg == construction RID, exercising the same code path. The fix
// only changed the bridge call's RID source; if any existing path
// silently relied on dl.ReplicaID being distinct from arg, it would
// break in this run. None do.
import (
"strings"
"testing"
"time"
"github.com/seaweedfs/seaweed-block/core/adapter"
"github.com/seaweedfs/seaweed-block/core/recovery"
"github.com/seaweedfs/seaweed-block/core/storage"
)
// TestPhase0Fix1_DriftRejected_FailsClosedAtEntry — construction
// RID and arg disagree (and construction RID is non-empty). The
// assert in startRebuildDualLane MUST fire before any dial / sink
// construction / coord state mutation.
//
// Observables:
// - StartRebuild returns an error mentioning "drift".
// - Neither replica ID has an active coord session (Phase == Idle).
// - Bridge has not been entered (no OnSessionStart fired).
func TestPhase0Fix1_DriftRejected_FailsClosedAtEntry(t *testing.T) {
const numBlocks = 16
const blockSize = 64
primary := storage.NewBlockStore(numBlocks, blockSize)
for lba := uint32(0); lba < 4; lba++ {
_, _ = primary.Write(lba, makeBytes(blockSize, byte(lba+1)))
}
_, _ = primary.Sync()
replica := storage.NewBlockStore(numBlocks, blockSize)
dualLaneAddr, stop := runDualLaneListener(t, replica)
defer stop()
coord := recovery.NewPeerShipCoordinator()
// Construction RID and the arg passed to StartRebuild diverge.
const constructionRID = "r-construction"
const argRID = "r-arg-mismatch"
exec := NewBlockExecutorWithDualLane(
primary, "127.0.0.1:0", dualLaneAddr, coord,
recovery.ReplicaID(constructionRID),
)
startCh := make(chan adapter.SessionStartResult, 1)
closeCh := make(chan adapter.SessionCloseResult, 1)
exec.SetOnSessionStart(func(r adapter.SessionStartResult) { startCh <- r })
exec.SetOnSessionClose(func(r adapter.SessionCloseResult) { closeCh <- r })
_, _, primaryH := primary.Boundaries()
err := exec.StartRebuild(argRID, 9, 1, 1, primaryH)
if err == nil {
t.Fatal("expected drift error; StartRebuild returned nil")
}
if !strings.Contains(err.Error(), "drift") {
t.Errorf("expected error to mention 'drift'; got %v", err)
}
// Brief window to confirm nothing fires asynchronously.
select {
case got := <-startCh:
t.Fatalf("OnSessionStart fired despite drift: %+v", got)
case got := <-closeCh:
t.Fatalf("OnSessionClose fired despite drift: %+v", got)
case <-time.After(200 * time.Millisecond):
}
// No coord session under either ID.
if got := coord.Phase(recovery.ReplicaID(constructionRID)); got != recovery.PhaseIdle {
t.Errorf("constructionRID coord.Phase=%s want Idle (no session should have started)", got)
}
if got := coord.Phase(recovery.ReplicaID(argRID)); got != recovery.PhaseIdle {
t.Errorf("argRID coord.Phase=%s want Idle (no session should have started)", got)
}
}
// TestPhase0Fix1_HappyPath_ArgEqualsConstructionID — baseline case
// with construction RID == arg. Post-fix this exercises the unified
// path: bridge call now uses the arg-derived recovery.ReplicaID
// (which equals dl.ReplicaID here, but the source is now the arg).
//
// Observables:
// - Rebuild completes successfully; achievedLSN == primary H.
// - Replica frontier matches primary.
// - Post-session coord.Phase(arg) returns Idle (the bridge keyed
// under arg, the sink keyed under arg — they aligned, so the
// session lifecycle ran cleanly).
func TestPhase0Fix1_HappyPath_ArgEqualsConstructionID(t *testing.T) {
const numBlocks = 32
const blockSize = 64
primary := storage.NewBlockStore(numBlocks, blockSize)
for lba := uint32(0); lba < 8; lba++ {
_, _ = primary.Write(lba, makeBytes(blockSize, byte(lba+0x40)))
}
_, _ = primary.Sync()
_, _, primaryH := primary.Boundaries()
replica := storage.NewBlockStore(numBlocks, blockSize)
dualLaneAddr, stop := runDualLaneListener(t, replica)
defer stop()
coord := recovery.NewPeerShipCoordinator()
const replicaID = "r-aligned"
exec := NewBlockExecutorWithDualLane(
primary, "127.0.0.1:0", dualLaneAddr, coord,
recovery.ReplicaID(replicaID),
)
closeCh := make(chan adapter.SessionCloseResult, 1)
exec.SetOnSessionClose(func(r adapter.SessionCloseResult) { closeCh <- r })
if err := exec.StartRebuild(replicaID, 11, 1, 1, primaryH); err != nil {
t.Fatalf("StartRebuild: %v", err)
}
select {
case r := <-closeCh:
if !r.Success {
t.Fatalf("session not Success: %s", r.FailReason)
}
if r.AchievedLSN != primaryH {
t.Errorf("achievedLSN=%d want %d", r.AchievedLSN, primaryH)
}
case <-time.After(5 * time.Second):
t.Fatal("OnSessionClose did not fire within 5s")
}
rR, _, _ := replica.Boundaries()
if rR != primaryH {
t.Errorf("replica frontier=%d want %d", rR, primaryH)
}
if got := coord.Phase(recovery.ReplicaID(replicaID)); got != recovery.PhaseIdle {
t.Errorf("post-session coord.Phase=%s want Idle", got)
}
}
// TestPhase0Fix1_EmptyConstructionRID_ArgFlowsThrough —
// construction RID is empty (a test/legacy oversight). The drift
// guard is intentionally permissive on empty (rationale: empty
// signals "not configured at construction"; the arg is the truth).
// Post-fix the bridge call still uses the engine arg, so coord and
// sink key uniformly under arg.
//
// Pre-fix this test would have wedged: bridge.StartRebuildSession
// WithSink(... dl.ReplicaID="" ...) keys coord.PinFloor under "",
// while the sink's post-emit hook keys RecordShipped under arg.
// The keying mismatch silently broke RecordShipped's contribution
// to PinFloor, but the rebuild barrier still completed — so the
// only observable bug was a stale shipCursor that operator surfaces
// would later misreport. Post-fix: the bridge gets the arg-derived
// RID, both sides line up, frontier converges cleanly.
func TestPhase0Fix1_EmptyConstructionRID_ArgFlowsThrough(t *testing.T) {
const numBlocks = 32
const blockSize = 64
primary := storage.NewBlockStore(numBlocks, blockSize)
for lba := uint32(0); lba < 6; lba++ {
_, _ = primary.Write(lba, makeBytes(blockSize, byte(lba+0x20)))
}
_, _ = primary.Sync()
_, _, primaryH := primary.Boundaries()
replica := storage.NewBlockStore(numBlocks, blockSize)
dualLaneAddr, stop := runDualLaneListener(t, replica)
defer stop()
coord := recovery.NewPeerShipCoordinator()
const argRID = "r-arg-only"
exec := NewBlockExecutorWithDualLane(
primary, "127.0.0.1:0", dualLaneAddr, coord,
recovery.ReplicaID(""), // intentionally empty: guard bypasses
)
closeCh := make(chan adapter.SessionCloseResult, 1)
exec.SetOnSessionClose(func(r adapter.SessionCloseResult) { closeCh <- r })
if err := exec.StartRebuild(argRID, 13, 1, 1, primaryH); err != nil {
t.Fatalf("StartRebuild: %v", err)
}
select {
case r := <-closeCh:
if !r.Success {
t.Fatalf("session not Success: %s", r.FailReason)
}
if r.AchievedLSN != primaryH {
t.Errorf("achievedLSN=%d want %d", r.AchievedLSN, primaryH)
}
case <-time.After(5 * time.Second):
t.Fatal("OnSessionClose did not fire within 5s")
}
rR, _, _ := replica.Boundaries()
if rR != primaryH {
t.Errorf("replica frontier=%d want %d", rR, primaryH)
}
// Coord lifecycle ran under arg (proves the bridge keyed under arg,
// not under the empty construction RID).
if got := coord.Phase(recovery.ReplicaID(argRID)); got != recovery.PhaseIdle {
t.Errorf("post-session coord.Phase(arg)=%s want Idle", got)
}
// And the empty-key path was never used.
if got := coord.Phase(recovery.ReplicaID("")); got != recovery.PhaseIdle {
t.Errorf("coord.Phase(\"\")=%s want Idle (empty key should never have been touched)", got)
}
}