-
Notifications
You must be signed in to change notification settings - Fork 952
accounts-db: relax intrabatch account locks #4253
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
Changes from 2 commits
29c3bc5
d0cabcc
9e73e22
169f1ea
37f5095
83bf900
cafa408
e2a0190
014e743
d957541
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,13 +5,13 @@ use { | |
| solana_message::AccountKeys, | ||
| solana_pubkey::Pubkey, | ||
| solana_transaction::sanitized::MAX_TX_ACCOUNT_LOCKS, | ||
| solana_transaction_error::TransactionError, | ||
| solana_transaction_error::{TransactionError, TransactionResult}, | ||
| std::{cell::RefCell, collections::hash_map}, | ||
| }; | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct AccountLocks { | ||
| write_locks: AHashSet<Pubkey>, | ||
| write_locks: AHashMap<Pubkey, u64>, | ||
| readonly_locks: AHashMap<Pubkey, u64>, | ||
| } | ||
|
|
||
|
|
@@ -20,31 +20,39 @@ impl AccountLocks { | |
| /// The bool in the tuple indicates if the account is writable. | ||
| /// Returns an error if any of the accounts are already locked in a way | ||
| /// that conflicts with the requested lock. | ||
| pub fn try_lock_accounts<'a>( | ||
| &mut self, | ||
| keys: impl Iterator<Item = (&'a Pubkey, bool)> + Clone, | ||
| ) -> Result<(), TransactionError> { | ||
| for (key, writable) in keys.clone() { | ||
| if writable { | ||
| if !self.can_write_lock(key) { | ||
| return Err(TransactionError::AccountInUse); | ||
| } | ||
| } else if !self.can_read_lock(key) { | ||
| return Err(TransactionError::AccountInUse); | ||
| } | ||
| } | ||
|
|
||
| for (key, writable) in keys { | ||
| if writable { | ||
| self.lock_write(key); | ||
| } else { | ||
| self.lock_readonly(key); | ||
| } | ||
| } | ||
| /// NOTE this is the pre-SIMD83 logic and can be removed once SIMD83 is active. | ||
| pub fn try_lock_accounts(&mut self, keys: &[(&Pubkey, bool)]) -> TransactionResult<()> { | ||
|
||
| self.can_lock_accounts(keys)?; | ||
| self.lock_accounts(keys); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Lock accounts for all transactions in a batch which don't conflict | ||
| /// with existing locks. Returns a vector of `TransactionResult` indicating | ||
| /// success or failure for each transaction in the batch. | ||
| /// NOTE this is the SIMD83 logic; after the feature is active, it becomes | ||
| /// the only logic, and this note can be removed with the feature gate. | ||
| pub fn try_lock_transaction_batch<'a>( | ||
| &mut self, | ||
| validated_batch_keys: impl Iterator<Item = TransactionResult<Vec<(&'a Pubkey, bool)>>>, | ||
| ) -> Vec<TransactionResult<()>> { | ||
| let available_batch_keys: Vec<_> = validated_batch_keys | ||
| .map(|validated_keys| match validated_keys { | ||
| Ok(ref keys) => match self.can_lock_accounts(keys) { | ||
| Ok(_) => validated_keys, | ||
| Err(e) => Err(e), | ||
| }, | ||
| Err(e) => Err(e), | ||
| }) | ||
| .collect(); | ||
|
|
||
|
||
| available_batch_keys | ||
| .into_iter() | ||
| .map(|available_keys| available_keys.map(|keys| self.lock_accounts(&keys))) | ||
| .collect() | ||
| } | ||
|
|
||
| /// Unlock the account keys in `keys` after a transaction. | ||
| /// The bool in the tuple indicates if the account is writable. | ||
| /// In debug-mode this function will panic if an attempt is made to unlock | ||
|
|
@@ -59,14 +67,38 @@ impl AccountLocks { | |
| } | ||
| } | ||
|
|
||
| fn can_lock_accounts(&self, keys: &[(&Pubkey, bool)]) -> TransactionResult<()> { | ||
| for (key, writable) in keys { | ||
| if *writable { | ||
| if !self.can_write_lock(key) { | ||
| return Err(TransactionError::AccountInUse); | ||
| } | ||
| } else if !self.can_read_lock(key) { | ||
| return Err(TransactionError::AccountInUse); | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn lock_accounts(&mut self, keys: &[(&Pubkey, bool)]) { | ||
| for (key, writable) in keys { | ||
| if *writable { | ||
| self.lock_write(key); | ||
| } else { | ||
| self.lock_readonly(key); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg_attr(feature = "dev-context-only-utils", qualifiers(pub))] | ||
| fn is_locked_readonly(&self, key: &Pubkey) -> bool { | ||
| self.readonly_locks.get(key).is_some_and(|count| *count > 0) | ||
| } | ||
|
|
||
| #[cfg_attr(feature = "dev-context-only-utils", qualifiers(pub))] | ||
| fn is_locked_write(&self, key: &Pubkey) -> bool { | ||
| self.write_locks.contains(key) | ||
| self.write_locks.get(key).is_some_and(|count| *count > 0) | ||
| } | ||
|
|
||
| fn can_read_lock(&self, key: &Pubkey) -> bool { | ||
|
|
@@ -84,7 +116,7 @@ impl AccountLocks { | |
| } | ||
|
|
||
| fn lock_write(&mut self, key: &Pubkey) { | ||
| self.write_locks.insert(*key); | ||
| *self.write_locks.entry(*key).or_default() += 1; | ||
| } | ||
|
|
||
| fn unlock_readonly(&mut self, key: &Pubkey) { | ||
|
|
@@ -103,19 +135,26 @@ impl AccountLocks { | |
| } | ||
|
|
||
| fn unlock_write(&mut self, key: &Pubkey) { | ||
| let removed = self.write_locks.remove(key); | ||
| debug_assert!( | ||
| removed, | ||
| "Attempted to remove a write-lock for a key that wasn't write-locked" | ||
| ); | ||
| if let hash_map::Entry::Occupied(mut occupied_entry) = self.write_locks.entry(*key) { | ||
| let count = occupied_entry.get_mut(); | ||
| *count -= 1; | ||
| if *count == 0 { | ||
| occupied_entry.remove_entry(); | ||
| } | ||
| } else { | ||
| debug_assert!( | ||
| false, | ||
| "Attempted to remove a write-lock for a key that wasn't write-locked" | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Validate account locks before locking. | ||
| pub fn validate_account_locks( | ||
| account_keys: AccountKeys, | ||
| tx_account_lock_limit: usize, | ||
| ) -> Result<(), TransactionError> { | ||
| ) -> TransactionResult<()> { | ||
| if account_keys.len() > tx_account_lock_limit { | ||
| Err(TransactionError::TooManyAccountLocks) | ||
| } else if has_duplicates(account_keys) { | ||
|
|
@@ -165,23 +204,23 @@ mod tests { | |
| let key2 = Pubkey::new_unique(); | ||
|
|
||
| // Add write and read-lock. | ||
| let result = account_locks.try_lock_accounts([(&key1, true), (&key2, false)].into_iter()); | ||
| let result = account_locks.try_lock_accounts(&[(&key1, true), (&key2, false)]); | ||
| assert!(result.is_ok()); | ||
|
|
||
| // Try to add duplicate write-lock. | ||
| let result = account_locks.try_lock_accounts([(&key1, true)].into_iter()); | ||
| let result = account_locks.try_lock_accounts(&[(&key1, true)]); | ||
| assert_eq!(result, Err(TransactionError::AccountInUse)); | ||
|
|
||
| // Try to add write lock on read-locked account. | ||
| let result = account_locks.try_lock_accounts([(&key2, true)].into_iter()); | ||
| let result = account_locks.try_lock_accounts(&[(&key2, true)]); | ||
| assert_eq!(result, Err(TransactionError::AccountInUse)); | ||
|
|
||
| // Try to add read lock on write-locked account. | ||
| let result = account_locks.try_lock_accounts([(&key1, false)].into_iter()); | ||
| let result = account_locks.try_lock_accounts(&[(&key1, false)]); | ||
| assert_eq!(result, Err(TransactionError::AccountInUse)); | ||
|
|
||
| // Add read lock on read-locked account. | ||
| let result = account_locks.try_lock_accounts([(&key2, false)].into_iter()); | ||
| let result = account_locks.try_lock_accounts(&[(&key2, false)]); | ||
| assert!(result.is_ok()); | ||
|
|
||
| // Unlock write and read locks. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
because the read- and write-lock hashmaps have the same type now, all the functions that change them are basically the same. we could use an enum or hashmap reference to discriminate and delete half of the functions, but i left it like this for your review before butchering it