Skip to content

Commit

Permalink
abort -> timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin committed Feb 29, 2024
1 parent 2115f6b commit 8368314
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion crates/storage/db/src/implementation/mdbx/tx.rs
Expand Up @@ -439,7 +439,7 @@ mod tests {

assert_eq!(
tx.get::<tables::Transactions>(0).err(),
Some(DatabaseError::Open(reth_libmdbx::Error::ReadTransactionAborted.into()))
Some(DatabaseError::Open(reth_libmdbx::Error::ReadTransactionTimeout.into()))
); // Transaction is timeout-ed
assert!(tx.metrics_handler.unwrap().backtrace_recorded.load(Ordering::Relaxed));
// Backtrace is recorded
Expand Down
6 changes: 3 additions & 3 deletions crates/storage/libmdbx-rs/src/error.rs
Expand Up @@ -117,8 +117,8 @@ pub enum Error {
/// [Mode::ReadOnly](crate::flags::Mode::ReadOnly), write transactions can't be opened.
#[error("write transactions are not supported in read-only mode")]
WriteTransactionUnsupportedInReadOnlyMode,
#[error("read transaction has been aborted by the transaction manager")]
ReadTransactionAborted,
#[error("read transaction has been timeouted")]
ReadTransactionTimeout,
/// Unknown error code.
#[error("unknown error code")]
Other(i32),
Expand Down Expand Up @@ -195,7 +195,7 @@ impl Error {
Error::BadSignature => ffi::MDBX_EBADSIGN,
Error::WriteTransactionUnsupportedInReadOnlyMode => ffi::MDBX_EACCESS,
Error::NestedTransactionsUnsupportedWithWriteMap => ffi::MDBX_EACCESS,
Error::ReadTransactionAborted => -96000, // Custom non-MDBX error code
Error::ReadTransactionTimeout => -96000, // Custom non-MDBX error code
Error::Other(err_code) => *err_code,
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/libmdbx-rs/src/transaction.rs
Expand Up @@ -543,7 +543,7 @@ impl TransactionPtr {
// because we're taking a lock for any actions on the transaction pointer, including a call
// to the `mdbx_txn_reset`.
if unsafe { ffi::mdbx_txn_flags(self.txn) } & ffi::MDBX_TXN_FINISHED != 0 {
return Err(Error::ReadTransactionAborted)
return Err(Error::ReadTransactionTimeout)
}

Ok((f)(self.txn))
Expand Down
32 changes: 16 additions & 16 deletions crates/storage/libmdbx-rs/src/txn_manager.rs
Expand Up @@ -199,11 +199,11 @@ mod read_transactions {
}

/// Spawns a new thread with [std::thread::spawn] that monitors the list of active read
/// transactions and aborts those that are open for longer than
/// transactions and timeouts those that are open for longer than
/// `ReadTransactions.max_duration`.
pub(super) fn start_monitor(self: Arc<Self>) {
std::thread::spawn(move || {
let mut aborted_active = Vec::new();
let mut timeouted_active = Vec::new();

loop {
let now = Instant::now();
Expand All @@ -220,7 +220,7 @@ mod read_transactions {
(
txn_ptr,
duration,
// Abort the transaction.
// Timeout the transaction.
//
// We use `mdbx_txn_reset` instead of `mdbx_txn_abort` here to
// prevent MDBX from reusing the pointer of the aborted
Expand All @@ -238,7 +238,7 @@ mod read_transactions {
// Add the transaction to `aborted_active`. We can't remove it
// instantly from the list of active
// transactions, because we iterate through it.
aborted_active.push((txn_ptr, duration, error));
timeouted_active.push((txn_ptr, duration, error));
}
Err(err) => {
error!(target: "libmdbx", %err, "Failed to abort the long-lived read transaction")
Expand All @@ -251,29 +251,29 @@ mod read_transactions {
}
}

// Walk through aborted transactions, and delete them from the list of active
// Walk through timeouted transactions, and delete them from the list of active
// transactions.
for (ptr, open_duration, err) in aborted_active.iter().copied() {
for (ptr, open_duration, err) in timeouted_active.iter().copied() {
// Try deleting the transaction from the list of active transactions.
let was_in_active = self.remove_active(ptr).is_some();
if let Err(err) = err {
if was_in_active {
// If the transaction was in the list of active transactions then
// user didn't abort it and we failed to do so.
error!(target: "libmdbx", %err, ?open_duration, "Failed to abort the long-lived read transaction");
// If the transaction was in the list of active transactions,
// then user didn't abort it and we failed to do so.
error!(target: "libmdbx", %err, ?open_duration, "Failed to timeout the long-lived read transaction");
}
} else {
// Happy path, the transaction has been aborted by us with no errors.
warn!(target: "libmdbx", ?open_duration, "Long-lived read transaction has been aborted");
// Happy path, the transaction has been timeouted by us with no errors.
warn!(target: "libmdbx", ?open_duration, "Long-lived read transaction has been timeouted");
// Add transaction to the list of timeouted transactions that were not
// aborted by the user yet.
self.timeouted_not_aborted.insert(ptr as usize);
}
}

// Clear the list of aborted transactions, but not de-allocate the reserved
// Clear the list of timeouted transactions, but not de-allocate the reserved
// capacity to save on further pushes.
aborted_active.clear();
timeouted_active.clear();

if !self.active.is_empty() {
trace!(
Expand Down Expand Up @@ -345,7 +345,7 @@ mod read_transactions {
}

// Create a read-only transaction, wait until `MAX_DURATION` time is elapsed so the
// manager kills it, use it two times and observe the `Error::ReadTransactionAborted`
// manager kills it, use it two times and observe the `Error::ReadTransactionTimeout`
// error. Also, ensure that the transaction pointer is not reused when opening a new
// read-only transaction.
{
Expand All @@ -357,10 +357,10 @@ mod read_transactions {

assert!(!read_transactions.active.contains_key(&tx_ptr));

assert_eq!(tx.open_db(None).err(), Some(Error::ReadTransactionAborted));
assert_eq!(tx.open_db(None).err(), Some(Error::ReadTransactionTimeout));
assert!(!read_transactions.active.contains_key(&tx_ptr));

assert_eq!(tx.id().err(), Some(Error::ReadTransactionAborted));
assert_eq!(tx.id().err(), Some(Error::ReadTransactionTimeout));
assert!(!read_transactions.active.contains_key(&tx_ptr));

let tx = env.begin_ro_txn().unwrap();
Expand Down

0 comments on commit 8368314

Please sign in to comment.