From 3ca4d251ea93254bad41abbac36969ced7877066 Mon Sep 17 00:00:00 2001 From: AurelienFT Date: Thu, 2 Jan 2025 09:28:46 +0100 Subject: [PATCH] Add an optional value for the cache of the database --- benches/benches/block_target_gas.rs | 2 +- bin/fuel-core/src/cli/run.rs | 10 ++-------- crates/fuel-core/src/combined_database.rs | 16 +++++++++++----- crates/fuel-core/src/database.rs | 15 +++++++++++---- crates/fuel-core/src/service/config.rs | 2 +- crates/fuel-core/src/state/rocks_db.rs | 9 ++++++++- tests/test-helpers/src/builder.rs | 6 ++++-- 7 files changed, 38 insertions(+), 22 deletions(-) diff --git a/benches/benches/block_target_gas.rs b/benches/benches/block_target_gas.rs index fddd14a24c9..940663576d9 100644 --- a/benches/benches/block_target_gas.rs +++ b/benches/benches/block_target_gas.rs @@ -266,7 +266,7 @@ fn service_with_many_contracts( .build() .unwrap(); let _drop = rt.enter(); - let mut database = Database::rocksdb_temp(StateRewindPolicy::NoRewind) + let mut database = Database::rocksdb_temp(None, StateRewindPolicy::NoRewind, -1) .expect("Failed to create database"); let mut chain_config = ChainConfig::local_testnet(); diff --git a/bin/fuel-core/src/cli/run.rs b/bin/fuel-core/src/cli/run.rs index 06d00219b50..789870e2228 100644 --- a/bin/fuel-core/src/cli/run.rs +++ b/bin/fuel-core/src/cli/run.rs @@ -85,8 +85,6 @@ use tracing::{ #[cfg(feature = "rocksdb")] use fuel_core::state::historical_rocksdb::StateRewindPolicy; -use super::DEFAULT_DATABASE_CACHE_SIZE; - #[cfg(feature = "p2p")] mod p2p; @@ -105,12 +103,8 @@ pub struct Command { pub service_name: String, /// The maximum database cache size in bytes. - #[arg( - long = "max-database-cache-size", - default_value_t = DEFAULT_DATABASE_CACHE_SIZE, - env - )] - pub max_database_cache_size: usize, + #[arg(long = "max-database-cache-size", env)] + pub max_database_cache_size: Option, #[clap( name = "DB_PATH", diff --git a/crates/fuel-core/src/combined_database.rs b/crates/fuel-core/src/combined_database.rs index c0b6d291af1..7dfc61b425a 100644 --- a/crates/fuel-core/src/combined_database.rs +++ b/crates/fuel-core/src/combined_database.rs @@ -36,7 +36,7 @@ use std::path::PathBuf; pub struct CombinedDatabaseConfig { pub database_path: PathBuf, pub database_type: DbType, - pub max_database_cache_size: usize, + pub max_database_cache_size: Option, #[cfg(feature = "rocksdb")] pub state_rewind_policy: StateRewindPolicy, #[cfg(feature = "rocksdb")] @@ -79,7 +79,7 @@ impl CombinedDatabase { #[cfg(feature = "rocksdb")] pub fn open( path: &std::path::Path, - capacity: usize, + capacity: impl Into>, state_rewind_policy: StateRewindPolicy, max_fds: i32, ) -> crate::database::Result { @@ -88,6 +88,7 @@ impl CombinedDatabase { -1 => -1, _ => max_fds.saturating_div(4), }; + let capacity = capacity.into(); // TODO: Use different cache sizes for different databases let on_chain = Database::open_rocksdb(path, capacity, state_rewind_policy, max_fds)?; @@ -108,11 +109,14 @@ impl CombinedDatabase { /// A test-only temporary rocksdb database with given rewind policy. #[cfg(feature = "rocksdb")] pub fn temp_database_with_state_rewind_policy( + max_database_cache_size: impl Into>, state_rewind_policy: StateRewindPolicy, + max_fds: i32, ) -> DatabaseResult { + let capacity = max_database_cache_size.into(); Ok(Self { - on_chain: Database::rocksdb_temp(state_rewind_policy)?, - off_chain: Database::rocksdb_temp(state_rewind_policy)?, + on_chain: Database::rocksdb_temp(capacity, state_rewind_policy, max_fds)?, + off_chain: Database::rocksdb_temp(capacity, state_rewind_policy, max_fds)?, relayer: Default::default(), gas_price: Default::default(), }) @@ -128,11 +132,13 @@ impl CombinedDatabase { "No RocksDB path configured, initializing database with a tmp directory" ); CombinedDatabase::temp_database_with_state_rewind_policy( + config.max_database_cache_size, config.state_rewind_policy, + config.max_fds, )? } else { tracing::info!( - "Opening database {:?} with cache size \"{}\" and state rewind policy \"{:?}\"", + "Opening database {:?} with cache size \"{:?}\" and state rewind policy \"{:?}\"", config.database_path, config.max_database_cache_size, config.state_rewind_policy, diff --git a/crates/fuel-core/src/database.rs b/crates/fuel-core/src/database.rs index 41d0451f221..dfb8f8639a5 100644 --- a/crates/fuel-core/src/database.rs +++ b/crates/fuel-core/src/database.rs @@ -255,9 +255,16 @@ where } #[cfg(feature = "rocksdb")] - pub fn rocksdb_temp(rewind_policy: StateRewindPolicy) -> Result { - let db = RocksDb::>::default_open_temp(None)?; - let historical_db = HistoricalRocksDB::new(db, rewind_policy)?; + pub fn rocksdb_temp( + max_database_cache_size: impl Into>, + state_rewind_policy: StateRewindPolicy, + max_fds: i32, + ) -> Result { + let db = RocksDb::>::default_open_temp_with_params( + max_database_cache_size.into(), + max_fds, + )?; + let historical_db = HistoricalRocksDB::new(db, state_rewind_policy)?; let data = Arc::new(historical_db); Ok(Self::from_storage(DataSource::new(data, Stage::default()))) } @@ -278,7 +285,7 @@ where } #[cfg(feature = "rocksdb")] { - Self::rocksdb_temp(StateRewindPolicy::NoRewind) + Self::rocksdb_temp(None, StateRewindPolicy::NoRewind, 512) .expect("Failed to create a temporary database") } } diff --git a/crates/fuel-core/src/service/config.rs b/crates/fuel-core/src/service/config.rs index 0e093c2b8b6..ca2d2fd7df2 100644 --- a/crates/fuel-core/src/service/config.rs +++ b/crates/fuel-core/src/service/config.rs @@ -117,7 +117,7 @@ impl Config { let combined_db_config = CombinedDatabaseConfig { // Set the cache for tests = 10MB - max_database_cache_size: 10 * 1024 * 1024, + max_database_cache_size: Some(10 * 1024 * 1024), database_path: Default::default(), #[cfg(feature = "rocksdb")] database_type: DbType::RocksDb, diff --git a/crates/fuel-core/src/state/rocks_db.rs b/crates/fuel-core/src/state/rocks_db.rs index 3df5c7127e5..e95128b7b27 100644 --- a/crates/fuel-core/src/state/rocks_db.rs +++ b/crates/fuel-core/src/state/rocks_db.rs @@ -126,13 +126,20 @@ where Description: DatabaseDescription, { pub fn default_open_temp(capacity: Option) -> DatabaseResult { + Self::default_open_temp_with_params(capacity, 512) + } + + pub fn default_open_temp_with_params( + capacity: Option, + max_fds: i32, + ) -> DatabaseResult { let tmp_dir = TempDir::new().unwrap(); let path = tmp_dir.path(); let result = Self::open( path, enum_iterator::all::().collect::>(), capacity, - 512, + max_fds, ); let mut db = result?; diff --git a/tests/test-helpers/src/builder.rs b/tests/test-helpers/src/builder.rs index 0d16bc48dc6..e969bdae9e5 100644 --- a/tests/test-helpers/src/builder.rs +++ b/tests/test-helpers/src/builder.rs @@ -231,14 +231,16 @@ impl TestSetupBuilder { ..StateConfig::default() }; - let config = Config { + let mut config = Config { utxo_validation: self.utxo_validation, txpool: fuel_core_txpool::config::Config::default(), block_production: self.trigger, starting_gas_price: self.starting_gas_price, ..Config::local_node_with_configs(chain_conf, state) }; - assert_eq!(config.combined_db_config.database_type, DbType::RocksDb); + config.combined_db_config.max_database_cache_size = Some(16 * 1024 * 1024 * 1024); + config.combined_db_config.max_fds = -1; + config.combined_db_config.database_type = DbType::RocksDb; let srv = FuelService::new_node(config).await.unwrap(); let client = FuelClient::from(srv.bound_address);