1use std::num::NonZeroU32;
2use std::time::Duration;
3
4use bytesize::ByteSize;
5use serde::{Deserialize, Serialize};
6use tycho_util::config::PartialConfig;
7use tycho_util::serde_helpers;
8
9#[derive(Debug, Clone, Serialize, Deserialize, PartialConfig)]
10#[serde(deny_unknown_fields, default)]
11pub struct CoreStorageConfig {
12 #[important]
16 pub cells_cache_size: ByteSize,
17
18 pub drop_interval: u32,
22
23 pub store_archives: bool,
27
28 pub shard_split_depth: u8,
32
33 pub store_shard_state_step: NonZeroU32,
37
38 pub max_new_cells_threshold: usize,
43
44 pub archives_gc: Option<ArchivesGcConfig>,
48
49 pub states_gc: Option<StatesGcConfig>,
53
54 pub blocks_gc: Option<BlocksGcConfig>,
58
59 pub blocks_cache: BlocksCacheConfig,
61
62 pub blob_db: BlobDbConfig,
64}
65
66impl CoreStorageConfig {
67 #[cfg(any(test, feature = "test"))]
68 pub fn new_potato() -> Self {
69 Self {
70 cells_cache_size: ByteSize::kb(1024),
71 blob_db: BlobDbConfig {
72 pre_create_cas_tree: false,
73 },
74 archives_gc: None,
75 states_gc: None,
76 blocks_gc: None,
77 ..Default::default()
78 }
79 }
80
81 pub fn without_gc(mut self) -> Self {
82 self.archives_gc = None;
83 self.states_gc = None;
84 self.blocks_gc = None;
85 self
86 }
87}
88
89impl Default for CoreStorageConfig {
90 fn default() -> Self {
91 Self {
92 cells_cache_size: ByteSize::mb(256),
93 drop_interval: 3,
94 store_archives: true,
95 shard_split_depth: 5,
96 store_shard_state_step: NonZeroU32::new(5).unwrap(),
97 max_new_cells_threshold: 500_000,
98 archives_gc: Some(ArchivesGcConfig::default()),
99 states_gc: Some(StatesGcConfig::default()),
100 blocks_gc: Some(BlocksGcConfig::default()),
101 blocks_cache: BlocksCacheConfig::default(),
102 blob_db: BlobDbConfig::default(),
103 }
104 }
105}
106
107#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
108#[serde(default, deny_unknown_fields)]
109pub struct ArchivesGcConfig {
110 #[serde(with = "serde_helpers::humantime")]
112 pub persistent_state_offset: Duration,
113}
114
115impl Default for ArchivesGcConfig {
116 fn default() -> Self {
117 Self {
118 persistent_state_offset: Duration::from_secs(300),
119 }
120 }
121}
122
123#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
124#[serde(default, deny_unknown_fields)]
125pub struct StatesGcConfig {
126 pub random_offset: bool,
130 #[serde(with = "serde_helpers::humantime")]
132 pub interval: Duration,
133}
134
135impl Default for StatesGcConfig {
136 fn default() -> Self {
137 Self {
138 random_offset: true,
139 interval: Duration::from_secs(60),
140 }
141 }
142}
143
144#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
145#[serde(default)]
146pub struct BlocksGcConfig {
147 #[serde(flatten)]
152 pub ty: BlocksGcType,
153
154 pub enable_for_sync: bool,
156
157 pub max_blocks_per_batch: Option<usize>,
159}
160
161impl Default for BlocksGcConfig {
162 fn default() -> Self {
163 Self {
164 ty: BlocksGcType::BeforeSafeDistance {
165 safe_distance: 1000,
166 min_interval: Duration::from_secs(60),
167 },
168 enable_for_sync: true,
169 max_blocks_per_batch: Some(100_000),
170 }
171 }
172}
173
174#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
175#[serde(tag = "type")]
176pub enum BlocksGcType {
177 BeforeSafeDistance {
179 safe_distance: u32,
181 #[serde(with = "serde_helpers::humantime")]
185 min_interval: Duration,
186 },
187 BeforePreviousKeyBlock,
189 BeforePreviousPersistentState,
191}
192
193#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
194#[serde(default, deny_unknown_fields)]
195pub struct BlocksCacheConfig {
196 #[serde(with = "serde_helpers::humantime")]
200 pub ttl: Duration,
201
202 pub size: ByteSize,
206}
207
208impl Default for BlocksCacheConfig {
209 fn default() -> Self {
210 Self {
211 ttl: Duration::from_secs(300),
212 size: ByteSize::mb(500),
213 }
214 }
215}
216
217#[derive(Debug, Clone, Serialize, Deserialize)]
218#[serde(default, deny_unknown_fields)]
219pub struct BlobDbConfig {
220 pub pre_create_cas_tree: bool,
221}
222
223impl Default for BlobDbConfig {
224 fn default() -> Self {
225 Self {
226 pre_create_cas_tree: true,
227 }
228 }
229}