Skip to content

Commit

Permalink
add metric
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin committed Feb 29, 2024
1 parent f5db349 commit 507d60c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crates/node-core/src/metrics/prometheus_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ where
describe_gauge!("db.table_pages", "The number of database pages for a table");
describe_gauge!("db.table_entries", "The number of entries for a table");
describe_gauge!("db.freelist", "The number of pages on the freelist");
describe_gauge!(
"db.timeouted_not_aborted_transactions",
"Number of timeouted transactions that were not aborted by the user yet"
);
process.describe();
describe_memory_stats();
describe_io_stats();
Expand Down
6 changes: 6 additions & 0 deletions crates/storage/db/src/implementation/mdbx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ impl DatabaseMetrics for DatabaseEnv {
metrics.push(("db.freelist", freelist as f64, vec![]));
}

metrics.push((
"db.timeouted_not_aborted_transactions",
self.timeouted_not_aborted_transactions() as f64,
vec![],
));

metrics
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/storage/libmdbx-rs/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ impl Environment {
&self.inner.txn_manager
}

#[cfg(feature = "read-tx-timeouts")]
pub fn timeouted_not_aborted_transactions(&self) -> usize {
self.inner.txn_manager.timeouted_not_aborted_read_transactions().unwrap_or(0)
}

/// Create a read-only transaction for use with the environment.
#[inline]
pub fn begin_ro_txn(&self) -> Result<Transaction<RO>> {
Expand Down
19 changes: 18 additions & 1 deletion crates/storage/libmdbx-rs/src/txn_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ mod read_transactions {
environment::EnvPtr, error::mdbx_result, transaction::TransactionPtr,
txn_manager::TxnManager,
};
use dashmap::DashMap;
use dashmap::{DashMap, DashSet};
use std::{
sync::{mpsc::sync_channel, Arc},
time::{Duration, Instant},
Expand Down Expand Up @@ -150,6 +150,13 @@ mod read_transactions {
) -> Option<(usize, (TransactionPtr, Instant))> {
self.read_transactions.as_ref()?.remove_active(ptr)
}

/// Returns the number of timeouted transactions that were not aborted by the user yet.
pub(crate) fn timeouted_not_aborted_read_transactions(&self) -> Option<usize> {
self.read_transactions
.as_ref()
.map(|read_transactions| read_transactions.timeouted_not_aborted())
}
}

#[derive(Debug, Default)]
Expand All @@ -162,6 +169,9 @@ mod read_transactions {
/// We store `usize` instead of a raw pointer as a key, because pointers are not
/// comparable. The time of transaction opening is stored as a value.
active: DashMap<usize, (TransactionPtr, Instant)>,
/// List of timeouted transactions that were not aborted by the user yet, hence have a
/// dangling read transaction pointer.
timeouted_not_aborted: DashSet<usize>,
}

impl ReadTransactions {
Expand All @@ -179,9 +189,15 @@ mod read_transactions {
&self,
ptr: *mut ffi::MDBX_txn,
) -> Option<(usize, (TransactionPtr, Instant))> {
self.timeouted_not_aborted.remove(&(ptr as usize));
self.active.remove(&(ptr as usize))
}

/// Returns the number of timeouted transactions that were not aborted by the user yet.
pub(super) fn timeouted_not_aborted(&self) -> usize {
self.timeouted_not_aborted.len()
}

/// Spawns a new thread with [std::thread::spawn] that monitors the list of active read
/// transactions and aborts those that are open for longer than
/// `ReadTransactions.max_duration`.
Expand Down Expand Up @@ -249,6 +265,7 @@ mod read_transactions {
} else {
// Happy path, the transaction has been aborted by us with no errors.
warn!(target: "libmdbx", ?open_duration, "Long-lived read transaction has been aborted");
self.timeouted_not_aborted.insert(ptr as usize);
}
}

Expand Down

0 comments on commit 507d60c

Please sign in to comment.