use {
super::{ArchiveFormat, SnapshotInterval, SnapshotVersion, ZstdConfig},
std::{
num::{NonZeroU64, NonZeroUsize},
path::PathBuf,
},
};
pub const DEFAULT_FULL_SNAPSHOT_ARCHIVE_INTERVAL_SLOTS: NonZeroU64 =
NonZeroU64::new(100_000).unwrap();
pub const DEFAULT_INCREMENTAL_SNAPSHOT_ARCHIVE_INTERVAL_SLOTS: NonZeroU64 =
NonZeroU64::new(100).unwrap();
pub const DEFAULT_MAX_FULL_SNAPSHOT_ARCHIVES_TO_RETAIN: NonZeroUsize =
NonZeroUsize::new(2).unwrap();
pub const DEFAULT_MAX_INCREMENTAL_SNAPSHOT_ARCHIVES_TO_RETAIN: NonZeroUsize =
NonZeroUsize::new(4).unwrap();
#[derive(Clone, Debug)]
pub struct SnapshotConfig {
pub usage: SnapshotUsage,
pub full_snapshot_archive_interval: SnapshotInterval,
pub incremental_snapshot_archive_interval: SnapshotInterval,
pub full_snapshot_archives_dir: PathBuf,
pub incremental_snapshot_archives_dir: PathBuf,
pub bank_snapshots_dir: PathBuf,
pub archive_format: ArchiveFormat,
pub snapshot_version: SnapshotVersion,
pub maximum_full_snapshot_archives_to_retain: NonZeroUsize,
pub maximum_incremental_snapshot_archives_to_retain: NonZeroUsize,
pub use_direct_io: bool,
pub use_registered_io_uring_buffers: bool,
}
impl Default for SnapshotConfig {
fn default() -> Self {
Self {
usage: SnapshotUsage::LoadAndGenerate,
full_snapshot_archive_interval: SnapshotInterval::Slots(
DEFAULT_FULL_SNAPSHOT_ARCHIVE_INTERVAL_SLOTS,
),
incremental_snapshot_archive_interval: SnapshotInterval::Slots(
DEFAULT_INCREMENTAL_SNAPSHOT_ARCHIVE_INTERVAL_SLOTS,
),
full_snapshot_archives_dir: PathBuf::default(),
incremental_snapshot_archives_dir: PathBuf::default(),
bank_snapshots_dir: PathBuf::default(),
archive_format: ArchiveFormat::TarZstd {
config: ZstdConfig::default(),
},
snapshot_version: SnapshotVersion::default(),
maximum_full_snapshot_archives_to_retain: DEFAULT_MAX_FULL_SNAPSHOT_ARCHIVES_TO_RETAIN,
maximum_incremental_snapshot_archives_to_retain:
DEFAULT_MAX_INCREMENTAL_SNAPSHOT_ARCHIVES_TO_RETAIN,
use_direct_io: true,
use_registered_io_uring_buffers: true,
}
}
}
impl SnapshotConfig {
pub fn new_load_only() -> Self {
Self {
usage: SnapshotUsage::LoadOnly,
full_snapshot_archive_interval: SnapshotInterval::Disabled,
incremental_snapshot_archive_interval: SnapshotInterval::Disabled,
..Self::default()
}
}
pub fn new_generate_snapshots_externally() -> Self {
Self {
usage: SnapshotUsage::LoadAndGenerate,
full_snapshot_archive_interval: SnapshotInterval::Disabled,
incremental_snapshot_archive_interval: SnapshotInterval::Disabled,
..Self::default()
}
}
pub fn new_disabled() -> Self {
Self {
usage: SnapshotUsage::Disabled,
full_snapshot_archive_interval: SnapshotInterval::Disabled,
incremental_snapshot_archive_interval: SnapshotInterval::Disabled,
..Self::default()
}
}
pub fn should_generate_snapshots(&self) -> bool {
self.usage == SnapshotUsage::LoadAndGenerate
}
pub fn should_load_snapshots(&self) -> bool {
self.usage == SnapshotUsage::LoadAndGenerate || self.usage == SnapshotUsage::LoadOnly
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum SnapshotUsage {
Disabled,
LoadOnly,
LoadAndGenerate,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_load_only() {
let cfg = SnapshotConfig::new_load_only();
assert!(!cfg.should_generate_snapshots());
assert!(cfg.should_load_snapshots());
assert_eq!(
cfg.full_snapshot_archive_interval,
SnapshotInterval::Disabled
);
assert_eq!(
cfg.incremental_snapshot_archive_interval,
SnapshotInterval::Disabled
);
}
#[test]
fn test_new_disabled() {
let cfg = SnapshotConfig::new_disabled();
assert!(!cfg.should_generate_snapshots());
assert!(!cfg.should_load_snapshots());
assert_eq!(
cfg.full_snapshot_archive_interval,
SnapshotInterval::Disabled
);
assert_eq!(
cfg.incremental_snapshot_archive_interval,
SnapshotInterval::Disabled
);
}
#[test]
fn test_new_generate_snapshots_externally() {
let cfg = SnapshotConfig::new_generate_snapshots_externally();
assert_eq!(cfg.usage, SnapshotUsage::LoadAndGenerate);
assert!(cfg.should_generate_snapshots());
assert!(cfg.should_load_snapshots());
assert_eq!(
cfg.full_snapshot_archive_interval,
SnapshotInterval::Disabled
);
assert_eq!(
cfg.incremental_snapshot_archive_interval,
SnapshotInterval::Disabled
);
}
}