Skip to content

Commit

Permalink
feat(storage): report backtrace only on read transactions (#5625)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin authored Nov 29, 2023
1 parent d9ef1a1 commit 5a76447
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
34 changes: 17 additions & 17 deletions crates/storage/db/src/implementation/mdbx/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,28 +176,28 @@ impl<K: TransactionKind> MetricsHandler<K> {
}
}

/// Logs the backtrace of current call if the duration that the transaction has been open is
/// more than [LONG_TRANSACTION_DURATION].
/// Logs the backtrace of current call if the duration that the read transaction has been open
/// is more than [LONG_TRANSACTION_DURATION].
/// The backtrace is recorded and logged just once, guaranteed by `backtrace_recorded` atomic.
///
/// NOTE: Backtrace is recorded using [Backtrace::force_capture], so `RUST_BACKTRACE` env var is
/// not needed.
fn log_backtrace_on_long_transaction(&self) {
if self.backtrace_recorded.load(Ordering::Relaxed) {
return
}

let open_duration = self.start.elapsed();
if open_duration > LONG_TRANSACTION_DURATION {
self.backtrace_recorded.store(true, Ordering::Relaxed);

let backtrace = Backtrace::force_capture();
debug!(
target: "storage::db::mdbx",
?open_duration,
?backtrace,
"The database transaction has been open for too long"
);
if !self.backtrace_recorded.load(Ordering::Relaxed) &&
self.transaction_mode().is_read_only()
{
let open_duration = self.start.elapsed();
if open_duration > LONG_TRANSACTION_DURATION {
self.backtrace_recorded.store(true, Ordering::Relaxed);

let backtrace = Backtrace::force_capture();
debug!(
target: "storage::db::mdbx",
?open_duration,
?backtrace,
"The database read transaction has been open for too long"
);
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/storage/db/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ impl TransactionMode {
TransactionMode::ReadWrite => "read-write",
}
}

/// Returns `true` if the transaction mode is read-only.
pub(crate) const fn is_read_only(&self) -> bool {
matches!(self, TransactionMode::ReadOnly)
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
Expand Down

0 comments on commit 5a76447

Please sign in to comment.