Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce InstalledSchedulerPool trait #33934

Merged
merged 7 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
19 changes: 5 additions & 14 deletions ledger/src/blockstore_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1945,9 +1945,7 @@ pub mod tests {
genesis_utils::{
self, create_genesis_config_with_vote_accounts, ValidatorVoteKeypairs,
},
installed_scheduler_pool::{
MockInstalledScheduler, MockInstalledSchedulerPool, WaitReason,
},
installed_scheduler_pool::{MockInstalledScheduler, SchedulingContext, WaitReason},
},
solana_sdk::{
account::{AccountSharedData, WritableAccount},
Expand Down Expand Up @@ -4529,6 +4527,7 @@ pub mod tests {
..
} = create_genesis_config_with_leader(500, &dummy_leader_pubkey, 100);
let bank = Arc::new(Bank::new_for_tests(&genesis_config));
let context = SchedulingContext::new(bank.clone());

let txs = create_test_transactions(&mint_keypair, &genesis_config.hash());

Expand All @@ -4538,7 +4537,7 @@ pub mod tests {
.expect_context()
.times(1)
.in_sequence(&mut seq)
.returning(|| None);
.return_const(context);
mocked_scheduler
.expect_schedule_execution()
.times(txs.len())
Expand All @@ -4550,18 +4549,10 @@ pub mod tests {
.in_sequence(&mut seq)
.returning(|_| None);
mocked_scheduler
.expect_pool()
.expect_return_to_pool()
.times(1)
.in_sequence(&mut seq)
.returning(move || {
let mut mocked_pool = MockInstalledSchedulerPool::new();
mocked_pool
.expect_return_to_pool()
.times(1)
.in_sequence(&mut seq)
.returning(|_| ());
Arc::new(mocked_pool)
});
.returning(|| ());
let bank = BankWithScheduler::new(bank, Some(Box::new(mocked_scheduler)));

let batch = bank.prepare_sanitized_batch(&txs);
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/bank_forks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl BankForks {
let bank = Arc::new(bank);
let bank = if let Some(scheduler_pool) = &self.scheduler_pool {
let context = SchedulingContext::new(bank.clone());
let scheduler = scheduler_pool.take_from_pool(context);
let scheduler = scheduler_pool.take_scheduler(context);
BankWithScheduler::new(bank, Some(scheduler))
} else {
BankWithScheduler::new_without_scheduler(bank)
Expand Down
51 changes: 24 additions & 27 deletions runtime/src/installed_scheduler_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ use {
#[cfg(feature = "dev-context-only-utils")]
use {mockall::automock, qualifier_attr::qualifiers};

#[cfg_attr(feature = "dev-context-only-utils", automock)]
pub trait InstalledSchedulerPool: Send + Sync + Debug {
fn take_from_pool(&self, context: SchedulingContext) -> DefaultInstalledSchedulerBox;
fn return_to_pool(&self, scheduler: DefaultInstalledSchedulerBox);
fn take_scheduler(&self, context: SchedulingContext) -> DefaultInstalledSchedulerBox;
}

#[cfg_attr(doc, aquamarine::aquamarine)]
Expand Down Expand Up @@ -101,7 +99,7 @@ pub trait InstalledSchedulerPool: Send + Sync + Debug {
)]
pub trait InstalledScheduler: Send + Sync + Debug + 'static {
fn id(&self) -> SchedulerId;
fn pool(&self) -> InstalledSchedulerPoolArc;
fn context(&self) -> &SchedulingContext;

// Calling this is illegal as soon as wait_for_termination is called.
fn schedule_execution<'a>(
Expand All @@ -127,8 +125,7 @@ pub trait InstalledScheduler: Send + Sync + Debug + 'static {
#[must_use]
fn wait_for_termination(&mut self, reason: &WaitReason) -> Option<ResultWithTimings>;

fn context<'a>(&'a self) -> Option<&'a SchedulingContext>;
fn replace_context(&mut self, context: SchedulingContext);
fn return_to_pool(self: Box<Self>);
}

pub type DefaultInstalledSchedulerBox = Box<dyn InstalledScheduler>;
Expand Down Expand Up @@ -229,9 +226,11 @@ pub type InstalledSchedulerRwLock = RwLock<Option<DefaultInstalledSchedulerBox>>
impl BankWithScheduler {
#[cfg_attr(feature = "dev-context-only-utils", qualifiers(pub))]
pub(crate) fn new(bank: Arc<Bank>, scheduler: Option<DefaultInstalledSchedulerBox>) -> Self {
if let Some(bank_in_context) = scheduler.as_ref().and_then(|scheduler| scheduler.context())
if let Some(bank_in_context) = scheduler
.as_ref()
.map(|scheduler| scheduler.context().bank())
{
assert_eq!(bank.slot(), bank_in_context.slot());
assert!(Arc::ptr_eq(&bank, bank_in_context));
apfitzge marked this conversation as resolved.
Show resolved Hide resolved
apfitzge marked this conversation as resolved.
Show resolved Hide resolved
}

Self {
Expand Down Expand Up @@ -347,7 +346,7 @@ impl BankWithSchedulerInner {
.and_then(|scheduler| scheduler.wait_for_termination(&reason));
if !reason.is_paused() {
let scheduler = scheduler.take().expect("scheduler after waiting");
scheduler.pool().return_to_pool(scheduler);
scheduler.return_to_pool();
}
result_with_timings
} else {
Expand Down Expand Up @@ -413,16 +412,8 @@ mod tests {
solana_sdk::system_transaction,
};

fn setup_mocked_scheduler_pool(seq: &mut Sequence) -> InstalledSchedulerPoolArc {
let mut mock = MockInstalledSchedulerPool::new();
mock.expect_return_to_pool()
.times(1)
.in_sequence(seq)
.returning(|_| ());
Arc::new(mock)
}

fn setup_mocked_scheduler_with_extra(
bank: &Arc<Bank>,
wait_reasons: impl Iterator<Item = WaitReason>,
f: Option<impl Fn(&mut MockInstalledScheduler)>,
) -> DefaultInstalledSchedulerBox {
Expand All @@ -432,7 +423,7 @@ mod tests {
mock.expect_context()
.times(1)
.in_sequence(&mut seq)
.returning(|| None);
.return_const(SchedulingContext::new(bank.clone()));

for wait_reason in wait_reasons {
mock.expect_wait_for_termination()
Expand All @@ -448,10 +439,10 @@ mod tests {
});
}

mock.expect_pool()
mock.expect_return_to_pool()
.times(1)
.in_sequence(&mut seq)
.returning(move || setup_mocked_scheduler_pool(&mut seq));
.returning(|| ());
if let Some(f) = f {
f(&mut mock);
}
Expand All @@ -460,9 +451,11 @@ mod tests {
}

fn setup_mocked_scheduler(
bank: &Arc<Bank>,
wait_reasons: impl Iterator<Item = WaitReason>,
) -> DefaultInstalledSchedulerBox {
setup_mocked_scheduler_with_extra(
bank,
wait_reasons,
None::<fn(&mut MockInstalledScheduler) -> ()>,
)
Expand All @@ -472,10 +465,11 @@ mod tests {
fn test_scheduler_normal_termination() {
solana_logger::setup();

let bank = Arc::new(Bank::default_for_tests());
let bank = &Arc::new(Bank::default_for_tests());
let bank = BankWithScheduler::new(
bank,
bank.clone(),
Some(setup_mocked_scheduler(
bank,
[WaitReason::TerminatedToFreeze].into_iter(),
)),
);
Expand Down Expand Up @@ -504,10 +498,11 @@ mod tests {
fn test_scheduler_termination_from_drop() {
solana_logger::setup();

let bank = Arc::new(Bank::default_for_tests());
let bank = &Arc::new(Bank::default_for_tests());
let bank = BankWithScheduler::new(
bank,
bank.clone(),
Some(setup_mocked_scheduler(
bank,
[WaitReason::DroppedFromBankForks].into_iter(),
)),
);
Expand All @@ -518,10 +513,11 @@ mod tests {
fn test_scheduler_pause() {
solana_logger::setup();

let bank = Arc::new(crate::bank::tests::create_simple_test_bank(42));
let bank = &Arc::new(crate::bank::tests::create_simple_test_bank(42));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why all these changes to use an &Arc<..> instead of owned Arc?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no strong reason other than to minimize diff. ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, thanks for pointing out. i've found way of further reducing the diff: 5df66e0

let bank = BankWithScheduler::new(
bank,
bank.clone(),
Some(setup_mocked_scheduler(
bank,
[
WaitReason::PausedForRecentBlockhash,
WaitReason::TerminatedToFreeze,
Expand Down Expand Up @@ -550,6 +546,7 @@ mod tests {
));
let bank = Arc::new(Bank::new_for_tests(&genesis_config));
let mocked_scheduler = setup_mocked_scheduler_with_extra(
&bank,
[WaitReason::DroppedFromBankForks].into_iter(),
Some(|mocked: &mut MockInstalledScheduler| {
mocked
Expand Down