mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 19:17:28 +01:00
transport: fix doRebuild race — wait for replica barrier ack
## 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
This commit is contained in:
1 parent
7f9232af28
commit
7ed813a3b7
3 files changed
+40
-5
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user