tycho_core/storage/
config.rs

1use std::time::Duration;
2
3use bytesize::ByteSize;
4use serde::{Deserialize, Serialize};
5use tycho_util::config::PartialConfig;
6use tycho_util::serde_helpers;
7
8#[derive(Debug, Clone, Serialize, Deserialize, PartialConfig)]
9#[serde(deny_unknown_fields, default)]
10pub struct CoreStorageConfig {
11    /// Runtime cells cache size.
12    ///
13    /// Default: 256 MB.
14    #[important]
15    pub cells_cache_size: ByteSize,
16
17    /// Archive chunk size.
18    ///
19    /// Default: 1 MB.
20    pub archive_chunk_size: ByteSize,
21
22    /// Number of concurrent running split block tasks.
23    ///
24    /// Default: 100.
25    pub split_block_tasks: usize,
26
27    /// Archives storage config.
28    ///
29    /// Archives are disabled if this field is `None`.
30    pub archives_gc: Option<ArchivesGcConfig>,
31
32    /// States GC config.
33    ///
34    /// States GC is disabled if this field is `None`.
35    pub states_gc: Option<StatesGcConfig>,
36
37    /// Blocks GC config.
38    ///
39    /// Blocks GC is disabled if this field is `None`.
40    pub blocks_gc: Option<BlocksGcConfig>,
41
42    /// Blocks cache config.
43    pub blocks_cache: BlocksCacheConfig,
44}
45
46impl CoreStorageConfig {
47    #[cfg(any(test, feature = "test"))]
48    pub fn new_potato() -> Self {
49        Self {
50            cells_cache_size: ByteSize::kb(1024),
51            ..Default::default()
52        }
53    }
54}
55
56impl Default for CoreStorageConfig {
57    fn default() -> Self {
58        Self {
59            cells_cache_size: ByteSize::mb(256),
60            split_block_tasks: 100,
61            archive_chunk_size: ByteSize::kb(1024),
62            archives_gc: Some(ArchivesGcConfig::default()),
63            states_gc: Some(StatesGcConfig::default()),
64            blocks_gc: Some(BlocksGcConfig::default()),
65            blocks_cache: BlocksCacheConfig::default(),
66        }
67    }
68}
69
70#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
71#[serde(default, deny_unknown_fields)]
72pub struct ArchivesGcConfig {
73    /// Remove archives after this interval after the new persistent state
74    #[serde(with = "serde_helpers::humantime")]
75    pub persistent_state_offset: Duration,
76}
77
78impl Default for ArchivesGcConfig {
79    fn default() -> Self {
80        Self {
81            persistent_state_offset: Duration::from_secs(300),
82        }
83    }
84}
85
86#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
87#[serde(default, deny_unknown_fields)]
88pub struct StatesGcConfig {
89    /// Wether to add random offset to the first interval.
90    ///
91    /// Default: true.
92    pub random_offset: bool,
93    /// Default: 900
94    #[serde(with = "serde_helpers::humantime")]
95    pub interval: Duration,
96}
97
98impl Default for StatesGcConfig {
99    fn default() -> Self {
100        Self {
101            random_offset: true,
102            interval: Duration::from_secs(60),
103        }
104    }
105}
106
107#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
108#[serde(default)]
109pub struct BlocksGcConfig {
110    /// Blocks GC type
111    /// - `before_previous_key_block` - on each new key block delete all blocks before the previous one
112    /// - `before_previous_persistent_state` - on each new key block delete all blocks before the
113    ///   previous key block with persistent state
114    #[serde(flatten)]
115    pub ty: BlocksGcType,
116
117    /// Whether to enable blocks GC during sync. Default: true
118    pub enable_for_sync: bool,
119
120    /// Max `WriteBatch` entries before apply
121    pub max_blocks_per_batch: Option<usize>,
122}
123
124impl Default for BlocksGcConfig {
125    fn default() -> Self {
126        Self {
127            ty: BlocksGcType::BeforeSafeDistance {
128                safe_distance: 1000,
129                min_interval: Duration::from_secs(60),
130            },
131            enable_for_sync: true,
132            max_blocks_per_batch: Some(100_000),
133        }
134    }
135}
136
137#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
138#[serde(tag = "type")]
139pub enum BlocksGcType {
140    /// Remove all blocks before the specified safe distance (of mc blocks).
141    BeforeSafeDistance {
142        /// Number of masterchain blocks to keep.
143        safe_distance: u32,
144        /// Minimum interval between GC runs.
145        ///
146        /// Should be about 1 minute.
147        #[serde(with = "serde_helpers::humantime")]
148        min_interval: Duration,
149    },
150    /// Remove all blocks before the previous key block.
151    BeforePreviousKeyBlock,
152    /// Remove all blocks before the previous persistent state.
153    BeforePreviousPersistentState,
154}
155
156#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
157#[serde(default, deny_unknown_fields)]
158pub struct BlocksCacheConfig {
159    /// Entry TTL.
160    ///
161    /// Default: `5 min`.
162    #[serde(with = "serde_helpers::humantime")]
163    pub ttl: Duration,
164
165    /// Cache capacity in bytes.
166    ///
167    /// Default: `500 MB`.
168    pub size: ByteSize,
169}
170
171impl Default for BlocksCacheConfig {
172    fn default() -> Self {
173        Self {
174            ttl: Duration::from_secs(300),
175            size: ByteSize::mb(500),
176        }
177    }
178}