Skip to content

Commit

Permalink
Add an optional value for the cache of the database
Browse files Browse the repository at this point in the history
  • Loading branch information
AurelienFT committed Jan 2, 2025
1 parent df0283c commit 3ca4d25
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 22 deletions.
2 changes: 1 addition & 1 deletion benches/benches/block_target_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 2 additions & 8 deletions bin/fuel-core/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<usize>,

#[clap(
name = "DB_PATH",
Expand Down
16 changes: 11 additions & 5 deletions crates/fuel-core/src/combined_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>,
#[cfg(feature = "rocksdb")]
pub state_rewind_policy: StateRewindPolicy,
#[cfg(feature = "rocksdb")]
Expand Down Expand Up @@ -79,7 +79,7 @@ impl CombinedDatabase {
#[cfg(feature = "rocksdb")]
pub fn open(
path: &std::path::Path,
capacity: usize,
capacity: impl Into<Option<usize>>,
state_rewind_policy: StateRewindPolicy,
max_fds: i32,
) -> crate::database::Result<Self> {
Expand All @@ -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)?;
Expand All @@ -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<Option<usize>>,
state_rewind_policy: StateRewindPolicy,
max_fds: i32,
) -> DatabaseResult<Self> {
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(),
})
Expand All @@ -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,
Expand Down
15 changes: 11 additions & 4 deletions crates/fuel-core/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,16 @@ where
}

#[cfg(feature = "rocksdb")]
pub fn rocksdb_temp(rewind_policy: StateRewindPolicy) -> Result<Self> {
let db = RocksDb::<Historical<Description>>::default_open_temp(None)?;
let historical_db = HistoricalRocksDB::new(db, rewind_policy)?;
pub fn rocksdb_temp(
max_database_cache_size: impl Into<Option<usize>>,
state_rewind_policy: StateRewindPolicy,
max_fds: i32,
) -> Result<Self> {
let db = RocksDb::<Historical<Description>>::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())))
}
Expand All @@ -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")
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/fuel-core/src/service/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion crates/fuel-core/src/state/rocks_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,20 @@ where
Description: DatabaseDescription,
{
pub fn default_open_temp(capacity: Option<usize>) -> DatabaseResult<Self> {
Self::default_open_temp_with_params(capacity, 512)
}

pub fn default_open_temp_with_params(
capacity: Option<usize>,
max_fds: i32,
) -> DatabaseResult<Self> {
let tmp_dir = TempDir::new().unwrap();
let path = tmp_dir.path();
let result = Self::open(
path,
enum_iterator::all::<Description::Column>().collect::<Vec<_>>(),
capacity,
512,
max_fds,
);
let mut db = result?;

Expand Down
6 changes: 4 additions & 2 deletions tests/test-helpers/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3ca4d25

Please sign in to comment.