Skip to content

Commit

Permalink
feat(storage): improve current process detection for MDBX HSR (#6917)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin committed Mar 6, 2024
1 parent 3308ddb commit 9f21d1c
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions crates/storage/db/src/implementation/mdbx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{
};
use eyre::Context;
use metrics::{gauge, Label};
use once_cell::sync::Lazy;
use reth_interfaces::db::LogLevel;
use reth_libmdbx::{
DatabaseFlags, Environment, EnvironmentFlags, Geometry, MaxReadTransactionDuration, Mode,
Expand All @@ -34,19 +33,6 @@ const DEFAULT_MAX_READERS: u64 = 32_000;
#[cfg(not(windows))]
const MAX_SAFE_READER_SPACE: usize = 10 * GIGABYTE;

#[cfg(not(windows))]
static PROCESS_ID: Lazy<u32> = Lazy::new(|| {
#[cfg(unix)]
{
std::os::unix::process::parent_id()
}

#[cfg(not(unix))]
{
std::process::id()
}
});

/// Environment used when opening a MDBX environment. RO/RW.
#[derive(Debug)]
pub enum DatabaseEnvKind {
Expand Down Expand Up @@ -255,11 +241,21 @@ impl DatabaseEnv {
});
#[cfg(not(windows))]
{
let _ = *PROCESS_ID; // Initialize the process ID at the time of environment opening
fn is_current_process(id: u32) -> bool {
#[cfg(unix)]
{
id == std::os::unix::process::parent_id() || id == std::process::id()
}

#[cfg(not(unix))]
{
id == std::process::id()
}
}
inner_env.set_handle_slow_readers(
|process_id: u32, thread_id: u32, read_txn_id: u64, gap: usize, space: usize, retry: isize| {
if space > MAX_SAFE_READER_SPACE {
let message = if process_id == *PROCESS_ID {
let message = if is_current_process(process_id) {
"Current process has a long-lived database transaction that grows the database file."
} else {
"External process has a long-lived database transaction that grows the database file. Use shorter-lived read transactions or shut down the node."
Expand Down

0 comments on commit 9f21d1c

Please sign in to comment.