stratus/eth/storage/permanent/rocks/
rocks_config.rs

1use rocksdb::BlockBasedOptions;
2use rocksdb::Cache;
3use rocksdb::Options;
4
5pub enum CacheSetting {
6    /// Enabled cache with the given size in bytes
7    Enabled(usize),
8    Disabled,
9}
10
11#[derive(Debug, Clone, Copy)]
12pub enum DbConfig {
13    OptimizedPointLookUp,
14    HistoricalData,
15    Default,
16}
17
18impl Default for DbConfig {
19    fn default() -> Self {
20        Self::Default
21    }
22}
23
24impl DbConfig {
25    pub fn to_options(self, cache_setting: CacheSetting) -> Options {
26        let mut opts = Options::default();
27        let mut block_based_options = BlockBasedOptions::default();
28
29        opts.create_if_missing(true);
30        opts.create_missing_column_families(true);
31        opts.increase_parallelism(16);
32
33        block_based_options.set_pin_l0_filter_and_index_blocks_in_cache(true);
34        block_based_options.set_cache_index_and_filter_blocks(true);
35        block_based_options.set_bloom_filter(15.5, false);
36
37        // due to the nature of our application enabling rocks metrics decreases point lookup performance by 5x.
38        #[cfg(feature = "rocks_metrics")]
39        {
40            opts.enable_statistics();
41            opts.set_statistics_level(rocksdb::statistics::StatsLevel::ExceptTimeForMutex);
42        }
43
44        if let CacheSetting::Enabled(cache_size) = cache_setting {
45            let block_cache = Cache::new_lru_cache(cache_size / 2);
46            let row_cache = Cache::new_lru_cache(cache_size / 2);
47
48            opts.set_row_cache(&row_cache);
49            block_based_options.set_block_cache(&block_cache);
50        }
51
52        match self {
53            DbConfig::OptimizedPointLookUp => {
54                block_based_options.set_data_block_index_type(rocksdb::DataBlockIndexType::BinaryAndHash);
55                block_based_options.set_data_block_hash_ratio(0.3);
56
57                opts.set_use_direct_reads(true);
58                opts.set_memtable_prefix_bloom_ratio(0.02);
59                opts.set_memtable_whole_key_filtering(true);
60                opts.set_compression_type(rocksdb::DBCompressionType::None);
61            }
62            DbConfig::HistoricalData | DbConfig::Default => {
63                opts.set_compression_per_level(&[
64                    rocksdb::DBCompressionType::None,
65                    rocksdb::DBCompressionType::None,
66                    rocksdb::DBCompressionType::Lz4,
67                ]);
68                opts.set_bottommost_compression_type(rocksdb::DBCompressionType::Zstd);
69                opts.set_bottommost_compression_options(-14, 32767, 0, 16 * 1024, true); // mostly defaults except max_dict_bytes
70                opts.set_bottommost_zstd_max_train_bytes(1600 * 1024, true);
71                if matches!(self, DbConfig::HistoricalData) {
72                    opts.set_memtable_whole_key_filtering(false);
73                    block_based_options.set_whole_key_filtering(false);
74
75                    opts.set_comparator("reverse", Box::new(|a, b| a.cmp(b).reverse()));
76                }
77            }
78        }
79
80        opts.set_block_based_table_factory(&block_based_options);
81
82        opts
83    }
84}