use std::num::NonZeroU8;
use std::time::Duration;
use bytesize::ByteSize;
use serde::{Deserialize, Serialize};
use tycho_util::config::PartialConfig;
use tycho_util::serde_helpers;
#[derive(Debug, Clone, Serialize, Deserialize, PartialConfig)]
#[serde(deny_unknown_fields, default)]
pub struct CoreStorageConfig {
#[important]
pub cells_cache_size: ByteSize,
pub drop_interval: u32,
pub store_archives: bool,
pub shard_split_depth: u8,
pub store_shard_state_step: NonZeroU8,
pub max_new_cells_threshold: usize,
pub archives_gc: Option<ArchivesGcConfig>,
pub states_gc: Option<StatesGcConfig>,
pub blocks_gc: Option<BlocksGcConfig>,
pub blocks_cache: BlocksCacheConfig,
pub blob_db: BlobDbConfig,
}
impl CoreStorageConfig {
#[cfg(any(test, feature = "test"))]
pub fn new_potato() -> Self {
Self {
cells_cache_size: ByteSize::kb(1024),
blob_db: BlobDbConfig {
pre_create_cas_tree: false,
},
archives_gc: None,
states_gc: None,
blocks_gc: None,
..Default::default()
}
}
pub fn without_gc(mut self) -> Self {
self.archives_gc = None;
self.states_gc = None;
self.blocks_gc = None;
self
}
}
impl Default for CoreStorageConfig {
fn default() -> Self {
Self {
cells_cache_size: ByteSize::mb(256),
drop_interval: 3,
store_archives: true,
shard_split_depth: 5,
store_shard_state_step: NonZeroU8::new(5).unwrap(),
max_new_cells_threshold: 500_000,
archives_gc: Some(ArchivesGcConfig::default()),
states_gc: Some(StatesGcConfig::default()),
blocks_gc: Some(BlocksGcConfig::default()),
blocks_cache: BlocksCacheConfig::default(),
blob_db: BlobDbConfig::default(),
}
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct ArchivesGcConfig {
#[serde(with = "serde_helpers::humantime")]
pub persistent_state_offset: Duration,
}
impl Default for ArchivesGcConfig {
fn default() -> Self {
Self {
persistent_state_offset: Duration::from_secs(300),
}
}
}
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct StatesGcConfig {
pub random_offset: bool,
#[serde(with = "serde_helpers::humantime")]
pub interval: Duration,
}
impl Default for StatesGcConfig {
fn default() -> Self {
Self {
random_offset: true,
interval: Duration::from_secs(60),
}
}
}
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct BlocksGcConfig {
#[serde(flatten)]
pub ty: BlocksGcType,
pub enable_for_sync: bool,
pub max_blocks_per_batch: Option<usize>,
}
impl Default for BlocksGcConfig {
fn default() -> Self {
Self {
ty: BlocksGcType::BeforeSafeDistance {
safe_distance: 1000,
min_interval: Duration::from_secs(60),
},
enable_for_sync: true,
max_blocks_per_batch: Some(100_000),
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum BlocksGcType {
BeforeSafeDistance {
safe_distance: u32,
#[serde(with = "serde_helpers::humantime")]
min_interval: Duration,
},
BeforePreviousKeyBlock,
BeforePreviousPersistentState,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct BlocksCacheConfig {
#[serde(with = "serde_helpers::humantime")]
pub ttl: Duration,
pub size: ByteSize,
}
impl Default for BlocksCacheConfig {
fn default() -> Self {
Self {
ttl: Duration::from_secs(300),
size: ByteSize::mb(500),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct BlobDbConfig {
pub pre_create_cas_tree: bool,
}
impl Default for BlobDbConfig {
fn default() -> Self {
Self {
pre_create_cas_tree: true,
}
}
}