forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 380
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
Make unified scheduler's new task code fallible #1071
Closed
ryoqun
wants to merge
2
commits into
anza-xyz:master
from
ryoqun:unified-scheduler-fallible-new-task
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ use { | |
solana_sdk::{ | ||
hash::Hash, | ||
slot_history::Slot, | ||
transaction::{Result, SanitizedTransaction}, | ||
transaction::{Result, SanitizedTransaction, TransactionError}, | ||
}, | ||
std::{ | ||
fmt::Debug, | ||
|
@@ -42,6 +42,10 @@ pub trait InstalledSchedulerPool: Send + Sync + Debug { | |
fn take_scheduler(&self, context: SchedulingContext) -> InstalledSchedulerBox; | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct SchedulerAborted; | ||
pub type ScheduleResult = std::result::Result<(), SchedulerAborted>; | ||
|
||
#[cfg_attr(doc, aquamarine::aquamarine)] | ||
/// Schedules, executes, and commits transactions under encapsulated implementation | ||
/// | ||
|
@@ -101,17 +105,51 @@ pub trait InstalledScheduler: Send + Sync + Debug + 'static { | |
fn id(&self) -> SchedulerId; | ||
fn context(&self) -> &SchedulingContext; | ||
|
||
// Calling this is illegal as soon as wait_for_termination is called. | ||
/// Schedule transaction for execution. | ||
/// | ||
/// This non-blocking function will return immediately without waiting for actual execution. | ||
/// | ||
/// Calling this is illegal as soon as `wait_for_termination()` is called. It would result in | ||
/// fatal logic error. | ||
/// | ||
/// Note that the returned result indicates whether the scheduler has been aborted due to a | ||
/// previously-scheduled bad transaction, which terminates further block verification. So, | ||
/// almost always, the returned error isn't due to the merely scheduling of the current | ||
/// transaction itself. At this point, calling this does nothing anymore while it's still safe | ||
/// to do. As soon as notified, callers are expected to stop processing upcoming transactions | ||
/// of the same `SchedulingContext` (i.e. same block). Internally, the aborted scheduler will | ||
/// be disposed cleanly, not repooled, after `wait_for_termination()` is called like | ||
/// not-aborted schedulers. | ||
/// | ||
/// Caller can acquire the error by calling a separate function called | ||
/// `recover_error_after_abort()`, which requires `&mut self`, instead of `&self`. This | ||
/// separation and the convoluted returned value semantics explained above are intentional to | ||
/// optimize the fast code-path of normal transaction scheduling to be multi-threaded at the | ||
/// cost of far slower error code-path while giving implementors increased flexibility by | ||
/// having &mut. | ||
fn schedule_execution<'a>( | ||
&'a self, | ||
transaction_with_index: &'a (&'a SanitizedTransaction, usize), | ||
); | ||
) -> ScheduleResult; | ||
|
||
/// Return the error which caused the scheduler to abort. | ||
/// | ||
/// Note that this must not be called until it's observed that `schedule_execution()` has | ||
/// returned `Err(SchedulerAborted)`. Violating this will `panic!()`. | ||
/// | ||
/// That said, calling this multiple times is completely acceptable after the error observation | ||
/// from `schedule_execution()`. While it's not guaranteed, the same `.clone()`-ed errors of | ||
/// the first bad transaction are usually returned across invocations. | ||
fn recover_error_after_abort(&mut self) -> TransactionError; | ||
|
||
/// Wait for a scheduler to terminate after processing. | ||
/// | ||
/// This function blocks the current thread while waiting for the scheduler to complete all of | ||
/// the executions for the scheduled transactions and to return the finalized | ||
/// `ResultWithTimings`. Along with the result, this function also makes the scheduler itself | ||
/// `ResultWithTimings`. This function still blocks for short period of time even in the case | ||
/// of aborted schedulers to gracefully shutdown the scheduler (like thread joining). | ||
/// | ||
/// Along with the result being returned, this function also makes the scheduler itself | ||
/// uninstalled from the bank by transforming the consumed self. | ||
/// | ||
/// If no transaction is scheduled, the result and timing will be `Ok(())` and | ||
|
@@ -286,11 +324,15 @@ impl BankWithScheduler { | |
self.inner.scheduler.read().unwrap().is_some() | ||
} | ||
|
||
/// Schedule the transaction as long as the scheduler hasn't been aborted. | ||
/// | ||
/// If the scheduler has been aborted, this doesn't schedule the transaction, instead just | ||
/// return the error of prior scheduled transaction. | ||
// 'a is needed; anonymous_lifetime_in_impl_trait isn't stabilized yet... | ||
pub fn schedule_transaction_executions<'a>( | ||
&self, | ||
transactions_with_indexes: impl ExactSizeIterator<Item = (&'a SanitizedTransaction, &'a usize)>, | ||
) { | ||
) -> Result<()> { | ||
trace!( | ||
"schedule_transaction_executions(): {} txs", | ||
transactions_with_indexes.len() | ||
|
@@ -300,8 +342,25 @@ impl BankWithScheduler { | |
let scheduler = scheduler_guard.as_ref().unwrap(); | ||
|
||
for (sanitized_transaction, &index) in transactions_with_indexes { | ||
scheduler.schedule_execution(&(sanitized_transaction, index)); | ||
if scheduler | ||
.schedule_execution(&(sanitized_transaction, index)) | ||
.is_err() | ||
{ | ||
drop(scheduler_guard); | ||
// This write lock isn't atomic with the above the read lock. So, another thread | ||
// could have called .recover_error_after_abort() while we're literally stuck at | ||
// the gaps of these locks (i.e. this comment in source code wise) under extreme | ||
// race conditions. Thus, .recover_error_after_abort() is made idempotetnt for that | ||
// consideration in mind. | ||
// | ||
// Lastly, this non-atomic nature is intentional for optimizing the fast code-path | ||
let mut scheduler_guard = self.inner.scheduler.write().unwrap(); | ||
let scheduler = scheduler_guard.as_mut().unwrap(); | ||
return Err(scheduler.recover_error_after_abort()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while the current actual wip |
||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
// take needless &mut only to communicate its semantic mutability to humans... | ||
|
@@ -550,8 +609,7 @@ mod tests { | |
assert_matches!(bank.wait_for_completed_scheduler(), Some(_)); | ||
} | ||
|
||
#[test] | ||
fn test_schedule_executions() { | ||
fn do_test_schedule_execution(should_succeed: bool) { | ||
solana_logger::setup(); | ||
|
||
let GenesisConfigInfo { | ||
|
@@ -570,14 +628,40 @@ mod tests { | |
bank.clone(), | ||
[true].into_iter(), | ||
Some(|mocked: &mut MockInstalledScheduler| { | ||
mocked | ||
.expect_schedule_execution() | ||
.times(1) | ||
.returning(|(_, _)| ()); | ||
if should_succeed { | ||
mocked | ||
.expect_schedule_execution() | ||
.times(1) | ||
.returning(|(_, _)| Ok(())); | ||
} else { | ||
mocked | ||
.expect_schedule_execution() | ||
.times(1) | ||
.returning(|(_, _)| Err(SchedulerAborted)); | ||
mocked | ||
.expect_recover_error_after_abort() | ||
.times(1) | ||
.returning(|| TransactionError::InsufficientFundsForFee); | ||
} | ||
}), | ||
); | ||
|
||
let bank = BankWithScheduler::new(bank, Some(mocked_scheduler)); | ||
bank.schedule_transaction_executions([(&tx0, &0)].into_iter()); | ||
let result = bank.schedule_transaction_executions([(&tx0, &0)].into_iter()); | ||
if should_succeed { | ||
assert_matches!(result, Ok(())); | ||
} else { | ||
assert_matches!(result, Err(TransactionError::InsufficientFundsForFee)); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_schedule_execution_success() { | ||
do_test_schedule_execution(true); | ||
} | ||
|
||
#[test] | ||
fn test_schedule_execution_failure() { | ||
do_test_schedule_execution(false); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the new fn for fallible new task code-path