kevy_config/schema.rs
1//! kevy `Config` schema, defaults, and error type. Apply-from-parser and
2//! value-coercion logic lives in `apply.rs` so this file stays focused on
3//! "what the settings ARE".
4
5use std::path::PathBuf;
6
7// ───────────── enums ─────────────
8
9/// AOF fsync policy. Matches Redis `appendfsync`.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum AppendFsync {
12 /// `fsync` after every write command. Zero data-loss but ~50% throughput.
13 Always,
14 /// Background `fsync` every second. Lose at most 1s on crash. Default.
15 EverySec,
16 /// No explicit `fsync`; let OS pagecache flush. Lose ~30s on crash.
17 No,
18}
19
20impl AppendFsync {
21 /// Canonical Redis-compatible name (`always` / `everysec` / `no`).
22 /// Used by `CONFIG GET appendfsync` and `CONFIG REWRITE`.
23 pub fn as_str(&self) -> &'static str {
24 match self {
25 Self::Always => "always",
26 Self::EverySec => "everysec",
27 Self::No => "no",
28 }
29 }
30 /// Inverse of [`Self::as_str`] — case-insensitive. `None` for any
31 /// other input; used by both the TOML parser and `CONFIG SET`.
32 pub fn parse(s: &str) -> Option<Self> {
33 match s.to_ascii_lowercase().as_str() {
34 "always" => Some(Self::Always),
35 "everysec" => Some(Self::EverySec),
36 "no" => Some(Self::No),
37 _ => None,
38 }
39 }
40}
41
42/// Maxmemory eviction policy. 8 variants matching Redis. `NoEviction`
43/// (default) returns an error on writes once `maxmemory` is hit.
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
45pub enum EvictionPolicy {
46 /// Refuse writes once `maxmemory` is hit. Default.
47 NoEviction,
48 /// Approximated LRU across all keys.
49 AllKeysLru,
50 /// Approximated LFU across all keys.
51 AllKeysLfu,
52 /// Random key across all keys.
53 AllKeysRandom,
54 /// Approximated LRU across keys with a TTL.
55 VolatileLru,
56 /// Approximated LFU across keys with a TTL.
57 VolatileLfu,
58 /// Random key from those with a TTL.
59 VolatileRandom,
60 /// Key with the shortest remaining TTL.
61 VolatileTtl,
62}
63
64impl EvictionPolicy {
65 /// Canonical Redis-compatible name.
66 pub fn as_str(&self) -> &'static str {
67 match self {
68 Self::NoEviction => "noeviction",
69 Self::AllKeysLru => "allkeys-lru",
70 Self::AllKeysLfu => "allkeys-lfu",
71 Self::AllKeysRandom => "allkeys-random",
72 Self::VolatileLru => "volatile-lru",
73 Self::VolatileLfu => "volatile-lfu",
74 Self::VolatileRandom => "volatile-random",
75 Self::VolatileTtl => "volatile-ttl",
76 }
77 }
78 /// Inverse of [`Self::as_str`] — case-insensitive.
79 pub fn parse(s: &str) -> Option<Self> {
80 match s.to_ascii_lowercase().as_str() {
81 "noeviction" => Some(Self::NoEviction),
82 "allkeys-lru" => Some(Self::AllKeysLru),
83 "allkeys-lfu" => Some(Self::AllKeysLfu),
84 "allkeys-random" => Some(Self::AllKeysRandom),
85 "volatile-lru" => Some(Self::VolatileLru),
86 "volatile-lfu" => Some(Self::VolatileLfu),
87 "volatile-random" => Some(Self::VolatileRandom),
88 "volatile-ttl" => Some(Self::VolatileTtl),
89 _ => None,
90 }
91 }
92}
93
94/// Log verbosity.
95#[derive(Debug, Clone, Copy, PartialEq, Eq)]
96pub enum LogLevel {
97 /// Very chatty, useful when debugging a kevy internal bug.
98 Trace,
99 /// Per-command / per-event detail; turn on locally to chase issues.
100 Debug,
101 /// Default; startup banner, WARNs, errors, key lifecycle events.
102 Info,
103 /// Only non-fatal warnings (e.g. unprotected bind) and errors.
104 Warn,
105 /// Only fatal errors.
106 Error,
107}
108
109impl LogLevel {
110 /// Canonical name. `Warn` renders as `warning` (Redis convention).
111 pub fn as_str(&self) -> &'static str {
112 match self {
113 Self::Trace => "trace",
114 Self::Debug => "debug",
115 Self::Info => "info",
116 Self::Warn => "warning",
117 Self::Error => "error",
118 }
119 }
120 /// Inverse of [`Self::as_str`] — case-insensitive; accepts both
121 /// `warn` and `warning` for the Warn level.
122 pub fn parse(s: &str) -> Option<Self> {
123 match s.to_ascii_lowercase().as_str() {
124 "trace" => Some(Self::Trace),
125 "debug" => Some(Self::Debug),
126 "info" => Some(Self::Info),
127 "warn" | "warning" => Some(Self::Warn),
128 "error" => Some(Self::Error),
129 _ => None,
130 }
131 }
132}
133
134/// Where to write log output.
135#[derive(Debug, Clone, PartialEq, Eq)]
136pub enum LogOutput {
137 /// Write to standard error (default).
138 Stderr,
139 /// Write to standard output.
140 Stdout,
141 /// Append to the named file (path resolved relative to cwd at startup).
142 File(PathBuf),
143}
144
145impl LogOutput {
146 /// Canonical name. `File(p)` renders as the path string.
147 pub fn as_str(&self) -> std::borrow::Cow<'_, str> {
148 match self {
149 Self::Stderr => "stderr".into(),
150 Self::Stdout => "stdout".into(),
151 Self::File(p) => p.display().to_string().into(),
152 }
153 }
154 /// Inverse of [`Self::as_str`]: `stderr` / `stdout` reserved; any
155 /// other string is treated as a file path.
156 pub fn parse(s: &str) -> Self {
157 match s {
158 "stderr" => Self::Stderr,
159 "stdout" => Self::Stdout,
160 path => Self::File(PathBuf::from(path)),
161 }
162 }
163}
164
165// ───────────── sections ─────────────
166
167/// `[server]` section.
168#[derive(Debug, Clone, PartialEq, Eq)]
169pub struct ServerSection {
170 /// IPv4 bind address. Default `127.0.0.1`.
171 pub bind: [u8; 4],
172 /// TCP port. Default `6004`.
173 pub port: u16,
174 /// Shard / reactor thread count. `0` = auto (CPU count). Default `0`.
175 pub threads: usize,
176 /// Snapshot + AOF location. Default `.`.
177 pub data_dir: PathBuf,
178}
179
180impl Default for ServerSection {
181 fn default() -> Self {
182 Self {
183 bind: [127, 0, 0, 1],
184 port: 6004,
185 threads: 0,
186 data_dir: PathBuf::from("."),
187 }
188 }
189}
190
191/// `[persistence]` section.
192#[derive(Debug, Clone, PartialEq, Eq)]
193pub struct PersistenceSection {
194 /// Append-only file enabled. Default `true`.
195 pub aof: bool,
196 /// AOF fsync policy. Default `EverySec`.
197 pub appendfsync: AppendFsync,
198 /// Trigger BGREWRITEAOF when current AOF is at least this fraction
199 /// (as a percent — 100 = 2× the last-rewrite size) larger than the
200 /// last rewrite. Default `100`.
201 pub auto_aof_rewrite_percentage: u32,
202 /// Never auto-rewrite an AOF smaller than this. Default `64mb` =
203 /// `64 * 1024 * 1024`.
204 pub auto_aof_rewrite_min_size: u64,
205}
206
207impl Default for PersistenceSection {
208 fn default() -> Self {
209 Self {
210 aof: true,
211 appendfsync: AppendFsync::EverySec,
212 auto_aof_rewrite_percentage: 100,
213 auto_aof_rewrite_min_size: 64 * 1024 * 1024,
214 }
215 }
216}
217
218/// `[memory]` section.
219#[derive(Debug, Clone, Copy, PartialEq, Eq)]
220pub struct MemorySection {
221 /// Soft memory ceiling in bytes. `0` = unlimited. Default `0`.
222 pub maxmemory: u64,
223 /// Action when `maxmemory` is hit. Default `NoEviction`.
224 pub maxmemory_policy: EvictionPolicy,
225}
226
227impl Default for MemorySection {
228 fn default() -> Self {
229 Self {
230 maxmemory: 0,
231 maxmemory_policy: EvictionPolicy::NoEviction,
232 }
233 }
234}
235
236/// `[expiry]` section. Controls the TTL background reaper.
237#[derive(Debug, Clone, Copy, PartialEq, Eq)]
238pub struct ExpirySection {
239 /// Reaper frequency in Hz. Default `10` (every 100 ms).
240 pub hz: u32,
241 /// Keys sampled per reaper cycle. Default `20`.
242 pub sample: u32,
243}
244
245impl Default for ExpirySection {
246 fn default() -> Self {
247 Self { hz: 10, sample: 20 }
248 }
249}
250
251/// `[log]` section.
252#[derive(Debug, Clone, PartialEq, Eq)]
253pub struct LogSection {
254 /// Log verbosity. Default `Info`.
255 pub level: LogLevel,
256 /// Log sink. Default `Stderr`.
257 pub output: LogOutput,
258}
259
260impl Default for LogSection {
261 fn default() -> Self {
262 Self {
263 level: LogLevel::Info,
264 output: LogOutput::Stderr,
265 }
266 }
267}
268
269/// `[advanced]` section — reactor-loop tuning knobs that used to be
270/// hardcoded `const`s in `kevy-rt`. Defaults match the values shipped
271/// in workspace v1.3 / earlier so the existing benchmark numbers
272/// translate one-to-one. Tune only if you know what you're doing
273/// (`bench/REPORT.md` documents the trade-offs).
274#[derive(Debug, Clone, Copy, PartialEq, Eq)]
275pub struct AdvancedSection {
276 /// Iterations the per-core reactor spins on `poll(timeout=0)`
277 /// before parking on a blocking wait. Higher = lower wake-up
278 /// latency under contention, higher idle CPU; lower = the inverse.
279 /// Default `256` (matches v1.0 const).
280 pub spin_limit: u32,
281 /// Bounded blocking wait in ms once the reactor parks. Acts as a
282 /// safety backstop for any missed cross-core wake (the per-pair
283 /// SeqCst fence is the primary mechanism since workspace v1.3.0).
284 /// Default `50` ms.
285 pub park_timeout_ms: u32,
286 /// How many reactor loop iterations between wall-clock reads for
287 /// the tick (TTL reaper / auto-AOF-rewrite / live-config refresh).
288 /// In busy-poll mode (~1M iter/s) the default `256` is one check
289 /// per ~256 µs — plenty for a 10 Hz tick. In park mode the
290 /// reactor bypasses this throttle (each iter is already ≥ 1 ms),
291 /// so the value only matters under sustained load. Default `256`.
292 pub tick_check_every: u32,
293 /// Per-direction SPSC ring slot count (one ring per ordered
294 /// core-pair). Must be a power of two; the ring code rounds up.
295 /// Overflow spills to a local backlog Vec rather than blocking,
296 /// so a small ring just shifts work to the slower path. Default
297 /// `1024`.
298 pub ring_capacity: usize,
299}
300
301impl Default for AdvancedSection {
302 fn default() -> Self {
303 Self {
304 spin_limit: 256,
305 park_timeout_ms: 50,
306 tick_check_every: 256,
307 ring_capacity: 1024,
308 }
309 }
310}
311
312/// `[notification]` section. `notify_keyspace_events` is a string of
313/// flag chars (Redis convention): `K` keyspace channel, `E` keyevent
314/// channel, `g` generic cmds, `$` string cmds, `l` list, `s` set, `h`
315/// hash, `z` zset, `A` alias for `g$lshz` (every event class except
316/// the not-yet-implemented `x`/`e`/`t`/`n`). Default empty = OFF
317/// (Redis default — zero hot-path cost).
318///
319/// Example: `notify_keyspace_events = "KEA"` enables every event
320/// class on BOTH channels. `"K$"` enables only string events on the
321/// keyspace channel.
322#[derive(Debug, Clone, Default, PartialEq, Eq)]
323pub struct NotificationSection {
324 /// Flag string controlling which keyspace notifications fire. Empty
325 /// (default) = OFF: writes pay one atomic load + skip, no publish.
326 pub notify_keyspace_events: String,
327}
328
329/// Parsed view of [`NotificationSection::notify_keyspace_events`]. The
330/// runtime caches this struct per-shard (hot-reload via the existing
331/// `LiveRuntimeConfig` tick path) so the per-write-command check
332/// reduces to four bool reads on the hot path.
333#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
334pub struct NotificationFlags {
335 /// `K` — publish on `__keyspace@<db>__:<key>` channel.
336 pub keyspace: bool,
337 /// `E` — publish on `__keyevent@<db>__:<event>` channel.
338 pub keyevent: bool,
339 /// `g` — DEL / EXPIRE / PERSIST / RENAME / TYPE / FLUSH etc.
340 pub generic: bool,
341 /// `$` — SET / GETSET / INCR* / APPEND / MSET / etc.
342 pub string: bool,
343 /// `l` — LPUSH / RPUSH / LPOP / RPOP / LREM / LSET / LTRIM / …
344 pub list: bool,
345 /// `s` — SADD / SREM / SPOP / SMOVE / …
346 pub set: bool,
347 /// `h` — HSET / HDEL / HINCRBY / HSETNX / …
348 pub hash: bool,
349 /// `z` — ZADD / ZINCRBY / ZREM / ZREMRANGEBY* / …
350 pub zset: bool,
351 /// `t` — XADD / XDEL / XTRIM / XGROUP / XACK / XCLAIM / XREADGROUP …
352 pub stream: bool,
353}
354
355impl NotificationFlags {
356 /// Notifications are entirely off (no channel enabled OR no class
357 /// enabled). The hot-path emits skip via this check before any
358 /// further classification or string formatting.
359 pub fn is_empty(&self) -> bool {
360 !(self.keyspace || self.keyevent)
361 || !(self.generic
362 || self.string
363 || self.list
364 || self.set
365 || self.hash
366 || self.zset
367 || self.stream)
368 }
369}
370
371/// `[slowlog]` section — controls the per-shard slow-command ring
372/// buffer surfaced by `SLOWLOG GET/LEN/RESET`. Default is OFF
373/// (`slower_than_micros = -1`) so the hot path never pays the
374/// `Instant::now()` pair around dispatch (~30 ns/op, ≈9 % at 3 M
375/// ops/s). To enable Redis-style 10 ms tracking, set
376/// `slower_than_micros = 10000` in `[slowlog]` or run
377/// `CONFIG SET slowlog-log-slower-than 10000`.
378#[derive(Debug, Clone, Copy, PartialEq, Eq)]
379pub struct SlowlogSection {
380 /// Record any command whose execution took at least this many
381 /// microseconds (Redis: `< slower_than_micros` is skipped). `-1`
382 /// disables the log (zero hot-path cost — no `Instant::now()`
383 /// taken); `0` records every command. Default `-1` (OFF).
384 pub slower_than_micros: i64,
385 /// Cap on the per-shard ring buffer. Once exceeded, the oldest
386 /// entry is dropped to make room. Across `nshards` shards the
387 /// effective server-wide cap is `max_len * nshards`. Default `128`.
388 pub max_len: u32,
389}
390
391impl Default for SlowlogSection {
392 fn default() -> Self {
393 Self {
394 slower_than_micros: -1,
395 max_len: 128,
396 }
397 }
398}
399
400/// Parse a Redis-style `notify_keyspace_events` flag string into
401/// [`NotificationFlags`]. Unknown chars are ignored (forward-compat
402/// for `x`/`e`/`t`/`n` not yet implemented — see the section docs).
403/// The `A` alias enables every event-class flag except channels.
404pub fn parse_notification_flags(s: &str) -> NotificationFlags {
405 let mut f = NotificationFlags::default();
406 for c in s.chars() {
407 match c {
408 'K' => f.keyspace = true,
409 'E' => f.keyevent = true,
410 'g' => f.generic = true,
411 '$' => f.string = true,
412 'l' => f.list = true,
413 's' => f.set = true,
414 'h' => f.hash = true,
415 'z' => f.zset = true,
416 't' => f.stream = true,
417 'A' => {
418 // Alias for "g$lshzxetd" — every implemented event class.
419 // Per Redis spec `A` includes the stream `t` class.
420 f.generic = true;
421 f.string = true;
422 f.list = true;
423 f.set = true;
424 f.hash = true;
425 f.zset = true;
426 f.stream = true;
427 }
428 _ => {} // forward-compat: silently ignore unknown chars
429 }
430 }
431 f
432}
433
434/// `[cluster]` section — single-node cluster mode: keys route by
435/// Redis-cluster slot (CRC16 `{hashtag}` & 16383) and every shard `i`
436/// gets a second, deterministic listener at `port_base + i` that answers
437/// wrong-shard keys with `-MOVED`, so stock cluster-aware clients
438/// (`redis-benchmark --cluster`, `redis-cli -c`) can address shards
439/// directly. The main SO_REUSEPORT port keeps full forward-anywhere
440/// behaviour for non-cluster clients. Not hot-settable: the routing
441/// scheme is a startup property of the data dir (`shards.meta`).
442#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
443pub struct ClusterSection {
444 /// Enable cluster mode. Default `false` (zero change).
445 pub enabled: bool,
446 /// First cluster port (shard `i` listens at `port_base + i`).
447 /// `0` (default) = `server.port + 1`.
448 pub port_base: u16,
449}
450
451// ───────────── top-level Config ─────────────
452
453/// Complete kevy config: defaults + per-section overrides loaded from
454/// the TOML file + env + CLI.
455#[derive(Debug, Clone, PartialEq, Eq, Default)]
456pub struct Config {
457 /// `[server]` settings.
458 pub server: ServerSection,
459 /// `[persistence]` settings.
460 pub persistence: PersistenceSection,
461 /// `[memory]` settings.
462 pub memory: MemorySection,
463 /// `[expiry]` settings.
464 pub expiry: ExpirySection,
465 /// `[log]` settings.
466 pub log: LogSection,
467 /// `[notification]` settings (keyspace events).
468 pub notification: NotificationSection,
469 /// `[advanced]` settings (reactor tuning knobs).
470 pub advanced: AdvancedSection,
471 /// `[slowlog]` settings (slow-command ring buffer).
472 pub slowlog: SlowlogSection,
473 /// `[cluster]` settings (single-node cluster mode).
474 pub cluster: ClusterSection,
475 /// Path the config was loaded from (for `CONFIG REWRITE`). `None` =
476 /// loaded from defaults only / from in-memory string.
477 pub source_path: Option<PathBuf>,
478}
479
480// `ConfigError` lives in [`crate::error`] — split out so this file
481// stays under the 500-LOC house rule. Re-exported below for any caller
482// that still does `kevy_config::schema::ConfigError`.
483pub use crate::error::ConfigError;