decision maker perf (#30618)

This commit is contained in:
Andrew Fitzgerald 2023-04-12 21:40:59 -07:00 committed by GitHub
parent c834d2fc95
commit c847236147
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 66 additions and 79 deletions

View File

@ -42,50 +42,51 @@ impl DecisionMaker {
} }
pub(crate) fn make_consume_or_forward_decision(&self) -> BufferedPacketsDecision { pub(crate) fn make_consume_or_forward_decision(&self) -> BufferedPacketsDecision {
let (leader_at_slot_offset, bank_start, would_be_leader, would_be_leader_shortly) = { let decision;
let poh = self.poh_recorder.read().unwrap(); {
let bank_start = poh let poh_recorder = self.poh_recorder.read().unwrap();
.bank_start() decision = Self::consume_or_forward_packets(
.filter(|bank_start| bank_start.should_working_bank_still_be_processing_txs()); &self.my_pubkey,
( || {
poh.leader_after_n_slots(FORWARD_TRANSACTIONS_TO_LEADER_AT_SLOT_OFFSET), poh_recorder.bank_start().filter(|bank_start| {
bank_start, bank_start.should_working_bank_still_be_processing_txs()
poh.would_be_leader(HOLD_TRANSACTIONS_SLOT_OFFSET * DEFAULT_TICKS_PER_SLOT), })
poh.would_be_leader( },
(FORWARD_TRANSACTIONS_TO_LEADER_AT_SLOT_OFFSET - 1) * DEFAULT_TICKS_PER_SLOT, || {
), poh_recorder
) .would_be_leader(HOLD_TRANSACTIONS_SLOT_OFFSET * DEFAULT_TICKS_PER_SLOT)
}; },
|| {
poh_recorder
.would_be_leader(HOLD_TRANSACTIONS_SLOT_OFFSET * DEFAULT_TICKS_PER_SLOT)
},
|| poh_recorder.leader_after_n_slots(FORWARD_TRANSACTIONS_TO_LEADER_AT_SLOT_OFFSET),
);
}
Self::consume_or_forward_packets( decision
&self.my_pubkey,
leader_at_slot_offset,
bank_start,
would_be_leader,
would_be_leader_shortly,
)
} }
fn consume_or_forward_packets( fn consume_or_forward_packets(
my_pubkey: &Pubkey, my_pubkey: &Pubkey,
leader_pubkey: Option<Pubkey>, bank_start_fn: impl FnOnce() -> Option<BankStart>,
bank_start: Option<BankStart>, would_be_leader_shortly_fn: impl FnOnce() -> bool,
would_be_leader: bool, would_be_leader_fn: impl FnOnce() -> bool,
would_be_leader_shortly: bool, leader_pubkey_fn: impl FnOnce() -> Option<Pubkey>,
) -> BufferedPacketsDecision { ) -> BufferedPacketsDecision {
// If has active bank, then immediately process buffered packets // If has active bank, then immediately process buffered packets
// otherwise, based on leader schedule to either forward or hold packets // otherwise, based on leader schedule to either forward or hold packets
if let Some(bank_start) = bank_start { if let Some(bank_start) = bank_start_fn() {
// If the bank is available, this node is the leader // If the bank is available, this node is the leader
BufferedPacketsDecision::Consume(bank_start) BufferedPacketsDecision::Consume(bank_start)
} else if would_be_leader_shortly { } else if would_be_leader_shortly_fn() {
// If the node will be the leader soon, hold the packets for now // If the node will be the leader soon, hold the packets for now
BufferedPacketsDecision::Hold BufferedPacketsDecision::Hold
} else if would_be_leader { } else if would_be_leader_fn() {
// Node will be leader within ~20 slots, hold the transactions in // Node will be leader within ~20 slots, hold the transactions in
// case it is the only node which produces an accepted slot. // case it is the only node which produces an accepted slot.
BufferedPacketsDecision::ForwardAndHold BufferedPacketsDecision::ForwardAndHold
} else if let Some(x) = leader_pubkey { } else if let Some(x) = leader_pubkey_fn() {
if x != *my_pubkey { if x != *my_pubkey {
// If the current node is not the leader, forward the buffered packets // If the current node is not the leader, forward the buffered packets
BufferedPacketsDecision::Forward BufferedPacketsDecision::Forward
@ -104,6 +105,7 @@ impl DecisionMaker {
mod tests { mod tests {
use { use {
super::*, super::*,
core::panic,
solana_runtime::bank::Bank, solana_runtime::bank::Bank,
std::{sync::Arc, time::Instant}, std::{sync::Arc, time::Instant},
}; };
@ -138,82 +140,67 @@ mod tests {
assert_matches!( assert_matches!(
DecisionMaker::consume_or_forward_packets( DecisionMaker::consume_or_forward_packets(
&my_pubkey, &my_pubkey,
None, || bank_start.clone(),
bank_start.clone(), || panic!("should not be called"),
false, || panic!("should not be called"),
false || panic!("should not be called")
), ),
BufferedPacketsDecision::Consume(_) BufferedPacketsDecision::Consume(_)
); );
assert_matches!( // Unknown leader, hold the packets
DecisionMaker::consume_or_forward_packets(&my_pubkey, None, None, false, false),
BufferedPacketsDecision::Hold
);
assert_matches!(
DecisionMaker::consume_or_forward_packets(&my_pubkey1, None, None, false, false),
BufferedPacketsDecision::Hold
);
assert_matches!( assert_matches!(
DecisionMaker::consume_or_forward_packets( DecisionMaker::consume_or_forward_packets(
&my_pubkey, &my_pubkey,
Some(my_pubkey1), || None,
None, || false,
false, || false,
false || None
),
BufferedPacketsDecision::Hold
);
// Leader other than me, forward the packets
assert_matches!(
DecisionMaker::consume_or_forward_packets(
&my_pubkey,
|| None,
|| false,
|| false,
|| Some(my_pubkey1),
), ),
BufferedPacketsDecision::Forward BufferedPacketsDecision::Forward
); );
// Will be leader shortly, hold the packets
assert_matches!( assert_matches!(
DecisionMaker::consume_or_forward_packets( DecisionMaker::consume_or_forward_packets(
&my_pubkey, &my_pubkey,
Some(my_pubkey1), || None,
None, || true,
true, || panic!("should not be called"),
true || panic!("should not be called"),
), ),
BufferedPacketsDecision::Hold BufferedPacketsDecision::Hold
); );
// Will be leader (not shortly), forward and hold
assert_matches!( assert_matches!(
DecisionMaker::consume_or_forward_packets( DecisionMaker::consume_or_forward_packets(
&my_pubkey, &my_pubkey,
Some(my_pubkey1), || None,
None, || false,
true, || true,
false || panic!("should not be called"),
), ),
BufferedPacketsDecision::ForwardAndHold BufferedPacketsDecision::ForwardAndHold
); );
assert_matches!( // Current leader matches my pubkey, hold
DecisionMaker::consume_or_forward_packets(
&my_pubkey,
Some(my_pubkey1),
bank_start.clone(),
false,
false
),
BufferedPacketsDecision::Consume(_)
);
assert_matches!( assert_matches!(
DecisionMaker::consume_or_forward_packets( DecisionMaker::consume_or_forward_packets(
&my_pubkey1, &my_pubkey1,
Some(my_pubkey1), || None,
None, || false,
false, || false,
false || Some(my_pubkey1),
), ),
BufferedPacketsDecision::Hold BufferedPacketsDecision::Hold
); );
assert_matches!(
DecisionMaker::consume_or_forward_packets(
&my_pubkey1,
Some(my_pubkey1),
bank_start,
false,
false
),
BufferedPacketsDecision::Consume(_)
);
} }
} }