forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 378
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
Reapply PR#31: optimize retry pool #5113
Open
KirillLykov
wants to merge
1
commit into
anza-xyz:master
Choose a base branch
from
KirillLykov:klykov/reapply-pr31
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+82
−39
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,7 @@ use { | |
hash::Hash, nonce_account, pubkey::Pubkey, saturating_add_assign, signature::Signature, | ||
}, | ||
std::{ | ||
collections::{ | ||
hash_map::{Entry, HashMap}, | ||
HashSet, | ||
}, | ||
collections::hash_map::{Entry, HashMap}, | ||
net::SocketAddr, | ||
sync::{ | ||
atomic::{AtomicBool, Ordering}, | ||
|
@@ -99,6 +96,16 @@ impl TransactionInfo { | |
last_sent_time, | ||
} | ||
} | ||
|
||
fn get_max_retries( | ||
&self, | ||
default_max_retries: Option<usize>, | ||
service_max_retries: usize, | ||
) -> Option<usize> { | ||
self.max_retries | ||
.or(default_max_retries) | ||
.map(|max_retries| max_retries.min(service_max_retries)) | ||
} | ||
} | ||
|
||
#[derive(Default, Debug, PartialEq, Eq)] | ||
|
@@ -109,6 +116,7 @@ struct ProcessTransactionsResult { | |
max_retries_elapsed: u64, | ||
failed: u64, | ||
retained: u64, | ||
last_sent_time: Option<Instant>, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
|
@@ -236,6 +244,8 @@ impl SendTransactionService { | |
batch_send_rate_ms, | ||
batch_size, | ||
retry_pool_max_size, | ||
default_max_retries, | ||
service_max_retries, | ||
.. | ||
}: Config, | ||
stats_report: Arc<SendTransactionServiceStatsReport>, | ||
|
@@ -298,9 +308,17 @@ impl SendTransactionService { | |
{ | ||
// take a lock of retry_transactions and move the batch to the retry set. | ||
let mut retry_transactions = retry_transactions.lock().unwrap(); | ||
let transactions_to_retry = transactions.len(); | ||
let mut transactions_to_retry: usize = 0; | ||
let mut transactions_added_to_retry: usize = 0; | ||
for (signature, mut transaction_info) in transactions.drain() { | ||
// drop transactions with 0 max retries | ||
let max_retries = transaction_info | ||
.get_max_retries(default_max_retries, service_max_retries); | ||
if max_retries == Some(0) { | ||
continue; | ||
} | ||
transactions_to_retry += 1; | ||
|
||
let retry_len = retry_transactions.len(); | ||
let entry = retry_transactions.entry(signature); | ||
if let Entry::Vacant(_) = entry { | ||
|
@@ -339,19 +357,20 @@ impl SendTransactionService { | |
exit: Arc<AtomicBool>, | ||
) -> JoinHandle<()> { | ||
debug!("Starting send-transaction-service::retry_thread."); | ||
let retry_interval_ms_default = MAX_RETRY_SLEEP_MS.min(config.retry_rate_ms); | ||
let mut retry_interval_ms = retry_interval_ms_default; | ||
Builder::new() | ||
.name("solStxRetry".to_string()) | ||
.spawn(move || loop { | ||
let retry_interval_ms = config.retry_rate_ms; | ||
let stats = &stats_report.stats; | ||
sleep(Duration::from_millis( | ||
MAX_RETRY_SLEEP_MS.min(retry_interval_ms), | ||
)); | ||
sleep(Duration::from_millis(retry_interval_ms)); | ||
if exit.load(Ordering::Relaxed) { | ||
break; | ||
} | ||
let mut transactions = retry_transactions.lock().unwrap(); | ||
if !transactions.is_empty() { | ||
if transactions.is_empty() { | ||
retry_interval_ms = retry_interval_ms_default; | ||
} else { | ||
let stats = &stats_report.stats; | ||
stats | ||
.retry_queue_size | ||
.store(transactions.len() as u64, Ordering::Relaxed); | ||
|
@@ -360,7 +379,7 @@ impl SendTransactionService { | |
(bank_forks.root_bank(), bank_forks.working_bank()) | ||
}; | ||
|
||
let _result = Self::process_transactions( | ||
let result = Self::process_transactions( | ||
&working_bank, | ||
&root_bank, | ||
&mut transactions, | ||
|
@@ -369,6 +388,17 @@ impl SendTransactionService { | |
stats, | ||
); | ||
stats_report.report(); | ||
|
||
// to send transactions as soon as possible we adjust retry interval | ||
retry_interval_ms = retry_interval_ms_default | ||
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. it means just |
||
.checked_sub( | ||
result | ||
.last_sent_time | ||
.and_then(|last| Instant::now().checked_duration_since(last)) | ||
.and_then(|interval| interval.as_millis().try_into().ok()) | ||
.unwrap_or(0), | ||
) | ||
.unwrap_or(retry_interval_ms_default); | ||
} | ||
}) | ||
.unwrap() | ||
|
@@ -391,7 +421,8 @@ impl SendTransactionService { | |
) -> ProcessTransactionsResult { | ||
let mut result = ProcessTransactionsResult::default(); | ||
|
||
let mut batched_transactions = HashSet::new(); | ||
let mut batched_transactions = Vec::new(); | ||
let mut exceeded_retries_transactions = Vec::new(); | ||
let retry_rate = Duration::from_millis(retry_rate_ms); | ||
|
||
transactions.retain(|signature, transaction_info| { | ||
|
@@ -410,7 +441,8 @@ impl SendTransactionService { | |
let now = Instant::now(); | ||
let expired = transaction_info | ||
.last_sent_time | ||
.map(|last| now.duration_since(last) >= retry_rate) | ||
.and_then(|last| now.checked_duration_since(last)) | ||
.map(|elapsed| elapsed >= retry_rate) | ||
.unwrap_or(false); | ||
let verify_nonce_account = | ||
nonce_account::verify_nonce_account(&nonce_account, &durable_nonce); | ||
|
@@ -449,21 +481,36 @@ impl SendTransactionService { | |
let now = Instant::now(); | ||
let need_send = transaction_info | ||
.last_sent_time | ||
.map(|last| now.duration_since(last) >= retry_rate) | ||
.and_then(|last| now.checked_duration_since(last)) | ||
.map(|elapsed| elapsed >= retry_rate) | ||
.unwrap_or(true); | ||
if need_send { | ||
if transaction_info.last_sent_time.is_some() { | ||
// Transaction sent before is unknown to the working bank, it might have been | ||
// dropped or landed in another fork. Re-send it | ||
// dropped or landed in another fork. Re-send it. | ||
|
||
info!("Retrying transaction: {}", signature); | ||
result.retried += 1; | ||
transaction_info.retries += 1; | ||
stats.retries.fetch_add(1, Ordering::Relaxed); | ||
} | ||
|
||
batched_transactions.insert(*signature); | ||
batched_transactions.push(*signature); | ||
transaction_info.last_sent_time = Some(now); | ||
|
||
let max_retries = transaction_info | ||
.get_max_retries(default_max_retries, service_max_retries); | ||
if let Some(max_retries) = max_retries { | ||
if transaction_info.retries >= max_retries { | ||
exceeded_retries_transactions.push(*signature); | ||
} | ||
} | ||
} else if let Some(last) = transaction_info.last_sent_time { | ||
result.last_sent_time = Some( | ||
result | ||
.last_sent_time | ||
.map(|result_last| result_last.min(last)) | ||
.unwrap_or(last), | ||
); | ||
} | ||
true | ||
} | ||
|
@@ -481,19 +528,31 @@ impl SendTransactionService { | |
} | ||
}); | ||
|
||
stats.retries.fetch_add(result.retried, Ordering::Relaxed); | ||
|
||
if !batched_transactions.is_empty() { | ||
// Processing the transactions in batch | ||
let wire_transactions = transactions | ||
let wire_transactions = batched_transactions | ||
.iter() | ||
.filter(|(signature, _)| batched_transactions.contains(signature)) | ||
.map(|(_, transaction_info)| transaction_info.wire_transaction.clone()); | ||
.filter_map(|signature| transactions.get(signature)) | ||
.map(|transaction_info| transaction_info.wire_transaction.clone()); | ||
|
||
let iter = wire_transactions.chunks(batch_size); | ||
for chunk in &iter { | ||
let chunk = chunk.collect(); | ||
client.send_transactions_in_batch(chunk, stats); | ||
} | ||
} | ||
|
||
result.max_retries_elapsed += exceeded_retries_transactions.len() as u64; | ||
stats | ||
.transactions_exceeding_max_retries | ||
.fetch_add(result.max_retries_elapsed, Ordering::Relaxed); | ||
for signature in exceeded_retries_transactions { | ||
info!("Dropping transaction due to max retries: {signature}"); | ||
transactions.remove(&signature); | ||
} | ||
|
||
result | ||
} | ||
|
||
|
@@ -845,28 +904,12 @@ mod test { | |
&config, | ||
&stats, | ||
); | ||
assert_eq!(transactions.len(), 1); | ||
assert_eq!( | ||
result, | ||
ProcessTransactionsResult { | ||
retried: 1, | ||
max_retries_elapsed: 1, | ||
..ProcessTransactionsResult::default() | ||
} | ||
); | ||
let result = SendTransactionService::process_transactions( | ||
&working_bank, | ||
&root_bank, | ||
&mut transactions, | ||
&client, | ||
&config, | ||
&stats, | ||
); | ||
assert!(transactions.is_empty()); | ||
assert_eq!( | ||
result, | ||
ProcessTransactionsResult { | ||
max_retries_elapsed: 1, | ||
retried: 1, | ||
max_retries_elapsed: 2, | ||
..ProcessTransactionsResult::default() | ||
} | ||
); | ||
|
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.
transactions_to_retry
changed the semantics: now it is number of transactions that haven't reached retry limit.