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

Combine account locking and block space reserving in single loop #34909

Closed
Closed
Changes from all 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
147 changes: 146 additions & 1 deletion core/src/banking_stage/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ impl Consumer {
pre_results: impl Iterator<Item = Result<(), TransactionError>>,
) -> ProcessTransactionBatchOutput {
let (
(transaction_qos_cost_results, cost_model_throttled_transactions_count),
(mut transaction_qos_cost_results, cost_model_throttled_transactions_count),
Copy link
Contributor

Choose a reason for hiding this comment

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

as an alternative to mutability here. we could also use the lock-results to inform the cost-adjustment post-execution. Not sure if that would be cleaner though.

cost_model_us,
) = measure_us!(self.qos_service.select_and_accumulate_transaction_costs(
bank,
Expand All @@ -479,6 +479,32 @@ impl Consumer {
})
));

// Remove reserved block space for transaction failed to acquire lock
let unlocked_cost_results: Vec<_> = batch
.lock_results()
.iter()
.zip(transaction_qos_cost_results.iter_mut())
.map(|(lock_result, cost)| {
if let Err(lock_err) = lock_result {
if cost.is_ok() {
// set cost to lock_err, so it won't be accidentally removed more than once
let mut c = Err(lock_err.clone());
std::mem::swap(cost, &mut c);
return Some(c.unwrap());
}
}
None
})
.collect();
Copy link
Contributor

Choose a reason for hiding this comment

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

we don't need to allocate a new vec here, we can just iterate over our qos and lock result pairs...something like

        // Remove reserved block space for transaction failed to acquire lock
        let mut cost_tracker = bank.write_cost_tracker().unwrap();
        transaction_qos_cost_results
            .iter_mut()
            .zip(batch.lock_results())
            .filter_map(|(qos_cost_result, lock_result)| {
                match (qos_cost_result.as_ref(), lock_result) {
                    (Ok(_), Err(lock_err)) => Some((qos_cost_result, lock_err)),
                    _ => None,
                }
            })
            .for_each(|(qos_cost_result, lock_err)| {
                cost_tracker.remove(qos_cost_result.as_ref().unwrap());
                *qos_cost_result = Err(lock_err.clone());
            });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TIL! I was having trouble to change qos_cost_result while it is borrowed by match (i think).

Before making the change tho, are we good with this approach, or would like to "lock-reserve-unlock" you mentioned elsewhere?

{
let mut cost_tracker = bank.write_cost_tracker().unwrap();
unlocked_cost_results.iter().for_each(|tx_cost| {
if let Some(tx_cost) = tx_cost {
cost_tracker.remove(tx_cost);
}
});
}

// retryable_txs includes AccountInUse, WouldExceedMaxBlockCostLimit
// WouldExceedMaxAccountCostLimit, WouldExceedMaxVoteCostLimit
// and WouldExceedMaxAccountDataCostLimit
Expand Down Expand Up @@ -2510,4 +2536,123 @@ mod tests {
[0, 3, 4, 5]
);
}

#[test]
fn test_process_and_record_transactions_with_pre_results() {
let test_would_fit_and_lock = TestLockingAndReserving {
would_fit: true,
could_lock: true,
expected_cost_model_throttled_transactions_count: 0,
expected_transactions_attempted_execution_count: 1,
expected_committed_transactions_count: 1,
expected_retryable_transaction_count: 0,
};
assert_process_transactions_with_locking_and_reserving(test_would_fit_and_lock);

let test_would_fit_could_not_lock = TestLockingAndReserving {
would_fit: true,
could_lock: false,
expected_cost_model_throttled_transactions_count: 0,
expected_transactions_attempted_execution_count: 1,
expected_committed_transactions_count: 0,
expected_retryable_transaction_count: 1,
};
assert_process_transactions_with_locking_and_reserving(test_would_fit_could_not_lock);

let test_would_not_fit_could_lock = TestLockingAndReserving {
would_fit: false,
could_lock: true,
expected_cost_model_throttled_transactions_count: 1,
expected_transactions_attempted_execution_count: 1,
expected_committed_transactions_count: 0,
expected_retryable_transaction_count: 1,
};
assert_process_transactions_with_locking_and_reserving(test_would_not_fit_could_lock);

let test_would_not_fit_could_not_lock = TestLockingAndReserving {
would_fit: false,
could_lock: false,
expected_cost_model_throttled_transactions_count: 1,
expected_transactions_attempted_execution_count: 1,
expected_committed_transactions_count: 0,
expected_retryable_transaction_count: 1,
};
assert_process_transactions_with_locking_and_reserving(test_would_not_fit_could_not_lock);
}

// test setup for combined scenarios of accounts locking and block space reserving
struct TestLockingAndReserving {
// input:
would_fit: bool,
could_lock: bool,
// expectations
expected_cost_model_throttled_transactions_count: usize,
expected_transactions_attempted_execution_count: usize,
expected_committed_transactions_count: usize,
expected_retryable_transaction_count: usize,
}

fn assert_process_transactions_with_locking_and_reserving(test: TestLockingAndReserving) {
solana_logger::setup();
let lamports = 100_000;
let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_slow_genesis_config(lamports);

let bank = Bank::new_no_wallclock_throttle_for_tests(&genesis_config).0;
let test_txs = vec![system_transaction::transfer(
&mint_keypair,
&Pubkey::new_unique(),
1,
genesis_config.hash(),
)];

// setup: if transcation should fail reserving space, set block_limit small.
let block_limit = if test.would_fit { std::u64::MAX } else { 1 };
bank.write_cost_tracker()
.unwrap()
.set_limits(block_limit, std::u64::MAX, std::u64::MAX);

// setup: if transaction should fail locking, pre-lock mint_keypair with blocker_txs
let blocker_txs = sanitize_transactions(vec![system_transaction::transfer(
&mint_keypair,
&Pubkey::new_unique(),
1,
genesis_config.hash(),
)]);
let _blocker = if test.could_lock {
None
} else {
Some(bank.prepare_sanitized_batch(&blocker_txs))
};

// execute:
let ProcessTransactionsSummary {
cost_model_throttled_transactions_count,
transactions_attempted_execution_count,
committed_transactions_count,
retryable_transaction_indexes,
..
} = execute_transactions_with_dummy_poh_service(bank.clone(), test_txs);

// check results:
assert_eq!(
cost_model_throttled_transactions_count,
test.expected_cost_model_throttled_transactions_count
);
assert_eq!(
transactions_attempted_execution_count,
test.expected_transactions_attempted_execution_count
);
assert_eq!(
committed_transactions_count,
test.expected_committed_transactions_count
);
assert_eq!(
retryable_transaction_indexes.len(),
test.expected_retryable_transaction_count
);
}
}