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

1//! RocksDB Column Family cache configuration.
2
3use clap::Parser;
4use display_json::DebugAsJson;
5use serde::Serialize;
6
7use crate::utils::GIGABYTE;
8
9const DEFAULT_ACCOUNTS_CACHE: usize = 10 * GIGABYTE;
10const DEFAULT_ACCOUNTS_HISTORY_CACHE: usize = 5 * GIGABYTE;
11const DEFAULT_ACCOUNT_SLOTS_CACHE: usize = 40 * GIGABYTE;
12const DEFAULT_ACCOUNT_SLOTS_HISTORY_CACHE: usize = 20 * GIGABYTE;
13const DEFAULT_TRANSACTIONS_CACHE: usize = 20 * GIGABYTE;
14const DEFAULT_BLOCKS_BY_HASH_CACHE: usize = 2 * GIGABYTE;
15const DEFAULT_BLOCKS_BY_NUMBER_CACHE: usize = 20 * GIGABYTE;
16const DEFAULT_BLOCKS_BY_TIMESTAMP_CACHE: usize = 2 * GIGABYTE;
17const DEFAULT_BLOCK_CHANGES_CACHE: usize = 2 * GIGABYTE;
18
19/// Configuration for individual RocksDB Column Family caches.
20///
21/// This replaces the previous cache_size_multiplier approach with
22/// individual cache size settings for each Column Family.
23#[derive(DebugAsJson, Clone, Parser, Serialize)]
24pub struct RocksCfCacheConfig {
25    /// Cache size in bytes for the 'accounts' column family.
26    #[arg(
27        long = "rocks-cf-cache-accounts",
28        env = "ROCKS_CF_CACHE_ACCOUNTS",
29        default_value_t = DEFAULT_ACCOUNTS_CACHE
30    )]
31    pub accounts: usize,
32
33    /// Cache size in bytes for the 'accounts_history' column family.
34    #[arg(
35        long = "rocks-cf-cache-accounts-history",
36        env = "ROCKS_CF_CACHE_ACCOUNTS_HISTORY",
37        default_value_t = DEFAULT_ACCOUNTS_HISTORY_CACHE
38    )]
39    pub accounts_history: usize,
40
41    /// Cache size in bytes for the 'account_slots' column family.
42    #[arg(
43        long = "rocks-cf-cache-account-slots",
44        env = "ROCKS_CF_CACHE_ACCOUNT_SLOTS",
45        default_value_t = DEFAULT_ACCOUNT_SLOTS_CACHE
46    )]
47    pub account_slots: usize,
48
49    /// Cache size in bytes for the 'account_slots_history' column family.
50    #[arg(
51        long = "rocks-cf-cache-account-slots-history",
52        env = "ROCKS_CF_CACHE_ACCOUNT_SLOTS_HISTORY",
53        default_value_t = DEFAULT_ACCOUNT_SLOTS_HISTORY_CACHE
54    )]
55    pub account_slots_history: usize,
56
57    /// Cache size in bytes for the 'transactions' column family.
58    #[arg(
59        long = "rocks-cf-cache-transactions",
60        env = "ROCKS_CF_CACHE_TRANSACTIONS",
61        default_value_t = DEFAULT_TRANSACTIONS_CACHE
62    )]
63    pub transactions: usize,
64
65    /// Cache size in bytes for the 'blocks_by_number' column family.
66    #[arg(
67        long = "rocks-cf-cache-blocks-by-number",
68        env = "ROCKS_CF_CACHE_BLOCKS_BY_NUMBER",
69        default_value_t = DEFAULT_BLOCKS_BY_NUMBER_CACHE
70    )]
71    pub blocks_by_number: usize,
72
73    /// Cache size in bytes for the 'blocks_by_hash' column family.
74    #[arg(
75        long = "rocks-cf-cache-blocks-by-hash",
76        env = "ROCKS_CF_CACHE_BLOCKS_BY_HASH",
77        default_value_t = DEFAULT_BLOCKS_BY_HASH_CACHE
78    )]
79    pub blocks_by_hash: usize,
80
81    /// Cache size in bytes for the 'blocks_by_timestamp' column family.
82    #[arg(
83        long = "rocks-cf-cache-blocks-by-timestamp",
84        env = "ROCKS_CF_CACHE_BLOCKS_BY_TIMESTAMP",
85        default_value_t = DEFAULT_BLOCKS_BY_TIMESTAMP_CACHE
86    )]
87    pub blocks_by_timestamp: usize,
88
89    /// Cache size in bytes for the 'block_changes' column family.
90    #[arg(
91        long = "rocks-cf-cache-block-changes",
92        env = "ROCKS_CF_CACHE_BLOCK_CHANGES",
93        default_value_t = DEFAULT_BLOCK_CHANGES_CACHE
94    )]
95    pub block_changes: usize,
96}
97
98impl Default for RocksCfCacheConfig {
99    fn default() -> Self {
100        Self {
101            accounts: DEFAULT_ACCOUNTS_CACHE,                           // 10GB
102            accounts_history: DEFAULT_ACCOUNTS_HISTORY_CACHE,           // 2GB
103            account_slots: DEFAULT_ACCOUNT_SLOTS_CACHE,                 // 30GB
104            account_slots_history: DEFAULT_ACCOUNT_SLOTS_HISTORY_CACHE, // 2GB
105            transactions: DEFAULT_TRANSACTIONS_CACHE,                   // 2GB
106            blocks_by_number: DEFAULT_BLOCKS_BY_NUMBER_CACHE,           // 2GB
107            blocks_by_hash: DEFAULT_BLOCKS_BY_HASH_CACHE,               // 2GB
108            blocks_by_timestamp: DEFAULT_BLOCKS_BY_TIMESTAMP_CACHE,     // 2GB
109            block_changes: DEFAULT_BLOCK_CHANGES_CACHE,                 // 2GB
110        }
111    }
112}
113
114impl RocksCfCacheConfig {
115    pub fn default_with_multiplier(multiplier: f64) -> Self {
116        Self::default().with_multiplier(multiplier)
117    }
118
119    pub fn with_multiplier(mut self, multiplier: f64) -> Self {
120        self.accounts = (self.accounts as f64 * multiplier) as usize;
121        self.accounts_history = (self.accounts_history as f64 * multiplier) as usize;
122        self.account_slots = (self.account_slots as f64 * multiplier) as usize;
123        self.account_slots_history = (self.account_slots_history as f64 * multiplier) as usize;
124        self.transactions = (self.transactions as f64 * multiplier) as usize;
125        self.blocks_by_number = (self.blocks_by_number as f64 * multiplier) as usize;
126        self.blocks_by_hash = (self.blocks_by_hash as f64 * multiplier) as usize;
127        self.blocks_by_timestamp = (self.blocks_by_timestamp as f64 * multiplier) as usize;
128        self.block_changes = (self.block_changes as f64 * multiplier) as usize;
129        self
130    }
131}