From 7ed813a3b75f0e2fe9ac3ce4d33524cdc72ed505 Mon Sep 17 00:00:00 2001 From: Ping Qiu Date: Tue, 14 Apr 2026 12:40:51 -0700 Subject: [PATCH] =?UTF-8?q?transport:=20fix=20doRebuild=20race=20=E2=80=94?= =?UTF-8?q?=20wait=20for=20replica=20barrier=20ack?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## The bug `doRebuild` returned `AchievedLSN = targetLSN` immediately after writing `MsgRebuildDone`, then closed the connection. The replica's handler runs on a separate goroutine and might not have processed the rebuild blocks yet when `OnSessionClose` fires — tests that read replica state after close saw unapplied blocks. `TestTransport_Rebuild_DoesNotCorruptLBA0` flaked at ~40% locally. ## The fix Structural, not a sleep. The replica's `MsgRebuildDone` handler now replies with `MsgBarrierResp` carrying the actual synced frontier, sent **after** `Sync()` returns. The primary's `doRebuild` reads that ack before returning, mirroring the existing `doCatchUp` barrier contract. The `time.Sleep(50ms)` previously needed in `TestTransport_Rebuild_StreamsAndAdvancesFrontier` is no longer required and has been removed — the structural ack guarantees durability when `closeCh` fires. `AchievedLSN` reported to `OnSessionClose` is now the replica's real frontier rather than the primary's intent — a more truthful durability boundary. ## Verification - `TestTransport_Rebuild_DoesNotCorruptLBA0` 5/5 runs pass (was ~3/5 before) - Full transport suite green under `-count=5` - Full repo suite green --- seaweed-block/core/transport/executor.go | 25 +++++++++++++++++-- seaweed-block/core/transport/replica.go | 15 ++++++++++- .../core/transport/transport_test.go | 5 ++-- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/seaweed-block/core/transport/executor.go b/seaweed-block/core/transport/executor.go index d0cc94adea..d512536639 100644 --- a/seaweed-block/core/transport/executor.go +++ b/seaweed-block/core/transport/executor.go @@ -207,8 +207,29 @@ func (e *BlockExecutor) doRebuild(targetLSN uint64) (uint64, error) { return 0, fmt.Errorf("rebuild done: %w", err) } - log.Printf("executor: rebuild complete, sent %d blocks (targetLSN=%d)", len(blocks), targetLSN) - return targetLSN, nil + // Wait for replica ack. Rebuild is not complete until the replica has + // actually applied the blocks, called AdvanceFrontier, and Sync'd. The + // ack carries the replica's real synced frontier; returning before this + // would let OnSessionClose fire while the replica is still catching up, + // producing a race where callers see "success" on a not-yet-applied + // store. Mirrors the catch-up barrier contract. + msgType, ackPayload, err := ReadMsg(conn) + if err != nil { + return 0, fmt.Errorf("rebuild ack: %w", err) + } + if msgType != MsgBarrierResp { + return 0, fmt.Errorf("rebuild ack: unexpected message type 0x%02x", msgType) + } + if len(ackPayload) < 8 { + return 0, fmt.Errorf("rebuild ack: short payload (%d)", len(ackPayload)) + } + replicaFrontier := binary.BigEndian.Uint64(ackPayload) + + log.Printf("executor: rebuild complete, sent %d blocks (targetLSN=%d, replicaFrontier=%d)", + len(blocks), targetLSN, replicaFrontier) + // Report the replica's actual frontier as achievedLSN — this is the + // truthful durability boundary, not the primary's intent. + return replicaFrontier, nil } func (e *BlockExecutor) InvalidateSession(replicaID string, sessionID uint64, reason string) { diff --git a/seaweed-block/core/transport/replica.go b/seaweed-block/core/transport/replica.go index ef0376e82a..c431422c8f 100644 --- a/seaweed-block/core/transport/replica.go +++ b/seaweed-block/core/transport/replica.go @@ -126,8 +126,21 @@ func (r *ReplicaListener) handleConn(conn net.Conn) { achievedLSN := binary.BigEndian.Uint64(payload) r.store.AdvanceFrontier(achievedLSN) } - if _, err := r.store.Sync(); err != nil { + // Sync, then acknowledge with the replica's actual synced + // frontier (same shape as MsgBarrierResp). The primary + // waits for this before returning from doRebuild — + // without it, the session could "complete" before the + // replica has applied and synced the rebuild blocks. Ack + // carries replica's true state. + frontier, err := r.store.Sync() + if err != nil { log.Printf("replica: sync after rebuild: %v", err) + return + } + resp := make([]byte, 8) + binary.BigEndian.PutUint64(resp, frontier) + if err := WriteMsg(conn, MsgBarrierResp, resp); err != nil { + log.Printf("replica: write rebuild ack: %v", err) } return diff --git a/seaweed-block/core/transport/transport_test.go b/seaweed-block/core/transport/transport_test.go index b2015ce661..c15080e8e7 100644 --- a/seaweed-block/core/transport/transport_test.go +++ b/seaweed-block/core/transport/transport_test.go @@ -140,8 +140,9 @@ func TestTransport_Rebuild_StreamsAndAdvancesFrontier(t *testing.T) { t.Fatal("timeout") } - // Small delay for replica handler to finish Sync after MsgRebuildDone. - time.Sleep(50 * time.Millisecond) + // No sleep needed: doRebuild waits for the replica's barrier ack, + // which is sent AFTER Sync(). When closeCh fires, the replica store + // has already applied all blocks and persisted the frontier. // Replica frontier must match primary. rR, _, _ := replica.Boundaries()