Skip to content

Commit

Permalink
chore(db): capture tx opening backtrace in debug mode (paradigmxyz#11477
Browse files Browse the repository at this point in the history
)
  • Loading branch information
shekhirin authored Oct 4, 2024
1 parent 227e293 commit b537672
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion crates/storage/db/src/implementation/mdbx/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,12 @@ struct MetricsHandler<K: TransactionKind> {
/// If `true`, the backtrace of transaction has already been recorded and logged.
/// See [`MetricsHandler::log_backtrace_on_long_read_transaction`].
backtrace_recorded: AtomicBool,
/// Shared database environment metrics.
env_metrics: Arc<DatabaseEnvMetrics>,
/// Backtrace of the location where the transaction has been opened. Reported only with debug
/// assertions, because capturing the backtrace on every transaction opening is expensive.
#[cfg(debug_assertions)]
open_backtrace: Backtrace,
_marker: PhantomData<K>,
}

Expand All @@ -193,6 +198,8 @@ impl<K: TransactionKind> MetricsHandler<K> {
close_recorded: false,
record_backtrace: true,
backtrace_recorded: AtomicBool::new(false),
#[cfg(debug_assertions)]
open_backtrace: Backtrace::force_capture(),
env_metrics,
_marker: PhantomData,
}
Expand Down Expand Up @@ -232,11 +239,22 @@ impl<K: TransactionKind> MetricsHandler<K> {
let open_duration = self.start.elapsed();
if open_duration >= self.long_transaction_duration {
self.backtrace_recorded.store(true, Ordering::Relaxed);
#[cfg(debug_assertions)]
let message = format!(
"The database read transaction has been open for too long. Open backtrace:\n{}\n\nCurrent backtrace:\n{}",
self.open_backtrace,
Backtrace::force_capture()
);
#[cfg(not(debug_assertions))]
let message = format!(
"The database read transaction has been open for too long. Backtrace:\n{}",
Backtrace::force_capture()
);
warn!(
target: "storage::db::mdbx",
?open_duration,
%self.txn_id,
"The database read transaction has been open for too long. Backtrace:\n{}", Backtrace::force_capture()
"{message}"
);
}
}
Expand Down

0 comments on commit b537672

Please sign in to comment.