From c7ffbcbff5c80a408250abc7355940e053706a9b Mon Sep 17 00:00:00 2001 From: "Claude Sonnet 5.5" Date: Sat, 14 Nov 2026 02:06:34 -0800 Subject: [PATCH] A leader queues its append packets before its own fsync The metadata and chunk consensus loops handed a Ready's packets to the peer queues only after process_ready had synced the journal, so a leader's replication to its followers waited behind its own fsync. A leader's append and heartbeat packets do not depend on its own copy being durable: matrixraft counts the leader's entry toward a quorum only after it reports the Ready persisted. matrixraft can hand those packets over before the sync through an opt-in sink, so install one at the start of each loop, and let the packets that do depend on durability keep returning from process_ready and queueing after it. The two places that queue packets now share one helper, queue_packets, so the packets a Ready returns after it is durable and the packets a leader hands over before it is durable follow a single rule: each goes to the queue of the peer it names, a full queue is counted and never waited on, and a packet for a peer with no queue is dropped. Measured through this repository's three-process chunk group, 200 sequential 256-byte replicated writes, 8 interleaved rounds: median 15.2 ms without the sink and 10.0 ms with it, faster in all 8 paired rounds. A debug build on a shared disk, one fixture. Co-Authored-By: Claude Sonnet 5.5 --- src/cluster/chunk_consensus.rs | 33 ++++++++++----- src/cluster/meta_consensus.rs | 33 ++++++++++----- src/cluster/peer_delivery.rs | 74 ++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 22 deletions(-) diff --git a/src/cluster/chunk_consensus.rs b/src/cluster/chunk_consensus.rs index 9844b998..5072c085 100644 --- a/src/cluster/chunk_consensus.rs +++ b/src/cluster/chunk_consensus.rs @@ -547,6 +547,22 @@ fn run( packets, outgoing, } = channels; + // A leader's append packets do not depend on its own copy being durable, so they leave as soon + // as the Ready is taken, before its journal write and fsync, and replication to the followers + // overlaps the leader's own fsync. Everything that does depend on durability is returned by + // `process_ready` and queued after it. + { + let (early, counters) = (outgoing.clone(), progress.peer_deliveries()); + peer.set_early_packet_sink(Some(Box::new(move |packets| { + crate::cluster::peer_delivery::queue_packets( + &early, + &counters, + NETWORK_TIMEOUT, + packets, + ); + Ok(()) + }))); + } let mut tick = Instant::now(); let mut last_request = Instant::now(); let mut pending: VecDeque = VecDeque::new(); @@ -705,17 +721,12 @@ fn run( ))); } } - for packet in output { - if let Some(tx) = outgoing.get(&packet.to) { - if tx.try_send(crate::cluster::peer_delivery::PeerDelivery::observed( - packet.bytes, - NETWORK_TIMEOUT, - progress.peer_deliveries(), - )).is_err() { - progress.peer_deliveries().queue_rejected(); - } - } - } + crate::cluster::peer_delivery::queue_packets( + &outgoing, + &progress.peer_deliveries(), + NETWORK_TIMEOUT, + output, + ); if pending .front() .is_some_and(|p| !peer.is_leader() || p.id.term != peer.term()) diff --git a/src/cluster/meta_consensus.rs b/src/cluster/meta_consensus.rs index 2ae452ed..c57f6d6e 100644 --- a/src/cluster/meta_consensus.rs +++ b/src/cluster/meta_consensus.rs @@ -412,6 +412,22 @@ fn run( packets, outgoing, } = channels; + // A leader's append packets do not depend on its own copy being durable, so they leave as soon + // as the Ready is taken, before its journal write and fsync, and replication to the followers + // overlaps the leader's own fsync. Everything that does depend on durability is returned by + // `process_ready` and queued after it. + { + let (early, counters) = (outgoing.clone(), progress.peer_deliveries()); + peer.set_early_packet_sink(Some(Box::new(move |packets| { + crate::cluster::peer_delivery::queue_packets( + &early, + &counters, + NETWORK_TIMEOUT, + packets, + ); + Ok(()) + }))); + } let mut tick = Instant::now(); let mut lease_sweep = Instant::now(); let mut pending: VecDeque = VecDeque::new(); @@ -501,17 +517,12 @@ fn run( ))); } } - for packet in output { - if let Some(tx) = outgoing.get(&packet.to) { - if tx.try_send(crate::cluster::peer_delivery::PeerDelivery::observed( - packet.bytes, - NETWORK_TIMEOUT, - progress.peer_deliveries(), - )).is_err() { - progress.peer_deliveries().queue_rejected(); - } - } - } + crate::cluster::peer_delivery::queue_packets( + &outgoing, + &progress.peer_deliveries(), + NETWORK_TIMEOUT, + output, + ); if pending .front() .is_some_and(|p| !peer.is_leader() || p.id.term != peer.term()) diff --git a/src/cluster/peer_delivery.rs b/src/cluster/peer_delivery.rs index ce34cbdf..b42ba3b7 100644 --- a/src/cluster/peer_delivery.rs +++ b/src/cluster/peer_delivery.rs @@ -32,6 +32,31 @@ impl PeerDeliveryCounters { } } +/// Queue `packets` for the peers they name. A packet whose peer has no queue is dropped, and one +/// whose queue is full is counted as rejected. The packets a Ready returns after it is durable and +/// the packets a leader hands over before it is durable go through this one rule. +pub(super) fn queue_packets( + outgoing: &std::collections::BTreeMap>, + counters: &PeerDeliveryCounters, + timeout: Duration, + packets: Vec, +) { + for packet in packets { + if let Some(tx) = outgoing.get(&packet.to) { + if tx + .try_send(PeerDelivery::observed( + packet.bytes, + timeout, + counters.clone(), + )) + .is_err() + { + counters.queue_rejected(); + } + } + } +} + pub(super) struct PeerDelivery { bytes: Vec, deadline: Instant, @@ -301,3 +326,52 @@ mod tests { ); } } + +#[cfg(test)] +mod queue_packet_tests { + use super::*; + use std::collections::BTreeMap; + use std::sync::mpsc::sync_channel; + + fn packet(to: u64, byte: u8) -> matrixraft::peer_runtime::PeerPacket { + matrixraft::peer_runtime::PeerPacket { + to, + bytes: vec![byte], + } + } + + /// The rule shared by the packets a Ready returns after it is durable and the packets a leader + /// hands over before: each goes to the queue of the peer it names, a full queue is counted and + /// never waited on, and a packet for a peer with no queue is dropped. + #[test] + fn a_packet_goes_to_its_peer_and_a_full_queue_is_counted_not_waited_on() { + let (one, one_rx) = sync_channel(1); + let (two, two_rx) = sync_channel(8); + let outgoing = BTreeMap::from([(1u64, one), (2u64, two)]); + let counters = PeerDeliveryCounters::default(); + + queue_packets( + &outgoing, + &counters, + Duration::from_secs(1), + vec![packet(1, 10), packet(1, 11), packet(2, 20), packet(9, 90)], + ); + + assert!(one_rx.try_recv().is_ok(), "peer 1 gets its first packet"); + assert!( + one_rx.try_recv().is_err(), + "peer 1's queue holds one, so its second packet was dropped, not queued" + ); + assert!(two_rx.try_recv().is_ok(), "peer 2 gets its packet"); + assert!(two_rx.try_recv().is_err()); + let described = counters.describe(); + assert!( + described.contains("peer_attempted=3;"), + "three packets named a peer with a queue: {described}" + ); + assert!( + described.contains("peer_queue_rejected=1;"), + "the second packet to peer 1 found its queue full: {described}" + ); + } +}