diff --git a/crates/storage/db/src/implementation/mdbx/mod.rs b/crates/storage/db/src/implementation/mdbx/mod.rs index 616029acd..219a4336a 100644 --- a/crates/storage/db/src/implementation/mdbx/mod.rs +++ b/crates/storage/db/src/implementation/mdbx/mod.rs @@ -11,11 +11,16 @@ use reth_libmdbx::{ SyncMode, RO, RW, }; use std::{ops::Deref, path::Path}; +use tx::Tx; pub mod cursor; - pub mod tx; -use tx::Tx; + +const GIGABYTE: usize = 1024 * 1024 * 1024; +const TERABYTE: usize = GIGABYTE * 1024; + +/// MDBX allows up to 32767 readers (`MDBX_READERS_LIMIT`), but we limit it to slightly below that +const DEFAULT_MAX_READERS: u64 = 32_000; /// Environment used when opening a MDBX environment. RO/RW. #[derive(Debug)] @@ -52,9 +57,6 @@ impl Database for Env { } } -const GIGABYTE: usize = 1024 * 1024 * 1024; -const TERABYTE: usize = GIGABYTE * 1024; - impl Env { /// Opens the database at the specified path with the given `EnvKind`. /// @@ -85,6 +87,8 @@ impl Env { coalesce: true, ..Default::default() }) + // configure more readers + .set_max_readers(DEFAULT_MAX_READERS) .open(path) .map_err(|e| DatabaseError::FailedToOpen(e.into()))?, }; diff --git a/crates/storage/db/src/lib.rs b/crates/storage/db/src/lib.rs index c2b5d539d..8ce4973f4 100644 --- a/crates/storage/db/src/lib.rs +++ b/crates/storage/db/src/lib.rs @@ -114,7 +114,7 @@ pub fn init_db>(path: P) -> eyre::Result } #[cfg(feature = "mdbx")] { - let db = Env::::open(rpath, EnvKind::RW)?; + let db = DatabaseEnv::open(rpath, EnvKind::RW)?; db.create_tables()?; Ok(db) } diff --git a/crates/storage/libmdbx-rs/src/environment.rs b/crates/storage/libmdbx-rs/src/environment.rs index abb3457c0..b37285a4d 100644 --- a/crates/storage/libmdbx-rs/src/environment.rs +++ b/crates/storage/libmdbx-rs/src/environment.rs @@ -6,7 +6,6 @@ use crate::{ Mode, Transaction, TransactionKind, }; use byteorder::{ByteOrder, NativeEndian}; -use libc::c_uint; use mem::size_of; use std::{ ffi::CString, @@ -376,7 +375,7 @@ where E: EnvironmentKind, { flags: EnvironmentFlags, - max_readers: Option, + max_readers: Option, max_dbs: Option, rp_augment_limit: Option, loose_limit: Option, @@ -453,6 +452,15 @@ where } } + // set max readers if specified + if let Some(max_readers) = self.max_readers { + mdbx_result(ffi::mdbx_env_set_option( + env, + ffi::MDBX_opt_max_readers, + max_readers, + ))?; + } + #[cfg(unix)] fn path_to_bytes>(path: P) -> Vec { use std::os::unix::ffi::OsStrExt; @@ -544,7 +552,7 @@ where /// This defines the number of slots in the lock table that is used to track readers in the /// the environment. The default is 126. Starting a read-only transaction normally ties a lock /// table slot to the [Transaction] object until it or the [Environment] object is destroyed. - pub fn set_max_readers(&mut self, max_readers: c_uint) -> &mut Self { + pub fn set_max_readers(&mut self, max_readers: u64) -> &mut Self { self.max_readers = Some(max_readers); self }