Adjustments to cost_tracker updates

- don't store pending tx signatures and costs in CostTracker
- apply tx costs to global state immediately again
- go from commit_or_cancel to update_or_remove, where the cost tracker
  is either updated with the true costs for successful tx, or the costs
  of a retryable tx is removed
- move the function into qos_service and hold the cost tracker lock for
  the whole loop

(cherry picked from commit 924b8ea1eb)
This commit is contained in:
Christian Kamm
2022-04-08 14:22:31 +02:00
committed by Tao Zhu
parent 637ac7933b
commit 864253a85b
3 changed files with 86 additions and 117 deletions

View File

@ -5,9 +5,7 @@
//!
use {
crate::{block_cost_limits::*, cost_model::TransactionCost},
solana_sdk::{
clock::Slot, pubkey::Pubkey, signature::Signature, transaction::SanitizedTransaction,
},
solana_sdk::{clock::Slot, pubkey::Pubkey, transaction::SanitizedTransaction},
std::collections::HashMap,
};
@ -45,12 +43,6 @@ pub struct CostTracker {
/// The amount of total account data size remaining. If `Some`, then do not add transactions
/// that would cause `account_data_size` to exceed this limit.
account_data_size_limit: Option<u64>,
// Transactions have passed would_fit check, is being executed.
// If the execution is successful, it's actual Units can be committed
// to cost_tracker; otherwise, it should be removed without impacting
// cost_tracker.
pending_transactions: HashMap<Signature, TransactionCost>,
}
impl Default for CostTracker {
@ -71,7 +63,6 @@ impl Default for CostTracker {
transaction_count: 0,
account_data_size: 0,
account_data_size_limit: None,
pending_transactions: HashMap::new(),
}
}
}
@ -100,31 +91,24 @@ impl CostTracker {
pub fn try_add(
&mut self,
transaction: &SanitizedTransaction,
_transaction: &SanitizedTransaction,
tx_cost: &TransactionCost,
) -> Result<u64, CostTrackerError> {
self.would_fit(tx_cost)?;
self.pending_transactions
.insert(*transaction.signature(), tx_cost.clone());
self.add_transaction_cost(tx_cost);
Ok(self.block_cost)
}
pub fn commit_transaction(
pub fn update_execution_cost(
&mut self,
transaction: &SanitizedTransaction,
actual_units: Option<u64>,
_estimated_tx_cost: &TransactionCost,
_actual_execution_cost: u64,
) {
if let Some(mut tx_cost) = self.pending_transactions.remove(transaction.signature()) {
if let Some(actual_units) = actual_units {
// using actual units to update cost tracker if available
tx_cost.execution_cost = actual_units;
}
self.add_transaction(&tx_cost);
}
// adjust block_cost / vote_cost / account_cost by (actual_execution_cost - execution_cost)
}
pub fn cancel_transaction(&mut self, transaction: &SanitizedTransaction) {
self.pending_transactions.remove(transaction.signature());
pub fn remove(&mut self, tx_cost: &TransactionCost) {
self.remove_transaction_cost(tx_cost);
}
pub fn report_stats(&self, bank_slot: Slot) {
@ -166,34 +150,10 @@ impl CostTracker {
}
fn would_fit(&self, tx_cost: &TransactionCost) -> Result<(), CostTrackerError> {
let mut writable_account = vec![];
writable_account.extend(&tx_cost.writable_accounts);
let mut cost = tx_cost.sum();
let mut account_data_size = tx_cost.account_data_size;
let mut vote_cost = if tx_cost.is_simple_vote { cost } else { 0 };
let writable_accounts = &tx_cost.writable_accounts;
let cost = tx_cost.sum();
let vote_cost = if tx_cost.is_simple_vote { cost } else { 0 };
for tx_cost in self.pending_transactions.values() {
writable_account.extend(&tx_cost.writable_accounts);
cost = cost.saturating_add(tx_cost.sum());
account_data_size = account_data_size.saturating_add(tx_cost.account_data_size);
vote_cost = vote_cost.saturating_add(if tx_cost.is_simple_vote { cost } else { 0 });
}
self.would_aggregated_transactions_fit(
&writable_account,
cost,
account_data_size,
vote_cost,
)
}
fn would_aggregated_transactions_fit(
&self,
keys: &[Pubkey],
cost: u64,
account_data_len: u64,
vote_cost: u64,
) -> Result<(), CostTrackerError> {
// check against the total package cost
if self.block_cost.saturating_add(cost) > self.block_cost_limit {
return Err(CostTrackerError::WouldExceedBlockMaxLimit);
@ -211,7 +171,9 @@ impl CostTracker {
// NOTE: Check if the total accounts data size is exceeded *before* the block accounts data
// size. This way, transactions are not unnecessarily retried.
let account_data_size = self.account_data_size.saturating_add(account_data_len);
let account_data_size = self
.account_data_size
.saturating_add(tx_cost.account_data_size);
if let Some(account_data_size_limit) = self.account_data_size_limit {
if account_data_size > account_data_size_limit {
return Err(CostTrackerError::WouldExceedAccountDataTotalLimit);
@ -223,7 +185,7 @@ impl CostTracker {
}
// check each account against account_cost_limit,
for account_key in keys.iter() {
for account_key in writable_accounts.iter() {
match self.cost_by_writable_accounts.get(account_key) {
Some(chained_cost) => {
if chained_cost.saturating_add(cost) > self.account_cost_limit {
@ -239,7 +201,7 @@ impl CostTracker {
Ok(())
}
fn add_transaction(&mut self, tx_cost: &TransactionCost) {
fn add_transaction_cost(&mut self, tx_cost: &TransactionCost) {
let cost = tx_cost.sum();
for account_key in tx_cost.writable_accounts.iter() {
let account_cost = self
@ -257,6 +219,25 @@ impl CostTracker {
.saturating_add(tx_cost.account_data_size);
self.transaction_count = self.transaction_count.saturating_add(1);
}
fn remove_transaction_cost(&mut self, tx_cost: &TransactionCost) {
let cost = tx_cost.sum();
for account_key in tx_cost.writable_accounts.iter() {
let account_cost = self
.cost_by_writable_accounts
.entry(*account_key)
.or_insert(0);
*account_cost = account_cost.saturating_sub(cost);
}
self.block_cost = self.block_cost.saturating_sub(cost);
if tx_cost.is_simple_vote {
self.vote_cost = self.vote_cost.saturating_sub(cost);
}
self.account_data_size = self
.account_data_size
.saturating_sub(tx_cost.account_data_size);
self.transaction_count = self.transaction_count.saturating_sub(1);
}
}
#[cfg(test)]