feat(storage): report backtrace only on read transactions (#5625)

This commit is contained in:
Alexey Shekhirin
2023-11-29 18:39:15 +00:00
committed by GitHub
parent d9ef1a14d5
commit 5a7644709b
2 changed files with 21 additions and 16 deletions

View File

@ -176,17 +176,16 @@ 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
}
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);
@ -196,11 +195,12 @@ impl<K: TransactionKind> MetricsHandler<K> {
target: "storage::db::mdbx",
?open_duration,
?backtrace,
"The database transaction has been open for too long"
"The database read transaction has been open for too long"
);
}
}
}
}
impl<K: TransactionKind> Drop for MetricsHandler<K> {
fn drop(&mut self) {

View File

@ -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)]