diff --git a/crates/sovereign-sdk/full-node/db/sov-db/src/ledger_db/mod.rs b/crates/sovereign-sdk/full-node/db/sov-db/src/ledger_db/mod.rs index 44c49ce089..e51bad462b 100644 --- a/crates/sovereign-sdk/full-node/db/sov-db/src/ledger_db/mod.rs +++ b/crates/sovereign-sdk/full-node/db/sov-db/src/ledger_db/mod.rs @@ -67,9 +67,15 @@ impl LedgerDB { /// The returned instance will be at the path `{path}/ledger`. #[instrument(level = "trace", skip_all, err)] pub fn with_config(cfg: &RocksdbConfig) -> Result { - let path = cfg.path.join(LedgerDB::DB_PATH_SUFFIX); - let raw_options = cfg.as_raw_options(false); - let tables = cfg + let mut ledger_db_config = cfg.clone(); + + // RocksDB config assign available open files but since we spawn 3 different rocksdb instances, we need to share between them + // Allocate 10% for LedgerDB + ledger_db_config.max_open_files = ledger_db_config.max_open_files * 10 / 100; + + let path = ledger_db_config.path.join(LedgerDB::DB_PATH_SUFFIX); + let raw_options = ledger_db_config.as_raw_options(false); + let tables = ledger_db_config .column_families .clone() .unwrap_or_else(|| LEDGER_TABLES.iter().map(|e| e.to_string()).collect()); diff --git a/crates/sovereign-sdk/full-node/db/sov-db/src/native_db.rs b/crates/sovereign-sdk/full-node/db/sov-db/src/native_db.rs index 82456be447..97cad4912e 100644 --- a/crates/sovereign-sdk/full-node/db/sov-db/src/native_db.rs +++ b/crates/sovereign-sdk/full-node/db/sov-db/src/native_db.rs @@ -24,8 +24,14 @@ impl NativeDB { /// Initialize [`sov_schema_db::DB`] that matches tables and columns for NativeDB pub fn setup_schema_db(cfg: &RocksdbConfig) -> anyhow::Result { - let raw_options = cfg.as_raw_options(false); - let path = cfg.path.join(Self::DB_PATH_SUFFIX); + let mut schema_db_config = cfg.clone(); + + // RocksDB config assign available open files but since we spawn 3 different rocksdb instances, we need to share between them + // Allocate 20% for NativeDB + schema_db_config.max_open_files = schema_db_config.max_open_files * 20 / 100; + + let raw_options = schema_db_config.as_raw_options(false); + let path = schema_db_config.path.join(Self::DB_PATH_SUFFIX); sov_schema_db::DB::open( path, Self::DB_NAME, diff --git a/crates/sovereign-sdk/full-node/db/sov-db/src/rocks_db_config.rs b/crates/sovereign-sdk/full-node/db/sov-db/src/rocks_db_config.rs index 0d291b9986..f62cc25b64 100644 --- a/crates/sovereign-sdk/full-node/db/sov-db/src/rocks_db_config.rs +++ b/crates/sovereign-sdk/full-node/db/sov-db/src/rocks_db_config.rs @@ -34,7 +34,7 @@ impl<'a> RocksdbConfig<'a> { max_open_files: Option, column_families: Option>, ) -> Self { - let max_open_files = max_open_files.unwrap_or_else(get_fd_limit); + let max_open_files = max_open_files.unwrap_or_else(get_rocksdb_fd_limit); Self { path, // Allow db to close old sst files, saving memory. @@ -104,19 +104,18 @@ impl<'a> RocksdbConfig<'a> { } } -fn get_fd_limit() -> i32 { - let (soft_limit, _) = getrlimit(Resource::NOFILE).unwrap_or_else(|err| { - warn!( - "Failed to retrieve max open file limit from the os, defaulting to 256. err={}", - err - ); - // Default is 256 due to it being the lowest default limit among operating systems, namely OSX. - (256, 0) - }); +fn get_rocksdb_fd_limit() -> i32 { + // Default is 256 due to it being the lowest default limit among operating systems, namely OSX. + const DEFAULT_FD_LIMIT: i32 = 256; - if soft_limit > (i32::MAX as u64) { - i32::MAX - } else { - soft_limit as i32 - } + let limit = getrlimit(Resource::NOFILE) + .inspect_err(|e| { + warn!("Failed to retrieve max open file limit from the os, defaulting to 256. err={e}") + }) + .map_or(DEFAULT_FD_LIMIT, |(soft_limit, _)| { + soft_limit.min(i32::MAX as u64) as i32 + }); + + // Allocates half of that for rocksdb + limit / 2 } diff --git a/crates/sovereign-sdk/full-node/db/sov-db/src/state_db.rs b/crates/sovereign-sdk/full-node/db/sov-db/src/state_db.rs index ad25e7acce..09388b358d 100644 --- a/crates/sovereign-sdk/full-node/db/sov-db/src/state_db.rs +++ b/crates/sovereign-sdk/full-node/db/sov-db/src/state_db.rs @@ -28,8 +28,13 @@ impl StateDB { /// Initialize [`DB`] that should be globally used pub fn setup_schema_db(cfg: &RocksdbConfig) -> anyhow::Result { - let raw_options = cfg.as_raw_options(false); - let state_db_path = cfg.path.join(Self::DB_PATH_SUFFIX); + let mut state_db_config = cfg.clone(); + + // RocksDB config assign available open files but since we spawn 3 different rocksdb instances, we need to share between them + // Allocate 70% for StateDB + state_db_config.max_open_files = state_db_config.max_open_files * 70 / 100; + let raw_options = state_db_config.as_raw_options(false); + let state_db_path = state_db_config.path.join(Self::DB_PATH_SUFFIX); sov_schema_db::DB::open( state_db_path, Self::DB_NAME,