Skip to main content

melin_server_runtime/replication/
mod.rs

1//! Replication — synchronous input-stream streaming from primary to replica.
2//!
3//! The primary's JournalStage encodes each `InputSlot` it just durably
4//! journaled into a wire-ready `InputBatch` frame in the replication ring
5//! (separately from the journal-codec bytes it writes to disk). The
6//! `ReplicationSender` thread forwards those frames as-is over TCP/DPDK
7//! — no decode + re-encode on the hot path. The replica decodes the
8//! frames straight back into `InputSlot`s, publishes them to its local
9//! input disruptor with the primary's pre-assigned sequences and
10//! timestamps, and the replica's JournalStage re-encodes them through
11//! its own writer for byte-exact-on-replay durability.
12//!
13//! ## Wire Protocol
14//!
15//! Length-prefixed frames, little-endian, over a dedicated TCP connection
16//! (or DPDK pipe). The full `InputBatch` payload layout lives in
17//! `melin_transport_core::replication::protocol` /
18//! `melin_transport_core::replication_wire`.
19//!
20//! ### Auth (before handshake)
21//! - **Challenge** (Primary → Replica): `[len:u32][0x03][nonce:[u8;32]]`
22//! - **ChallengeResponse** (Replica → Primary): `[len:u32][0x04][signature:[u8;64]][pubkey:[u8;32]]`
23//! - **AuthOk** (Primary → Replica): `[len:u32][0x05]`
24//! - **AuthFailed** (Primary → Replica): `[len:u32][0x06]`
25//!
26//! ### Replica → Primary
27//! - **Handshake**: `[len:u32][0x01][last_sequence:u64][chain_hash:[u8;32]]`
28//! - **Ack**: `[len:u32][0x02][acked_sequence:u64][in_memory_sequence:u64]`
29//!
30//! ### Primary → Replica
31//! - **StreamStart**: `[len:u32][0x10][start_sequence:u64][segment_start_sequence:u64][anchor_hash:[u8;32]]`
32//!   — the segment header identity a fresh replica creates its journal
33//!   with (lineage origin for full catch-up, the seeded segment's
34//!   identity after a snapshot transfer)
35//! - **NeedSnapshot**: `[len:u32][0x11]`
36//! - **HashMismatch**: `[len:u32][0x12]` — divergent replica journal;
37//!   the replica archives its lineage, then the snapshot flow follows
38//! - **SnapshotBegin**: `[len:u32][0x13][snapshot_len:u64][snap_sequence:u64][snap_chain_hash:[u8;32]]`
39//! - **SnapshotChunk**: `[len:u32][0x14][data...]`
40//! - **SnapshotEnd**: `[len:u32][0x15][crc32c:u32]`
41//! - **Rotate**: `[len:u32][0x16][boundary_seq:u64][tail_hash:[u8;32]]`
42//!   — primary-driven rotation, verified + adopted by the replica
43//! - **ChainCheck**: `[len:u32][0x17][sequence:u64][chain_hash:[u8;32]]`
44//!   — periodic live-stream chain validation
45//! - **SegmentSeedBegin**: `[len:u32][0x18][seed_len:u64]` — raw byte
46//!   prefix of the primary's segment containing the snapshot boundary;
47//!   body rides SnapshotChunk frames, ends with SnapshotEnd
48//! - **InputBatch**: `[len:u32][0x21][count:u16][slot...]` — see
49//!   `transport-core::replication_wire` for the per-slot layout
50//! - **Heartbeat**: `[len:u32][0x30][sequence:u64]`
51//!
52//! ## Limitations
53//!
54//! - Dual replication (up to 2 replicas in parallel)
55//!
56//! See `docs/replication.md` for the full design document and limitation details.
57
58use std::sync::Arc;
59#[cfg(test)]
60use std::sync::atomic::AtomicU64;
61use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
62
63use melin_journal::{BufferedWriter, JournalWrite};
64
65use melin_app::Application;
66use melin_transport_core::pipeline::{InputSlot, OutputSlot};
67use melin_transport_core::replication::archive::{ArchiveReason, archive_local_lineage};
68use melin_transport_core::replication::protocol::{MAX_CONTROL_FRAME, decode_primary_message};
69
70mod auth;
71#[cfg(feature = "dpdk")]
72mod dpdk;
73mod receiver_transport;
74mod tcp_receiver;
75mod tcp_sender;
76// Only the DPDK sender uses the worker, but it is pure thread/channel
77// plumbing with no libdpdk dependency — so it is also compiled under
78// `test`, keeping its mechanics (and the affinity trap it exists to
79// avoid) testable on a build without the `dpdk` feature.
80#[cfg(any(feature = "dpdk", test))]
81mod validation_worker;
82
83use receiver_transport::{ControlFrameSource, SessionExit, StreamingResult, receive_chunked_body};
84
85/// Writer-side view of the trading-halt gate (the `replicas_connected`
86/// counter). The matching stage refuses new orders while the count is zero, so
87/// the counter must reflect the number of replicas that have **authenticated**
88/// — a bare connection must not lift the halt. Both senders (kernel-TCP and
89/// DPDK) lift/lower the gate through this view so the policy, the memory
90/// orderings, and the "trading halted" warning live in one place and cannot
91/// drift apart.
92///
93/// Deliberately a *borrowed view*, not an owner: `melin_transport_core` owns
94/// the `Arc<AtomicU32>` and reads it on the matching hot path and for the
95/// `melin_replicas_connected` gauge. This type is only the senders' write
96/// surface, so centralizing it costs nothing on the read side.
97pub(crate) struct ReplicaGate<'a> {
98    count: &'a AtomicU32,
99}
100
101impl<'a> ReplicaGate<'a> {
102    pub(crate) fn new(count: &'a AtomicU32) -> Self {
103        Self { count }
104    }
105
106    /// A replica has authenticated — lift the halt by one. `Release` so a peer
107    /// that observes the connect also observes everything that preceded it.
108    pub(crate) fn lift(&self) {
109        self.count.fetch_add(1, Ordering::Release);
110    }
111
112    /// A replica left — lower the halt by one. Returns `true` if it was the
113    /// last one (trading is now unprotected), emitting the halt warning here so
114    /// both senders share the wording. `fetch_sub` returns the *prior* count,
115    /// so `== 1` means this call took it to zero; deriving "last one" from the
116    /// returned value rather than a follow-up load avoids a TOCTOU race with a
117    /// concurrent reconnect's `lift`.
118    pub(crate) fn lower(&self) -> bool {
119        let was_last = self.count.fetch_sub(1, Ordering::Release) == 1;
120        if was_last {
121            tracing::warn!("all replicas disconnected — trading halted");
122        }
123        was_last
124    }
125}
126
127// Wire-protocol types, auth, catch-up, ack queueing, dual-track
128// cursor management, and per-replica metrics now live in
129// `melin_transport_core::replication`. Re-export the public types
130// here so the module's public API surface (e.g.
131// `melin_server_runtime::replication::Ack` / `::ReplicationMetrics`) is
132// unchanged for downstream consumers and tests.
133pub use melin_transport_core::replication::ack_queue::{
134    PendingAck, PendingAckQueue, try_flush_dual_track, wait_for_journal_cursor,
135};
136pub use melin_transport_core::replication::protocol::{
137    Ack, Handshake, PrimaryMessage, ReplicaMessage,
138};
139pub use melin_transport_core::replication::{ReplicaCursors, ReplicationMetrics, SentHighWater};
140
141#[cfg(feature = "dpdk")]
142pub(crate) use dpdk::REPL_DISPATCH_BURST;
143#[cfg(feature = "dpdk")]
144pub use dpdk::{DpdkReplicationDriver, run_receiver_dpdk};
145pub use tcp_receiver::{ReceiverResult, run_receiver};
146pub use tcp_sender::{ReplicationListener, Sender, run_sender};
147
148/// The handles a replica's receive loop shares with the rest of the
149/// node — the admin endpoint and the control-plane raft driver.
150/// Bundled (rather than five positional parameters) because they
151/// always travel together through the kernel-TCP and DPDK receiver
152/// signatures, and a transposition between same-typed flags is exactly
153/// the bug a bundle prevents.
154#[derive(Clone)]
155pub struct ReplicaControlPlane {
156    /// Promotion request: polled by the receive loop; filed by the
157    /// admin `PROMOTE` command or the raft driver (auto-promotion).
158    pub promote: crate::promotion::PromotionRequest,
159    /// Flipped `true` once journal recovery has seeded the fence epoch,
160    /// so the raft driver can trust this node's advertised tip — see
161    /// `melin_raft::recency::TipSource`.
162    pub tip_ready: std::sync::Arc<AtomicBool>,
163    /// Sequence half of the advertised journal tip — see
164    /// [`melin_transport_core::AdvertisedJournalTip`].
165    pub journal_tip: melin_transport_core::AdvertisedJournalTip,
166    /// `true` while this replica holds an authenticated replication
167    /// connection to its primary. The raft driver refuses to
168    /// auto-promote while set: a live link means the primary is
169    /// demonstrably alive, and winning a control-plane election (e.g.
170    /// because a *different* node's raft died) must not depose a
171    /// healthy primary.
172    pub primary_link_up: std::sync::Arc<AtomicBool>,
173    /// The ack policy last advertised by the primary
174    /// (`AckPolicy::as_u8`; `ACK_POLICY_UNKNOWN` until first
175    /// contact), refreshed by `StreamStart` and every `Heartbeat`. The
176    /// raft driver's auto-promotion refusal reads this — the policy the
177    /// *primary* acked under decides whether an election win proves
178    /// this replica holds every acked order, not this node's own
179    /// (possibly pre-staged) configuration. Falls back to this node's
180    /// own policy while unknown, which is exactly the pre-propagation
181    /// behavior.
182    pub primary_ack_policy: std::sync::Arc<std::sync::atomic::AtomicU8>,
183    /// Whether the replica's pipeline is healthy — fed to the replica's
184    /// minimal health endpoint (`/health` OK/ERR and the
185    /// `melin_pipeline_healthy` gauge). Process-lifetime (the per-session
186    /// pipeline is torn down and rebuilt across resyncs, so its own
187    /// `journal_failed` latch cannot be handed to the endpoint):
188    /// `true` from boot, latched `false` by the journal stage's failure
189    /// wrapper, reset `true` when a fresh pipeline is built.
190    pub pipeline_healthy: std::sync::Arc<AtomicBool>,
191}
192
193impl ReplicaControlPlane {
194    /// Fresh handles for a replica boot: nothing requested, tip not
195    /// trustworthy yet, tip sequence 0, primary link down, pipeline
196    /// healthy (nothing has failed yet).
197    pub fn new() -> Self {
198        Self {
199            promote: crate::promotion::PromotionRequest::new(),
200            tip_ready: std::sync::Arc::new(AtomicBool::new(false)),
201            journal_tip: melin_transport_core::AdvertisedJournalTip::new(
202                melin_transport_core::WireSeq::new(0),
203            ),
204            primary_link_up: std::sync::Arc::new(AtomicBool::new(false)),
205            primary_ack_policy: std::sync::Arc::new(std::sync::atomic::AtomicU8::new(
206                crate::ack_policy::ACK_POLICY_UNKNOWN,
207            )),
208            pipeline_healthy: std::sync::Arc::new(AtomicBool::new(true)),
209        }
210    }
211}
212
213impl Default for ReplicaControlPlane {
214    fn default() -> Self {
215        Self::new()
216    }
217}
218
219/// Diagnostic: emit a `tcp_info` span at debug level describing the
220/// kernel's view of the socket (rtt, cwnd, retrans, unacked, rcv_space,
221/// rto). Guarded internally with `tracing::enabled!` so the
222/// `getsockopt(TCP_INFO)` syscall is skipped when debug logging is off —
223/// the call is free at runtime unless `RUST_LOG=debug` (or a more
224/// specific filter) is active. Used to distinguish user-space stalls
225/// from TCP-level congestion collapse when diagnosing replication
226/// slowdowns.
227pub(super) fn log_tcp_info(fd: std::os::unix::io::RawFd, tag: &str, slot: usize) {
228    if !tracing::enabled!(tracing::Level::DEBUG) {
229        return;
230    }
231    // SAFETY: all-zero pattern is a valid `tcp_info` (all numeric fields).
232    // The kernel fills in whatever the running version supports and
233    // returns the written length in `len`.
234    let mut info: libc::tcp_info = unsafe { std::mem::zeroed() };
235    let mut len = std::mem::size_of::<libc::tcp_info>() as libc::socklen_t;
236    let rc = unsafe {
237        libc::getsockopt(
238            fd,
239            libc::IPPROTO_TCP,
240            libc::TCP_INFO,
241            &mut info as *mut _ as *mut libc::c_void,
242            &mut len,
243        )
244    };
245    if rc != 0 {
246        tracing::debug!(
247            slot,
248            tag,
249            errno = std::io::Error::last_os_error().raw_os_error().unwrap_or(0),
250            "tcp_info getsockopt failed"
251        );
252        return;
253    }
254    tracing::debug!(
255        slot,
256        tag,
257        state = info.tcpi_state,
258        ca_state = info.tcpi_ca_state,
259        rtt_us = info.tcpi_rtt,
260        rttvar_us = info.tcpi_rttvar,
261        snd_cwnd = info.tcpi_snd_cwnd,
262        snd_ssthresh = info.tcpi_snd_ssthresh,
263        snd_mss = info.tcpi_snd_mss,
264        rcv_mss = info.tcpi_rcv_mss,
265        unacked = info.tcpi_unacked,
266        retrans = info.tcpi_retrans,
267        total_retrans = info.tcpi_total_retrans,
268        lost = info.tcpi_lost,
269        rcv_space = info.tcpi_rcv_space,
270        rto_us = info.tcpi_rto,
271        "tcp_info"
272    );
273}
274
275/// Sleep for the given duration in 100ms increments, checking the shutdown
276/// flag and the promotion request between increments. Returns early if
277/// either is set.
278pub(super) fn sleep_checking_flags(
279    duration: std::time::Duration,
280    shutdown: &AtomicBool,
281    promote: &crate::promotion::PromotionRequest,
282) {
283    let deadline = std::time::Instant::now() + duration;
284    while std::time::Instant::now() < deadline {
285        if shutdown.load(Ordering::Relaxed) || promote.is_requested() {
286            return;
287        }
288        std::thread::sleep(std::time::Duration::from_millis(100));
289    }
290}
291
292/// Wait out one retry delay: sleep for the current `backoff` (checking
293/// the shutdown and promotion flags — see [`sleep_checking_flags`]),
294/// then double it, capped at [`MAX_BACKOFF`], for the next attempt.
295/// Sleep-then-double, so the first retry waits the base delay rather
296/// than twice it. Every retry arm in both receivers goes through this —
297/// don't hand-roll the idiom, the copies drift.
298pub(super) fn sleep_then_double_backoff(
299    backoff: &mut std::time::Duration,
300    shutdown: &AtomicBool,
301    promote: &crate::promotion::PromotionRequest,
302) {
303    sleep_checking_flags(*backoff, shutdown, promote);
304    *backoff = (*backoff * 2).min(MAX_BACKOFF);
305}
306
307/// Outcome of shutting down a replica pipeline. All stage threads are
308/// joined in every variant — by the time the caller sees this, no
309/// pipeline thread can still touch the journal or snapshot files.
310pub(super) enum TeardownOutcome<A, W> {
311    /// Clean exit: both stages returned their state, reusable for the
312    /// next pipeline build (post-snapshot) or promotion.
313    Clean(A, W),
314    /// The journal stage exited with an error — its writer is gone and
315    /// the matching stage's state is unusable (it may have applied
316    /// events the journal never persisted). Carries the error so the
317    /// orchestrator can distinguish repairable chain divergence
318    /// (in-process resync) from journal I/O death (exit).
319    JournalFailed(melin_journal::JournalError),
320    /// A stage thread panicked; no state survives.
321    Panicked,
322}
323
324/// Shut down the replica pipeline and extract Exchange + journal writer
325/// from the stage threads.
326///
327/// Relies on the caller having published a `JournalEvent::Shutdown`
328/// sentinel to the input ring before invoking this — the journal and
329/// matching stages exit when they consume the sentinel via the normal
330/// event-processing path. We deliberately don't flip `shutdown_flag`
331/// here: doing so could cause a stage to take its emergency-abort
332/// branch *before* consuming the sentinel, hitting the
333/// drain-vs-cursor race that the sentinel design exists to avoid.
334///
335/// `drain_handle` and `shadow_handle` still observe `shutdown_flag`
336/// because they don't process the input ring (no sentinel reaches
337/// them); set the flag for them only.
338pub(super) fn shutdown_pipeline<A: Send + 'static, W: Send + 'static>(
339    shutdown_flag: &AtomicBool,
340    journal_handle: std::thread::JoinHandle<Result<W, melin_journal::JournalError>>,
341    matching_handle: std::thread::JoinHandle<A>,
342    drain_handle: std::thread::JoinHandle<()>,
343    shadow_handle: Option<std::thread::JoinHandle<()>>,
344) -> TeardownOutcome<A, W> {
345    // Defense-in-depth: set the flag before joining. The sentinel was
346    // already published by `teardown_replica_pipeline` before this call,
347    // so no further events can arrive in the input ring — setting the
348    // flag here cannot race with new publishes. The flag is the fallback
349    // exit signal for paths that don't observe the sentinel: the sequencer
350    // in `no-persist` builds, the drain consumer, the shadow stage, and
351    // any case where the receiver thread panicked before publishing the
352    // sentinel.
353    //
354    // The flag is also what makes these joins safe when the journal
355    // stage is already dead: the matching stage's gate on the (frozen)
356    // journal cursor is a non-blocking spin that re-checks the flag
357    // every iteration, and its shutdown drain is `try_consume`-bounded.
358    shutdown_flag.store(true, Ordering::Release);
359    // Join EVERY thread before reporting the outcome, even on failure:
360    // the in-process divergence-resync path archives the journal and
361    // snapshot right after this returns, and a still-running shadow
362    // thread could be mid-snapshot-write during those renames.
363    let journal_result = journal_handle.join();
364    let matching_result = matching_handle.join();
365    let _ = drain_handle.join();
366    if let Some(h) = shadow_handle {
367        let _ = h.join();
368    }
369    let writer = match journal_result {
370        Ok(Ok(w)) => w,
371        // Already error!-logged by the journal thread's spawn wrapper.
372        Ok(Err(e)) => return TeardownOutcome::JournalFailed(e),
373        Err(_) => return TeardownOutcome::Panicked,
374    };
375    match matching_result {
376        Ok(exchange) => TeardownOutcome::Clean(exchange, writer),
377        Err(_) => TeardownOutcome::Panicked,
378    }
379}
380
381/// Live replica pipeline — built once on first connect (or after a snapshot
382/// transfer), persists across `Disconnected` reconnects so the orchestrator
383/// doesn't pay the journal-recover + thread-spawn cost on every drop.
384///
385/// Shared between the kernel-TCP and DPDK receivers; both build pipelines
386/// with the same shape (journal / matching / drain / optional shadow), and
387/// both refresh handshake state from `last_seq` + `chain_hash_lock` at
388/// reconnect time.
389pub(super) struct ReplicaPipelineHandles<A: Application, W: Send + 'static> {
390    pub(super) input_producer: melin_pipeline::ring::Producer<InputSlot<A::Event>>,
391    pub(super) journal_cursor: Arc<melin_pipeline::padding::Sequence>,
392    /// Highest wire seq durably persisted, published by JournalStage after
393    /// each fsync. Read by the orchestrator to fill in the reconnect
394    /// handshake without owning the writer. Typed so the handshake's resume
395    /// point can never be sourced from a ring-space counter (the adjacent
396    /// `journal_cursor` resets to ~0 every process start).
397    pub(super) last_seq: melin_transport_core::DurableWireSeqCursor,
398    /// Seqlock-published fsync state (chain hash + journal seq + ring
399    /// cursor), read half. Option to mirror the primary-side pattern;
400    /// always Some on replicas now.
401    pub(super) chain_hash_lock:
402        Option<melin_pipeline::seqlock::SeqLockReader<melin_transport_core::pipeline::FsyncState>>,
403    /// Primary-announced rotation hand-off: the receiver thread pushes
404    /// `Rotate` boundaries here (in stream order), the journal stage
405    /// pops and rotates at exactly those sequences. Replicas have no
406    /// local rotation triggers.
407    pub(super) stream_marks: melin_transport_core::pipeline::StreamMarkQueue,
408    /// Latched by the journal thread's spawn wrapper when the stage
409    /// exits with an error (chain divergence, journal I/O failure).
410    /// The streaming receiver checks it and tears the session down —
411    /// without this, a dead journal stage freezes the journal cursor
412    /// and the receiver wedges forever on ring backpressure or the
413    /// ack-durability wait.
414    pub(super) journal_failed: Arc<AtomicBool>,
415    /// Per-pipeline shutdown flag — flipped only on a controlled teardown
416    /// (Promote/Shutdown/Fatal/Snapshot). NOT flipped on `Disconnected`.
417    pub(super) pipeline_shutdown: Arc<AtomicBool>,
418    pub(super) journal_handle: std::thread::JoinHandle<Result<W, melin_journal::JournalError>>,
419    pub(super) matching_handle: std::thread::JoinHandle<A>,
420    pub(super) drain_handle: std::thread::JoinHandle<()>,
421    pub(super) shadow_handle: Option<std::thread::JoinHandle<()>>,
422}
423
424/// [`ReplicaPipelineHandles`] over the journal's writer — the only
425/// instantiation that exists. The struct keeps its writer parameter
426/// because the teardown helpers are writer-agnostic plumbing; this
427/// alias spares every signature from spelling the concrete type out.
428pub(super) type ReplicaHandles<A> =
429    ReplicaPipelineHandles<A, BufferedWriter<<A as Application>::Event>>;
430
431/// Build the replica pipeline and spawn its stage threads on the configured
432/// cores. Returns the bundle of state the orchestrator keeps across
433/// `Disconnected` reconnects.
434#[allow(clippy::too_many_arguments)]
435pub(super) fn build_replica_pipeline_with_threads<A>(
436    exchange: A,
437    writer: BufferedWriter<A::Event>,
438    cores: crate::layout::PipelineCores,
439    // How the replica's segment preparer materialises staged extents.
440    // A replica's rotation stall sits on the ack path — under
441    // `disk+ram`/`two-disks` it delays the primary's ack gate — so this
442    // is not a primary-only tuning knob.
443    staging_mode: melin_journal::StagingMode,
444    snapshot_interval_ms: u64,
445    snapshot_path: std::path::PathBuf,
446    group_commit_delay: std::time::Duration,
447    fence_state: Arc<melin_transport_core::fence::FenceState>,
448    // Process-lifetime health mirror for the replica health endpoint —
449    // see `ReplicaControlPlane::pipeline_healthy`. Reset `true` here (a
450    // fresh pipeline is healthy) and latched `false` by the journal
451    // thread's failure wrapper, alongside the per-pipeline
452    // `journal_failed` latch.
453    pipeline_healthy: Arc<AtomicBool>,
454) -> Result<ReplicaHandles<A>, Box<dyn std::error::Error>>
455where
456    A: Application + Send + 'static,
457    A::Event: Send + Sync + 'static,
458    A::Report: Send + 'static,
459    A::QueryResponse: Send + 'static,
460{
461    let shadow_exchange = <A as Application>::clone_via_snapshot(&exchange)?;
462
463    let enable_shadow = snapshot_interval_ms > 0;
464    // Shadow snapshot seeds its epoch from the fence state's current value
465    // (set from the replica's recovered journal before this builder runs).
466    let shadow_initial_epoch = fence_state.epoch();
467    let pipeline = melin_transport_core::pipeline::build_replica_pipeline(
468        exchange,
469        writer,
470        4096, // max_journal_batch
471        group_commit_delay,
472        cores.stage_waits(),
473        enable_shadow,
474        fence_state,
475    );
476
477    let pipeline_shutdown = Arc::new(AtomicBool::new(false));
478
479    let ps = Arc::clone(&pipeline_shutdown);
480    let journal_core = cores.journal_seq.core;
481    let mut journal_stage = pipeline.journal_stage;
482    // Replicas never rotate on local triggers (size or operator
483    // command) — they adopt the boundaries the primary announces over
484    // the stream, which keeps segment boundaries (and with them chain
485    // values and journal bytes) identical across nodes.
486    let stream_marks: melin_transport_core::pipeline::StreamMarkQueue =
487        Arc::new(std::sync::Mutex::new(std::collections::VecDeque::new()));
488    journal_stage.set_stream_marks(Arc::clone(&stream_marks));
489    cores.place_journal_children(&mut journal_stage);
490    journal_stage.set_staging_mode(staging_mode);
491    let journal_failed = Arc::new(AtomicBool::new(false));
492    let journal_failed_latch = Arc::clone(&journal_failed);
493    // Start the journal's disk and preparer threads here rather than on
494    // journal-seq — see `JournalStage::start`. Before the health latch
495    // below, so a failed start never reports a healthy pipeline.
496    let sequencer = journal_stage.start()?;
497    // A fresh pipeline is healthy — this also clears the latch after a
498    // successful in-process resync rebuild.
499    pipeline_healthy.store(true, Ordering::Release);
500    let journal_handle = std::thread::Builder::new()
501        .name("journal-seq".into())
502        .spawn(move || {
503            melin_app::affinity::pin_thread("journal-seq", journal_core);
504            let result = sequencer.run(&ps);
505            if let Err(ref e) = result {
506                // Latch before logging so the receiver reacts even if
507                // logging stalls. A dead journal stage freezes the
508                // journal cursor; every downstream wait on it (ring
509                // backpressure, ack durability) would spin forever —
510                // the streaming loop polls this latch and tears the
511                // session down instead.
512                journal_failed_latch.store(true, Ordering::Release);
513                // Mirror into the process-lifetime gauge the replica
514                // health endpoint serves.
515                pipeline_healthy.store(false, Ordering::Release);
516                tracing::error!(error = %e, "replica journal stage failed — session teardown");
517            }
518            result
519        })
520        .expect("spawn journal thread");
521
522    let ps = Arc::clone(&pipeline_shutdown);
523    let matching_core = cores.matching.core;
524    let matching_stage = pipeline.matching_stage;
525    let matching_handle = std::thread::Builder::new()
526        .name("matching".into())
527        .spawn(move || {
528            melin_app::affinity::pin_thread("matching", matching_core);
529            matching_stage.run(&ps)
530        })
531        .expect("spawn matching thread");
532
533    // Drain thread uses the response core — replicas have no response stage,
534    // but the consumer needs to be drained so the output ring doesn't fill.
535    let ps = Arc::clone(&pipeline_shutdown);
536    let drain = cores.response;
537    let drain_consumer = pipeline.drain_consumer;
538    let drain_handle = std::thread::Builder::new()
539        .name("drain".into())
540        .spawn(move || {
541            melin_app::affinity::pin_thread("drain", drain.core);
542            let mut consumer = drain_consumer;
543            let mut batch = vec![OutputSlot::<A::Report, A::QueryResponse>::default(); 256];
544            let mut waiter = drain.wait.waiter();
545            loop {
546                if ps.load(Ordering::Relaxed) {
547                    return;
548                }
549                let count = consumer.consume_batch(&mut batch, 256);
550                if count == 0 {
551                    waiter.idle();
552                } else {
553                    waiter.reset();
554                }
555            }
556        })
557        .expect("spawn drain thread");
558
559    let shadow_handle = if let Some(shadow_cons) = pipeline.shadow_consumer {
560        let snap_path = snapshot_path;
561        let chain_lock = pipeline
562            .chain_hash_lock
563            .as_ref()
564            .expect("chain hash lock with shadow")
565            .clone();
566        let ps = Arc::clone(&pipeline_shutdown);
567        let shadow = cores.shadow;
568        Some(
569            std::thread::Builder::new()
570                .name("replica-shadow".into())
571                .spawn(move || {
572                    melin_app::affinity::pin_thread("replica-shadow", shadow.core);
573                    melin_transport_core::shadow::run(
574                        shadow_cons,
575                        shadow_exchange,
576                        snap_path,
577                        std::time::Duration::from_millis(snapshot_interval_ms),
578                        chain_lock,
579                        &ps,
580                        shadow.wait,
581                        shadow_initial_epoch,
582                    );
583                })
584                .expect("spawn shadow thread"),
585        )
586    } else {
587        None
588    };
589
590    Ok(ReplicaPipelineHandles {
591        input_producer: pipeline.input_producer,
592        journal_cursor: pipeline.cursors.journal_ring_arc(),
593        last_seq: pipeline.cursors.durable_wire_seq(),
594        chain_hash_lock: pipeline.chain_hash_lock,
595        stream_marks,
596        journal_failed,
597        pipeline_shutdown,
598        journal_handle,
599        matching_handle,
600        drain_handle,
601        shadow_handle,
602    })
603}
604
605/// Tear down the pipeline: publish the shutdown sentinel, join all
606/// threads, return the recovered (App, journal writer) so the orchestrator
607/// can use them for the next pipeline build (e.g., post-snapshot) or
608/// pass them up on promotion.
609pub(super) fn teardown_replica_pipeline<A: Application + Send + 'static, W: Send + 'static>(
610    mut handles: ReplicaPipelineHandles<A, W>,
611) -> TeardownOutcome<A, W> {
612    // The sentinel is published here, not by callers, so no teardown
613    // path can forget it — the journal and matching stages exit by
614    // consuming it through the normal event path, draining anything
615    // received-but-not-yet-journaled (see `shutdown_pipeline` for why
616    // the flag alone is only the emergency fallback). Bounded retry
617    // rather than the blocking `publish`: if the journal stage dies
618    // with the ring full, its gate cursor freezes and a blocking
619    // publish would spin forever — re-check the failure latch between
620    // attempts and fall through to the flag-only teardown once it
621    // trips (the sentinel has no reader then anyway: the matching
622    // stage is gated behind the frozen journal cursor).
623    //
624    // Deliberately a plain yield rather than the node's wait strategy:
625    // this is the orchestrator thread on a teardown path, where
626    // latency is irrelevant and handing the CPU to the stage threads
627    // is unconditionally the right move, whatever the node's policy.
628    while !handles.journal_failed.load(Ordering::Acquire) {
629        match handles
630            .input_producer
631            .try_publish(InputSlot::<A::Event>::shutdown_sentinel())
632        {
633            Ok(_) => break,
634            // Ring full — consumers need to make progress first.
635            Err(_) => std::thread::yield_now(),
636        }
637    }
638    shutdown_pipeline::<A, W>(
639        &handles.pipeline_shutdown,
640        handles.journal_handle,
641        handles.matching_handle,
642        handles.drain_handle,
643        handles.shadow_handle,
644    )
645}
646
647/// How many mid-stream divergence resyncs the receiver attempts
648/// in-process (per process lifetime) before giving up. Mid-stream
649/// divergence is never expected in a healthy cluster — it means
650/// corruption or a serious bug somewhere — so the budget is exactly
651/// one: the first occurrence repairs automatically (archive the local
652/// lineage as `.divergent.<n>`, re-seed from the primary) and pages
653/// the operator via `melin_replica_divergence_total`; a second in the
654/// same process lifetime is systematic, and continuing to repair
655/// would fill the disk with archives while masking the underlying
656/// fault. Exit hard instead.
657pub(super) const MAX_INPROCESS_DIVERGENCE_RESYNCS: u32 = 1;
658
659/// Recover replica boot state from disk.
660///
661/// Recovers whenever any journal segment survives — live OR archived
662/// (a crash between rotation's rename and the new live file's creation
663/// leaves archives with no live segment, and recovery handles that
664/// layout; treating it as a fresh replica would discard local durable
665/// history and then fail `create_new` against the surviving lineage).
666/// Returns `(None, None, 0, zeros)` for a genuinely fresh replica.
667/// Also seeds the fencing epoch from the recovered journal.
668///
669/// Called at receiver startup, and again after a mid-stream chain
670/// divergence tears the pipeline down: the on-disk journal is
671/// self-consistent (merely forked from the primary's history), so
672/// recovery re-derives a truthful handshake pair `(last_sequence,
673/// chain_hash)` and the next connection takes the primary's
674/// HashMismatch → archive → reseed path in-process.
675#[allow(clippy::type_complexity)]
676pub(super) fn recover_replica_state<A, W>(
677    journal_path: &std::path::Path,
678    snapshot_path: &std::path::Path,
679    factory: &dyn melin_app::app_factory::AppFactory<App = A>,
680    fence_state: &melin_transport_core::fence::FenceState,
681) -> Result<(Option<A>, Option<W>, u64, [u8; 32]), Box<dyn std::error::Error>>
682where
683    A: Application,
684    W: melin_journal::JournalWrite<A::Event>,
685{
686    let lineage_exists =
687        journal_path.exists() || !melin_journal::segment::list_archives(journal_path)?.is_empty();
688    if !lineage_exists {
689        return Ok((None, None, 0u64, [0u8; 32]));
690    }
691    let engine = if snapshot_path.exists() {
692        tracing::info!("recovering replica from snapshot + journal");
693        melin_transport_core::JournaledApp::<A, W>::recover_from_snapshot(
694            snapshot_path,
695            journal_path,
696        )?
697    } else {
698        melin_transport_core::JournaledApp::<A, W>::recover(factory.empty(), journal_path)?
699    };
700    let next = engine.next_sequence();
701    let last = next.saturating_sub(1);
702    let hash = engine.chain_hash().unwrap_or([0u8; 32]);
703    // Seed the observed epoch from the replica's own recovered journal.
704    // Streaming `EpochBump`s and the snapshot-resync path raise it later.
705    fence_state.observe_epoch(engine.recovered_epoch());
706    let (mut exchange, writer) = engine.into_parts();
707    factory.apply_operator_policy(&mut exchange);
708    Ok((Some(exchange), Some(writer), last, hash))
709}
710
711/// Reconnect backoff cap shared by both receivers — exponential from
712/// 1 s, clamped here so a long outage settles to one attempt every 30 s
713/// without hammering a flapping primary. Reset to 1 s only when a
714/// session heard the primary speak (data or heartbeat) — see
715/// [`handle_session_exit`].
716pub(super) const MAX_BACKOFF: std::time::Duration = std::time::Duration::from_secs(30);
717
718/// What the receiver's reconnect loop does after a streaming session
719/// ends — the transport-independent half of the exit dispatch, returned
720/// by [`handle_session_exit`].
721pub(in crate::replication) enum AfterSession<A, W> {
722    /// Terminal: `run_receiver*` returns this verbatim — clean shutdown
723    /// (`Ok(None)`), a promotion hand-off (`Ok(Some(state))`), or a
724    /// fatal error.
725    Return(ReceiverResult<A, W>),
726    /// A mid-stream divergence was repaired in-process: the recovered
727    /// on-disk state is the new handshake position. The caller adopts it
728    /// and reconnects; the primary's `HashMismatch` verdict then routes
729    /// the replica through archive + re-seed.
730    Resync {
731        exchange: Option<A>,
732        journal_writer: Option<W>,
733        last_sequence: u64,
734        chain_hash: [u8; 32],
735    },
736    /// A plain disconnect or a stream contiguity break — backoff has
737    /// already been applied (and the flags checked); the caller
738    /// reconnects, reusing the still-live pipeline.
739    Reconnect,
740}
741
742/// Dispatch a finished streaming session — shared by the kernel-TCP and
743/// DPDK receivers.
744///
745/// Folds the behaviours that were previously copied between the two
746/// receiver loops (and had drifted — the copies reset the post-resync
747/// writer differently, a latent corruption bug): the
748/// `Shutdown`/`Promote`/`Fatal` teardown (including the once-per-process
749/// in-process divergence-resync policy) and the `Disconnected` reconnect
750/// backoff. The shutdown-sentinel publish lives in
751/// [`teardown_replica_pipeline`].
752///
753/// `close` runs any transport-specific teardown that must precede a
754/// reconnect — the DPDK receiver closes its smoltcp socket so the
755/// primary's slot and the local socket-set entry are freed; the
756/// kernel-TCP receiver passes a no-op (its `TcpStream` is dropped by the
757/// caller on the next loop turn). It is invoked only on the
758/// reconnecting paths (in-process resync, plain disconnect, stream
759/// gap), never on a terminal return.
760// Twelve arguments is a lot, but each is a distinct piece of the
761// receiver loop's state; bundling them would only move the noise.
762#[allow(clippy::too_many_arguments)]
763pub(in crate::replication) fn handle_session_exit<A, W>(
764    result: StreamingResult,
765    pipeline: &mut Option<ReplicaPipelineHandles<A, W>>,
766    divergence_resyncs: &mut u32,
767    backoff: &mut std::time::Duration,
768    last_sequence: u64,
769    journal_path: &std::path::Path,
770    snapshot_path: &std::path::Path,
771    factory: &dyn melin_app::app_factory::AppFactory<App = A>,
772    fence_state: &melin_transport_core::fence::FenceState,
773    shutdown: &AtomicBool,
774    promote: &crate::promotion::PromotionRequest,
775    mut close: impl FnMut(),
776) -> AfterSession<A, W>
777where
778    A: Application + Send + 'static,
779    W: JournalWrite<A::Event> + Send + 'static,
780{
781    let StreamingResult {
782        exit,
783        heard_from_primary,
784    } = result;
785
786    match exit {
787        SessionExit::Shutdown => {
788            if let Some(p) = pipeline.take() {
789                let _ = teardown_replica_pipeline::<A, W>(p);
790            }
791            AfterSession::Return(Ok(None))
792        }
793
794        SessionExit::Promote => AfterSession::Return(match pipeline.take() {
795            Some(p) => match teardown_replica_pipeline::<A, W>(p) {
796                TeardownOutcome::Clean(ex, wr) => Ok(Some((ex, wr))),
797                _ => Err("pipeline failed during promotion".into()),
798            },
799            None => Err("pipeline missing on promote".into()),
800        }),
801
802        SessionExit::Fatal(e) => {
803            let outcome = match pipeline.take() {
804                Some(p) => teardown_replica_pipeline::<A, W>(p),
805                // Fatal implies a streaming session, which implies a
806                // pipeline — but don't turn a missing one into a resync.
807                None => return AfterSession::Return(Err(e)),
808            };
809            // Mid-stream chain divergence is repairable in-process: the
810            // on-disk journal is self-consistent (merely forked from the
811            // primary's history), so re-derive the handshake state from
812            // disk and reconnect — the primary judges the recovered
813            // position divergent and the next session takes the
814            // HashMismatch → archive → reseed path, no restart needed.
815            // Every other fatal exits as before: protocol violations and
816            // journal I/O death (ENOSPC, RO-FS) would fail the same way
817            // after a resync.
818            let TeardownOutcome::JournalFailed(
819                je @ melin_journal::JournalError::ReplicaChainDivergence { .. },
820            ) = outcome
821            else {
822                return AfterSession::Return(Err(e));
823            };
824
825            *divergence_resyncs += 1;
826            let attempt = *divergence_resyncs;
827            if attempt > MAX_INPROCESS_DIVERGENCE_RESYNCS {
828                return AfterSession::Return(Err(format!(
829                    "mid-stream chain divergence recurred {attempt} times — giving up on \
830                     in-process resync (each cycle archives the local journal and re-seeds \
831                     from the primary; recurrence at this rate means the primary keeps \
832                     streaming history that forks from what it announces): {je}"
833                )
834                .into()));
835            }
836            tracing::warn!(
837                error = %je,
838                attempt,
839                max_attempts = MAX_INPROCESS_DIVERGENCE_RESYNCS,
840                "mid-stream chain divergence — re-deriving local state for in-process resync"
841            );
842            // Transport-specific teardown before reconnecting.
843            close();
844            match recover_replica_state::<A, W>(journal_path, snapshot_path, factory, fence_state) {
845                Ok((exchange, journal_writer, seq, hash)) => AfterSession::Resync {
846                    exchange,
847                    journal_writer,
848                    last_sequence: seq,
849                    chain_hash: hash,
850                },
851                Err(e) => AfterSession::Return(Err(e)),
852            }
853        }
854
855        SessionExit::StreamGap(e) => {
856            // The primary's stream skipped past what we hold; the
857            // contiguous prefix is committed and the journal stage keeps
858            // flushing it, so re-handshaking at the durable position
859            // (re-read from `FsyncState` at the top of the reconnect
860            // loop, after the backoff) lets the primary re-stream the
861            // hole. The primary evidently spoke, so the backoff resets
862            // exactly as a heard-from disconnect does. Not a resync:
863            // nothing on disk is wrong.
864            close();
865            *backoff = std::time::Duration::from_secs(1);
866            tracing::warn!(
867                error = %e,
868                last_sequence,
869                "replication stream broke contiguity — reconnecting from the durable position"
870            );
871            sleep_then_double_backoff(backoff, shutdown, promote);
872            AfterSession::Reconnect
873        }
874
875        SessionExit::Disconnected => {
876            // Transport-specific teardown before reconnecting (smoltcp
877            // socket reclaim on DPDK; no-op on kernel TCP).
878            close();
879            // A session in which the primary spoke — data or heartbeat
880            // (heartbeats flow even on a quiet system) — proves it
881            // alive and serving: treat the drop as transient and reset
882            // the backoff. A session with no word from the primary
883            // keeps escalating: that covers instant-drop flaps and
884            // synthetic results from sessions that never started
885            // streaming (a persistent local transport failure must not
886            // redial at 1 Hz forever).
887            if heard_from_primary {
888                *backoff = std::time::Duration::from_secs(1);
889            }
890            tracing::warn!(
891                last_sequence,
892                heard_from_primary,
893                backoff_secs = backoff.as_secs(),
894                "reconnecting to primary"
895            );
896            sleep_then_double_backoff(backoff, shutdown, promote);
897            AfterSession::Reconnect
898        }
899    }
900}
901
902/// Tear the live pipeline down for a promotion that fired while the
903/// receiver was disconnected — at the top of the reconnect loop or
904/// during reconnect backoff — and return the warm Exchange + writer for
905/// the promoted primary. Shared by both receivers.
906///
907/// A clean teardown hands back the warm state; if there is no pipeline
908/// (promotion before the first connect, or after a resync that left the
909/// state in the receiver's locals) those locals carry it. A missing
910/// pair is a hard error — a promote with nothing to promote.
911pub(in crate::replication) fn take_pipeline_for_promotion<A, W>(
912    pipeline: &mut Option<ReplicaPipelineHandles<A, W>>,
913    exchange: &mut Option<A>,
914    journal_writer: &mut Option<W>,
915) -> ReceiverResult<A, W>
916where
917    A: Application + Send + 'static,
918    W: JournalWrite<A::Event> + Send + 'static,
919{
920    if let Some(p) = pipeline.take()
921        && let TeardownOutcome::Clean(e, w) = teardown_replica_pipeline::<A, W>(p)
922    {
923        *exchange = Some(e);
924        *journal_writer = Some(w);
925    }
926    match (exchange.take(), journal_writer.take()) {
927        (Some(e), Some(w)) => Ok(Some((e, w))),
928        _ => Err("promotion requested but no local state available".into()),
929    }
930}
931
932/// The four facts a successful snapshot + segment-seed transfer yields:
933/// `(snapshot App, snapshot sequence, snapshot chain hash, seed length)`.
934type ResyncTransfer<A> = (A, u64, [u8; 32], u64);
935
936/// What [`handle_resync_verdict`] resolved a `NeedSnapshot` /
937/// `HashMismatch` verdict to.
938pub(in crate::replication) enum ResyncDecision {
939    /// Resync complete — resume streaming from `resume_sequence` on the
940    /// re-seeded lineage `(segment_start_sequence, anchor_hash)`. The
941    /// recovered App + writer are left in the receiver's `exchange` /
942    /// `journal_writer` locals.
943    Ready {
944        segment_start_sequence: u64,
945        anchor_hash: [u8; 32],
946        resume_sequence: u64,
947    },
948    /// The transfer failed network-shaped (drop / restart mid-body); the
949    /// caller backs off and reconnects. The pre-resync lineage is already
950    /// archived and half-applied transfer state has been cleaned up.
951    Retry,
952}
953
954/// Receive the snapshot + segment-seed transfer that follows a resync
955/// verdict, installing both (snapshot → `snapshot_path`, seed →
956/// `journal_path`). Returns the snapshot App, its sequence + chain hash,
957/// and the seed length (the journal's `valid_end`). Shared by both
958/// receivers via [`ControlFrameSource`].
959///
960/// Errors here are network-shaped — the caller retries (see
961/// [`ResyncDecision::Retry`]); the post-transfer install in
962/// [`handle_resync_verdict`] is what's fatal.
963fn receive_resync_transfer<A, S>(
964    source: &mut S,
965    snapshot_path: &std::path::Path,
966    journal_path: &std::path::Path,
967    fence_state: &melin_transport_core::fence::FenceState,
968) -> Result<ResyncTransfer<A>, Box<dyn std::error::Error + Send + Sync>>
969where
970    A: Application,
971    S: ControlFrameSource,
972{
973    let (snap_len, snap_sequence, snap_chain_hash) =
974        match decode_primary_message(&source.next_frame(MAX_CONTROL_FRAME)?)? {
975            PrimaryMessage::SnapshotBegin {
976                snapshot_len,
977                snap_sequence,
978                snap_chain_hash,
979            } => (snapshot_len, snap_sequence, snap_chain_hash),
980            other => return Err(format!("expected SnapshotBegin, got {other:?}").into()),
981        };
982
983    tracing::info!(snap_sequence, snap_len, "receiving snapshot");
984    let tmp_path = snapshot_path.with_extension("snapshot.tmp");
985    receive_chunked_body(source, &tmp_path, snap_len, "snapshot")?;
986    std::fs::rename(&tmp_path, snapshot_path)?;
987    tracing::info!(snap_sequence, snap_len, "snapshot received and verified");
988
989    let (snap_exchange, _snap_seq, snap_hash, snap_epoch) =
990        melin_transport_core::snapshot::load::<A>(snapshot_path)?;
991    if snap_hash != snap_chain_hash {
992        return Err(format!(
993            "snapshot chain hash mismatch: primary sent {snap_chain_hash:02x?}, \
994             loaded snapshot has {snap_hash:02x?}"
995        )
996        .into());
997    }
998    // Adopt the primary's snapshot epoch — the resync rebases this replica
999    // onto the primary's lineage, including its epoch.
1000    fence_state.observe_epoch(snap_epoch);
1001
1002    // Segment seed: the raw byte prefix of the primary's segment
1003    // containing `snap_sequence`. Written verbatim as our live segment, it
1004    // makes our segmentation a byte-copy of the primary's from birth —
1005    // chain values comparable and `Rotate` verification valid immediately.
1006    let seed_len = match decode_primary_message(&source.next_frame(MAX_CONTROL_FRAME)?)? {
1007        PrimaryMessage::SegmentSeedBegin { seed_len } => seed_len,
1008        other => {
1009            return Err(format!("expected SegmentSeedBegin after snapshot, got {other:?}").into());
1010        }
1011    };
1012    let seed_tmp = journal_path.with_extension("seed.tmp");
1013    receive_chunked_body(source, &seed_tmp, seed_len, "segment seed")?;
1014    // Structural check before installing: the CRC only proves transport
1015    // integrity, not that the primary sent a well-formed prefix ending at
1016    // the snapshot sequence. (With hash-chain on, the chain cross-check in
1017    // `handle_resync_verdict` subsumes this; without it, this is the only
1018    // guard.)
1019    if let Err(e) =
1020        melin_journal::segment::verify_segment_prefix(&seed_tmp, snap_sequence, seed_len)
1021    {
1022        let _ = std::fs::remove_file(&seed_tmp);
1023        return Err(format!("segment seed failed structural verification: {e}").into());
1024    }
1025    std::fs::rename(&seed_tmp, journal_path)?;
1026    melin_journal::segment::fsync_parent_dir(journal_path)?;
1027    Ok((snap_exchange, snap_sequence, snap_chain_hash, seed_len))
1028}
1029
1030/// Handle a `NeedSnapshot` / `HashMismatch` resync verdict — shared by
1031/// both receivers. Tears the pipeline down, archives the local lineage
1032/// (never deleted), resets the handshake position, receives + installs
1033/// the snapshot and segment seed via `source`, opens the re-seeded
1034/// segment and ties its chain to the snapshot, then validates the
1035/// post-snapshot `StreamStart` inline before resuming.
1036///
1037/// On success the recovered App + writer are left in `exchange` /
1038/// `journal_writer` and [`ResyncDecision::Ready`] carries the resume
1039/// lineage. A network-shaped transfer failure yields
1040/// [`ResyncDecision::Retry`] (the caller backs off and reconnects). An
1041/// inconsistent primary or a local install failure is fatal (`Err`).
1042#[allow(clippy::too_many_arguments)]
1043pub(in crate::replication) fn handle_resync_verdict<A, W, S>(
1044    divergent: bool,
1045    source: &mut S,
1046    pipeline: &mut Option<ReplicaPipelineHandles<A, W>>,
1047    exchange: &mut Option<A>,
1048    journal_writer: &mut Option<W>,
1049    journal_path: &std::path::Path,
1050    snapshot_path: &std::path::Path,
1051    fence_state: &melin_transport_core::fence::FenceState,
1052    control: &ReplicaControlPlane,
1053    last_sequence: &mut u64,
1054    chain_hash: &mut [u8; 32],
1055) -> Result<ResyncDecision, Box<dyn std::error::Error + Send + Sync>>
1056where
1057    A: Application + Send + 'static,
1058    W: JournalWrite<A::Event> + Send + 'static,
1059    S: ControlFrameSource,
1060{
1061    // HashMismatch is NeedSnapshot plus the verdict that our local journal
1062    // holds divergent history (forked from the primary's — e.g. an
1063    // ex-primary rejoining after failover with an acked-but-unreplicated
1064    // suffix).
1065    if divergent {
1066        tracing::warn!(
1067            last_sequence = *last_sequence,
1068            "primary reports chain divergence — archiving local journal, resyncing from snapshot"
1069        );
1070    } else {
1071        tracing::info!("primary requires snapshot transfer — receiving snapshot");
1072    }
1073
1074    if let Some(p) = pipeline.take() {
1075        let _ = teardown_replica_pipeline::<A, W>(p);
1076    }
1077
1078    // Invalidate the in-memory App + writer before moving their backing
1079    // files aside. On the in-process divergence repair path these still
1080    // hold the recovered handles; a transfer failure returns `Retry`, and
1081    // without this reset the stale writer — now pointing at an
1082    // archived-away journal — would survive the fresh-replica create gate
1083    // and get rebuilt into the next pipeline.
1084    *exchange = None;
1085    *journal_writer = None;
1086
1087    // Move the local lineage aside — never delete. Divergent journals are
1088    // audit-trail material; stale ones may be the last copy of pruned
1089    // history.
1090    let reason = if divergent {
1091        ArchiveReason::Divergent
1092    } else {
1093        ArchiveReason::Resync
1094    };
1095    archive_local_lineage(journal_path, snapshot_path, reason)?;
1096    // Archived — a retried handshake must present as a fresh replica.
1097    *last_sequence = 0;
1098    *chain_hash = [0u8; 32];
1099    // The advertised control-plane tip must shrink with the holdings:
1100    // the archived suffix is either divergent (never acked) or being
1101    // replaced wholesale, so this is the one legitimate tip regression
1102    // (see `AdvertisedJournalTip::reset`).
1103    control
1104        .journal_tip
1105        .reset(melin_transport_core::WireSeq::new(0));
1106
1107    let (snap_exchange, snap_sequence, snap_chain_hash, seed_len) =
1108        match receive_resync_transfer::<A, S>(source, snapshot_path, journal_path, fence_state) {
1109            Ok(v) => v,
1110            Err(e) => {
1111                tracing::warn!(error = %e, "snapshot transfer failed — retrying");
1112                // Half-applied resync state, not audit material — drop it
1113                // so the retry starts clean (the pre-resync lineage is
1114                // already archived; this is not it).
1115                let _ = std::fs::remove_file(snapshot_path);
1116                return Ok(ResyncDecision::Retry);
1117            }
1118        };
1119    *exchange = Some(snap_exchange);
1120
1121    // Open the seeded segment for appending at the snapshot position —
1122    // recovery's resume path: the chain rebuilds from the seeded bytes and
1123    // its value at `snap_sequence` must equal the (verified) snapshot's
1124    // chain hash. `chain_hash()` is `None` only with `hash-chain` disabled
1125    // (nothing to tie); an all-zeros snapshot hash means the primary runs
1126    // without `hash-chain` (also nothing to tie).
1127    let writer = W::open_append(journal_path, snap_sequence, seed_len)?;
1128    let seeded_chain = writer.chain_hash().unwrap_or(snap_chain_hash);
1129    if snap_chain_hash != [0u8; 32] && seeded_chain != snap_chain_hash {
1130        return Err(format!(
1131            "segment seed chain at {snap_sequence} disagrees with the transferred snapshot's \
1132             hash — inconsistent primary"
1133        )
1134        .into());
1135    }
1136    let seeded_info = melin_journal::segment::read_header_info(journal_path)?;
1137    *journal_writer = Some(writer);
1138
1139    // Validate the post-snapshot StreamStart inline before resuming — its
1140    // lineage must agree with the seed the primary just transferred.
1141    match decode_primary_message(&source.next_frame(MAX_CONTROL_FRAME)?)? {
1142        PrimaryMessage::StreamStart {
1143            start_sequence,
1144            segment_start_sequence,
1145            anchor_hash,
1146            epoch,
1147            ack_policy,
1148        } => {
1149            if segment_start_sequence != seeded_info.starting_sequence
1150                || anchor_hash != seeded_info.anchor_hash
1151            {
1152                return Err(format!(
1153                    "post-snapshot StreamStart lineage (start {segment_start_sequence}) \
1154                     disagrees with the transferred segment seed (start {}) — inconsistent \
1155                     primary",
1156                    seeded_info.starting_sequence
1157                )
1158                .into());
1159            }
1160            // We just rebased onto this primary's snapshot, so adopt its
1161            // epoch wholesale (no stale-primary refusal — our prior state
1162            // was discarded).
1163            fence_state.observe_epoch(epoch);
1164            control
1165                .primary_ack_policy
1166                .store(ack_policy, Ordering::Release);
1167            tracing::info!(
1168                start_sequence,
1169                epoch,
1170                ack_policy,
1171                "streaming resumed after snapshot transfer"
1172            );
1173            // The transfer is installed and validated: the node's holdings
1174            // are exactly the snapshot position again.
1175            control
1176                .journal_tip
1177                .reset(melin_transport_core::WireSeq::new(snap_sequence));
1178            Ok(ResyncDecision::Ready {
1179                segment_start_sequence,
1180                anchor_hash,
1181                resume_sequence: snap_sequence,
1182            })
1183        }
1184        other => Err(format!("expected StreamStart after snapshot, got {other:?}").into()),
1185    }
1186}
1187
1188#[cfg(test)]
1189mod tests {
1190    use std::io::Write;
1191    use std::sync::Arc;
1192
1193    use super::auth::{authenticate_replica, authenticate_with_primary};
1194    use super::*;
1195    // Any `AppEvent` works as the pipeline's event type here — these
1196    // protocol-level tests never construct a real app event (the slot's
1197    // event is always `JournalEvent::Tick`), so the counter example's
1198    // event type stands in for an exchange event and keeps the runtime's
1199    // test deps free of exchange crates.
1200    use counter_server::CounterEvent;
1201    type InputSlot = melin_transport_core::pipeline::InputSlot<CounterEvent>;
1202    use melin_transport_core::replication::protocol::{
1203        MAX_CONTROL_FRAME, MAX_DATA_FRAME, MSG_AUTH_OK, MSG_CHALLENGE_RESPONSE, MSG_SNAPSHOT_BEGIN,
1204        MSG_SNAPSHOT_CHUNK, MSG_SNAPSHOT_END, decode_auth_result, decode_challenge,
1205        decode_challenge_response, decode_primary_message, decode_replica_message, encode_ack,
1206        encode_auth_failed, encode_auth_ok, encode_chain_check, encode_challenge,
1207        encode_challenge_response, encode_handshake, encode_hash_mismatch, encode_heartbeat,
1208        encode_input_batch, encode_need_snapshot, encode_rotate, encode_segment_seed_begin,
1209        encode_snapshot_begin, encode_snapshot_chunk, encode_snapshot_end, encode_stream_start,
1210        read_frame, try_decode_input_batch,
1211    };
1212
1213    /// Build a wire-ready `InputBatch` frame containing a single `Tick`
1214    /// slot at the given sequence — the protocol-level tests don't need
1215    /// real journal payloads, just something with a known max sequence.
1216    fn encode_input_batch_with_seq(end_sequence: u64, buf: &mut Vec<u8>) {
1217        let slot = InputSlot {
1218            connection_id: 0,
1219            key_hash: 0,
1220            request_seq: 0,
1221            sequence: end_sequence,
1222            timestamp_ns: 0,
1223            event: melin_journal::JournalEvent::Tick { now_ns: 0 },
1224            publish_ts: Default::default(),
1225            recv_ts: Default::default(),
1226        };
1227        encode_input_batch(&[slot], buf);
1228    }
1229
1230    #[test]
1231    fn handshake_encode_decode_round_trip() {
1232        let handshake = Handshake {
1233            last_sequence: 42,
1234            chain_hash: [0xAB; 32],
1235            // Non-zero so a dropped/zeroed epoch field is caught.
1236            epoch: 9,
1237        };
1238        let mut buf = Vec::new();
1239        encode_handshake(&handshake, &mut buf);
1240
1241        // Read frame: skip 4-byte length prefix.
1242        let payload = &buf[4..];
1243        let msg = decode_replica_message(payload).unwrap();
1244        match msg {
1245            ReplicaMessage::Handshake(h) => {
1246                assert_eq!(h.last_sequence, 42);
1247                assert_eq!(h.chain_hash, [0xAB; 32]);
1248                assert_eq!(h.epoch, 9);
1249            }
1250            _ => panic!("expected Handshake"),
1251        }
1252    }
1253
1254    #[test]
1255    fn ack_encode_decode_round_trip() {
1256        let ack = Ack {
1257            acked_sequence: 1000,
1258            in_memory_sequence: 1024,
1259        };
1260        let mut buf = Vec::new();
1261        encode_ack(&ack, &mut buf);
1262
1263        let payload = &buf[4..];
1264        let msg = decode_replica_message(payload).unwrap();
1265        match msg {
1266            ReplicaMessage::Ack(a) => {
1267                assert_eq!(a.acked_sequence, 1000);
1268                assert_eq!(a.in_memory_sequence, 1024);
1269            }
1270            _ => panic!("expected Ack"),
1271        }
1272    }
1273
1274    /// Pin the exact on-the-wire byte layout of an Ack frame. A future
1275    /// `repr(C)` field reorder, alignment change, or accidental
1276    /// big-endian wrapper substitution would silently break replica/
1277    /// primary compatibility — the const `size_of` assert in
1278    /// `protocol.rs` only catches size changes, not layout changes.
1279    /// This test pins the expected bytes so any such break shows up
1280    /// loudly on the next CI run.
1281    #[test]
1282    fn ack_wire_byte_pattern() {
1283        let ack = Ack {
1284            acked_sequence: 0xDEAD_BEEF_CAFE_F00D,
1285            in_memory_sequence: 0x1122_3344_5566_7788,
1286        };
1287        let mut buf = Vec::new();
1288        encode_ack(&ack, &mut buf);
1289        // [length:u32 LE = 17][tag:u8 = MSG_ACK = 0x02]
1290        // [acked_sequence:u64 LE][in_memory_sequence:u64 LE]
1291        let expected: &[u8] = &[
1292            0x11, 0x00, 0x00, 0x00, // length = 17 LE
1293            0x02, // MSG_ACK
1294            0x0D, 0xF0, 0xFE, 0xCA, 0xEF, 0xBE, 0xAD, 0xDE, // acked_sequence LE
1295            0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, // in_memory_sequence LE
1296        ];
1297        assert_eq!(buf.as_slice(), expected, "Ack wire layout drifted");
1298    }
1299
1300    #[test]
1301    fn stream_start_encode_decode_round_trip() {
1302        let mut buf = Vec::new();
1303        // Non-zero epoch/policy so a dropped/zeroed field is caught.
1304        encode_stream_start(99, 42, [0xAA; 32], 5, 2, &mut buf);
1305
1306        let payload = &buf[4..];
1307        let msg = decode_primary_message(payload).unwrap();
1308        match msg {
1309            PrimaryMessage::StreamStart {
1310                start_sequence,
1311                segment_start_sequence,
1312                anchor_hash,
1313                epoch,
1314                ack_policy,
1315            } => {
1316                assert_eq!(start_sequence, 99);
1317                assert_eq!(segment_start_sequence, 42);
1318                assert_eq!(anchor_hash, [0xAA; 32]);
1319                assert_eq!(ack_policy, 2);
1320                assert_eq!(epoch, 5);
1321            }
1322            _ => panic!("expected StreamStart"),
1323        }
1324    }
1325
1326    #[test]
1327    fn heartbeat_encode_decode_round_trip() {
1328        let mut buf = Vec::new();
1329        encode_heartbeat(123, 2, &mut buf);
1330
1331        let payload = &buf[4..];
1332        let msg = decode_primary_message(payload).unwrap();
1333        match msg {
1334            PrimaryMessage::Heartbeat {
1335                sequence,
1336                ack_policy,
1337            } => {
1338                assert_eq!(sequence, 123);
1339                assert_eq!(ack_policy, 2);
1340            }
1341            _ => panic!("expected Heartbeat"),
1342        }
1343    }
1344
1345    #[test]
1346    fn need_snapshot_encode_decode_round_trip() {
1347        let mut buf = Vec::new();
1348        encode_need_snapshot(&mut buf);
1349
1350        let payload = &buf[4..];
1351        let msg = decode_primary_message(payload).unwrap();
1352        assert!(matches!(msg, PrimaryMessage::NeedSnapshot));
1353    }
1354
1355    #[test]
1356    fn hash_mismatch_encode_decode_round_trip() {
1357        let mut buf = Vec::new();
1358        encode_hash_mismatch(&mut buf);
1359
1360        let payload = &buf[4..];
1361        let msg = decode_primary_message(payload).unwrap();
1362        assert!(matches!(msg, PrimaryMessage::HashMismatch));
1363    }
1364
1365    #[test]
1366    fn rotate_encode_decode_round_trip() {
1367        let mut buf = Vec::new();
1368        encode_rotate(7_000_000, &[0xBB; 32], &mut buf);
1369
1370        let payload = &buf[4..];
1371        let msg = decode_primary_message(payload).unwrap();
1372        match msg {
1373            PrimaryMessage::Rotate {
1374                boundary_seq,
1375                tail_hash,
1376            } => {
1377                assert_eq!(boundary_seq, 7_000_000);
1378                assert_eq!(tail_hash, [0xBB; 32]);
1379            }
1380            _ => panic!("expected Rotate"),
1381        }
1382    }
1383
1384    #[test]
1385    fn chain_check_encode_decode_round_trip() {
1386        let mut buf = Vec::new();
1387        encode_chain_check(555, &[0xCC; 32], &mut buf);
1388
1389        let payload = &buf[4..];
1390        let msg = decode_primary_message(payload).unwrap();
1391        match msg {
1392            PrimaryMessage::ChainCheck {
1393                sequence,
1394                chain_hash,
1395            } => {
1396                assert_eq!(sequence, 555);
1397                assert_eq!(chain_hash, [0xCC; 32]);
1398            }
1399            _ => panic!("expected ChainCheck"),
1400        }
1401    }
1402
1403    /// Pin the wire layout of the shared `(sequence, hash)` frame body —
1404    /// same rationale as `ack_wire_byte_pattern`. Rotate and ChainCheck
1405    /// share the layout; only the tag differs.
1406    #[test]
1407    fn rotate_wire_byte_pattern() {
1408        let mut hash = [0u8; 32];
1409        for (i, b) in hash.iter_mut().enumerate() {
1410            *b = i as u8;
1411        }
1412        let mut buf = Vec::new();
1413        encode_rotate(0xDEAD_BEEF_CAFE_F00D, &hash, &mut buf);
1414        // [length:u32 LE = 41][tag:u8 = MSG_ROTATE = 0x16]
1415        // [boundary_seq:u64 LE][tail_hash:32 bytes verbatim]
1416        let mut expected = vec![
1417            0x29, 0x00, 0x00, 0x00, // length = 41 LE
1418            0x16, // MSG_ROTATE
1419            0x0D, 0xF0, 0xFE, 0xCA, 0xEF, 0xBE, 0xAD, 0xDE, // boundary_seq LE
1420        ];
1421        expected.extend_from_slice(&hash);
1422        assert_eq!(
1423            buf.as_slice(),
1424            expected.as_slice(),
1425            "Rotate wire layout drifted"
1426        );
1427    }
1428
1429    #[test]
1430    fn segment_seed_begin_encode_decode_round_trip() {
1431        let mut buf = Vec::new();
1432        encode_segment_seed_begin(123_456, &mut buf);
1433
1434        let payload = &buf[4..];
1435        let msg = decode_primary_message(payload).unwrap();
1436        match msg {
1437            PrimaryMessage::SegmentSeedBegin { seed_len } => {
1438                assert_eq!(seed_len, 123_456);
1439            }
1440            _ => panic!("expected SegmentSeedBegin"),
1441        }
1442    }
1443
1444    #[test]
1445    fn unknown_replica_message_type_is_error() {
1446        let payload = [0xFF, 0, 0, 0, 0, 0, 0, 0, 0];
1447        let result = decode_replica_message(&payload);
1448        assert!(result.is_err());
1449    }
1450
1451    #[test]
1452    fn unknown_primary_message_type_is_error() {
1453        let payload = [0xFF, 0, 0, 0, 0, 0, 0, 0, 0];
1454        let result = decode_primary_message(&payload);
1455        assert!(result.is_err());
1456    }
1457
1458    #[test]
1459    fn read_frame_enforces_max_size() {
1460        // Create a buffer with a length prefix claiming 1000 bytes.
1461        let mut buf = Vec::new();
1462        buf.extend_from_slice(&1000u32.to_le_bytes());
1463        buf.extend_from_slice(&[0u8; 100]); // not enough data, but max_size check comes first
1464
1465        let mut cursor = std::io::Cursor::new(buf);
1466        let result = read_frame(&mut cursor, 64);
1467        assert!(result.is_err());
1468    }
1469
1470    #[test]
1471    fn challenge_encode_decode_round_trip() {
1472        let nonce = [0x42; 32];
1473        let mut buf = Vec::new();
1474        encode_challenge(&nonce, &mut buf);
1475
1476        let payload = &buf[4..];
1477        let decoded = decode_challenge(payload).unwrap();
1478        assert_eq!(decoded, nonce);
1479    }
1480
1481    #[test]
1482    fn challenge_response_encode_decode_round_trip() {
1483        let sig = [0xAA; 64];
1484        let pubkey = [0xBB; 32];
1485        let mut buf = Vec::new();
1486        encode_challenge_response(&sig, &pubkey, &mut buf);
1487
1488        let payload = &buf[4..];
1489        let (decoded_sig, decoded_pubkey) = decode_challenge_response(payload).unwrap();
1490        assert_eq!(decoded_sig, sig);
1491        assert_eq!(decoded_pubkey, pubkey);
1492    }
1493
1494    #[test]
1495    fn auth_ok_encode_decode_round_trip() {
1496        let mut buf = Vec::new();
1497        encode_auth_ok(&mut buf);
1498
1499        let payload = &buf[4..];
1500        assert!(decode_auth_result(payload).unwrap());
1501    }
1502
1503    #[test]
1504    fn auth_failed_encode_decode_round_trip() {
1505        let mut buf = Vec::new();
1506        encode_auth_failed(&mut buf);
1507
1508        let payload = &buf[4..];
1509        assert!(!decode_auth_result(payload).unwrap());
1510    }
1511
1512    #[test]
1513    fn decode_challenge_rejects_wrong_tag() {
1514        let mut payload = [0u8; 33];
1515        payload[0] = MSG_AUTH_OK;
1516        assert!(decode_challenge(&payload).is_err());
1517    }
1518
1519    #[test]
1520    fn decode_challenge_response_rejects_short_payload() {
1521        let payload = [MSG_CHALLENGE_RESPONSE; 10]; // too short
1522        assert!(decode_challenge_response(&payload).is_err());
1523    }
1524
1525    #[test]
1526    fn auth_round_trip_valid_key() {
1527        use ed25519_dalek::SigningKey;
1528        use std::os::unix::net::UnixStream;
1529
1530        let repl_key = SigningKey::from_bytes(&[0xFC; 32]);
1531        let pub_b64 = base64::Engine::encode(
1532            &base64::engine::general_purpose::STANDARD,
1533            repl_key.verifying_key().to_bytes(),
1534        );
1535        let keys_content = format!("replication {pub_b64} test-replica\n");
1536        let authorized_keys = melin_app::auth::AuthorizedKeys::parse(&keys_content).unwrap();
1537
1538        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
1539        primary_stream
1540            .set_read_timeout(Some(std::time::Duration::from_secs(5)))
1541            .unwrap();
1542        replica_stream
1543            .set_read_timeout(Some(std::time::Duration::from_secs(5)))
1544            .unwrap();
1545
1546        let repl_key_clone = SigningKey::from_bytes(&[0xFC; 32]);
1547        let replica_handle = std::thread::spawn(move || {
1548            let mut conn = replica_stream;
1549            authenticate_with_primary(&mut conn, &repl_key_clone)
1550        });
1551
1552        let mut conn = primary_stream;
1553        authenticate_replica(&mut conn, &authorized_keys).unwrap();
1554
1555        replica_handle.join().unwrap().unwrap();
1556    }
1557
1558    #[test]
1559    fn auth_rejects_unknown_key() {
1560        use ed25519_dalek::SigningKey;
1561        use std::os::unix::net::UnixStream;
1562
1563        // authorized_keys has one key, but the replica uses a different one.
1564        let authorized_key = SigningKey::from_bytes(&[0xAA; 32]);
1565        let rogue_key = SigningKey::from_bytes(&[0xBB; 32]);
1566        let pub_b64 = base64::Engine::encode(
1567            &base64::engine::general_purpose::STANDARD,
1568            authorized_key.verifying_key().to_bytes(),
1569        );
1570        let keys_content = format!("replication {pub_b64} authorized-replica\n");
1571        let authorized_keys = melin_app::auth::AuthorizedKeys::parse(&keys_content).unwrap();
1572
1573        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
1574        primary_stream
1575            .set_read_timeout(Some(std::time::Duration::from_secs(5)))
1576            .unwrap();
1577        replica_stream
1578            .set_read_timeout(Some(std::time::Duration::from_secs(5)))
1579            .unwrap();
1580
1581        let replica_handle = std::thread::spawn(move || {
1582            let mut conn = replica_stream;
1583            authenticate_with_primary(&mut conn, &rogue_key)
1584        });
1585
1586        let mut conn = primary_stream;
1587        let result = authenticate_replica(&mut conn, &authorized_keys);
1588        assert!(result.is_err());
1589        assert!(result.unwrap_err().to_string().contains("unknown"));
1590
1591        // Replica should also get a rejection.
1592        let replica_result = replica_handle.join().unwrap();
1593        assert!(replica_result.is_err());
1594    }
1595
1596    #[test]
1597    fn auth_rejects_wrong_permission() {
1598        use ed25519_dalek::SigningKey;
1599        use std::os::unix::net::UnixStream;
1600
1601        // Key exists but has Trader permission, not Replication.
1602        let key = SigningKey::from_bytes(&[0xCC; 32]);
1603        let pub_b64 = base64::Engine::encode(
1604            &base64::engine::general_purpose::STANDARD,
1605            key.verifying_key().to_bytes(),
1606        );
1607        let keys_content = format!("trader {pub_b64} wrong-role\n");
1608        let authorized_keys = melin_app::auth::AuthorizedKeys::parse(&keys_content).unwrap();
1609
1610        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
1611        primary_stream
1612            .set_read_timeout(Some(std::time::Duration::from_secs(5)))
1613            .unwrap();
1614        replica_stream
1615            .set_read_timeout(Some(std::time::Duration::from_secs(5)))
1616            .unwrap();
1617
1618        let replica_handle = std::thread::spawn(move || {
1619            let mut conn = replica_stream;
1620            authenticate_with_primary(&mut conn, &key)
1621        });
1622
1623        let mut conn = primary_stream;
1624        let result = authenticate_replica(&mut conn, &authorized_keys);
1625        assert!(result.is_err());
1626        assert!(result.unwrap_err().to_string().contains("Replication"));
1627
1628        let replica_result = replica_handle.join().unwrap();
1629        assert!(replica_result.is_err());
1630    }
1631
1632    /// A replica that sends a validly-formatted but tampered signature
1633    /// (correct public key, wrong signature bytes) is rejected.
1634    #[test]
1635    fn auth_rejects_invalid_signature() {
1636        use ed25519_dalek::SigningKey;
1637        use std::os::unix::net::UnixStream;
1638
1639        // Register the correct key.
1640        let correct_key = SigningKey::from_bytes(&[0xDD; 32]);
1641        let pub_b64 = base64::Engine::encode(
1642            &base64::engine::general_purpose::STANDARD,
1643            correct_key.verifying_key().to_bytes(),
1644        );
1645        let keys_content = format!("replication {pub_b64} test-replica\n");
1646        let authorized_keys = melin_app::auth::AuthorizedKeys::parse(&keys_content).unwrap();
1647
1648        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
1649        primary_stream
1650            .set_read_timeout(Some(std::time::Duration::from_secs(5)))
1651            .unwrap();
1652        replica_stream
1653            .set_read_timeout(Some(std::time::Duration::from_secs(5)))
1654            .unwrap();
1655
1656        // Replica side: read challenge, but sign with a DIFFERENT key,
1657        // then send the response with the correct public key (spoofing).
1658        let replica_handle = std::thread::spawn(move || {
1659            use melin_transport_core::replication::protocol::*;
1660
1661            let mut reader = replica_stream.try_clone().unwrap();
1662            let mut writer = replica_stream;
1663
1664            // Read the challenge.
1665            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
1666            let nonce = decode_challenge(&frame).unwrap();
1667
1668            // Sign with a WRONG key but send the CORRECT public key.
1669            let wrong_key = SigningKey::from_bytes(&[0xEE; 32]);
1670            let bad_signature = ed25519_dalek::Signer::sign(&wrong_key, &nonce);
1671            let correct_pubkey = correct_key.verifying_key();
1672
1673            let mut buf = Vec::with_capacity(128);
1674            encode_challenge_response(
1675                &bad_signature.to_bytes(),
1676                correct_pubkey.as_bytes(),
1677                &mut buf,
1678            );
1679            writer.write_all(&buf).unwrap();
1680            writer.flush().unwrap();
1681
1682            // Should receive AuthFailed.
1683            let result_frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
1684            let ok = decode_auth_result(&result_frame).unwrap();
1685            assert!(!ok, "should receive auth failure");
1686        });
1687
1688        let mut conn = primary_stream;
1689        let result = authenticate_replica(&mut conn, &authorized_keys);
1690        assert!(result.is_err());
1691        assert!(
1692            result
1693                .unwrap_err()
1694                .to_string()
1695                .contains("signature verification failed"),
1696            "should fail on signature verification"
1697        );
1698
1699        replica_handle.join().unwrap();
1700    }
1701
1702    #[test]
1703    fn sender_receiver_end_to_end() {
1704        use std::os::unix::net::UnixStream;
1705
1706        // Create a mock connection.
1707        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
1708
1709        // Spawn a thread simulating the replica side.
1710        let replica_handle = std::thread::spawn(move || {
1711            let mut reader = replica_stream.try_clone().unwrap();
1712            let mut writer = replica_stream;
1713
1714            // Send handshake.
1715            let mut buf = Vec::new();
1716            let handshake = Handshake {
1717                last_sequence: 0,
1718                chain_hash: [0u8; 32],
1719                epoch: 0,
1720            };
1721            encode_handshake(&handshake, &mut buf);
1722            writer.write_all(&buf).unwrap();
1723            writer.flush().unwrap();
1724            buf.clear();
1725
1726            // Read StreamStart.
1727            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
1728            let msg = decode_primary_message(&frame).unwrap();
1729            assert!(matches!(msg, PrimaryMessage::StreamStart { .. }));
1730
1731            // Read InputBatch.
1732            let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
1733            let slots: Vec<InputSlot> = try_decode_input_batch(&frame).expect("decode InputBatch");
1734            let end_seq = slots
1735                .last()
1736                .map(|s| s.sequence)
1737                .expect("InputBatch carried at least one slot");
1738
1739            // Send ack.
1740            let ack = Ack {
1741                acked_sequence: end_seq,
1742                in_memory_sequence: end_seq,
1743            };
1744            encode_ack(&ack, &mut buf);
1745            writer.write_all(&buf).unwrap();
1746            writer.flush().unwrap();
1747
1748            end_seq
1749        });
1750
1751        // Primary side: simulate handle_replica_connection partially.
1752        let mut p_reader = primary_stream.try_clone().unwrap();
1753        let mut p_writer = primary_stream;
1754
1755        // Read handshake.
1756        let frame = read_frame(&mut p_reader, MAX_CONTROL_FRAME).unwrap();
1757        let handshake = match decode_replica_message(&frame).unwrap() {
1758            ReplicaMessage::Handshake(h) => h,
1759            _ => panic!("expected Handshake"),
1760        };
1761        assert_eq!(handshake.last_sequence, 0);
1762
1763        // Send StreamStart.
1764        let mut buf = Vec::new();
1765        encode_stream_start(0, 1, [0u8; 32], 0, 1, &mut buf); // fake lineage for test
1766        p_writer.write_all(&buf).unwrap();
1767        p_writer.flush().unwrap();
1768        buf.clear();
1769
1770        // Send an InputBatch with a single Tick slot at seq 42.
1771        encode_input_batch_with_seq(42, &mut buf);
1772        p_writer.write_all(&buf).unwrap();
1773        p_writer.flush().unwrap();
1774        buf.clear();
1775
1776        // Read ack.
1777        let frame = read_frame(&mut p_reader, MAX_CONTROL_FRAME).unwrap();
1778        let ack = match decode_replica_message(&frame).unwrap() {
1779            ReplicaMessage::Ack(a) => a,
1780            _ => panic!("expected Ack"),
1781        };
1782        assert_eq!(ack.acked_sequence, 42);
1783
1784        // Join replica thread.
1785        let end_seq = replica_handle.join().unwrap();
1786        assert_eq!(end_seq, 42);
1787    }
1788
1789    #[test]
1790    fn multiple_data_batches_acked_in_order() {
1791        // Send multiple InputBatch frames, verify replica acks each one
1792        // and the cursor advances correctly.
1793        use std::os::unix::net::UnixStream;
1794
1795        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
1796
1797        let replica_handle = std::thread::spawn(move || {
1798            let mut reader = replica_stream.try_clone().unwrap();
1799            let mut writer = replica_stream;
1800            let mut buf = Vec::new();
1801
1802            // Send handshake.
1803            encode_handshake(
1804                &Handshake {
1805                    last_sequence: 0,
1806                    chain_hash: [0u8; 32],
1807                    epoch: 0,
1808                },
1809                &mut buf,
1810            );
1811            writer.write_all(&buf).unwrap();
1812            writer.flush().unwrap();
1813            buf.clear();
1814
1815            // Read StreamStart.
1816            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
1817            assert!(matches!(
1818                decode_primary_message(&frame).unwrap(),
1819                PrimaryMessage::StreamStart { .. }
1820            ));
1821
1822            // Read and ack 3 InputBatches.
1823            let mut acked_seqs = Vec::new();
1824            for _ in 0..3 {
1825                let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
1826                let slots: Vec<InputSlot> =
1827                    try_decode_input_batch(&frame).expect("decode InputBatch");
1828                let end_seq = slots
1829                    .last()
1830                    .map(|s| s.sequence)
1831                    .expect("InputBatch carried at least one slot");
1832                acked_seqs.push(end_seq);
1833
1834                encode_ack(
1835                    &Ack {
1836                        acked_sequence: end_seq,
1837                        in_memory_sequence: end_seq,
1838                    },
1839                    &mut buf,
1840                );
1841                writer.write_all(&buf).unwrap();
1842                writer.flush().unwrap();
1843                buf.clear();
1844            }
1845
1846            acked_seqs
1847        });
1848
1849        // Primary side.
1850        let mut p_reader = primary_stream.try_clone().unwrap();
1851        let mut p_writer = primary_stream;
1852        let mut buf = Vec::new();
1853
1854        // Read handshake.
1855        let frame = read_frame(&mut p_reader, MAX_CONTROL_FRAME).unwrap();
1856        assert!(matches!(
1857            decode_replica_message(&frame).unwrap(),
1858            ReplicaMessage::Handshake(_)
1859        ));
1860
1861        // Send StreamStart.
1862        encode_stream_start(0, 1, [0u8; 32], 0, 1, &mut buf);
1863        p_writer.write_all(&buf).unwrap();
1864        p_writer.flush().unwrap();
1865        buf.clear();
1866
1867        // Send 3 InputBatches with increasing sequence numbers.
1868        for seq in [10u64, 20, 30] {
1869            encode_input_batch_with_seq(seq, &mut buf);
1870            p_writer.write_all(&buf).unwrap();
1871            p_writer.flush().unwrap();
1872            buf.clear();
1873        }
1874
1875        // Read 3 acks.
1876        for expected_seq in [10u64, 20, 30] {
1877            let frame = read_frame(&mut p_reader, MAX_CONTROL_FRAME).unwrap();
1878            let ack = match decode_replica_message(&frame).unwrap() {
1879                ReplicaMessage::Ack(a) => a,
1880                other => panic!("expected Ack, got {other:?}"),
1881            };
1882            assert_eq!(ack.acked_sequence, expected_seq);
1883        }
1884
1885        let acked = replica_handle.join().unwrap();
1886        assert_eq!(acked, vec![10, 20, 30]);
1887    }
1888
1889    #[test]
1890    fn heartbeat_encode_contains_sequence() {
1891        // Heartbeat messages carry the last known sequence so the replica
1892        // can verify it hasn't missed any data.
1893        let mut buf = Vec::new();
1894        encode_heartbeat(999, 1, &mut buf);
1895
1896        let payload = &buf[4..];
1897        match decode_primary_message(payload).unwrap() {
1898            PrimaryMessage::Heartbeat { sequence, .. } => {
1899                assert_eq!(sequence, 999);
1900            }
1901            other => panic!("expected Heartbeat, got {other:?}"),
1902        }
1903    }
1904
1905    #[test]
1906    fn replica_mid_stream_handshake_with_nonzero_sequence() {
1907        // A replica that already has some data sends a non-zero last_sequence
1908        // in its handshake. The primary should respond with StreamStart
1909        // containing that sequence, and the replica should only receive
1910        // events after that point.
1911        use std::os::unix::net::UnixStream;
1912
1913        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
1914
1915        let replica_handle = std::thread::spawn(move || {
1916            let mut reader = replica_stream.try_clone().unwrap();
1917            let mut writer = replica_stream;
1918            let mut buf = Vec::new();
1919
1920            // Replica already has events up to sequence 100.
1921            encode_handshake(
1922                &Handshake {
1923                    last_sequence: 100,
1924                    chain_hash: [0xBB; 32],
1925                    epoch: 0,
1926                },
1927                &mut buf,
1928            );
1929            writer.write_all(&buf).unwrap();
1930            writer.flush().unwrap();
1931            buf.clear();
1932
1933            // Read StreamStart — should echo back our last_sequence.
1934            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
1935            match decode_primary_message(&frame).unwrap() {
1936                PrimaryMessage::StreamStart { start_sequence, .. } => {
1937                    assert_eq!(
1938                        start_sequence, 100,
1939                        "StreamStart should echo replica's last_sequence"
1940                    );
1941                }
1942                other => panic!("expected StreamStart, got {other:?}"),
1943            }
1944
1945            // Read an InputBatch — should be for events AFTER 100.
1946            let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
1947            let slots: Vec<InputSlot> = try_decode_input_batch(&frame).expect("decode InputBatch");
1948            let end_sequence = slots
1949                .last()
1950                .map(|s| s.sequence)
1951                .expect("InputBatch carried at least one slot");
1952            assert!(
1953                end_sequence > 100,
1954                "InputBatch should be after replica's last_sequence"
1955            );
1956        });
1957
1958        // Primary side.
1959        let mut p_reader = primary_stream.try_clone().unwrap();
1960        let mut p_writer = primary_stream;
1961        let mut buf = Vec::new();
1962
1963        // Read handshake.
1964        let frame = read_frame(&mut p_reader, MAX_CONTROL_FRAME).unwrap();
1965        let handshake = match decode_replica_message(&frame).unwrap() {
1966            ReplicaMessage::Handshake(h) => h,
1967            _ => panic!("expected Handshake"),
1968        };
1969        assert_eq!(handshake.last_sequence, 100);
1970        assert_eq!(handshake.chain_hash, [0xBB; 32]);
1971
1972        // Send StreamStart echoing the replica's sequence.
1973        encode_stream_start(handshake.last_sequence, 1, [0u8; 32], 0, 1, &mut buf);
1974        p_writer.write_all(&buf).unwrap();
1975        p_writer.flush().unwrap();
1976        buf.clear();
1977
1978        // Send InputBatch with sequence 150 (after replica's 100).
1979        encode_input_batch_with_seq(150, &mut buf);
1980        p_writer.write_all(&buf).unwrap();
1981        p_writer.flush().unwrap();
1982
1983        replica_handle.join().unwrap();
1984    }
1985
1986    /// The receiver's stale-primary refusal: a replica that has observed
1987    /// epoch 5 must refuse a `StreamStart` advertising a *lower* epoch
1988    /// rather than follow that (divergent) lineage on top of its newer
1989    /// state. This reproduces the exact decision `run_receiver` makes on
1990    /// the normal-resume `StreamStart` — decode the frame off the wire,
1991    /// then `fence_state.refuses_primary(epoch)` — over a real socket so
1992    /// the epoch wire field and the policy are exercised together.
1993    ///
1994    /// In a current-build cluster this branch is a *second* line of
1995    /// defense: a primary reads the replica's higher-epoch handshake and
1996    /// self-demotes before it ever sends a `StreamStart` (see
1997    /// `tcp_sender::handle_replica_connection`). The receiver check guards
1998    /// the case where the primary does *not* fence — a non-fencing or
1999    /// older build — which is exactly what the mock primary below is.
2000    #[test]
2001    fn receiver_refuses_stream_start_from_stale_primary() {
2002        use std::os::unix::net::UnixStream;
2003
2004        use melin_transport_core::fence::FenceState;
2005
2006        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
2007
2008        // Mock primary that — unlike a current build — does *not* fence on
2009        // the handshake and streams its stale lineage anyway. It advertises
2010        // epoch 3 on the StreamStart.
2011        const STALE_EPOCH: u64 = 3;
2012        let primary_handle = std::thread::spawn(move || {
2013            let mut reader = primary_stream.try_clone().unwrap();
2014            let mut writer = primary_stream;
2015
2016            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
2017            let handshake = match decode_replica_message(&frame).unwrap() {
2018                ReplicaMessage::Handshake(h) => h,
2019                _ => panic!("expected Handshake"),
2020            };
2021            // The replica truthfully advertises its higher epoch; a correct
2022            // primary would fence here. This mock deliberately doesn't.
2023            assert_eq!(handshake.epoch, 5, "replica advertises its real epoch");
2024
2025            let mut buf = Vec::new();
2026            encode_stream_start(
2027                handshake.last_sequence,
2028                1,
2029                [0u8; 32],
2030                STALE_EPOCH,
2031                1,
2032                &mut buf,
2033            );
2034            writer.write_all(&buf).unwrap();
2035            writer.flush().unwrap();
2036        });
2037
2038        // Replica side: epoch 5, mirroring `run_receiver`'s handshake +
2039        // StreamStart handling.
2040        let fence_state = Arc::new(FenceState::new(5));
2041        let mut r_reader = replica_stream.try_clone().unwrap();
2042        let mut r_writer = replica_stream;
2043
2044        let mut buf = Vec::new();
2045        encode_handshake(
2046            &Handshake {
2047                last_sequence: 0,
2048                chain_hash: [0u8; 32],
2049                epoch: fence_state.epoch(),
2050            },
2051            &mut buf,
2052        );
2053        r_writer.write_all(&buf).unwrap();
2054        r_writer.flush().unwrap();
2055
2056        let frame = read_frame(&mut r_reader, MAX_CONTROL_FRAME).unwrap();
2057        let stream_epoch = match decode_primary_message(&frame).unwrap() {
2058            PrimaryMessage::StreamStart { epoch, .. } => epoch,
2059            other => panic!("expected StreamStart, got {other:?}"),
2060        };
2061        assert_eq!(stream_epoch, STALE_EPOCH, "epoch must survive the wire");
2062
2063        // This is the receiver's refusal decision. A stale primary must be
2064        // refused; the replica's own epoch must not have been lowered.
2065        assert!(
2066            fence_state.refuses_primary(stream_epoch),
2067            "replica at epoch 5 must refuse a StreamStart from epoch {STALE_EPOCH}"
2068        );
2069        assert_eq!(fence_state.epoch(), 5, "refusal must not lower our epoch");
2070
2071        // Sanity: an equal or newer epoch is followed, not refused (the
2072        // ex-primary-rejoin path). Guards against an inverted comparison.
2073        assert!(!fence_state.refuses_primary(5), "same tenure is followed");
2074        assert!(!fence_state.refuses_primary(6), "newer primary is followed");
2075
2076        primary_handle.join().unwrap();
2077    }
2078
2079    #[test]
2080    fn snapshot_begin_encode_decode_round_trip() {
2081        let mut buf = Vec::new();
2082        encode_snapshot_begin(1_000_000, 42, &[0xAB; 32], &mut buf);
2083
2084        let payload = &buf[4..];
2085        let msg = decode_primary_message(payload).unwrap();
2086        match msg {
2087            PrimaryMessage::SnapshotBegin {
2088                snapshot_len,
2089                snap_sequence,
2090                snap_chain_hash,
2091            } => {
2092                assert_eq!(snapshot_len, 1_000_000);
2093                assert_eq!(snap_sequence, 42);
2094                assert_eq!(snap_chain_hash, [0xAB; 32]);
2095            }
2096            _ => panic!("expected SnapshotBegin"),
2097        }
2098    }
2099
2100    #[test]
2101    fn snapshot_chunk_encode_decode_round_trip() {
2102        let data = vec![1, 2, 3, 4, 5, 6, 7, 8];
2103        let mut buf = Vec::new();
2104        encode_snapshot_chunk(&data, &mut buf);
2105
2106        let payload = &buf[4..];
2107        let msg = decode_primary_message(payload).unwrap();
2108        match msg {
2109            PrimaryMessage::SnapshotChunk(chunk) => {
2110                assert_eq!(chunk, data);
2111            }
2112            _ => panic!("expected SnapshotChunk"),
2113        }
2114    }
2115
2116    #[test]
2117    fn snapshot_end_encode_decode_round_trip() {
2118        let mut buf = Vec::new();
2119        encode_snapshot_end(0xDEADBEEF, &mut buf);
2120
2121        let payload = &buf[4..];
2122        let msg = decode_primary_message(payload).unwrap();
2123        match msg {
2124            PrimaryMessage::SnapshotEnd { crc32c } => {
2125                assert_eq!(crc32c, 0xDEADBEEF);
2126            }
2127            _ => panic!("expected SnapshotEnd"),
2128        }
2129    }
2130
2131    /// Simulate the receiver side of a snapshot transfer where the
2132    /// advertised snap_len doesn't match the actual bytes sent.
2133    /// The receiver must detect this and return an error.
2134    #[test]
2135    fn snapshot_receiver_detects_length_mismatch() {
2136        use std::os::unix::net::UnixStream;
2137
2138        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
2139
2140        // Receiver thread — reads NeedSnapshot, then the snapshot transfer.
2141        let receiver = std::thread::spawn(move || -> String {
2142            let mut reader = replica_stream.try_clone().unwrap();
2143
2144            // Read NeedSnapshot.
2145            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
2146            assert!(matches!(
2147                decode_primary_message(&frame).unwrap(),
2148                PrimaryMessage::NeedSnapshot,
2149            ));
2150
2151            // Read SnapshotBegin.
2152            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
2153            let (snap_len, _snap_sequence, _snap_chain_hash) =
2154                match decode_primary_message(&frame).unwrap() {
2155                    PrimaryMessage::SnapshotBegin {
2156                        snapshot_len,
2157                        snap_sequence,
2158                        snap_chain_hash,
2159                    } => (snapshot_len, snap_sequence, snap_chain_hash),
2160                    other => panic!("expected SnapshotBegin, got {other:?}"),
2161                };
2162
2163            // Receive chunks and check length at SnapshotEnd.
2164            let mut received: u64 = 0;
2165            loop {
2166                let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
2167                match decode_primary_message(&frame).unwrap() {
2168                    PrimaryMessage::SnapshotChunk(data) => {
2169                        received += data.len() as u64;
2170                    }
2171                    PrimaryMessage::SnapshotEnd { .. } => {
2172                        if received != snap_len {
2173                            return format!(
2174                                "snapshot length mismatch: expected {snap_len} bytes, got {received}"
2175                            );
2176                        }
2177                        return String::new(); // no error
2178                    }
2179                    other => panic!("unexpected message: {other:?}"),
2180                }
2181            }
2182        });
2183
2184        // Primary side — send snapshot with wrong advertised length.
2185        let mut writer = primary_stream;
2186        let mut buf = Vec::new();
2187
2188        let actual_data = vec![0xAA; 100];
2189        let wrong_len = 999u64; // advertise 999 bytes, send only 100
2190
2191        encode_need_snapshot(&mut buf);
2192        std::io::Write::write_all(&mut writer, &buf).unwrap();
2193        buf.clear();
2194
2195        encode_snapshot_begin(wrong_len, 42, &[0xBB; 32], &mut buf);
2196        std::io::Write::write_all(&mut writer, &buf).unwrap();
2197        buf.clear();
2198
2199        encode_snapshot_chunk(&actual_data, &mut buf);
2200        std::io::Write::write_all(&mut writer, &buf).unwrap();
2201        buf.clear();
2202
2203        let crc = crc32c::crc32c(&actual_data);
2204        encode_snapshot_end(crc, &mut buf);
2205        std::io::Write::write_all(&mut writer, &buf).unwrap();
2206        std::io::Write::flush(&mut writer).unwrap();
2207
2208        let error_msg = receiver.join().unwrap();
2209        assert!(
2210            error_msg.contains("length mismatch"),
2211            "expected length mismatch error, got: {error_msg:?}"
2212        );
2213    }
2214
2215    /// Simulate the receiver side of a snapshot transfer where the CRC
2216    /// in SnapshotEnd doesn't match the actual data. The receiver must
2217    /// detect and reject the transfer.
2218    #[test]
2219    fn snapshot_receiver_detects_crc_mismatch() {
2220        use std::os::unix::net::UnixStream;
2221
2222        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
2223
2224        let receiver = std::thread::spawn(move || -> String {
2225            let mut reader = replica_stream.try_clone().unwrap();
2226
2227            // Read NeedSnapshot.
2228            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
2229            assert!(matches!(
2230                decode_primary_message(&frame).unwrap(),
2231                PrimaryMessage::NeedSnapshot,
2232            ));
2233
2234            // Read SnapshotBegin.
2235            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
2236            let snap_len = match decode_primary_message(&frame).unwrap() {
2237                PrimaryMessage::SnapshotBegin { snapshot_len, .. } => snapshot_len,
2238                other => panic!("expected SnapshotBegin, got {other:?}"),
2239            };
2240
2241            // Receive chunks, verify CRC at SnapshotEnd.
2242            let mut received_data = Vec::new();
2243            let mut received: u64 = 0;
2244            loop {
2245                let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
2246                match decode_primary_message(&frame).unwrap() {
2247                    PrimaryMessage::SnapshotChunk(data) => {
2248                        received += data.len() as u64;
2249                        received_data.extend_from_slice(&data);
2250                    }
2251                    PrimaryMessage::SnapshotEnd {
2252                        crc32c: expected_crc,
2253                    } => {
2254                        if received != snap_len {
2255                            return format!("length mismatch: {snap_len} vs {received}");
2256                        }
2257                        let actual_crc = crc32c::crc32c(&received_data);
2258                        if actual_crc != expected_crc {
2259                            return format!(
2260                                "CRC mismatch: expected {expected_crc:#x}, got {actual_crc:#x}"
2261                            );
2262                        }
2263                        return String::new();
2264                    }
2265                    other => panic!("unexpected message: {other:?}"),
2266                }
2267            }
2268        });
2269
2270        // Primary side — send correct length but wrong CRC.
2271        let mut writer = primary_stream;
2272        let mut buf = Vec::new();
2273
2274        let data = vec![0xAA; 100];
2275
2276        encode_need_snapshot(&mut buf);
2277        std::io::Write::write_all(&mut writer, &buf).unwrap();
2278        buf.clear();
2279
2280        encode_snapshot_begin(data.len() as u64, 42, &[0xBB; 32], &mut buf);
2281        std::io::Write::write_all(&mut writer, &buf).unwrap();
2282        buf.clear();
2283
2284        encode_snapshot_chunk(&data, &mut buf);
2285        std::io::Write::write_all(&mut writer, &buf).unwrap();
2286        buf.clear();
2287
2288        // Send a wrong CRC (flip bits).
2289        let wrong_crc = !crc32c::crc32c(&data);
2290        encode_snapshot_end(wrong_crc, &mut buf);
2291        std::io::Write::write_all(&mut writer, &buf).unwrap();
2292        std::io::Write::flush(&mut writer).unwrap();
2293
2294        let error_msg = receiver.join().unwrap();
2295        assert!(
2296            error_msg.contains("CRC mismatch"),
2297            "expected CRC mismatch error, got: {error_msg:?}"
2298        );
2299    }
2300
2301    /// The receiver verifies the chain hash from the loaded snapshot
2302    /// matches the one advertised in SnapshotBegin. Simulate a mismatch.
2303    #[test]
2304    fn snapshot_receiver_detects_chain_hash_mismatch() {
2305        use std::os::unix::net::UnixStream;
2306
2307        let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
2308
2309        let receiver = std::thread::spawn(move || -> String {
2310            let mut reader = replica_stream.try_clone().unwrap();
2311
2312            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
2313            assert!(matches!(
2314                decode_primary_message(&frame).unwrap(),
2315                PrimaryMessage::NeedSnapshot,
2316            ));
2317
2318            let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
2319            let (snap_len, _snap_sequence, snap_chain_hash) =
2320                match decode_primary_message(&frame).unwrap() {
2321                    PrimaryMessage::SnapshotBegin {
2322                        snapshot_len,
2323                        snap_sequence,
2324                        snap_chain_hash,
2325                    } => (snapshot_len, snap_sequence, snap_chain_hash),
2326                    other => panic!("expected SnapshotBegin, got {other:?}"),
2327                };
2328
2329            // Receive the snapshot data.
2330            let mut received_data = Vec::new();
2331            let mut received: u64 = 0;
2332            loop {
2333                let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
2334                match decode_primary_message(&frame).unwrap() {
2335                    PrimaryMessage::SnapshotChunk(data) => {
2336                        received += data.len() as u64;
2337                        received_data.extend_from_slice(&data);
2338                    }
2339                    PrimaryMessage::SnapshotEnd {
2340                        crc32c: expected_crc,
2341                    } => {
2342                        assert_eq!(received, snap_len, "length should match");
2343                        let actual_crc = crc32c::crc32c(&received_data);
2344                        assert_eq!(actual_crc, expected_crc, "CRC should match");
2345                        break;
2346                    }
2347                    other => panic!("unexpected message: {other:?}"),
2348                }
2349            }
2350
2351            // Simulate chain hash verification: the loaded snapshot would
2352            // have a different chain hash than what SnapshotBegin advertised.
2353            let loaded_hash = [0xFF; 32]; // different from snap_chain_hash
2354            if loaded_hash != snap_chain_hash {
2355                return format!(
2356                    "snapshot chain hash mismatch: primary sent {snap_chain_hash:02x?}, \
2357                     loaded snapshot has {loaded_hash:02x?}"
2358                );
2359            }
2360            String::new()
2361        });
2362
2363        // Primary side — send valid snapshot but with a chain hash in
2364        // SnapshotBegin that won't match what the replica "loads".
2365        let mut writer = primary_stream;
2366        let mut buf = Vec::new();
2367
2368        let data = vec![0xAA; 64];
2369        // Advertise chain hash [0xBB; 32] — receiver will "load" [0xFF; 32].
2370        let advertised_hash = [0xBB; 32];
2371
2372        encode_need_snapshot(&mut buf);
2373        std::io::Write::write_all(&mut writer, &buf).unwrap();
2374        buf.clear();
2375
2376        encode_snapshot_begin(data.len() as u64, 10, &advertised_hash, &mut buf);
2377        std::io::Write::write_all(&mut writer, &buf).unwrap();
2378        buf.clear();
2379
2380        encode_snapshot_chunk(&data, &mut buf);
2381        std::io::Write::write_all(&mut writer, &buf).unwrap();
2382        buf.clear();
2383
2384        let crc = crc32c::crc32c(&data);
2385        encode_snapshot_end(crc, &mut buf);
2386        std::io::Write::write_all(&mut writer, &buf).unwrap();
2387        std::io::Write::flush(&mut writer).unwrap();
2388
2389        let error_msg = receiver.join().unwrap();
2390        assert!(
2391            error_msg.contains("chain hash mismatch"),
2392            "expected chain hash mismatch error, got: {error_msg:?}"
2393        );
2394    }
2395
2396    /// Primary-side magic validation: a file without the SNAP magic
2397    /// (0x534E4150) must be rejected before transfer.
2398    #[test]
2399    fn primary_rejects_snapshot_with_invalid_magic() {
2400        let tmp = tempfile::tempdir().unwrap();
2401        let snap_path = tmp.path().join("test.snapshot");
2402
2403        // Write a file with wrong magic but enough bytes for a header.
2404        let mut bad_snap = vec![0u8; 64];
2405        bad_snap[0..4].copy_from_slice(&0xDEAD_BEEFu32.to_le_bytes()); // wrong magic
2406
2407        std::fs::write(&snap_path, &bad_snap).unwrap();
2408
2409        // Replicate the primary's validation logic.
2410        let snap_data = std::fs::read(&snap_path).unwrap();
2411        assert!(
2412            snap_data.len() >= 48,
2413            "file should be big enough for header"
2414        );
2415
2416        let magic = u32::from_le_bytes(snap_data[0..4].try_into().unwrap());
2417        assert_ne!(magic, 0x534E_4150);
2418        assert_eq!(magic, 0xDEAD_BEEF);
2419    }
2420
2421    /// Primary-side: a snapshot file smaller than the 48-byte header
2422    /// must be rejected.
2423    #[test]
2424    fn primary_rejects_snapshot_too_small_for_header() {
2425        let tmp = tempfile::tempdir().unwrap();
2426        let snap_path = tmp.path().join("test.snapshot");
2427
2428        // Write a file smaller than the 48-byte header.
2429        std::fs::write(&snap_path, [0u8; 20]).unwrap();
2430
2431        let snap_data = std::fs::read(&snap_path).unwrap();
2432        assert!(
2433            snap_data.len() < 48,
2434            "file must be too small for header validation"
2435        );
2436    }
2437
2438    #[test]
2439    fn decode_snapshot_begin_too_short() {
2440        // SnapshotBegin needs type(1) + snapshot_len(8) + snap_sequence(8) + chain_hash(32) = 49.
2441        // Send only the type byte + a few extra bytes.
2442        let payload = [MSG_SNAPSHOT_BEGIN, 0x01, 0x02, 0x03];
2443        let err = decode_primary_message(&payload).unwrap_err();
2444        assert!(
2445            err.to_string().contains("SnapshotBegin too short"),
2446            "expected 'SnapshotBegin too short', got: {err}"
2447        );
2448    }
2449
2450    #[test]
2451    fn decode_snapshot_end_too_short() {
2452        // SnapshotEnd needs type(1) + crc32c(4) = 5. Send only the type byte.
2453        let payload = [MSG_SNAPSHOT_END];
2454        let err = decode_primary_message(&payload).unwrap_err();
2455        assert!(
2456            err.to_string().contains("SnapshotEnd too short"),
2457            "expected 'SnapshotEnd too short', got: {err}"
2458        );
2459    }
2460
2461    #[test]
2462    fn decode_snapshot_chunk_empty_data() {
2463        // SnapshotChunk with just the type byte — valid but empty payload.
2464        let payload = [MSG_SNAPSHOT_CHUNK];
2465        let msg = decode_primary_message(&payload).unwrap();
2466        match msg {
2467            PrimaryMessage::SnapshotChunk(data) => {
2468                assert!(data.is_empty());
2469            }
2470            _ => panic!("expected SnapshotChunk"),
2471        }
2472    }
2473
2474    // --- PendingAckQueue tests ---
2475
2476    fn make_journal_cursor(val: u64) -> melin_pipeline::padding::Sequence {
2477        melin_pipeline::padding::Sequence::new(AtomicU64::new(val))
2478    }
2479
2480    #[test]
2481    fn pending_ack_queue_push_and_pop_ready() {
2482        let mut q = PendingAckQueue::new(8);
2483        assert!(q.is_empty());
2484        assert!(!q.is_full());
2485
2486        q.push(10, 100);
2487        q.push(20, 200);
2488        assert!(!q.is_empty());
2489
2490        // Cursor at 5 — neither ready.
2491        let cursor = make_journal_cursor(5);
2492        assert!(q.pop_ready(&cursor).is_none());
2493
2494        // Cursor at 15 — first ready, second not.
2495        cursor.get().store(15, Ordering::Relaxed);
2496        assert_eq!(q.pop_ready(&cursor), Some(100));
2497        // Only one popped — second still pending.
2498        assert!(!q.is_empty());
2499
2500        // Cursor at 25 — second now ready.
2501        cursor.get().store(25, Ordering::Relaxed);
2502        assert_eq!(q.pop_ready(&cursor), Some(200));
2503        assert!(q.is_empty());
2504    }
2505
2506    #[test]
2507    fn pending_ack_queue_pop_ready_returns_highest_sequence() {
2508        // When multiple acks become ready simultaneously, pop_ready
2509        // returns the highest acked_sequence (ack semantics are
2510        // cumulative — "everything up to this sequence is durable").
2511        let mut q = PendingAckQueue::new(8);
2512        q.push(10, 100);
2513        q.push(20, 200);
2514        q.push(30, 300);
2515
2516        let cursor = make_journal_cursor(30);
2517        assert_eq!(q.pop_ready(&cursor), Some(300));
2518        assert!(q.is_empty());
2519    }
2520
2521    #[test]
2522    fn pending_ack_queue_capacity_and_full() {
2523        let mut q = PendingAckQueue::new(8);
2524        for i in 0..8 {
2525            assert!(!q.is_full());
2526            q.push(i as u64 + 1, (i + 1) as u64 * 100);
2527        }
2528        assert!(q.is_full());
2529    }
2530
2531    /// A full queue must absorb the push by merging it into the newest
2532    /// entry — never block, never drop, and never release a sequence
2533    /// before its own journal target. The merge is a *coarsening*: the
2534    /// entry it absorbed is acked later than it would have been.
2535    #[test]
2536    fn pending_ack_queue_merges_into_the_newest_entry_when_full() {
2537        let mut q = PendingAckQueue::new(4);
2538        for i in 1..=4u64 {
2539            q.push(i * 10, i * 100);
2540        }
2541        assert!(q.is_full());
2542        assert_eq!(q.merged(), 0);
2543
2544        // Fifth batch: target 50, sequence 500. Merges into (40, 400).
2545        q.push(50, 500);
2546        assert_eq!(q.merged(), 1);
2547        assert_eq!(q.len(), 4, "merge must not grow the queue");
2548
2549        // Cursor at 40 would have released 400 before the merge; now the
2550        // newest entry needs 50, so 300 is the highest safe ack.
2551        let cursor = make_journal_cursor(40);
2552        assert_eq!(q.pop_ready(&cursor), Some(300));
2553        assert!(!q.is_empty(), "merged entry is not durable yet");
2554
2555        // Cursor reaches the merged target — the merged entry releases
2556        // the newer sequence, subsuming the one it absorbed.
2557        cursor.get().store(50, Ordering::Relaxed);
2558        assert_eq!(q.pop_ready(&cursor), Some(500));
2559        assert!(q.is_empty());
2560    }
2561
2562    /// Repeated merges keep collapsing onto the same slot, so a journal
2563    /// stall of any length costs granularity and nothing else.
2564    #[test]
2565    fn pending_ack_queue_merges_repeatedly_without_growing() {
2566        let mut q = PendingAckQueue::new(2);
2567        for i in 1..=100u64 {
2568            q.push(i, i * 10);
2569        }
2570        assert_eq!(q.len(), 2);
2571        assert_eq!(q.merged(), 98);
2572
2573        // Nothing releases before its target...
2574        let cursor = make_journal_cursor(99);
2575        assert_eq!(q.pop_ready(&cursor), Some(10), "only the first entry");
2576        // ...and the merged tail carries the newest pair.
2577        cursor.get().store(100, Ordering::Relaxed);
2578        assert_eq!(q.pop_ready(&cursor), Some(1000));
2579    }
2580
2581    #[test]
2582    fn pending_ack_queue_pop_oldest_blocking() {
2583        let mut q = PendingAckQueue::new(8);
2584        q.push(10, 100);
2585        q.push(20, 200);
2586
2587        // Cursor already past both targets — pop_oldest_blocking
2588        // returns immediately.
2589        let cursor = make_journal_cursor(25);
2590        let seq = q.pop_oldest_blocking(
2591            &cursor,
2592            melin_pipeline::wait::WaitStrategy::BusySpin,
2593            &AtomicBool::new(false),
2594        );
2595        // Should pop both (oldest + any others that became ready).
2596        assert_eq!(seq, Some(200));
2597        assert!(q.is_empty());
2598    }
2599
2600    /// A dead journal stage (abort latch set) must abort the blocking
2601    /// wait instead of spinning forever on the frozen cursor.
2602    #[test]
2603    fn pending_ack_queue_blocking_wait_aborts_on_journal_failure() {
2604        let mut q = PendingAckQueue::new(8);
2605        q.push(10, 100);
2606
2607        // Cursor frozen BELOW the target; abort pre-latched.
2608        let cursor = make_journal_cursor(5);
2609        let abort = AtomicBool::new(true);
2610        assert_eq!(
2611            q.pop_oldest_blocking(
2612                &cursor,
2613                melin_pipeline::wait::WaitStrategy::BusySpin,
2614                &abort
2615            ),
2616            None
2617        );
2618        assert!(!q.is_empty(), "aborted wait must not pop the entry");
2619        assert_eq!(
2620            q.pop_all_blocking(
2621                &cursor,
2622                melin_pipeline::wait::WaitStrategy::BusySpin,
2623                &abort
2624            ),
2625            None
2626        );
2627    }
2628
2629    /// `shutdown_pipeline` must surface the journal stage's error kind —
2630    /// the orchestrator routes `ReplicaChainDivergence` into in-process
2631    /// resync and everything else into process exit, so collapsing the
2632    /// error into a None (the old behavior) breaks that dispatch. It
2633    /// must also join EVERY thread in the failure arms: the resync path
2634    /// archives journal + snapshot right after teardown, and a
2635    /// still-running shadow thread mid-snapshot-write would race the
2636    /// renames.
2637    #[test]
2638    fn shutdown_pipeline_surfaces_journal_error_kind() {
2639        let flag = AtomicBool::new(false);
2640        let matching_joined = Arc::new(AtomicBool::new(false));
2641        let mj = Arc::clone(&matching_joined);
2642
2643        let journal = std::thread::spawn(|| -> Result<u32, melin_journal::JournalError> {
2644            Err(melin_journal::JournalError::ReplicaChainDivergence {
2645                sequence: 42,
2646                expected: [1u8; 32],
2647                actual: [2u8; 32],
2648            })
2649        });
2650        let matching = std::thread::spawn(move || {
2651            mj.store(true, Ordering::Release);
2652            7u64
2653        });
2654        let drain = std::thread::spawn(|| {});
2655
2656        let outcome = shutdown_pipeline::<u64, u32>(&flag, journal, matching, drain, None);
2657        assert!(flag.load(Ordering::Acquire), "shutdown flag must be set");
2658        assert!(
2659            matching_joined.load(Ordering::Acquire),
2660            "matching thread must be joined even when the journal failed"
2661        );
2662        match outcome {
2663            TeardownOutcome::JournalFailed(
2664                melin_journal::JournalError::ReplicaChainDivergence { sequence, .. },
2665            ) => assert_eq!(sequence, 42),
2666            _ => panic!("expected JournalFailed(ReplicaChainDivergence)"),
2667        }
2668    }
2669
2670    #[test]
2671    fn shutdown_pipeline_clean_returns_both_states() {
2672        let flag = AtomicBool::new(false);
2673        let journal =
2674            std::thread::spawn(|| -> Result<u32, melin_journal::JournalError> { Ok(11u32) });
2675        let matching = std::thread::spawn(|| 7u64);
2676        let drain = std::thread::spawn(|| {});
2677
2678        match shutdown_pipeline::<u64, u32>(&flag, journal, matching, drain, None) {
2679            TeardownOutcome::Clean(app, writer) => {
2680                assert_eq!(app, 7);
2681                assert_eq!(writer, 11);
2682            }
2683            _ => panic!("expected Clean"),
2684        }
2685    }
2686
2687    #[test]
2688    fn shutdown_pipeline_panicked_stage_reports_panicked() {
2689        let flag = AtomicBool::new(false);
2690        let journal =
2691            std::thread::spawn(|| -> Result<u32, melin_journal::JournalError> { Ok(11u32) });
2692        let matching = std::thread::spawn(|| -> u64 { panic!("matching stage died") });
2693        let drain = std::thread::spawn(|| {});
2694
2695        assert!(matches!(
2696            shutdown_pipeline::<u64, u32>(&flag, journal, matching, drain, None),
2697            TeardownOutcome::Panicked
2698        ));
2699    }
2700
2701    /// `ReplicaPipelineHandles` over a real input ring (one gate
2702    /// consumer) with immediately-returning stage threads — just enough
2703    /// structure for `teardown_replica_pipeline` to run for real. The
2704    /// returned consumer observes what teardown published.
2705    fn teardown_fixture(
2706        capacity: usize,
2707    ) -> (
2708        ReplicaPipelineHandles<counter_server::Counter, u32>,
2709        melin_pipeline::ring::Consumer<InputSlot>,
2710    ) {
2711        use melin_app::app_factory::AppFactory;
2712        let (input_producer, mut consumers) =
2713            melin_pipeline::ring::DisruptorBuilder::<InputSlot>::new(capacity)
2714                .add_consumer()
2715                .build(melin_pipeline::wait::WaitStrategy::SpinThenYield);
2716        let consumer = consumers.pop().expect("one consumer");
2717        let handles = ReplicaPipelineHandles {
2718            input_producer,
2719            journal_cursor: Arc::new(make_journal_cursor(0)),
2720            last_seq: melin_transport_core::DurableWireSeqCursor::detached(
2721                melin_transport_core::WireSeq::new(0),
2722            ),
2723            chain_hash_lock: None,
2724            stream_marks: Arc::new(std::sync::Mutex::new(std::collections::VecDeque::new())),
2725            journal_failed: Arc::new(AtomicBool::new(false)),
2726            pipeline_shutdown: Arc::new(AtomicBool::new(false)),
2727            journal_handle: std::thread::spawn(|| -> Result<u32, melin_journal::JournalError> {
2728                Ok(11)
2729            }),
2730            matching_handle: std::thread::spawn(|| counter_server::CounterFactory.empty()),
2731            drain_handle: std::thread::spawn(|| {}),
2732            shadow_handle: None,
2733        };
2734        (handles, consumer)
2735    }
2736
2737    /// The invariant behind moving the sentinel publish into
2738    /// `teardown_replica_pipeline`: a healthy teardown always puts the
2739    /// shutdown sentinel on the ring, so the stages exit by consuming it
2740    /// through the normal event path (draining anything ahead of it),
2741    /// not via the flag's emergency-abort branch.
2742    #[test]
2743    fn teardown_publishes_shutdown_sentinel() {
2744        let (handles, mut consumer) = teardown_fixture(8);
2745        assert!(matches!(
2746            teardown_replica_pipeline::<counter_server::Counter, u32>(handles),
2747            TeardownOutcome::Clean(..)
2748        ));
2749        let (_, slot) = consumer.try_consume().expect("sentinel on the ring");
2750        assert!(
2751            slot.event.is_shutdown(),
2752            "published slot must be the sentinel"
2753        );
2754        assert!(consumer.try_consume().is_none(), "exactly one sentinel");
2755    }
2756
2757    /// Drive `handle_session_exit` through one `Disconnected` exit with
2758    /// the given liveness flag and a backoff pre-escalated to
2759    /// `MAX_BACKOFF`, returning the post-exit backoff. Shutdown is
2760    /// latched so the backoff sleep returns immediately (the receiver's
2761    /// loop top would handle it on the next turn); the journal, factory
2762    /// and fence arguments are inert on the `Disconnected` path.
2763    fn backoff_after_disconnect(heard_from_primary: bool) -> std::time::Duration {
2764        let dir = tempfile::tempdir().expect("tempdir");
2765        type Writer = melin_journal::BufferedWriter<CounterEvent>;
2766        let mut pipeline: Option<ReplicaPipelineHandles<counter_server::Counter, Writer>> = None;
2767        let mut divergence_resyncs = 0u32;
2768        let mut backoff = MAX_BACKOFF;
2769        let shutdown = AtomicBool::new(true);
2770        let promote = crate::promotion::PromotionRequest::new();
2771        let after = handle_session_exit::<counter_server::Counter, Writer>(
2772            StreamingResult {
2773                exit: SessionExit::Disconnected,
2774                heard_from_primary,
2775            },
2776            &mut pipeline,
2777            &mut divergence_resyncs,
2778            &mut backoff,
2779            0,
2780            &dir.path().join("r.journal"),
2781            &dir.path().join("r.snapshot"),
2782            &counter_server::CounterFactory,
2783            &melin_transport_core::fence::FenceState::new(0),
2784            &shutdown,
2785            &promote,
2786            || {},
2787        );
2788        assert!(matches!(after, AfterSession::Reconnect));
2789        backoff
2790    }
2791
2792    /// A session in which the primary spoke — even heartbeat-only on a
2793    /// quiet system — must reset the disconnect backoff. A stale
2794    /// escalated backoff delays primary-link recovery, and with it the
2795    /// auto-promotion veto, by up to MAX_BACKOFF.
2796    #[test]
2797    fn disconnect_resets_backoff_when_primary_spoke() {
2798        // Reset to 1s, then doubled for the next attempt by the shared
2799        // helper — NOT stuck at MAX_BACKOFF.
2800        assert_eq!(
2801            backoff_after_disconnect(true),
2802            std::time::Duration::from_secs(2)
2803        );
2804    }
2805
2806    /// A session with no word from the primary — an instant-drop flap,
2807    /// or the synthetic result of a session that never started
2808    /// streaming (local transport init failure) — must keep its
2809    /// escalated backoff rather than redialing at 1 Hz forever.
2810    #[test]
2811    fn disconnect_keeps_escalated_backoff_when_primary_silent() {
2812        assert_eq!(backoff_after_disconnect(false), MAX_BACKOFF);
2813    }
2814
2815    /// A stream contiguity break is a reconnect, never a process exit:
2816    /// the contiguous prefix is committed and the journal is intact, so
2817    /// the receiver re-handshakes from its durable position and the
2818    /// primary re-streams the hole. It used to fall through the `Fatal`
2819    /// arm and take the replica down for a documented-benign primary
2820    /// handoff corner. The pipeline must be left standing (it is what
2821    /// the reconnect resumes into) and the backoff reset — the primary
2822    /// was evidently speaking.
2823    #[test]
2824    fn stream_gap_reconnects_and_keeps_pipeline() {
2825        use melin_app::app_factory::AppFactory;
2826        let dir = tempfile::tempdir().expect("tempdir");
2827        type Writer = melin_journal::BufferedWriter<CounterEvent>;
2828        // Same shape as `teardown_fixture`, but with a real writer type:
2829        // `handle_session_exit` is bounded on `JournalWrite` for the
2830        // resync arm even though this path never touches it.
2831        let (input_producer, mut consumers) =
2832            melin_pipeline::ring::DisruptorBuilder::<InputSlot>::new(4)
2833                .add_consumer()
2834                .build(melin_pipeline::wait::WaitStrategy::SpinThenYield);
2835        let _consumer = consumers.pop().expect("one consumer");
2836        let writer_path = dir.path().join("w.journal");
2837        let handles = ReplicaPipelineHandles {
2838            input_producer,
2839            journal_cursor: Arc::new(make_journal_cursor(0)),
2840            last_seq: melin_transport_core::DurableWireSeqCursor::detached(
2841                melin_transport_core::WireSeq::new(0),
2842            ),
2843            chain_hash_lock: None,
2844            stream_marks: Arc::new(std::sync::Mutex::new(std::collections::VecDeque::new())),
2845            journal_failed: Arc::new(AtomicBool::new(false)),
2846            pipeline_shutdown: Arc::new(AtomicBool::new(false)),
2847            journal_handle: std::thread::spawn(move || Writer::create(&writer_path)),
2848            matching_handle: std::thread::spawn(|| counter_server::CounterFactory.empty()),
2849            drain_handle: std::thread::spawn(|| {}),
2850            shadow_handle: None,
2851        };
2852        let mut pipeline: Option<ReplicaPipelineHandles<counter_server::Counter, Writer>> =
2853            Some(handles);
2854        let mut divergence_resyncs = 0u32;
2855        let mut backoff = MAX_BACKOFF;
2856        let shutdown = AtomicBool::new(true);
2857        let promote = crate::promotion::PromotionRequest::new();
2858        let mut closed = false;
2859        let after = handle_session_exit::<counter_server::Counter, Writer>(
2860            StreamingResult {
2861                exit: SessionExit::StreamGap("sequence gap: expected 3, got 5".into()),
2862                heard_from_primary: true,
2863            },
2864            &mut pipeline,
2865            &mut divergence_resyncs,
2866            &mut backoff,
2867            2,
2868            &dir.path().join("r.journal"),
2869            &dir.path().join("r.snapshot"),
2870            &counter_server::CounterFactory,
2871            &melin_transport_core::fence::FenceState::new(0),
2872            &shutdown,
2873            &promote,
2874            || closed = true,
2875        );
2876        assert!(
2877            matches!(after, AfterSession::Reconnect),
2878            "a stream gap must reconnect, not return an error"
2879        );
2880        assert!(
2881            pipeline.is_some(),
2882            "the pipeline must survive for the reconnect"
2883        );
2884        assert!(closed, "the transport must be closed before redialing");
2885        assert_eq!(
2886            backoff,
2887            std::time::Duration::from_secs(2),
2888            "backoff reset to 1s then doubled by the shared helper"
2889        );
2890        assert_eq!(divergence_resyncs, 0, "not a resync");
2891        if let Some(p) = pipeline.take() {
2892            let _ = teardown_replica_pipeline::<counter_server::Counter, Writer>(p);
2893        }
2894    }
2895
2896    /// With the journal-failure latch set and the ring full — the state
2897    /// a dead journal stage leaves behind (its frozen gate cursor means
2898    /// the ring never drains) — teardown must skip the sentinel and
2899    /// return, instead of spinning forever on ring backpressure.
2900    #[test]
2901    fn teardown_skips_sentinel_when_journal_failed_and_ring_full() {
2902        let (mut handles, mut consumer) = teardown_fixture(4);
2903        for seq in 0..4 {
2904            assert!(
2905                handles
2906                    .input_producer
2907                    .try_publish(InputSlot {
2908                        sequence: seq,
2909                        ..InputSlot::default()
2910                    })
2911                    .is_ok(),
2912                "ring must have space while filling"
2913            );
2914        }
2915        assert!(
2916            handles
2917                .input_producer
2918                .try_publish(InputSlot::default())
2919                .is_err(),
2920            "ring must be full"
2921        );
2922        handles.journal_failed.store(true, Ordering::Release);
2923        // Would spin forever on the frozen gate without the latch check.
2924        assert!(matches!(
2925            teardown_replica_pipeline::<counter_server::Counter, u32>(handles),
2926            TeardownOutcome::Clean(..)
2927        ));
2928        while let Some((_, slot)) = consumer.try_consume() {
2929            assert!(!slot.event.is_shutdown(), "no sentinel may be published");
2930        }
2931    }
2932
2933    #[test]
2934    fn pending_ack_queue_wraps_around() {
2935        let mut q = PendingAckQueue::new(8);
2936        let cursor = make_journal_cursor(100);
2937
2938        // Fill and drain multiple times to exercise circular buffer wrap.
2939        for round in 0..3 {
2940            for i in 0..8 {
2941                let target = (round * 8 + i) as u64 + 1;
2942                q.push(target, target * 10);
2943            }
2944            assert!(q.is_full());
2945            let seq = q.pop_ready(&cursor).expect("should be ready");
2946            assert_eq!(seq, (round * 8 + 8) as u64 * 10);
2947            assert!(q.is_empty());
2948        }
2949    }
2950
2951    #[test]
2952    fn pending_ack_queue_pop_all_blocking_empty() {
2953        let mut q = PendingAckQueue::new(8);
2954        let cursor = make_journal_cursor(0);
2955        assert!(
2956            q.pop_all_blocking(
2957                &cursor,
2958                melin_pipeline::wait::WaitStrategy::BusySpin,
2959                &AtomicBool::new(false)
2960            )
2961            .is_none()
2962        );
2963    }
2964
2965    // --- try_flush_dual_track tests ---
2966
2967    #[test]
2968    fn dual_track_returns_none_when_both_tracks_idle() {
2969        let mut q = PendingAckQueue::new(8);
2970        let cursor = make_journal_cursor(0);
2971        assert!(
2972            try_flush_dual_track(&mut q, &cursor, 0, 0, 0).is_none(),
2973            "no advance on either track → no ack"
2974        );
2975    }
2976
2977    #[test]
2978    fn dual_track_fires_on_persisted_advance_only() {
2979        // Persisted track moves: pop_ready returns 100; in-memory
2980        // stayed at 50 (last_sent). Expect ack with acked=100,
2981        // in_memory=50 (no regression — the wire field carries the
2982        // current sample, not a "no change" marker).
2983        let mut q = PendingAckQueue::new(8);
2984        q.push(10, 100);
2985        let cursor = make_journal_cursor(20);
2986        let ack = try_flush_dual_track(&mut q, &cursor, 50, 0, 50).expect("persisted advanced");
2987        assert_eq!(ack.acked_sequence, 100);
2988        assert_eq!(ack.in_memory_sequence, 50);
2989    }
2990
2991    #[test]
2992    fn dual_track_fires_on_in_memory_advance_only() {
2993        // Persisted is idle (queue empty): unwrap_or keeps the
2994        // last-sent acked at 100. In-memory bumped from 100 to 200.
2995        let mut q = PendingAckQueue::new(8);
2996        let cursor = make_journal_cursor(0);
2997        let ack = try_flush_dual_track(&mut q, &cursor, 200, 100, 100).expect("in-memory advanced");
2998        assert_eq!(ack.acked_sequence, 100);
2999        assert_eq!(ack.in_memory_sequence, 200);
3000    }
3001
3002    #[test]
3003    fn dual_track_coalesces_until_caller_updates_trackers() {
3004        // Caller did not advance trackers between calls. The queue
3005        // popped on call 1; on call 2 it's empty so persisted stays
3006        // at 100 (unwrap_or). In-memory advanced 50 → 80 between
3007        // calls. Second call must still fire because tracker is
3008        // still 50.
3009        let mut q = PendingAckQueue::new(8);
3010        q.push(10, 100);
3011        let cursor = make_journal_cursor(20);
3012        let ack1 = try_flush_dual_track(&mut q, &cursor, 50, 0, 50).expect("call 1 fires");
3013        assert_eq!(ack1.acked_sequence, 100);
3014        // Caller "forgot" to update trackers (simulates send failure).
3015        let ack2 = try_flush_dual_track(&mut q, &cursor, 80, 0, 50)
3016            .expect("call 2 fires on in-memory advance");
3017        assert_eq!(
3018            ack2.acked_sequence, 0,
3019            "no new persisted pop → unwrap_or(last_sent)"
3020        );
3021        assert_eq!(ack2.in_memory_sequence, 80);
3022    }
3023
3024    #[test]
3025    fn dual_track_no_duplicate_ack_after_backpressure_drain() {
3026        // Models the backpressure-drain → resume-normal-flush path
3027        // taken by every receiver when `PendingAckQueue` fills up.
3028        // The receiver must drain the queue (via the
3029        // pop_oldest_blocking path in production; pop_ready here),
3030        // update *both* trackers from the drained ack, and only
3031        // then resume the normal cursor-driven flush. The bug
3032        // classes this pins are:
3033        //   (a) emitting a follow-up ack whose `acked_sequence`
3034        //       regresses below what the drain already sent
3035        //       (caught by the debug_assert! in
3036        //       `try_flush_dual_track`, also asserted at value
3037        //       level here);
3038        //   (b) emitting a duplicate ack carrying the same
3039        //       cursors as the drain when neither track actually
3040        //       advanced — the leak class fixed by ensuring every
3041        //       send site updates both `last_sent_acked_seq` and
3042        //       `last_sent_in_memory_seq`.
3043        let mut q = PendingAckQueue::new(4);
3044        // Fill the queue: four pending acks at primary seqs
3045        // 100, 200, 300, 400 with journal targets 10..=40.
3046        for i in 1..=4u64 {
3047            q.push(i * 10, i * 100);
3048        }
3049        assert!(q.is_full());
3050
3051        // Backpressure path: caller drains all ready entries
3052        // before pushing more. Highest acked seen = 400.
3053        let cursor = make_journal_cursor(40);
3054        let drained = q.pop_ready(&cursor).expect("all entries durable");
3055        assert_eq!(drained, 400);
3056        assert!(q.is_empty());
3057
3058        // Caller updates BOTH trackers from the drain. in_memory
3059        // at the time of the drained batch was 450.
3060        let mut last_sent_acked = drained;
3061        let mut last_sent_in_mem = 450u64;
3062
3063        // Quiescent immediately after drain: no fresh push, no
3064        // in-memory advance → try_flush must return None.
3065        // Failing here means we'd emit a duplicate ack carrying
3066        // the same (400, 450) the backpressure drain already sent.
3067        assert!(
3068            try_flush_dual_track(
3069                &mut q,
3070                &cursor,
3071                last_sent_in_mem,
3072                last_sent_acked,
3073                last_sent_in_mem,
3074            )
3075            .is_none(),
3076            "no advance on either track after backpressure drain → no duplicate ack",
3077        );
3078
3079        // Push a fresh batch (primary seq 500, journal target 50),
3080        // cursor catches up, in_memory advances to 500.
3081        q.push(50, 500);
3082        let cursor2 = make_journal_cursor(50);
3083        let ack = try_flush_dual_track(&mut q, &cursor2, 500, last_sent_acked, last_sent_in_mem)
3084            .expect("fresh batch after drain fires a new ack");
3085        assert!(
3086            ack.acked_sequence >= last_sent_acked,
3087            "regression: drain sent {last_sent_acked} but next ack carries {}",
3088            ack.acked_sequence,
3089        );
3090        assert_eq!(ack.acked_sequence, 500);
3091        assert_eq!(ack.in_memory_sequence, 500);
3092
3093        last_sent_acked = ack.acked_sequence;
3094        last_sent_in_mem = ack.in_memory_sequence;
3095
3096        // Post-resume quiescent: confirms idempotency past the
3097        // resume point — the second flush sees neither track
3098        // advance and must stay silent.
3099        assert!(
3100            try_flush_dual_track(
3101                &mut q,
3102                &cursor2,
3103                last_sent_in_mem,
3104                last_sent_acked,
3105                last_sent_in_mem,
3106            )
3107            .is_none(),
3108            "post-resume idle → no further ack",
3109        );
3110    }
3111}