use std::sync::Arc;
#[cfg(test)]
use std::sync::atomic::AtomicU64;
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use melin_journal::{BufferedWriter, JournalWrite};
use melin_app::Application;
use melin_transport_core::pipeline::{InputSlot, OutputSlot};
use melin_transport_core::replication::archive::{ArchiveReason, archive_local_lineage};
use melin_transport_core::replication::protocol::{MAX_CONTROL_FRAME, decode_primary_message};
mod auth;
#[cfg(feature = "dpdk")]
mod dpdk;
mod receiver_transport;
mod tcp_receiver;
mod tcp_sender;
#[cfg(any(feature = "dpdk", test))]
mod validation_worker;
use receiver_transport::{ControlFrameSource, SessionExit, StreamingResult, receive_chunked_body};
pub(crate) struct ReplicaGate<'a> {
count: &'a AtomicU32,
}
impl<'a> ReplicaGate<'a> {
pub(crate) fn new(count: &'a AtomicU32) -> Self {
Self { count }
}
pub(crate) fn lift(&self) {
self.count.fetch_add(1, Ordering::Release);
}
pub(crate) fn lower(&self) -> bool {
let was_last = self.count.fetch_sub(1, Ordering::Release) == 1;
if was_last {
tracing::warn!("all replicas disconnected — trading halted");
}
was_last
}
}
pub use melin_transport_core::replication::ack_queue::{
PendingAck, PendingAckQueue, try_flush_dual_track, wait_for_journal_cursor,
};
pub use melin_transport_core::replication::protocol::{
Ack, Handshake, PrimaryMessage, ReplicaMessage,
};
pub use melin_transport_core::replication::{ReplicaCursors, ReplicationMetrics, SentHighWater};
#[cfg(feature = "dpdk")]
pub(crate) use dpdk::REPL_DISPATCH_BURST;
#[cfg(feature = "dpdk")]
pub use dpdk::{DpdkReplicationDriver, run_receiver_dpdk};
pub use tcp_receiver::{ReceiverResult, run_receiver};
pub use tcp_sender::{ReplicationListener, Sender, run_sender};
#[derive(Clone)]
pub struct ReplicaControlPlane {
pub promote: crate::promotion::PromotionRequest,
pub tip_ready: std::sync::Arc<AtomicBool>,
pub journal_tip: melin_transport_core::AdvertisedJournalTip,
pub primary_link_up: std::sync::Arc<AtomicBool>,
pub primary_ack_policy: std::sync::Arc<std::sync::atomic::AtomicU8>,
pub pipeline_healthy: std::sync::Arc<AtomicBool>,
}
impl ReplicaControlPlane {
pub fn new() -> Self {
Self {
promote: crate::promotion::PromotionRequest::new(),
tip_ready: std::sync::Arc::new(AtomicBool::new(false)),
journal_tip: melin_transport_core::AdvertisedJournalTip::new(
melin_transport_core::WireSeq::new(0),
),
primary_link_up: std::sync::Arc::new(AtomicBool::new(false)),
primary_ack_policy: std::sync::Arc::new(std::sync::atomic::AtomicU8::new(
crate::ack_policy::ACK_POLICY_UNKNOWN,
)),
pipeline_healthy: std::sync::Arc::new(AtomicBool::new(true)),
}
}
}
impl Default for ReplicaControlPlane {
fn default() -> Self {
Self::new()
}
}
pub(super) fn log_tcp_info(fd: std::os::unix::io::RawFd, tag: &str, slot: usize) {
if !tracing::enabled!(tracing::Level::DEBUG) {
return;
}
let mut info: libc::tcp_info = unsafe { std::mem::zeroed() };
let mut len = std::mem::size_of::<libc::tcp_info>() as libc::socklen_t;
let rc = unsafe {
libc::getsockopt(
fd,
libc::IPPROTO_TCP,
libc::TCP_INFO,
&mut info as *mut _ as *mut libc::c_void,
&mut len,
)
};
if rc != 0 {
tracing::debug!(
slot,
tag,
errno = std::io::Error::last_os_error().raw_os_error().unwrap_or(0),
"tcp_info getsockopt failed"
);
return;
}
tracing::debug!(
slot,
tag,
state = info.tcpi_state,
ca_state = info.tcpi_ca_state,
rtt_us = info.tcpi_rtt,
rttvar_us = info.tcpi_rttvar,
snd_cwnd = info.tcpi_snd_cwnd,
snd_ssthresh = info.tcpi_snd_ssthresh,
snd_mss = info.tcpi_snd_mss,
rcv_mss = info.tcpi_rcv_mss,
unacked = info.tcpi_unacked,
retrans = info.tcpi_retrans,
total_retrans = info.tcpi_total_retrans,
lost = info.tcpi_lost,
rcv_space = info.tcpi_rcv_space,
rto_us = info.tcpi_rto,
"tcp_info"
);
}
pub(super) fn sleep_checking_flags(
duration: std::time::Duration,
shutdown: &AtomicBool,
promote: &crate::promotion::PromotionRequest,
) {
let deadline = std::time::Instant::now() + duration;
while std::time::Instant::now() < deadline {
if shutdown.load(Ordering::Relaxed) || promote.is_requested() {
return;
}
std::thread::sleep(std::time::Duration::from_millis(100));
}
}
pub(super) fn sleep_then_double_backoff(
backoff: &mut std::time::Duration,
shutdown: &AtomicBool,
promote: &crate::promotion::PromotionRequest,
) {
sleep_checking_flags(*backoff, shutdown, promote);
*backoff = (*backoff * 2).min(MAX_BACKOFF);
}
pub(super) enum TeardownOutcome<A, W> {
Clean(A, W),
JournalFailed(melin_journal::JournalError),
Panicked,
}
pub(super) fn shutdown_pipeline<A: Send + 'static, W: Send + 'static>(
shutdown_flag: &AtomicBool,
journal_handle: std::thread::JoinHandle<Result<W, melin_journal::JournalError>>,
matching_handle: std::thread::JoinHandle<A>,
drain_handle: std::thread::JoinHandle<()>,
shadow_handle: Option<std::thread::JoinHandle<()>>,
) -> TeardownOutcome<A, W> {
shutdown_flag.store(true, Ordering::Release);
let journal_result = journal_handle.join();
let matching_result = matching_handle.join();
let _ = drain_handle.join();
if let Some(h) = shadow_handle {
let _ = h.join();
}
let writer = match journal_result {
Ok(Ok(w)) => w,
Ok(Err(e)) => return TeardownOutcome::JournalFailed(e),
Err(_) => return TeardownOutcome::Panicked,
};
match matching_result {
Ok(exchange) => TeardownOutcome::Clean(exchange, writer),
Err(_) => TeardownOutcome::Panicked,
}
}
pub(super) struct ReplicaPipelineHandles<A: Application, W: Send + 'static> {
pub(super) input_producer: melin_pipeline::ring::Producer<InputSlot<A::Event>>,
pub(super) journal_cursor: Arc<melin_pipeline::padding::Sequence>,
pub(super) last_seq: melin_transport_core::DurableWireSeqCursor,
pub(super) chain_hash_lock:
Option<melin_pipeline::seqlock::SeqLockReader<melin_transport_core::pipeline::FsyncState>>,
pub(super) stream_marks: melin_transport_core::pipeline::StreamMarkQueue,
pub(super) journal_failed: Arc<AtomicBool>,
pub(super) pipeline_shutdown: Arc<AtomicBool>,
pub(super) journal_handle: std::thread::JoinHandle<Result<W, melin_journal::JournalError>>,
pub(super) matching_handle: std::thread::JoinHandle<A>,
pub(super) drain_handle: std::thread::JoinHandle<()>,
pub(super) shadow_handle: Option<std::thread::JoinHandle<()>>,
}
pub(super) type ReplicaHandles<A> =
ReplicaPipelineHandles<A, BufferedWriter<<A as Application>::Event>>;
#[allow(clippy::too_many_arguments)]
pub(super) fn build_replica_pipeline_with_threads<A>(
exchange: A,
writer: BufferedWriter<A::Event>,
cores: crate::server::PipelineCores,
snapshot_interval_ms: u64,
snapshot_path: std::path::PathBuf,
group_commit_delay: std::time::Duration,
busy_spin: bool,
fence_state: Arc<melin_transport_core::fence::FenceState>,
pipeline_healthy: Arc<AtomicBool>,
) -> Result<ReplicaHandles<A>, Box<dyn std::error::Error>>
where
A: Application + Send + 'static,
A::Event: Send + Sync + 'static,
A::Report: Send + 'static,
A::QueryResponse: Send + 'static,
{
let shadow_exchange = <A as Application>::clone_via_snapshot(&exchange)?;
let enable_shadow = snapshot_interval_ms > 0;
let shadow_initial_epoch = fence_state.epoch();
let pipeline = melin_transport_core::pipeline::build_replica_pipeline(
exchange,
writer,
4096, group_commit_delay,
busy_spin,
enable_shadow,
fence_state,
);
let pipeline_shutdown = Arc::new(AtomicBool::new(false));
let ps = Arc::clone(&pipeline_shutdown);
let journal_core = cores.journal;
let mut journal_stage = pipeline.journal_stage;
let stream_marks: melin_transport_core::pipeline::StreamMarkQueue =
Arc::new(std::sync::Mutex::new(std::collections::VecDeque::new()));
journal_stage.set_stream_marks(Arc::clone(&stream_marks));
journal_stage.set_preparer_core(cores.journal_prep);
journal_stage.set_disk_core(cores.journal_disk);
let journal_failed = Arc::new(AtomicBool::new(false));
let journal_failed_latch = Arc::clone(&journal_failed);
pipeline_healthy.store(true, Ordering::Release);
let journal_handle = std::thread::Builder::new()
.name("journal".into())
.spawn(move || {
melin_app::affinity::pin_thread("journal", journal_core);
let result = journal_stage.run(&ps);
if let Err(ref e) = result {
journal_failed_latch.store(true, Ordering::Release);
pipeline_healthy.store(false, Ordering::Release);
tracing::error!(error = %e, "replica journal stage failed — session teardown");
}
result
})
.expect("spawn journal thread");
let ps = Arc::clone(&pipeline_shutdown);
let matching_core = cores.matching;
let matching_stage = pipeline.matching_stage;
let matching_handle = std::thread::Builder::new()
.name("matching".into())
.spawn(move || {
melin_app::affinity::pin_thread("matching", matching_core);
matching_stage.run(&ps)
})
.expect("spawn matching thread");
let ps = Arc::clone(&pipeline_shutdown);
let drain_core = cores.response;
let drain_consumer = pipeline.drain_consumer;
let drain_handle = std::thread::Builder::new()
.name("drain".into())
.spawn(move || {
melin_app::affinity::pin_thread("drain", drain_core);
let mut consumer = drain_consumer;
let mut batch = vec![OutputSlot::<A::Report, A::QueryResponse>::default(); 256];
loop {
if ps.load(Ordering::Relaxed) {
return;
}
let count = consumer.consume_batch(&mut batch, 256);
if count == 0 {
if busy_spin {
std::hint::spin_loop();
} else {
std::thread::yield_now();
}
}
}
})
.expect("spawn drain thread");
let shadow_handle = if let Some(shadow_cons) = pipeline.shadow_consumer {
let snap_path = snapshot_path;
let chain_lock = pipeline
.chain_hash_lock
.as_ref()
.expect("chain hash lock with shadow")
.clone();
let ps = Arc::clone(&pipeline_shutdown);
let shadow_core = cores.shadow;
Some(
std::thread::Builder::new()
.name("replica-shadow".into())
.spawn(move || {
melin_app::affinity::pin_thread("replica-shadow", shadow_core);
melin_transport_core::shadow::run(
shadow_cons,
shadow_exchange,
snap_path,
std::time::Duration::from_millis(snapshot_interval_ms),
chain_lock,
&ps,
false,
shadow_initial_epoch,
);
})
.expect("spawn shadow thread"),
)
} else {
None
};
Ok(ReplicaPipelineHandles {
input_producer: pipeline.input_producer,
journal_cursor: pipeline.cursors.journal_ring_arc(),
last_seq: pipeline.cursors.durable_wire_seq(),
chain_hash_lock: pipeline.chain_hash_lock,
stream_marks,
journal_failed,
pipeline_shutdown,
journal_handle,
matching_handle,
drain_handle,
shadow_handle,
})
}
pub(super) fn teardown_replica_pipeline<A: Application + Send + 'static, W: Send + 'static>(
mut handles: ReplicaPipelineHandles<A, W>,
) -> TeardownOutcome<A, W> {
while !handles.journal_failed.load(Ordering::Acquire) {
match handles
.input_producer
.try_publish(InputSlot::<A::Event>::shutdown_sentinel())
{
Ok(_) => break,
Err(_) => std::thread::yield_now(),
}
}
shutdown_pipeline::<A, W>(
&handles.pipeline_shutdown,
handles.journal_handle,
handles.matching_handle,
handles.drain_handle,
handles.shadow_handle,
)
}
pub(super) const MAX_INPROCESS_DIVERGENCE_RESYNCS: u32 = 1;
#[allow(clippy::type_complexity)]
pub(super) fn recover_replica_state<A, W>(
journal_path: &std::path::Path,
snapshot_path: &std::path::Path,
factory: &dyn melin_app::app_factory::AppFactory<App = A>,
fence_state: &melin_transport_core::fence::FenceState,
) -> Result<(Option<A>, Option<W>, u64, [u8; 32]), Box<dyn std::error::Error>>
where
A: Application,
W: melin_journal::JournalWrite<A::Event>,
{
let lineage_exists =
journal_path.exists() || !melin_journal::segment::list_archives(journal_path)?.is_empty();
if !lineage_exists {
return Ok((None, None, 0u64, [0u8; 32]));
}
let engine = if snapshot_path.exists() {
tracing::info!("recovering replica from snapshot + journal");
melin_transport_core::JournaledApp::<A, W>::recover_from_snapshot(
snapshot_path,
journal_path,
)?
} else {
melin_transport_core::JournaledApp::<A, W>::recover(factory.empty(), journal_path)?
};
let next = engine.next_sequence();
let last = next.saturating_sub(1);
let hash = engine.chain_hash().unwrap_or([0u8; 32]);
fence_state.observe_epoch(engine.recovered_epoch());
let (mut exchange, writer) = engine.into_parts();
factory.apply_operator_policy(&mut exchange);
Ok((Some(exchange), Some(writer), last, hash))
}
pub(super) const MAX_BACKOFF: std::time::Duration = std::time::Duration::from_secs(30);
pub(in crate::replication) enum AfterSession<A, W> {
Return(ReceiverResult<A, W>),
Resync {
exchange: Option<A>,
journal_writer: Option<W>,
last_sequence: u64,
chain_hash: [u8; 32],
},
Reconnect,
}
#[allow(clippy::too_many_arguments)]
pub(in crate::replication) fn handle_session_exit<A, W>(
result: StreamingResult,
pipeline: &mut Option<ReplicaPipelineHandles<A, W>>,
divergence_resyncs: &mut u32,
backoff: &mut std::time::Duration,
last_sequence: u64,
journal_path: &std::path::Path,
snapshot_path: &std::path::Path,
factory: &dyn melin_app::app_factory::AppFactory<App = A>,
fence_state: &melin_transport_core::fence::FenceState,
shutdown: &AtomicBool,
promote: &crate::promotion::PromotionRequest,
mut close: impl FnMut(),
) -> AfterSession<A, W>
where
A: Application + Send + 'static,
W: JournalWrite<A::Event> + Send + 'static,
{
let StreamingResult {
exit,
heard_from_primary,
} = result;
match exit {
SessionExit::Shutdown => {
if let Some(p) = pipeline.take() {
let _ = teardown_replica_pipeline::<A, W>(p);
}
AfterSession::Return(Ok(None))
}
SessionExit::Promote => AfterSession::Return(match pipeline.take() {
Some(p) => match teardown_replica_pipeline::<A, W>(p) {
TeardownOutcome::Clean(ex, wr) => Ok(Some((ex, wr))),
_ => Err("pipeline failed during promotion".into()),
},
None => Err("pipeline missing on promote".into()),
}),
SessionExit::Fatal(e) => {
let outcome = match pipeline.take() {
Some(p) => teardown_replica_pipeline::<A, W>(p),
None => return AfterSession::Return(Err(e)),
};
let TeardownOutcome::JournalFailed(
je @ melin_journal::JournalError::ReplicaChainDivergence { .. },
) = outcome
else {
return AfterSession::Return(Err(e));
};
*divergence_resyncs += 1;
let attempt = *divergence_resyncs;
if attempt > MAX_INPROCESS_DIVERGENCE_RESYNCS {
return AfterSession::Return(Err(format!(
"mid-stream chain divergence recurred {attempt} times — giving up on \
in-process resync (each cycle archives the local journal and re-seeds \
from the primary; recurrence at this rate means the primary keeps \
streaming history that forks from what it announces): {je}"
)
.into()));
}
tracing::warn!(
error = %je,
attempt,
max_attempts = MAX_INPROCESS_DIVERGENCE_RESYNCS,
"mid-stream chain divergence — re-deriving local state for in-process resync"
);
close();
match recover_replica_state::<A, W>(journal_path, snapshot_path, factory, fence_state) {
Ok((exchange, journal_writer, seq, hash)) => AfterSession::Resync {
exchange,
journal_writer,
last_sequence: seq,
chain_hash: hash,
},
Err(e) => AfterSession::Return(Err(e)),
}
}
SessionExit::StreamGap(e) => {
close();
*backoff = std::time::Duration::from_secs(1);
tracing::warn!(
error = %e,
last_sequence,
"replication stream broke contiguity — reconnecting from the durable position"
);
sleep_then_double_backoff(backoff, shutdown, promote);
AfterSession::Reconnect
}
SessionExit::Disconnected => {
close();
if heard_from_primary {
*backoff = std::time::Duration::from_secs(1);
}
tracing::warn!(
last_sequence,
heard_from_primary,
backoff_secs = backoff.as_secs(),
"reconnecting to primary"
);
sleep_then_double_backoff(backoff, shutdown, promote);
AfterSession::Reconnect
}
}
}
pub(in crate::replication) fn take_pipeline_for_promotion<A, W>(
pipeline: &mut Option<ReplicaPipelineHandles<A, W>>,
exchange: &mut Option<A>,
journal_writer: &mut Option<W>,
) -> ReceiverResult<A, W>
where
A: Application + Send + 'static,
W: JournalWrite<A::Event> + Send + 'static,
{
if let Some(p) = pipeline.take()
&& let TeardownOutcome::Clean(e, w) = teardown_replica_pipeline::<A, W>(p)
{
*exchange = Some(e);
*journal_writer = Some(w);
}
match (exchange.take(), journal_writer.take()) {
(Some(e), Some(w)) => Ok(Some((e, w))),
_ => Err("promotion requested but no local state available".into()),
}
}
type ResyncTransfer<A> = (A, u64, [u8; 32], u64);
pub(in crate::replication) enum ResyncDecision {
Ready {
segment_start_sequence: u64,
anchor_hash: [u8; 32],
resume_sequence: u64,
},
Retry,
}
fn receive_resync_transfer<A, S>(
source: &mut S,
snapshot_path: &std::path::Path,
journal_path: &std::path::Path,
fence_state: &melin_transport_core::fence::FenceState,
) -> Result<ResyncTransfer<A>, Box<dyn std::error::Error + Send + Sync>>
where
A: Application,
S: ControlFrameSource,
{
let (snap_len, snap_sequence, snap_chain_hash) =
match decode_primary_message(&source.next_frame(MAX_CONTROL_FRAME)?)? {
PrimaryMessage::SnapshotBegin {
snapshot_len,
snap_sequence,
snap_chain_hash,
} => (snapshot_len, snap_sequence, snap_chain_hash),
other => return Err(format!("expected SnapshotBegin, got {other:?}").into()),
};
tracing::info!(snap_sequence, snap_len, "receiving snapshot");
let tmp_path = snapshot_path.with_extension("snapshot.tmp");
receive_chunked_body(source, &tmp_path, snap_len, "snapshot")?;
std::fs::rename(&tmp_path, snapshot_path)?;
tracing::info!(snap_sequence, snap_len, "snapshot received and verified");
let (snap_exchange, _snap_seq, snap_hash, snap_epoch) =
melin_transport_core::snapshot::load::<A>(snapshot_path)?;
if snap_hash != snap_chain_hash {
return Err(format!(
"snapshot chain hash mismatch: primary sent {snap_chain_hash:02x?}, \
loaded snapshot has {snap_hash:02x?}"
)
.into());
}
fence_state.observe_epoch(snap_epoch);
let seed_len = match decode_primary_message(&source.next_frame(MAX_CONTROL_FRAME)?)? {
PrimaryMessage::SegmentSeedBegin { seed_len } => seed_len,
other => {
return Err(format!("expected SegmentSeedBegin after snapshot, got {other:?}").into());
}
};
let seed_tmp = journal_path.with_extension("seed.tmp");
receive_chunked_body(source, &seed_tmp, seed_len, "segment seed")?;
if let Err(e) =
melin_journal::segment::verify_segment_prefix(&seed_tmp, snap_sequence, seed_len)
{
let _ = std::fs::remove_file(&seed_tmp);
return Err(format!("segment seed failed structural verification: {e}").into());
}
std::fs::rename(&seed_tmp, journal_path)?;
melin_journal::segment::fsync_parent_dir(journal_path)?;
Ok((snap_exchange, snap_sequence, snap_chain_hash, seed_len))
}
#[allow(clippy::too_many_arguments)]
pub(in crate::replication) fn handle_resync_verdict<A, W, S>(
divergent: bool,
source: &mut S,
pipeline: &mut Option<ReplicaPipelineHandles<A, W>>,
exchange: &mut Option<A>,
journal_writer: &mut Option<W>,
journal_path: &std::path::Path,
snapshot_path: &std::path::Path,
fence_state: &melin_transport_core::fence::FenceState,
control: &ReplicaControlPlane,
last_sequence: &mut u64,
chain_hash: &mut [u8; 32],
) -> Result<ResyncDecision, Box<dyn std::error::Error + Send + Sync>>
where
A: Application + Send + 'static,
W: JournalWrite<A::Event> + Send + 'static,
S: ControlFrameSource,
{
if divergent {
tracing::warn!(
last_sequence = *last_sequence,
"primary reports chain divergence — archiving local journal, resyncing from snapshot"
);
} else {
tracing::info!("primary requires snapshot transfer — receiving snapshot");
}
if let Some(p) = pipeline.take() {
let _ = teardown_replica_pipeline::<A, W>(p);
}
*exchange = None;
*journal_writer = None;
let reason = if divergent {
ArchiveReason::Divergent
} else {
ArchiveReason::Resync
};
archive_local_lineage(journal_path, snapshot_path, reason)?;
*last_sequence = 0;
*chain_hash = [0u8; 32];
control
.journal_tip
.reset(melin_transport_core::WireSeq::new(0));
let (snap_exchange, snap_sequence, snap_chain_hash, seed_len) =
match receive_resync_transfer::<A, S>(source, snapshot_path, journal_path, fence_state) {
Ok(v) => v,
Err(e) => {
tracing::warn!(error = %e, "snapshot transfer failed — retrying");
let _ = std::fs::remove_file(snapshot_path);
return Ok(ResyncDecision::Retry);
}
};
*exchange = Some(snap_exchange);
let writer = W::open_append(journal_path, snap_sequence, seed_len)?;
let seeded_chain = writer.chain_hash().unwrap_or(snap_chain_hash);
if snap_chain_hash != [0u8; 32] && seeded_chain != snap_chain_hash {
return Err(format!(
"segment seed chain at {snap_sequence} disagrees with the transferred snapshot's \
hash — inconsistent primary"
)
.into());
}
let seeded_info = melin_journal::segment::read_header_info(journal_path)?;
*journal_writer = Some(writer);
match decode_primary_message(&source.next_frame(MAX_CONTROL_FRAME)?)? {
PrimaryMessage::StreamStart {
start_sequence,
segment_start_sequence,
anchor_hash,
epoch,
ack_policy,
} => {
if segment_start_sequence != seeded_info.starting_sequence
|| anchor_hash != seeded_info.anchor_hash
{
return Err(format!(
"post-snapshot StreamStart lineage (start {segment_start_sequence}) \
disagrees with the transferred segment seed (start {}) — inconsistent \
primary",
seeded_info.starting_sequence
)
.into());
}
fence_state.observe_epoch(epoch);
control
.primary_ack_policy
.store(ack_policy, Ordering::Release);
tracing::info!(
start_sequence,
epoch,
ack_policy,
"streaming resumed after snapshot transfer"
);
control
.journal_tip
.reset(melin_transport_core::WireSeq::new(snap_sequence));
Ok(ResyncDecision::Ready {
segment_start_sequence,
anchor_hash,
resume_sequence: snap_sequence,
})
}
other => Err(format!("expected StreamStart after snapshot, got {other:?}").into()),
}
}
#[cfg(test)]
mod tests {
use std::io::Write;
use std::sync::Arc;
use super::auth::{authenticate_replica, authenticate_with_primary};
use super::*;
use counter_server::CounterEvent;
type InputSlot = melin_transport_core::pipeline::InputSlot<CounterEvent>;
use melin_transport_core::replication::protocol::{
MAX_CONTROL_FRAME, MAX_DATA_FRAME, MSG_AUTH_OK, MSG_CHALLENGE_RESPONSE, MSG_SNAPSHOT_BEGIN,
MSG_SNAPSHOT_CHUNK, MSG_SNAPSHOT_END, decode_auth_result, decode_challenge,
decode_challenge_response, decode_primary_message, decode_replica_message, encode_ack,
encode_auth_failed, encode_auth_ok, encode_chain_check, encode_challenge,
encode_challenge_response, encode_handshake, encode_hash_mismatch, encode_heartbeat,
encode_input_batch, encode_need_snapshot, encode_rotate, encode_segment_seed_begin,
encode_snapshot_begin, encode_snapshot_chunk, encode_snapshot_end, encode_stream_start,
read_frame, try_decode_input_batch,
};
fn encode_input_batch_with_seq(end_sequence: u64, buf: &mut Vec<u8>) {
let slot = InputSlot {
connection_id: 0,
key_hash: 0,
request_seq: 0,
sequence: end_sequence,
timestamp_ns: 0,
event: melin_journal::JournalEvent::Tick { now_ns: 0 },
publish_ts: Default::default(),
recv_ts: Default::default(),
};
encode_input_batch(&[slot], buf);
}
#[test]
fn handshake_encode_decode_round_trip() {
let handshake = Handshake {
last_sequence: 42,
chain_hash: [0xAB; 32],
epoch: 9,
};
let mut buf = Vec::new();
encode_handshake(&handshake, &mut buf);
let payload = &buf[4..];
let msg = decode_replica_message(payload).unwrap();
match msg {
ReplicaMessage::Handshake(h) => {
assert_eq!(h.last_sequence, 42);
assert_eq!(h.chain_hash, [0xAB; 32]);
assert_eq!(h.epoch, 9);
}
_ => panic!("expected Handshake"),
}
}
#[test]
fn ack_encode_decode_round_trip() {
let ack = Ack {
acked_sequence: 1000,
in_memory_sequence: 1024,
};
let mut buf = Vec::new();
encode_ack(&ack, &mut buf);
let payload = &buf[4..];
let msg = decode_replica_message(payload).unwrap();
match msg {
ReplicaMessage::Ack(a) => {
assert_eq!(a.acked_sequence, 1000);
assert_eq!(a.in_memory_sequence, 1024);
}
_ => panic!("expected Ack"),
}
}
#[test]
fn ack_wire_byte_pattern() {
let ack = Ack {
acked_sequence: 0xDEAD_BEEF_CAFE_F00D,
in_memory_sequence: 0x1122_3344_5566_7788,
};
let mut buf = Vec::new();
encode_ack(&ack, &mut buf);
let expected: &[u8] = &[
0x11, 0x00, 0x00, 0x00, 0x02, 0x0D, 0xF0, 0xFE, 0xCA, 0xEF, 0xBE, 0xAD, 0xDE, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, ];
assert_eq!(buf.as_slice(), expected, "Ack wire layout drifted");
}
#[test]
fn stream_start_encode_decode_round_trip() {
let mut buf = Vec::new();
encode_stream_start(99, 42, [0xAA; 32], 5, 2, &mut buf);
let payload = &buf[4..];
let msg = decode_primary_message(payload).unwrap();
match msg {
PrimaryMessage::StreamStart {
start_sequence,
segment_start_sequence,
anchor_hash,
epoch,
ack_policy,
} => {
assert_eq!(start_sequence, 99);
assert_eq!(segment_start_sequence, 42);
assert_eq!(anchor_hash, [0xAA; 32]);
assert_eq!(ack_policy, 2);
assert_eq!(epoch, 5);
}
_ => panic!("expected StreamStart"),
}
}
#[test]
fn heartbeat_encode_decode_round_trip() {
let mut buf = Vec::new();
encode_heartbeat(123, 2, &mut buf);
let payload = &buf[4..];
let msg = decode_primary_message(payload).unwrap();
match msg {
PrimaryMessage::Heartbeat {
sequence,
ack_policy,
} => {
assert_eq!(sequence, 123);
assert_eq!(ack_policy, 2);
}
_ => panic!("expected Heartbeat"),
}
}
#[test]
fn need_snapshot_encode_decode_round_trip() {
let mut buf = Vec::new();
encode_need_snapshot(&mut buf);
let payload = &buf[4..];
let msg = decode_primary_message(payload).unwrap();
assert!(matches!(msg, PrimaryMessage::NeedSnapshot));
}
#[test]
fn hash_mismatch_encode_decode_round_trip() {
let mut buf = Vec::new();
encode_hash_mismatch(&mut buf);
let payload = &buf[4..];
let msg = decode_primary_message(payload).unwrap();
assert!(matches!(msg, PrimaryMessage::HashMismatch));
}
#[test]
fn rotate_encode_decode_round_trip() {
let mut buf = Vec::new();
encode_rotate(7_000_000, &[0xBB; 32], &mut buf);
let payload = &buf[4..];
let msg = decode_primary_message(payload).unwrap();
match msg {
PrimaryMessage::Rotate {
boundary_seq,
tail_hash,
} => {
assert_eq!(boundary_seq, 7_000_000);
assert_eq!(tail_hash, [0xBB; 32]);
}
_ => panic!("expected Rotate"),
}
}
#[test]
fn chain_check_encode_decode_round_trip() {
let mut buf = Vec::new();
encode_chain_check(555, &[0xCC; 32], &mut buf);
let payload = &buf[4..];
let msg = decode_primary_message(payload).unwrap();
match msg {
PrimaryMessage::ChainCheck {
sequence,
chain_hash,
} => {
assert_eq!(sequence, 555);
assert_eq!(chain_hash, [0xCC; 32]);
}
_ => panic!("expected ChainCheck"),
}
}
#[test]
fn rotate_wire_byte_pattern() {
let mut hash = [0u8; 32];
for (i, b) in hash.iter_mut().enumerate() {
*b = i as u8;
}
let mut buf = Vec::new();
encode_rotate(0xDEAD_BEEF_CAFE_F00D, &hash, &mut buf);
let mut expected = vec![
0x29, 0x00, 0x00, 0x00, 0x16, 0x0D, 0xF0, 0xFE, 0xCA, 0xEF, 0xBE, 0xAD, 0xDE, ];
expected.extend_from_slice(&hash);
assert_eq!(
buf.as_slice(),
expected.as_slice(),
"Rotate wire layout drifted"
);
}
#[test]
fn segment_seed_begin_encode_decode_round_trip() {
let mut buf = Vec::new();
encode_segment_seed_begin(123_456, &mut buf);
let payload = &buf[4..];
let msg = decode_primary_message(payload).unwrap();
match msg {
PrimaryMessage::SegmentSeedBegin { seed_len } => {
assert_eq!(seed_len, 123_456);
}
_ => panic!("expected SegmentSeedBegin"),
}
}
#[test]
fn unknown_replica_message_type_is_error() {
let payload = [0xFF, 0, 0, 0, 0, 0, 0, 0, 0];
let result = decode_replica_message(&payload);
assert!(result.is_err());
}
#[test]
fn unknown_primary_message_type_is_error() {
let payload = [0xFF, 0, 0, 0, 0, 0, 0, 0, 0];
let result = decode_primary_message(&payload);
assert!(result.is_err());
}
#[test]
fn read_frame_enforces_max_size() {
let mut buf = Vec::new();
buf.extend_from_slice(&1000u32.to_le_bytes());
buf.extend_from_slice(&[0u8; 100]);
let mut cursor = std::io::Cursor::new(buf);
let result = read_frame(&mut cursor, 64);
assert!(result.is_err());
}
#[test]
fn challenge_encode_decode_round_trip() {
let nonce = [0x42; 32];
let mut buf = Vec::new();
encode_challenge(&nonce, &mut buf);
let payload = &buf[4..];
let decoded = decode_challenge(payload).unwrap();
assert_eq!(decoded, nonce);
}
#[test]
fn challenge_response_encode_decode_round_trip() {
let sig = [0xAA; 64];
let pubkey = [0xBB; 32];
let mut buf = Vec::new();
encode_challenge_response(&sig, &pubkey, &mut buf);
let payload = &buf[4..];
let (decoded_sig, decoded_pubkey) = decode_challenge_response(payload).unwrap();
assert_eq!(decoded_sig, sig);
assert_eq!(decoded_pubkey, pubkey);
}
#[test]
fn auth_ok_encode_decode_round_trip() {
let mut buf = Vec::new();
encode_auth_ok(&mut buf);
let payload = &buf[4..];
assert!(decode_auth_result(payload).unwrap());
}
#[test]
fn auth_failed_encode_decode_round_trip() {
let mut buf = Vec::new();
encode_auth_failed(&mut buf);
let payload = &buf[4..];
assert!(!decode_auth_result(payload).unwrap());
}
#[test]
fn decode_challenge_rejects_wrong_tag() {
let mut payload = [0u8; 33];
payload[0] = MSG_AUTH_OK;
assert!(decode_challenge(&payload).is_err());
}
#[test]
fn decode_challenge_response_rejects_short_payload() {
let payload = [MSG_CHALLENGE_RESPONSE; 10]; assert!(decode_challenge_response(&payload).is_err());
}
#[test]
fn auth_round_trip_valid_key() {
use ed25519_dalek::SigningKey;
use std::os::unix::net::UnixStream;
let repl_key = SigningKey::from_bytes(&[0xFC; 32]);
let pub_b64 = base64::Engine::encode(
&base64::engine::general_purpose::STANDARD,
repl_key.verifying_key().to_bytes(),
);
let keys_content = format!("replication {pub_b64} test-replica\n");
let authorized_keys = melin_app::auth::AuthorizedKeys::parse(&keys_content).unwrap();
let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
primary_stream
.set_read_timeout(Some(std::time::Duration::from_secs(5)))
.unwrap();
replica_stream
.set_read_timeout(Some(std::time::Duration::from_secs(5)))
.unwrap();
let repl_key_clone = SigningKey::from_bytes(&[0xFC; 32]);
let replica_handle = std::thread::spawn(move || {
let mut conn = replica_stream;
authenticate_with_primary(&mut conn, &repl_key_clone)
});
let mut conn = primary_stream;
authenticate_replica(&mut conn, &authorized_keys).unwrap();
replica_handle.join().unwrap().unwrap();
}
#[test]
fn auth_rejects_unknown_key() {
use ed25519_dalek::SigningKey;
use std::os::unix::net::UnixStream;
let authorized_key = SigningKey::from_bytes(&[0xAA; 32]);
let rogue_key = SigningKey::from_bytes(&[0xBB; 32]);
let pub_b64 = base64::Engine::encode(
&base64::engine::general_purpose::STANDARD,
authorized_key.verifying_key().to_bytes(),
);
let keys_content = format!("replication {pub_b64} authorized-replica\n");
let authorized_keys = melin_app::auth::AuthorizedKeys::parse(&keys_content).unwrap();
let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
primary_stream
.set_read_timeout(Some(std::time::Duration::from_secs(5)))
.unwrap();
replica_stream
.set_read_timeout(Some(std::time::Duration::from_secs(5)))
.unwrap();
let replica_handle = std::thread::spawn(move || {
let mut conn = replica_stream;
authenticate_with_primary(&mut conn, &rogue_key)
});
let mut conn = primary_stream;
let result = authenticate_replica(&mut conn, &authorized_keys);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("unknown"));
let replica_result = replica_handle.join().unwrap();
assert!(replica_result.is_err());
}
#[test]
fn auth_rejects_wrong_permission() {
use ed25519_dalek::SigningKey;
use std::os::unix::net::UnixStream;
let key = SigningKey::from_bytes(&[0xCC; 32]);
let pub_b64 = base64::Engine::encode(
&base64::engine::general_purpose::STANDARD,
key.verifying_key().to_bytes(),
);
let keys_content = format!("trader {pub_b64} wrong-role\n");
let authorized_keys = melin_app::auth::AuthorizedKeys::parse(&keys_content).unwrap();
let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
primary_stream
.set_read_timeout(Some(std::time::Duration::from_secs(5)))
.unwrap();
replica_stream
.set_read_timeout(Some(std::time::Duration::from_secs(5)))
.unwrap();
let replica_handle = std::thread::spawn(move || {
let mut conn = replica_stream;
authenticate_with_primary(&mut conn, &key)
});
let mut conn = primary_stream;
let result = authenticate_replica(&mut conn, &authorized_keys);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("Replication"));
let replica_result = replica_handle.join().unwrap();
assert!(replica_result.is_err());
}
#[test]
fn auth_rejects_invalid_signature() {
use ed25519_dalek::SigningKey;
use std::os::unix::net::UnixStream;
let correct_key = SigningKey::from_bytes(&[0xDD; 32]);
let pub_b64 = base64::Engine::encode(
&base64::engine::general_purpose::STANDARD,
correct_key.verifying_key().to_bytes(),
);
let keys_content = format!("replication {pub_b64} test-replica\n");
let authorized_keys = melin_app::auth::AuthorizedKeys::parse(&keys_content).unwrap();
let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
primary_stream
.set_read_timeout(Some(std::time::Duration::from_secs(5)))
.unwrap();
replica_stream
.set_read_timeout(Some(std::time::Duration::from_secs(5)))
.unwrap();
let replica_handle = std::thread::spawn(move || {
use melin_transport_core::replication::protocol::*;
let mut reader = replica_stream.try_clone().unwrap();
let mut writer = replica_stream;
let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
let nonce = decode_challenge(&frame).unwrap();
let wrong_key = SigningKey::from_bytes(&[0xEE; 32]);
let bad_signature = ed25519_dalek::Signer::sign(&wrong_key, &nonce);
let correct_pubkey = correct_key.verifying_key();
let mut buf = Vec::with_capacity(128);
encode_challenge_response(
&bad_signature.to_bytes(),
correct_pubkey.as_bytes(),
&mut buf,
);
writer.write_all(&buf).unwrap();
writer.flush().unwrap();
let result_frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
let ok = decode_auth_result(&result_frame).unwrap();
assert!(!ok, "should receive auth failure");
});
let mut conn = primary_stream;
let result = authenticate_replica(&mut conn, &authorized_keys);
assert!(result.is_err());
assert!(
result
.unwrap_err()
.to_string()
.contains("signature verification failed"),
"should fail on signature verification"
);
replica_handle.join().unwrap();
}
#[test]
fn sender_receiver_end_to_end() {
use std::os::unix::net::UnixStream;
let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
let replica_handle = std::thread::spawn(move || {
let mut reader = replica_stream.try_clone().unwrap();
let mut writer = replica_stream;
let mut buf = Vec::new();
let handshake = Handshake {
last_sequence: 0,
chain_hash: [0u8; 32],
epoch: 0,
};
encode_handshake(&handshake, &mut buf);
writer.write_all(&buf).unwrap();
writer.flush().unwrap();
buf.clear();
let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
let msg = decode_primary_message(&frame).unwrap();
assert!(matches!(msg, PrimaryMessage::StreamStart { .. }));
let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
let slots: Vec<InputSlot> = try_decode_input_batch(&frame).expect("decode InputBatch");
let end_seq = slots
.last()
.map(|s| s.sequence)
.expect("InputBatch carried at least one slot");
let ack = Ack {
acked_sequence: end_seq,
in_memory_sequence: end_seq,
};
encode_ack(&ack, &mut buf);
writer.write_all(&buf).unwrap();
writer.flush().unwrap();
end_seq
});
let mut p_reader = primary_stream.try_clone().unwrap();
let mut p_writer = primary_stream;
let frame = read_frame(&mut p_reader, MAX_CONTROL_FRAME).unwrap();
let handshake = match decode_replica_message(&frame).unwrap() {
ReplicaMessage::Handshake(h) => h,
_ => panic!("expected Handshake"),
};
assert_eq!(handshake.last_sequence, 0);
let mut buf = Vec::new();
encode_stream_start(0, 1, [0u8; 32], 0, 1, &mut buf); p_writer.write_all(&buf).unwrap();
p_writer.flush().unwrap();
buf.clear();
encode_input_batch_with_seq(42, &mut buf);
p_writer.write_all(&buf).unwrap();
p_writer.flush().unwrap();
buf.clear();
let frame = read_frame(&mut p_reader, MAX_CONTROL_FRAME).unwrap();
let ack = match decode_replica_message(&frame).unwrap() {
ReplicaMessage::Ack(a) => a,
_ => panic!("expected Ack"),
};
assert_eq!(ack.acked_sequence, 42);
let end_seq = replica_handle.join().unwrap();
assert_eq!(end_seq, 42);
}
#[test]
fn multiple_data_batches_acked_in_order() {
use std::os::unix::net::UnixStream;
let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
let replica_handle = std::thread::spawn(move || {
let mut reader = replica_stream.try_clone().unwrap();
let mut writer = replica_stream;
let mut buf = Vec::new();
encode_handshake(
&Handshake {
last_sequence: 0,
chain_hash: [0u8; 32],
epoch: 0,
},
&mut buf,
);
writer.write_all(&buf).unwrap();
writer.flush().unwrap();
buf.clear();
let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
assert!(matches!(
decode_primary_message(&frame).unwrap(),
PrimaryMessage::StreamStart { .. }
));
let mut acked_seqs = Vec::new();
for _ in 0..3 {
let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
let slots: Vec<InputSlot> =
try_decode_input_batch(&frame).expect("decode InputBatch");
let end_seq = slots
.last()
.map(|s| s.sequence)
.expect("InputBatch carried at least one slot");
acked_seqs.push(end_seq);
encode_ack(
&Ack {
acked_sequence: end_seq,
in_memory_sequence: end_seq,
},
&mut buf,
);
writer.write_all(&buf).unwrap();
writer.flush().unwrap();
buf.clear();
}
acked_seqs
});
let mut p_reader = primary_stream.try_clone().unwrap();
let mut p_writer = primary_stream;
let mut buf = Vec::new();
let frame = read_frame(&mut p_reader, MAX_CONTROL_FRAME).unwrap();
assert!(matches!(
decode_replica_message(&frame).unwrap(),
ReplicaMessage::Handshake(_)
));
encode_stream_start(0, 1, [0u8; 32], 0, 1, &mut buf);
p_writer.write_all(&buf).unwrap();
p_writer.flush().unwrap();
buf.clear();
for seq in [10u64, 20, 30] {
encode_input_batch_with_seq(seq, &mut buf);
p_writer.write_all(&buf).unwrap();
p_writer.flush().unwrap();
buf.clear();
}
for expected_seq in [10u64, 20, 30] {
let frame = read_frame(&mut p_reader, MAX_CONTROL_FRAME).unwrap();
let ack = match decode_replica_message(&frame).unwrap() {
ReplicaMessage::Ack(a) => a,
other => panic!("expected Ack, got {other:?}"),
};
assert_eq!(ack.acked_sequence, expected_seq);
}
let acked = replica_handle.join().unwrap();
assert_eq!(acked, vec![10, 20, 30]);
}
#[test]
fn heartbeat_encode_contains_sequence() {
let mut buf = Vec::new();
encode_heartbeat(999, 1, &mut buf);
let payload = &buf[4..];
match decode_primary_message(payload).unwrap() {
PrimaryMessage::Heartbeat { sequence, .. } => {
assert_eq!(sequence, 999);
}
other => panic!("expected Heartbeat, got {other:?}"),
}
}
#[test]
fn replica_mid_stream_handshake_with_nonzero_sequence() {
use std::os::unix::net::UnixStream;
let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
let replica_handle = std::thread::spawn(move || {
let mut reader = replica_stream.try_clone().unwrap();
let mut writer = replica_stream;
let mut buf = Vec::new();
encode_handshake(
&Handshake {
last_sequence: 100,
chain_hash: [0xBB; 32],
epoch: 0,
},
&mut buf,
);
writer.write_all(&buf).unwrap();
writer.flush().unwrap();
buf.clear();
let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
match decode_primary_message(&frame).unwrap() {
PrimaryMessage::StreamStart { start_sequence, .. } => {
assert_eq!(
start_sequence, 100,
"StreamStart should echo replica's last_sequence"
);
}
other => panic!("expected StreamStart, got {other:?}"),
}
let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
let slots: Vec<InputSlot> = try_decode_input_batch(&frame).expect("decode InputBatch");
let end_sequence = slots
.last()
.map(|s| s.sequence)
.expect("InputBatch carried at least one slot");
assert!(
end_sequence > 100,
"InputBatch should be after replica's last_sequence"
);
});
let mut p_reader = primary_stream.try_clone().unwrap();
let mut p_writer = primary_stream;
let mut buf = Vec::new();
let frame = read_frame(&mut p_reader, MAX_CONTROL_FRAME).unwrap();
let handshake = match decode_replica_message(&frame).unwrap() {
ReplicaMessage::Handshake(h) => h,
_ => panic!("expected Handshake"),
};
assert_eq!(handshake.last_sequence, 100);
assert_eq!(handshake.chain_hash, [0xBB; 32]);
encode_stream_start(handshake.last_sequence, 1, [0u8; 32], 0, 1, &mut buf);
p_writer.write_all(&buf).unwrap();
p_writer.flush().unwrap();
buf.clear();
encode_input_batch_with_seq(150, &mut buf);
p_writer.write_all(&buf).unwrap();
p_writer.flush().unwrap();
replica_handle.join().unwrap();
}
#[test]
fn receiver_refuses_stream_start_from_stale_primary() {
use std::os::unix::net::UnixStream;
use melin_transport_core::fence::FenceState;
let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
const STALE_EPOCH: u64 = 3;
let primary_handle = std::thread::spawn(move || {
let mut reader = primary_stream.try_clone().unwrap();
let mut writer = primary_stream;
let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
let handshake = match decode_replica_message(&frame).unwrap() {
ReplicaMessage::Handshake(h) => h,
_ => panic!("expected Handshake"),
};
assert_eq!(handshake.epoch, 5, "replica advertises its real epoch");
let mut buf = Vec::new();
encode_stream_start(
handshake.last_sequence,
1,
[0u8; 32],
STALE_EPOCH,
1,
&mut buf,
);
writer.write_all(&buf).unwrap();
writer.flush().unwrap();
});
let fence_state = Arc::new(FenceState::new(5));
let mut r_reader = replica_stream.try_clone().unwrap();
let mut r_writer = replica_stream;
let mut buf = Vec::new();
encode_handshake(
&Handshake {
last_sequence: 0,
chain_hash: [0u8; 32],
epoch: fence_state.epoch(),
},
&mut buf,
);
r_writer.write_all(&buf).unwrap();
r_writer.flush().unwrap();
let frame = read_frame(&mut r_reader, MAX_CONTROL_FRAME).unwrap();
let stream_epoch = match decode_primary_message(&frame).unwrap() {
PrimaryMessage::StreamStart { epoch, .. } => epoch,
other => panic!("expected StreamStart, got {other:?}"),
};
assert_eq!(stream_epoch, STALE_EPOCH, "epoch must survive the wire");
assert!(
fence_state.refuses_primary(stream_epoch),
"replica at epoch 5 must refuse a StreamStart from epoch {STALE_EPOCH}"
);
assert_eq!(fence_state.epoch(), 5, "refusal must not lower our epoch");
assert!(!fence_state.refuses_primary(5), "same tenure is followed");
assert!(!fence_state.refuses_primary(6), "newer primary is followed");
primary_handle.join().unwrap();
}
#[test]
fn snapshot_begin_encode_decode_round_trip() {
let mut buf = Vec::new();
encode_snapshot_begin(1_000_000, 42, &[0xAB; 32], &mut buf);
let payload = &buf[4..];
let msg = decode_primary_message(payload).unwrap();
match msg {
PrimaryMessage::SnapshotBegin {
snapshot_len,
snap_sequence,
snap_chain_hash,
} => {
assert_eq!(snapshot_len, 1_000_000);
assert_eq!(snap_sequence, 42);
assert_eq!(snap_chain_hash, [0xAB; 32]);
}
_ => panic!("expected SnapshotBegin"),
}
}
#[test]
fn snapshot_chunk_encode_decode_round_trip() {
let data = vec![1, 2, 3, 4, 5, 6, 7, 8];
let mut buf = Vec::new();
encode_snapshot_chunk(&data, &mut buf);
let payload = &buf[4..];
let msg = decode_primary_message(payload).unwrap();
match msg {
PrimaryMessage::SnapshotChunk(chunk) => {
assert_eq!(chunk, data);
}
_ => panic!("expected SnapshotChunk"),
}
}
#[test]
fn snapshot_end_encode_decode_round_trip() {
let mut buf = Vec::new();
encode_snapshot_end(0xDEADBEEF, &mut buf);
let payload = &buf[4..];
let msg = decode_primary_message(payload).unwrap();
match msg {
PrimaryMessage::SnapshotEnd { crc32c } => {
assert_eq!(crc32c, 0xDEADBEEF);
}
_ => panic!("expected SnapshotEnd"),
}
}
#[test]
fn snapshot_receiver_detects_length_mismatch() {
use std::os::unix::net::UnixStream;
let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
let receiver = std::thread::spawn(move || -> String {
let mut reader = replica_stream.try_clone().unwrap();
let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
assert!(matches!(
decode_primary_message(&frame).unwrap(),
PrimaryMessage::NeedSnapshot,
));
let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
let (snap_len, _snap_sequence, _snap_chain_hash) =
match decode_primary_message(&frame).unwrap() {
PrimaryMessage::SnapshotBegin {
snapshot_len,
snap_sequence,
snap_chain_hash,
} => (snapshot_len, snap_sequence, snap_chain_hash),
other => panic!("expected SnapshotBegin, got {other:?}"),
};
let mut received: u64 = 0;
loop {
let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
match decode_primary_message(&frame).unwrap() {
PrimaryMessage::SnapshotChunk(data) => {
received += data.len() as u64;
}
PrimaryMessage::SnapshotEnd { .. } => {
if received != snap_len {
return format!(
"snapshot length mismatch: expected {snap_len} bytes, got {received}"
);
}
return String::new(); }
other => panic!("unexpected message: {other:?}"),
}
}
});
let mut writer = primary_stream;
let mut buf = Vec::new();
let actual_data = vec![0xAA; 100];
let wrong_len = 999u64;
encode_need_snapshot(&mut buf);
std::io::Write::write_all(&mut writer, &buf).unwrap();
buf.clear();
encode_snapshot_begin(wrong_len, 42, &[0xBB; 32], &mut buf);
std::io::Write::write_all(&mut writer, &buf).unwrap();
buf.clear();
encode_snapshot_chunk(&actual_data, &mut buf);
std::io::Write::write_all(&mut writer, &buf).unwrap();
buf.clear();
let crc = crc32c::crc32c(&actual_data);
encode_snapshot_end(crc, &mut buf);
std::io::Write::write_all(&mut writer, &buf).unwrap();
std::io::Write::flush(&mut writer).unwrap();
let error_msg = receiver.join().unwrap();
assert!(
error_msg.contains("length mismatch"),
"expected length mismatch error, got: {error_msg:?}"
);
}
#[test]
fn snapshot_receiver_detects_crc_mismatch() {
use std::os::unix::net::UnixStream;
let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
let receiver = std::thread::spawn(move || -> String {
let mut reader = replica_stream.try_clone().unwrap();
let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
assert!(matches!(
decode_primary_message(&frame).unwrap(),
PrimaryMessage::NeedSnapshot,
));
let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
let snap_len = match decode_primary_message(&frame).unwrap() {
PrimaryMessage::SnapshotBegin { snapshot_len, .. } => snapshot_len,
other => panic!("expected SnapshotBegin, got {other:?}"),
};
let mut received_data = Vec::new();
let mut received: u64 = 0;
loop {
let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
match decode_primary_message(&frame).unwrap() {
PrimaryMessage::SnapshotChunk(data) => {
received += data.len() as u64;
received_data.extend_from_slice(&data);
}
PrimaryMessage::SnapshotEnd {
crc32c: expected_crc,
} => {
if received != snap_len {
return format!("length mismatch: {snap_len} vs {received}");
}
let actual_crc = crc32c::crc32c(&received_data);
if actual_crc != expected_crc {
return format!(
"CRC mismatch: expected {expected_crc:#x}, got {actual_crc:#x}"
);
}
return String::new();
}
other => panic!("unexpected message: {other:?}"),
}
}
});
let mut writer = primary_stream;
let mut buf = Vec::new();
let data = vec![0xAA; 100];
encode_need_snapshot(&mut buf);
std::io::Write::write_all(&mut writer, &buf).unwrap();
buf.clear();
encode_snapshot_begin(data.len() as u64, 42, &[0xBB; 32], &mut buf);
std::io::Write::write_all(&mut writer, &buf).unwrap();
buf.clear();
encode_snapshot_chunk(&data, &mut buf);
std::io::Write::write_all(&mut writer, &buf).unwrap();
buf.clear();
let wrong_crc = !crc32c::crc32c(&data);
encode_snapshot_end(wrong_crc, &mut buf);
std::io::Write::write_all(&mut writer, &buf).unwrap();
std::io::Write::flush(&mut writer).unwrap();
let error_msg = receiver.join().unwrap();
assert!(
error_msg.contains("CRC mismatch"),
"expected CRC mismatch error, got: {error_msg:?}"
);
}
#[test]
fn snapshot_receiver_detects_chain_hash_mismatch() {
use std::os::unix::net::UnixStream;
let (primary_stream, replica_stream) = UnixStream::pair().unwrap();
let receiver = std::thread::spawn(move || -> String {
let mut reader = replica_stream.try_clone().unwrap();
let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
assert!(matches!(
decode_primary_message(&frame).unwrap(),
PrimaryMessage::NeedSnapshot,
));
let frame = read_frame(&mut reader, MAX_CONTROL_FRAME).unwrap();
let (snap_len, _snap_sequence, snap_chain_hash) =
match decode_primary_message(&frame).unwrap() {
PrimaryMessage::SnapshotBegin {
snapshot_len,
snap_sequence,
snap_chain_hash,
} => (snapshot_len, snap_sequence, snap_chain_hash),
other => panic!("expected SnapshotBegin, got {other:?}"),
};
let mut received_data = Vec::new();
let mut received: u64 = 0;
loop {
let frame = read_frame(&mut reader, MAX_DATA_FRAME).unwrap();
match decode_primary_message(&frame).unwrap() {
PrimaryMessage::SnapshotChunk(data) => {
received += data.len() as u64;
received_data.extend_from_slice(&data);
}
PrimaryMessage::SnapshotEnd {
crc32c: expected_crc,
} => {
assert_eq!(received, snap_len, "length should match");
let actual_crc = crc32c::crc32c(&received_data);
assert_eq!(actual_crc, expected_crc, "CRC should match");
break;
}
other => panic!("unexpected message: {other:?}"),
}
}
let loaded_hash = [0xFF; 32]; if loaded_hash != snap_chain_hash {
return format!(
"snapshot chain hash mismatch: primary sent {snap_chain_hash:02x?}, \
loaded snapshot has {loaded_hash:02x?}"
);
}
String::new()
});
let mut writer = primary_stream;
let mut buf = Vec::new();
let data = vec![0xAA; 64];
let advertised_hash = [0xBB; 32];
encode_need_snapshot(&mut buf);
std::io::Write::write_all(&mut writer, &buf).unwrap();
buf.clear();
encode_snapshot_begin(data.len() as u64, 10, &advertised_hash, &mut buf);
std::io::Write::write_all(&mut writer, &buf).unwrap();
buf.clear();
encode_snapshot_chunk(&data, &mut buf);
std::io::Write::write_all(&mut writer, &buf).unwrap();
buf.clear();
let crc = crc32c::crc32c(&data);
encode_snapshot_end(crc, &mut buf);
std::io::Write::write_all(&mut writer, &buf).unwrap();
std::io::Write::flush(&mut writer).unwrap();
let error_msg = receiver.join().unwrap();
assert!(
error_msg.contains("chain hash mismatch"),
"expected chain hash mismatch error, got: {error_msg:?}"
);
}
#[test]
fn primary_rejects_snapshot_with_invalid_magic() {
let tmp = tempfile::tempdir().unwrap();
let snap_path = tmp.path().join("test.snapshot");
let mut bad_snap = vec![0u8; 64];
bad_snap[0..4].copy_from_slice(&0xDEAD_BEEFu32.to_le_bytes());
std::fs::write(&snap_path, &bad_snap).unwrap();
let snap_data = std::fs::read(&snap_path).unwrap();
assert!(
snap_data.len() >= 48,
"file should be big enough for header"
);
let magic = u32::from_le_bytes(snap_data[0..4].try_into().unwrap());
assert_ne!(magic, 0x534E_4150);
assert_eq!(magic, 0xDEAD_BEEF);
}
#[test]
fn primary_rejects_snapshot_too_small_for_header() {
let tmp = tempfile::tempdir().unwrap();
let snap_path = tmp.path().join("test.snapshot");
std::fs::write(&snap_path, [0u8; 20]).unwrap();
let snap_data = std::fs::read(&snap_path).unwrap();
assert!(
snap_data.len() < 48,
"file must be too small for header validation"
);
}
#[test]
fn decode_snapshot_begin_too_short() {
let payload = [MSG_SNAPSHOT_BEGIN, 0x01, 0x02, 0x03];
let err = decode_primary_message(&payload).unwrap_err();
assert!(
err.to_string().contains("SnapshotBegin too short"),
"expected 'SnapshotBegin too short', got: {err}"
);
}
#[test]
fn decode_snapshot_end_too_short() {
let payload = [MSG_SNAPSHOT_END];
let err = decode_primary_message(&payload).unwrap_err();
assert!(
err.to_string().contains("SnapshotEnd too short"),
"expected 'SnapshotEnd too short', got: {err}"
);
}
#[test]
fn decode_snapshot_chunk_empty_data() {
let payload = [MSG_SNAPSHOT_CHUNK];
let msg = decode_primary_message(&payload).unwrap();
match msg {
PrimaryMessage::SnapshotChunk(data) => {
assert!(data.is_empty());
}
_ => panic!("expected SnapshotChunk"),
}
}
fn make_journal_cursor(val: u64) -> melin_pipeline::padding::Sequence {
melin_pipeline::padding::Sequence::new(AtomicU64::new(val))
}
#[test]
fn pending_ack_queue_push_and_pop_ready() {
let mut q = PendingAckQueue::new(8);
assert!(q.is_empty());
assert!(!q.is_full());
q.push(10, 100);
q.push(20, 200);
assert!(!q.is_empty());
let cursor = make_journal_cursor(5);
assert!(q.pop_ready(&cursor).is_none());
cursor.get().store(15, Ordering::Relaxed);
assert_eq!(q.pop_ready(&cursor), Some(100));
assert!(!q.is_empty());
cursor.get().store(25, Ordering::Relaxed);
assert_eq!(q.pop_ready(&cursor), Some(200));
assert!(q.is_empty());
}
#[test]
fn pending_ack_queue_pop_ready_returns_highest_sequence() {
let mut q = PendingAckQueue::new(8);
q.push(10, 100);
q.push(20, 200);
q.push(30, 300);
let cursor = make_journal_cursor(30);
assert_eq!(q.pop_ready(&cursor), Some(300));
assert!(q.is_empty());
}
#[test]
fn pending_ack_queue_capacity_and_full() {
let mut q = PendingAckQueue::new(8);
for i in 0..8 {
assert!(!q.is_full());
q.push(i as u64 + 1, (i + 1) as u64 * 100);
}
assert!(q.is_full());
}
#[test]
fn pending_ack_queue_merges_into_the_newest_entry_when_full() {
let mut q = PendingAckQueue::new(4);
for i in 1..=4u64 {
q.push(i * 10, i * 100);
}
assert!(q.is_full());
assert_eq!(q.merged(), 0);
q.push(50, 500);
assert_eq!(q.merged(), 1);
assert_eq!(q.len(), 4, "merge must not grow the queue");
let cursor = make_journal_cursor(40);
assert_eq!(q.pop_ready(&cursor), Some(300));
assert!(!q.is_empty(), "merged entry is not durable yet");
cursor.get().store(50, Ordering::Relaxed);
assert_eq!(q.pop_ready(&cursor), Some(500));
assert!(q.is_empty());
}
#[test]
fn pending_ack_queue_merges_repeatedly_without_growing() {
let mut q = PendingAckQueue::new(2);
for i in 1..=100u64 {
q.push(i, i * 10);
}
assert_eq!(q.len(), 2);
assert_eq!(q.merged(), 98);
let cursor = make_journal_cursor(99);
assert_eq!(q.pop_ready(&cursor), Some(10), "only the first entry");
cursor.get().store(100, Ordering::Relaxed);
assert_eq!(q.pop_ready(&cursor), Some(1000));
}
#[test]
fn pending_ack_queue_pop_oldest_blocking() {
let mut q = PendingAckQueue::new(8);
q.push(10, 100);
q.push(20, 200);
let cursor = make_journal_cursor(25);
let seq = q.pop_oldest_blocking(&cursor, true, &AtomicBool::new(false));
assert_eq!(seq, Some(200));
assert!(q.is_empty());
}
#[test]
fn pending_ack_queue_blocking_wait_aborts_on_journal_failure() {
let mut q = PendingAckQueue::new(8);
q.push(10, 100);
let cursor = make_journal_cursor(5);
let abort = AtomicBool::new(true);
assert_eq!(q.pop_oldest_blocking(&cursor, true, &abort), None);
assert!(!q.is_empty(), "aborted wait must not pop the entry");
assert_eq!(q.pop_all_blocking(&cursor, true, &abort), None);
}
#[test]
fn shutdown_pipeline_surfaces_journal_error_kind() {
let flag = AtomicBool::new(false);
let matching_joined = Arc::new(AtomicBool::new(false));
let mj = Arc::clone(&matching_joined);
let journal = std::thread::spawn(|| -> Result<u32, melin_journal::JournalError> {
Err(melin_journal::JournalError::ReplicaChainDivergence {
sequence: 42,
expected: [1u8; 32],
actual: [2u8; 32],
})
});
let matching = std::thread::spawn(move || {
mj.store(true, Ordering::Release);
7u64
});
let drain = std::thread::spawn(|| {});
let outcome = shutdown_pipeline::<u64, u32>(&flag, journal, matching, drain, None);
assert!(flag.load(Ordering::Acquire), "shutdown flag must be set");
assert!(
matching_joined.load(Ordering::Acquire),
"matching thread must be joined even when the journal failed"
);
match outcome {
TeardownOutcome::JournalFailed(
melin_journal::JournalError::ReplicaChainDivergence { sequence, .. },
) => assert_eq!(sequence, 42),
_ => panic!("expected JournalFailed(ReplicaChainDivergence)"),
}
}
#[test]
fn shutdown_pipeline_clean_returns_both_states() {
let flag = AtomicBool::new(false);
let journal =
std::thread::spawn(|| -> Result<u32, melin_journal::JournalError> { Ok(11u32) });
let matching = std::thread::spawn(|| 7u64);
let drain = std::thread::spawn(|| {});
match shutdown_pipeline::<u64, u32>(&flag, journal, matching, drain, None) {
TeardownOutcome::Clean(app, writer) => {
assert_eq!(app, 7);
assert_eq!(writer, 11);
}
_ => panic!("expected Clean"),
}
}
#[test]
fn shutdown_pipeline_panicked_stage_reports_panicked() {
let flag = AtomicBool::new(false);
let journal =
std::thread::spawn(|| -> Result<u32, melin_journal::JournalError> { Ok(11u32) });
let matching = std::thread::spawn(|| -> u64 { panic!("matching stage died") });
let drain = std::thread::spawn(|| {});
assert!(matches!(
shutdown_pipeline::<u64, u32>(&flag, journal, matching, drain, None),
TeardownOutcome::Panicked
));
}
fn teardown_fixture(
capacity: usize,
) -> (
ReplicaPipelineHandles<counter_server::Counter, u32>,
melin_pipeline::ring::Consumer<InputSlot>,
) {
use melin_app::app_factory::AppFactory;
let (input_producer, mut consumers) =
melin_pipeline::ring::DisruptorBuilder::<InputSlot>::new(capacity)
.add_consumer()
.build();
let consumer = consumers.pop().expect("one consumer");
let handles = ReplicaPipelineHandles {
input_producer,
journal_cursor: Arc::new(make_journal_cursor(0)),
last_seq: melin_transport_core::DurableWireSeqCursor::detached(
melin_transport_core::WireSeq::new(0),
),
chain_hash_lock: None,
stream_marks: Arc::new(std::sync::Mutex::new(std::collections::VecDeque::new())),
journal_failed: Arc::new(AtomicBool::new(false)),
pipeline_shutdown: Arc::new(AtomicBool::new(false)),
journal_handle: std::thread::spawn(|| -> Result<u32, melin_journal::JournalError> {
Ok(11)
}),
matching_handle: std::thread::spawn(|| counter_server::CounterFactory.empty()),
drain_handle: std::thread::spawn(|| {}),
shadow_handle: None,
};
(handles, consumer)
}
#[test]
fn teardown_publishes_shutdown_sentinel() {
let (handles, mut consumer) = teardown_fixture(8);
assert!(matches!(
teardown_replica_pipeline::<counter_server::Counter, u32>(handles),
TeardownOutcome::Clean(..)
));
let (_, slot) = consumer.try_consume().expect("sentinel on the ring");
assert!(
slot.event.is_shutdown(),
"published slot must be the sentinel"
);
assert!(consumer.try_consume().is_none(), "exactly one sentinel");
}
fn backoff_after_disconnect(heard_from_primary: bool) -> std::time::Duration {
let dir = tempfile::tempdir().expect("tempdir");
type Writer = melin_journal::BufferedWriter<CounterEvent>;
let mut pipeline: Option<ReplicaPipelineHandles<counter_server::Counter, Writer>> = None;
let mut divergence_resyncs = 0u32;
let mut backoff = MAX_BACKOFF;
let shutdown = AtomicBool::new(true);
let promote = crate::promotion::PromotionRequest::new();
let after = handle_session_exit::<counter_server::Counter, Writer>(
StreamingResult {
exit: SessionExit::Disconnected,
heard_from_primary,
},
&mut pipeline,
&mut divergence_resyncs,
&mut backoff,
0,
&dir.path().join("r.journal"),
&dir.path().join("r.snapshot"),
&counter_server::CounterFactory,
&melin_transport_core::fence::FenceState::new(0),
&shutdown,
&promote,
|| {},
);
assert!(matches!(after, AfterSession::Reconnect));
backoff
}
#[test]
fn disconnect_resets_backoff_when_primary_spoke() {
assert_eq!(
backoff_after_disconnect(true),
std::time::Duration::from_secs(2)
);
}
#[test]
fn disconnect_keeps_escalated_backoff_when_primary_silent() {
assert_eq!(backoff_after_disconnect(false), MAX_BACKOFF);
}
#[test]
fn stream_gap_reconnects_and_keeps_pipeline() {
use melin_app::app_factory::AppFactory;
let dir = tempfile::tempdir().expect("tempdir");
type Writer = melin_journal::BufferedWriter<CounterEvent>;
let (input_producer, mut consumers) =
melin_pipeline::ring::DisruptorBuilder::<InputSlot>::new(4)
.add_consumer()
.build();
let _consumer = consumers.pop().expect("one consumer");
let writer_path = dir.path().join("w.journal");
let handles = ReplicaPipelineHandles {
input_producer,
journal_cursor: Arc::new(make_journal_cursor(0)),
last_seq: melin_transport_core::DurableWireSeqCursor::detached(
melin_transport_core::WireSeq::new(0),
),
chain_hash_lock: None,
stream_marks: Arc::new(std::sync::Mutex::new(std::collections::VecDeque::new())),
journal_failed: Arc::new(AtomicBool::new(false)),
pipeline_shutdown: Arc::new(AtomicBool::new(false)),
journal_handle: std::thread::spawn(move || Writer::create(&writer_path)),
matching_handle: std::thread::spawn(|| counter_server::CounterFactory.empty()),
drain_handle: std::thread::spawn(|| {}),
shadow_handle: None,
};
let mut pipeline: Option<ReplicaPipelineHandles<counter_server::Counter, Writer>> =
Some(handles);
let mut divergence_resyncs = 0u32;
let mut backoff = MAX_BACKOFF;
let shutdown = AtomicBool::new(true);
let promote = crate::promotion::PromotionRequest::new();
let mut closed = false;
let after = handle_session_exit::<counter_server::Counter, Writer>(
StreamingResult {
exit: SessionExit::StreamGap("sequence gap: expected 3, got 5".into()),
heard_from_primary: true,
},
&mut pipeline,
&mut divergence_resyncs,
&mut backoff,
2,
&dir.path().join("r.journal"),
&dir.path().join("r.snapshot"),
&counter_server::CounterFactory,
&melin_transport_core::fence::FenceState::new(0),
&shutdown,
&promote,
|| closed = true,
);
assert!(
matches!(after, AfterSession::Reconnect),
"a stream gap must reconnect, not return an error"
);
assert!(
pipeline.is_some(),
"the pipeline must survive for the reconnect"
);
assert!(closed, "the transport must be closed before redialing");
assert_eq!(
backoff,
std::time::Duration::from_secs(2),
"backoff reset to 1s then doubled by the shared helper"
);
assert_eq!(divergence_resyncs, 0, "not a resync");
if let Some(p) = pipeline.take() {
let _ = teardown_replica_pipeline::<counter_server::Counter, Writer>(p);
}
}
#[test]
fn teardown_skips_sentinel_when_journal_failed_and_ring_full() {
let (mut handles, mut consumer) = teardown_fixture(4);
for seq in 0..4 {
assert!(
handles
.input_producer
.try_publish(InputSlot {
sequence: seq,
..InputSlot::default()
})
.is_ok(),
"ring must have space while filling"
);
}
assert!(
handles
.input_producer
.try_publish(InputSlot::default())
.is_err(),
"ring must be full"
);
handles.journal_failed.store(true, Ordering::Release);
assert!(matches!(
teardown_replica_pipeline::<counter_server::Counter, u32>(handles),
TeardownOutcome::Clean(..)
));
while let Some((_, slot)) = consumer.try_consume() {
assert!(!slot.event.is_shutdown(), "no sentinel may be published");
}
}
#[test]
fn pending_ack_queue_wraps_around() {
let mut q = PendingAckQueue::new(8);
let cursor = make_journal_cursor(100);
for round in 0..3 {
for i in 0..8 {
let target = (round * 8 + i) as u64 + 1;
q.push(target, target * 10);
}
assert!(q.is_full());
let seq = q.pop_ready(&cursor).expect("should be ready");
assert_eq!(seq, (round * 8 + 8) as u64 * 10);
assert!(q.is_empty());
}
}
#[test]
fn pending_ack_queue_pop_all_blocking_empty() {
let mut q = PendingAckQueue::new(8);
let cursor = make_journal_cursor(0);
assert!(
q.pop_all_blocking(&cursor, true, &AtomicBool::new(false))
.is_none()
);
}
#[test]
fn dual_track_returns_none_when_both_tracks_idle() {
let mut q = PendingAckQueue::new(8);
let cursor = make_journal_cursor(0);
assert!(
try_flush_dual_track(&mut q, &cursor, 0, 0, 0).is_none(),
"no advance on either track → no ack"
);
}
#[test]
fn dual_track_fires_on_persisted_advance_only() {
let mut q = PendingAckQueue::new(8);
q.push(10, 100);
let cursor = make_journal_cursor(20);
let ack = try_flush_dual_track(&mut q, &cursor, 50, 0, 50).expect("persisted advanced");
assert_eq!(ack.acked_sequence, 100);
assert_eq!(ack.in_memory_sequence, 50);
}
#[test]
fn dual_track_fires_on_in_memory_advance_only() {
let mut q = PendingAckQueue::new(8);
let cursor = make_journal_cursor(0);
let ack = try_flush_dual_track(&mut q, &cursor, 200, 100, 100).expect("in-memory advanced");
assert_eq!(ack.acked_sequence, 100);
assert_eq!(ack.in_memory_sequence, 200);
}
#[test]
fn dual_track_coalesces_until_caller_updates_trackers() {
let mut q = PendingAckQueue::new(8);
q.push(10, 100);
let cursor = make_journal_cursor(20);
let ack1 = try_flush_dual_track(&mut q, &cursor, 50, 0, 50).expect("call 1 fires");
assert_eq!(ack1.acked_sequence, 100);
let ack2 = try_flush_dual_track(&mut q, &cursor, 80, 0, 50)
.expect("call 2 fires on in-memory advance");
assert_eq!(
ack2.acked_sequence, 0,
"no new persisted pop → unwrap_or(last_sent)"
);
assert_eq!(ack2.in_memory_sequence, 80);
}
#[test]
fn dual_track_no_duplicate_ack_after_backpressure_drain() {
let mut q = PendingAckQueue::new(4);
for i in 1..=4u64 {
q.push(i * 10, i * 100);
}
assert!(q.is_full());
let cursor = make_journal_cursor(40);
let drained = q.pop_ready(&cursor).expect("all entries durable");
assert_eq!(drained, 400);
assert!(q.is_empty());
let mut last_sent_acked = drained;
let mut last_sent_in_mem = 450u64;
assert!(
try_flush_dual_track(
&mut q,
&cursor,
last_sent_in_mem,
last_sent_acked,
last_sent_in_mem,
)
.is_none(),
"no advance on either track after backpressure drain → no duplicate ack",
);
q.push(50, 500);
let cursor2 = make_journal_cursor(50);
let ack = try_flush_dual_track(&mut q, &cursor2, 500, last_sent_acked, last_sent_in_mem)
.expect("fresh batch after drain fires a new ack");
assert!(
ack.acked_sequence >= last_sent_acked,
"regression: drain sent {last_sent_acked} but next ack carries {}",
ack.acked_sequence,
);
assert_eq!(ack.acked_sequence, 500);
assert_eq!(ack.in_memory_sequence, 500);
last_sent_acked = ack.acked_sequence;
last_sent_in_mem = ack.in_memory_sequence;
assert!(
try_flush_dual_track(
&mut q,
&cursor2,
last_sent_in_mem,
last_sent_acked,
last_sent_in_mem,
)
.is_none(),
"post-resume idle → no further ack",
);
}
}