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()