Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/cluster/chunk_consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pending> = VecDeque::new();
Expand Down Expand Up @@ -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())
Expand Down
33 changes: 22 additions & 11 deletions src/cluster/meta_consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pending> = VecDeque::new();
Expand Down Expand Up @@ -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())
Expand Down
74 changes: 74 additions & 0 deletions src/cluster/peer_delivery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64, std::sync::mpsc::SyncSender<PeerDelivery>>,
counters: &PeerDeliveryCounters,
timeout: Duration,
packets: Vec<matrixraft::peer_runtime::PeerPacket>,
) {
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<u8>,
deadline: Instant,
Expand Down Expand Up @@ -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}"
);
}
}
Loading