Skip to content

Commit

Permalink
feat(db): capture tx opening backtrace in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin committed Oct 4, 2024
1 parent c5cfaf1 commit 63bbe0e
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,11 @@ 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
#[cfg(debug_assertions)]
open_backtrace: Backtrace,
_marker: PhantomData<K>,
}

Expand All @@ -193,6 +197,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 +238,23 @@ 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);
let message = if cfg!(debug_assertions) {
format!(
"The database read transaction has been open for too long. Open backtrace:\n{}\n\nBacktrace:\n{}",
self.open_backtrace,
Backtrace::force_capture()
)
} else {
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 63bbe0e

Please sign in to comment.