@@ -8,10 +8,8 @@ use db_metrics::DbMetrics;
88use mp_chain_config:: ChainConfig ;
99use mp_utils:: service:: Service ;
1010use rocksdb:: backup:: { BackupEngine , BackupEngineOptions } ;
11- use rocksdb:: {
12- BoundColumnFamily , ColumnFamilyDescriptor , DBCompressionType , DBWithThreadMode , Env , FlushOptions , MultiThreaded ,
13- Options , SliceTransform ,
14- } ;
11+ use rocksdb:: { BoundColumnFamily , ColumnFamilyDescriptor , DBWithThreadMode , Env , FlushOptions , MultiThreaded } ;
12+ use rocksdb_options:: rocksdb_global_options;
1513use starknet_types_core:: hash:: { Pedersen , Poseidon , StarkHash } ;
1614use std:: path:: { Path , PathBuf } ;
1715use std:: sync:: { Arc , Mutex } ;
@@ -28,6 +26,7 @@ pub mod db_metrics;
2826pub mod devnet_db;
2927mod error;
3028pub mod l1_db;
29+ mod rocksdb_options;
3130pub mod storage_updates;
3231pub mod tests;
3332
@@ -38,39 +37,8 @@ pub type WriteBatchWithTransaction = rocksdb::WriteBatchWithTransaction<false>;
3837
3938const DB_UPDATES_BATCH_SIZE : usize = 1024 ;
4039
41- #[ allow( clippy:: identity_op) ] // allow 1 * MiB
42- #[ allow( non_upper_case_globals) ] // allow KiB/MiB/GiB names
43- pub fn open_rocksdb ( path : & Path , create : bool ) -> Result < Arc < DB > > {
44- const KiB : usize = 1024 ;
45- const MiB : usize = 1024 * KiB ;
46- const GiB : usize = 1024 * MiB ;
47-
48- let mut opts = Options :: default ( ) ;
49- opts. set_report_bg_io_stats ( true ) ;
50- opts. set_use_fsync ( false ) ;
51- opts. create_if_missing ( create) ;
52- opts. create_missing_column_families ( true ) ;
53- opts. set_keep_log_file_num ( 1 ) ;
54- opts. optimize_level_style_compaction ( 4 * GiB ) ;
55- opts. set_compression_type ( DBCompressionType :: Zstd ) ;
56- let cores = std:: thread:: available_parallelism ( ) . map ( |e| e. get ( ) as i32 ) . unwrap_or ( 1 ) ;
57- opts. increase_parallelism ( cores) ;
58-
59- opts. set_atomic_flush ( true ) ;
60- opts. set_manual_wal_flush ( true ) ;
61- opts. set_max_subcompactions ( cores as _ ) ;
62-
63- opts. set_max_log_file_size ( 1 * MiB ) ;
64- opts. set_max_open_files ( 512 ) ; // 512 is the value used by substrate for reference
65- opts. set_keep_log_file_num ( 3 ) ;
66- opts. set_log_level ( rocksdb:: LogLevel :: Warn ) ;
67-
68- let mut env = Env :: new ( ) . context ( "Creating rocksdb env" ) ?;
69- // env.set_high_priority_background_threads(cores); // flushes
70- env. set_low_priority_background_threads ( cores) ; // compaction
71-
72- opts. set_env ( & env) ;
73-
40+ pub fn open_rocksdb ( path : & Path ) -> Result < Arc < DB > > {
41+ let opts = rocksdb_global_options ( ) ?;
7442 tracing:: debug!( "opening db at {:?}" , path. display( ) ) ;
7543 let db = DB :: open_cf_descriptors (
7644 & opts,
@@ -265,31 +233,6 @@ impl Column {
265233 Devnet => "devnet" ,
266234 }
267235 }
268-
269- /// Per column rocksdb options, like memory budget, compaction profiles, block sizes for hdd/sdd
270- /// etc. TODO: add basic sensible defaults
271- pub ( crate ) fn rocksdb_options ( & self ) -> Options {
272- let mut opts = Options :: default ( ) ;
273- match self {
274- Column :: ContractStorage => {
275- opts. set_prefix_extractor ( SliceTransform :: create_fixed_prefix (
276- contract_db:: CONTRACT_STORAGE_PREFIX_EXTRACTOR ,
277- ) ) ;
278- }
279- Column :: ContractToClassHashes => {
280- opts. set_prefix_extractor ( SliceTransform :: create_fixed_prefix (
281- contract_db:: CONTRACT_CLASS_HASH_PREFIX_EXTRACTOR ,
282- ) ) ;
283- }
284- Column :: ContractToNonces => {
285- opts. set_prefix_extractor ( SliceTransform :: create_fixed_prefix (
286- contract_db:: CONTRACT_NONCES_PREFIX_EXTRACTOR ,
287- ) ) ;
288- }
289- _ => { }
290- }
291- opts
292- }
293236}
294237
295238pub trait DatabaseExt {
@@ -386,7 +329,7 @@ impl MadaraBackend {
386329 let temp_dir = tempfile:: TempDir :: with_prefix ( "madara-test" ) . unwrap ( ) ;
387330 Arc :: new ( Self {
388331 backup_handle : None ,
389- db : open_rocksdb ( temp_dir. as_ref ( ) , true ) . unwrap ( ) ,
332+ db : open_rocksdb ( temp_dir. as_ref ( ) ) . unwrap ( ) ,
390333 last_flush_time : Default :: default ( ) ,
391334 chain_config,
392335 db_metrics : DbMetrics :: register ( ) . unwrap ( ) ,
@@ -425,7 +368,7 @@ impl MadaraBackend {
425368 None
426369 } ;
427370
428- let db = open_rocksdb ( & db_path, true ) ?;
371+ let db = open_rocksdb ( & db_path) ?;
429372
430373 let backend = Arc :: new ( Self {
431374 db_metrics : DbMetrics :: register ( ) . context ( "Registering db metrics" ) ?,
0 commit comments