From d7a22de195224a051446554eef6442fad7e65d91 Mon Sep 17 00:00:00 2001 From: Quinn Diggity <64798147+quinndiggitypolymath@users.noreply.github.com> Date: Thu, 1 Dec 2022 05:02:48 -0800 Subject: [PATCH] max_total_wal_size (#29) * adds static `db_config.max_total_wal_size`, pinned to `40 GB` to match current documentation @ https://github.com/PolymeshAssociation/polymesh-tools/blob/7d8146deb459ffca3de6cd5da270928a529f233c/docs/operator/README.md#node-resource-requirements : `It is recommended that you reserve an additional 40GB of disk space for the WAL.` * adds cli flag `--db-max-total-wal-size` to allow specifying `db_config.max_total_wal_size` * adds cli flag `--db-max-total-wal-size` to allow specifying `db_config.max_total_wal_size` * adds cli flag `--db-max-total-wal-size` to allow specifying `db_config.max_total_wal_size` Co-authored-by: Quinn Diggity Max wal size (#30) * Use `Option` for max_total_wal_size. Signed-off-by: Robert G. Jakabosky --- substrate/bin/node/testing/src/bench.rs | 2 +- substrate/client/cli/src/config.rs | 15 ++++++++++++--- .../client/cli/src/params/database_params.rs | 9 +++++++++ substrate/client/db/src/lib.rs | 4 ++++ substrate/client/db/src/utils.rs | 11 +++++++---- substrate/client/service/test/src/lib.rs | 2 +- 6 files changed, 34 insertions(+), 9 deletions(-) diff --git a/substrate/bin/node/testing/src/bench.rs b/substrate/bin/node/testing/src/bench.rs index f1ab2212239b..de25670987be 100644 --- a/substrate/bin/node/testing/src/bench.rs +++ b/substrate/bin/node/testing/src/bench.rs @@ -218,7 +218,7 @@ pub enum DatabaseType { impl DatabaseType { fn into_settings(self, path: PathBuf) -> sc_client_db::DatabaseSource { match self { - Self::RocksDb => sc_client_db::DatabaseSource::RocksDb { path, cache_size: 512 }, + Self::RocksDb => sc_client_db::DatabaseSource::RocksDb { path, cache_size: 512, max_total_wal_size: None }, Self::ParityDb => sc_client_db::DatabaseSource::ParityDb { path }, } } diff --git a/substrate/client/cli/src/config.rs b/substrate/client/cli/src/config.rs index 4d218da6aa89..2f4fb8f5aa61 100644 --- a/substrate/client/cli/src/config.rs +++ b/substrate/client/cli/src/config.rs @@ -201,6 +201,13 @@ pub trait CliConfiguration: Sized { Ok(self.database_params().map(|x| x.database_cache_size()).unwrap_or_default()) } + /// Get the database max total wal size. + /// + /// By default this is retrieved from `DatabaseParams` if it is available. Otherwise its `None`. + fn database_max_total_wal_size(&self) -> Result> { + Ok(self.database_params().map(|x| x.database_max_total_wal_size()).unwrap_or_default()) + } + /// Get the database backend variant. /// /// By default this is retrieved from `DatabaseParams` if it is available. Otherwise its `None`. @@ -213,6 +220,7 @@ pub trait CliConfiguration: Sized { &self, base_path: &PathBuf, cache_size: usize, + max_total_wal_size: Option, database: Database, ) -> Result { let role_dir = "full"; @@ -220,7 +228,7 @@ pub trait CliConfiguration: Sized { let paritydb_path = base_path.join("paritydb").join(role_dir); Ok(match database { #[cfg(feature = "rocksdb")] - Database::RocksDb => DatabaseSource::RocksDb { path: rocksdb_path, cache_size }, + Database::RocksDb => DatabaseSource::RocksDb { path: rocksdb_path, cache_size, max_total_wal_size }, Database::ParityDb => DatabaseSource::ParityDb { path: paritydb_path }, Database::ParityDbDeprecated => { eprintln!( @@ -229,7 +237,7 @@ pub trait CliConfiguration: Sized { ); DatabaseSource::ParityDb { path: paritydb_path } }, - Database::Auto => DatabaseSource::Auto { paritydb_path, rocksdb_path, cache_size }, + Database::Auto => DatabaseSource::Auto { paritydb_path, rocksdb_path, cache_size, max_total_wal_size }, }) } @@ -451,6 +459,7 @@ pub trait CliConfiguration: Sized { let net_config_dir = config_dir.join(DEFAULT_NETWORK_CONFIG_PATH); let client_id = C::client_id(); let database_cache_size = self.database_cache_size()?.unwrap_or(1024); + let database_max_total_wal_size = self.database_max_total_wal_size()?; let database = self.database()?.unwrap_or( #[cfg(feature = "rocksdb")] { @@ -485,7 +494,7 @@ pub trait CliConfiguration: Sized { DCV::p2p_listen_port(), )?, keystore, - database: self.database_config(&config_dir, database_cache_size, database)?, + database: self.database_config(&config_dir, database_cache_size, database_max_total_wal_size, database)?, data_path: config_dir, trie_cache_maximum_size: self.trie_cache_maximum_size()?, state_pruning: self.state_pruning()?, diff --git a/substrate/client/cli/src/params/database_params.rs b/substrate/client/cli/src/params/database_params.rs index cbc602cb877b..a045bcf47b11 100644 --- a/substrate/client/cli/src/params/database_params.rs +++ b/substrate/client/cli/src/params/database_params.rs @@ -29,6 +29,10 @@ pub struct DatabaseParams { /// Limit the memory the database cache can use. #[arg(long = "db-cache", value_name = "MiB")] pub database_cache_size: Option, + + /// Limit the total storage capacity that the database can use for WAL files. + #[clap(long = "db-max-total-wal-size", value_name = "MiB")] + pub database_max_total_wal_size: Option, } impl DatabaseParams { @@ -41,4 +45,9 @@ impl DatabaseParams { pub fn database_cache_size(&self) -> Option { self.database_cache_size } + + /// Limit the total storage capacity that the database can use for WAL files. + pub fn database_max_total_wal_size(&self) -> Option { + self.database_max_total_wal_size + } } diff --git a/substrate/client/db/src/lib.rs b/substrate/client/db/src/lib.rs index aba5b0829b5b..545d683560fb 100644 --- a/substrate/client/db/src/lib.rs +++ b/substrate/client/db/src/lib.rs @@ -318,6 +318,8 @@ pub enum DatabaseSource { rocksdb_path: PathBuf, /// Cache size in MiB. Used only by `RocksDb` variant of `DatabaseSource`. cache_size: usize, + /// Max total WAL size in MiB. Used only by `RocksDb` variant of `DatabaseSource`. + max_total_wal_size: Option, }, /// Load a RocksDB database from a given path. Recommended for most uses. #[cfg(feature = "rocksdb")] @@ -326,6 +328,8 @@ pub enum DatabaseSource { path: PathBuf, /// Cache size in MiB. cache_size: usize, + /// Max total WAL size in MiB. + max_total_wal_size: Option, }, /// Load a ParityDb database from a given path. diff --git a/substrate/client/db/src/utils.rs b/substrate/client/db/src/utils.rs index abf9c4629cee..65193a87539a 100644 --- a/substrate/client/db/src/utils.rs +++ b/substrate/client/db/src/utils.rs @@ -193,17 +193,17 @@ fn open_database_at( let db: Arc> = match &db_source { DatabaseSource::ParityDb { path } => open_parity_db::(path, db_type, create)?, #[cfg(feature = "rocksdb")] - DatabaseSource::RocksDb { path, cache_size } => - open_kvdb_rocksdb::(path, db_type, create, *cache_size)?, + DatabaseSource::RocksDb { path, cache_size, max_total_wal_size } => + open_kvdb_rocksdb::(path, db_type, create, *cache_size, *max_total_wal_size)?, DatabaseSource::Custom { db, require_create_flag } => { if *require_create_flag && !create { return Err(OpenDbError::DoesNotExist) } db.clone() }, - DatabaseSource::Auto { paritydb_path, rocksdb_path, cache_size } => { + DatabaseSource::Auto { paritydb_path, rocksdb_path, cache_size, max_total_wal_size } => { // check if rocksdb exists first, if not, open paritydb - match open_kvdb_rocksdb::(rocksdb_path, db_type, false, *cache_size) { + match open_kvdb_rocksdb::(rocksdb_path, db_type, false, *cache_size, *max_total_wal_size) { Ok(db) => db, Err(OpenDbError::NotEnabled(_)) | Err(OpenDbError::DoesNotExist) => open_parity_db::(paritydb_path, db_type, create)?, @@ -299,6 +299,7 @@ fn open_kvdb_rocksdb( db_type: DatabaseType, create: bool, cache_size: usize, + max_total_wal_size: Option, ) -> OpenDbResult { // first upgrade database to required version match crate::upgrade::upgrade_db::(path, db_type) { @@ -311,6 +312,7 @@ fn open_kvdb_rocksdb( // and now open database assuming that it has the latest version let mut db_config = kvdb_rocksdb::DatabaseConfig::with_columns(NUM_COLUMNS); db_config.create_if_missing = create; + db_config.max_total_wal_size = max_total_wal_size.map(|m| m << 20); let mut memory_budget = std::collections::HashMap::new(); match db_type { @@ -349,6 +351,7 @@ fn open_kvdb_rocksdb( _db_type: DatabaseType, _create: bool, _cache_size: usize, + _max_total_wal_size: Option, ) -> OpenDbResult { Err(OpenDbError::NotEnabled("with-kvdb-rocksdb")) } diff --git a/substrate/client/service/test/src/lib.rs b/substrate/client/service/test/src/lib.rs index 38a811acc740..75f6161b6deb 100644 --- a/substrate/client/service/test/src/lib.rs +++ b/substrate/client/service/test/src/lib.rs @@ -238,7 +238,7 @@ fn node_config< transaction_pool: Default::default(), network: network_config, keystore: KeystoreConfig::Path { path: root.join("key"), password: None }, - database: DatabaseSource::RocksDb { path: root.join("db"), cache_size: 128 }, + database: DatabaseSource::RocksDb { path: root.join("db"), cache_size: 128, max_total_wal_size: None }, trie_cache_maximum_size: Some(16 * 1024 * 1024), state_pruning: Default::default(), blocks_pruning: BlocksPruning::KeepFinalized,