use std::time::Duration;
pub(crate) const DEFAULT_L1_SHARDS: usize = 32;
pub(crate) const MAX_APPEND_SHARDS: u32 = 256;
pub(crate) const MAX_WRITE_FLUSH_THRESHOLD_BYTES: usize = 4 * 1024 * 1024;
pub(crate) const MAX_READ_IO_WAIT_TIMEOUT: Duration = Duration::from_secs(5);
const DEFAULT_L1_CAPACITY_BYTES: usize = 256 * 1024 * 1024;
const DEFAULT_APPEND_SHARDS: u32 = 4;
const IO_URING_DEPTH_PER_WORKER: usize = 64;
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum IoEngine {
#[default]
Posix,
IoUring,
}
impl IoEngine {
pub(crate) const fn is_available(self) -> bool {
match self {
Self::Posix => true,
Self::IoUring => cfg!(all(
feature = "io-uring",
target_os = "linux",
any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "loongarch64",
target_arch = "powerpc64"
)
)),
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum IoMode {
#[default]
Buffered,
Direct,
}
impl IoMode {
pub(crate) const fn is_available(self) -> bool {
match self {
Self::Buffered => true,
Self::Direct => cfg!(target_os = "linux"),
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum L1EvictionPolicy {
#[default]
Clock,
S3Fifo,
}
#[derive(Clone, Debug)]
pub struct RuntimeConfig {
pub(crate) io_engine: IoEngine,
pub(crate) io_mode: IoMode,
pub(crate) read_io_workers: usize,
pub(crate) read_io_wait_timeout: Duration,
pub(crate) write_io_workers: usize,
pub(crate) reclaim_workers: usize,
pub(crate) append_shards: u32,
pub(crate) l1_capacity_bytes: usize,
pub(crate) l1_eviction_policy: L1EvictionPolicy,
pub(crate) managed_memory_limit_bytes: usize,
pub(crate) l1_shards: usize,
pub(crate) write_flush_threshold_bytes: usize,
pub(crate) statistics: bool,
}
impl Default for RuntimeConfig {
fn default() -> Self {
Self {
io_engine: IoEngine::Posix,
io_mode: IoMode::Buffered,
read_io_workers: 4,
read_io_wait_timeout: Duration::ZERO,
write_io_workers: 4,
reclaim_workers: 1,
append_shards: DEFAULT_APPEND_SHARDS,
l1_capacity_bytes: DEFAULT_L1_CAPACITY_BYTES,
l1_eviction_policy: L1EvictionPolicy::Clock,
managed_memory_limit_bytes: 1024 * 1024 * 1024,
l1_shards: DEFAULT_L1_SHARDS,
write_flush_threshold_bytes: MAX_WRITE_FLUSH_THRESHOLD_BYTES,
statistics: false,
}
}
}
impl RuntimeConfig {
pub fn with_io_engine(mut self, engine: IoEngine) -> Self {
self.io_engine = engine;
self
}
pub fn with_io_mode(mut self, mode: IoMode) -> Self {
self.io_mode = mode;
self
}
pub fn with_read_io_workers(mut self, workers: usize) -> Self {
self.read_io_workers = workers;
self
}
pub fn with_read_io_wait_timeout(mut self, timeout: Duration) -> Self {
self.read_io_wait_timeout = timeout;
self
}
pub fn with_write_io_workers(mut self, workers: usize) -> Self {
self.write_io_workers = workers;
self
}
pub fn with_reclaim_workers(mut self, workers: usize) -> Self {
self.reclaim_workers = workers;
self
}
pub fn with_append_shards(mut self, shards: u32) -> Self {
self.append_shards = shards;
self
}
pub fn with_l1_capacity_bytes(mut self, bytes: usize) -> Self {
self.l1_capacity_bytes = bytes;
self
}
pub fn with_l1_eviction_policy(mut self, policy: L1EvictionPolicy) -> Self {
self.l1_eviction_policy = policy;
self
}
pub fn with_managed_memory_limit_bytes(mut self, bytes: usize) -> Self {
self.managed_memory_limit_bytes = bytes;
self
}
pub fn with_l1_shards(mut self, shards: usize) -> Self {
self.l1_shards = shards;
self
}
pub fn with_write_flush_threshold_bytes(mut self, bytes: usize) -> Self {
self.write_flush_threshold_bytes = bytes;
self
}
pub fn with_statistics(mut self, enabled: bool) -> Self {
self.statistics = enabled;
self
}
pub const fn io_engine(&self) -> IoEngine {
self.io_engine
}
pub const fn io_mode(&self) -> IoMode {
self.io_mode
}
pub const fn read_io_workers(&self) -> usize {
self.read_io_workers
}
pub const fn read_io_wait_timeout(&self) -> Duration {
self.read_io_wait_timeout
}
pub const fn write_io_workers(&self) -> usize {
self.write_io_workers
}
pub const fn reclaim_workers(&self) -> usize {
self.reclaim_workers
}
pub const fn append_shards(&self) -> u32 {
self.append_shards
}
pub const fn l1_capacity_bytes(&self) -> usize {
self.l1_capacity_bytes
}
pub const fn l1_eviction_policy(&self) -> L1EvictionPolicy {
self.l1_eviction_policy
}
pub const fn managed_memory_limit_bytes(&self) -> usize {
self.managed_memory_limit_bytes
}
pub const fn l1_shards(&self) -> usize {
self.l1_shards
}
pub const fn write_flush_threshold_bytes(&self) -> usize {
self.write_flush_threshold_bytes
}
pub const fn statistics_enabled(&self) -> bool {
self.statistics
}
pub(crate) const fn io_engine_count(&self, workers: usize) -> usize {
match self.io_engine {
IoEngine::Posix => 1,
IoEngine::IoUring => workers,
}
}
pub(crate) const fn io_depth_per_engine(&self, workers: usize) -> usize {
match self.io_engine {
IoEngine::Posix => workers,
IoEngine::IoUring => IO_URING_DEPTH_PER_WORKER,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn runtime_defaults_are_stable() {
let config = RuntimeConfig::default();
assert_eq!(config.io_engine(), IoEngine::Posix);
assert_eq!(config.io_mode(), IoMode::Buffered);
assert_eq!(config.read_io_workers(), 4);
assert_eq!(config.read_io_wait_timeout(), Duration::ZERO);
assert_eq!(config.write_io_workers(), 4);
assert_eq!(config.reclaim_workers(), 1);
assert_eq!(config.append_shards(), DEFAULT_APPEND_SHARDS);
assert_eq!(config.l1_capacity_bytes(), DEFAULT_L1_CAPACITY_BYTES);
assert_eq!(config.l1_eviction_policy(), L1EvictionPolicy::Clock);
assert_eq!(config.managed_memory_limit_bytes(), 1024 * 1024 * 1024);
assert_eq!(config.l1_shards(), DEFAULT_L1_SHARDS);
assert_eq!(
config.write_flush_threshold_bytes(),
MAX_WRITE_FLUSH_THRESHOLD_BYTES
);
assert!(!config.statistics_enabled());
}
#[test]
fn io_engine_capacity_matches_backend_shape() {
let posix = RuntimeConfig::default();
assert_eq!(posix.io_engine_count(7), 1);
assert_eq!(posix.io_depth_per_engine(7), 7);
let io_uring = posix.with_io_engine(IoEngine::IoUring);
assert_eq!(io_uring.io_engine_count(7), 7);
assert_eq!(io_uring.io_depth_per_engine(7), IO_URING_DEPTH_PER_WORKER);
}
}