From 04adb4cc2e702536eb338fe740e9359cddfa9bc8 Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Fri, 4 Oct 2024 12:59:00 +0300 Subject: [PATCH 1/2] feat(db): capture tx opening backtrace in debug mode --- .../storage/db/src/implementation/mdbx/tx.rs | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/storage/db/src/implementation/mdbx/tx.rs b/crates/storage/db/src/implementation/mdbx/tx.rs index 8feb6c90ab27..0e53fc403be7 100644 --- a/crates/storage/db/src/implementation/mdbx/tx.rs +++ b/crates/storage/db/src/implementation/mdbx/tx.rs @@ -180,7 +180,12 @@ struct MetricsHandler { /// 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, + /// 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, } @@ -193,6 +198,8 @@ impl MetricsHandler { close_recorded: false, record_backtrace: true, backtrace_recorded: AtomicBool::new(false), + #[cfg(debug_assertions)] + open_backtrace: Backtrace::force_capture(), env_metrics, _marker: PhantomData, } @@ -232,11 +239,23 @@ impl MetricsHandler { 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\nCurrent backtrace:\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}" ); } } From 2a7bcc7bed56167cbb94e82600e58b59fda03e4b Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Fri, 4 Oct 2024 14:57:14 +0300 Subject: [PATCH 2/2] use cfg attr --- .../storage/db/src/implementation/mdbx/tx.rs | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/crates/storage/db/src/implementation/mdbx/tx.rs b/crates/storage/db/src/implementation/mdbx/tx.rs index 0e53fc403be7..2ff2789ea698 100644 --- a/crates/storage/db/src/implementation/mdbx/tx.rs +++ b/crates/storage/db/src/implementation/mdbx/tx.rs @@ -239,18 +239,17 @@ impl MetricsHandler { 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\nCurrent backtrace:\n{}", - self.open_backtrace, - Backtrace::force_capture() - ) - } else { - format!( - "The database read transaction has been open for too long. Backtrace:\n{}", - Backtrace::force_capture() - ) - }; + #[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,