Skip to main content

simraft_core/
config.rs

1//! Static configuration for a Raft node and its validation.
2//!
3//! Timing validation (invariant T-01) enforces the ratios the liveness
4//! argument depends on: `min_election_timeout >= 10 * heartbeat_interval`
5//! and `max_election_timeout >= 2 * min_election_timeout`, with
6//! `max_election_timeout > min_election_timeout` so the randomized
7//! election window is never empty.
8
9use crate::error::ConfigError;
10
11/// Static per-node configuration. All durations are in ticks (virtual
12/// milliseconds, supplied by the driver).
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct Config {
15    /// How often a leader sends heartbeats, in ticks.
16    pub heartbeat_interval: u64,
17    /// The shortest election timeout a follower or candidate will use.
18    pub min_election_timeout: u64,
19    /// The longest election timeout a follower or candidate will use.
20    pub max_election_timeout: u64,
21    /// The most entries one `AppendEntries` message may carry.
22    pub max_entries_per_append: usize,
23    /// The approximate byte budget for one `AppendEntries` message.
24    pub max_append_bytes: usize,
25    /// The chunk size for `InstallSnapshot` transfers.
26    pub snapshot_chunk_bytes: usize,
27    /// The most client sessions the KV state machine retains.
28    pub max_sessions: usize,
29    /// The maximum number of uncommitted entries a leader accepts
30    /// before rejecting new client proposals.
31    pub max_uncommitted_entries: usize,
32}
33
34impl Default for Config {
35    fn default() -> Self {
36        Self {
37            heartbeat_interval: 50,
38            min_election_timeout: 500,
39            max_election_timeout: 1_000,
40            max_entries_per_append: 64,
41            max_append_bytes: 1_048_576,
42            snapshot_chunk_bytes: 65_536,
43            max_sessions: 10_000,
44            max_uncommitted_entries: 10_000,
45        }
46    }
47}
48
49impl Config {
50    /// Validate this configuration.
51    ///
52    /// # Errors
53    ///
54    /// Returns a `ConfigError` naming both offending values when a timing
55    /// ratio is violated, a limit is zero, or a duration is zero.
56    pub fn validate(&self) -> Result<(), ConfigError> {
57        if self.heartbeat_interval == 0 {
58            return Err(ConfigError::ZeroValue {
59                field: "heartbeat_interval",
60            });
61        }
62        if self.min_election_timeout == 0 {
63            return Err(ConfigError::ZeroValue {
64                field: "min_election_timeout",
65            });
66        }
67        let required_min = self.heartbeat_interval.saturating_mul(10);
68        if self.min_election_timeout < required_min {
69            return Err(ConfigError::ElectionTimeoutRatio {
70                min_election_timeout: self.min_election_timeout,
71                heartbeat_interval: self.heartbeat_interval,
72                required_min,
73            });
74        }
75        if self.max_election_timeout <= self.min_election_timeout {
76            return Err(ConfigError::ElectionRangeOrder {
77                min_election_timeout: self.min_election_timeout,
78                max_election_timeout: self.max_election_timeout,
79            });
80        }
81        let required_min_max = self.min_election_timeout.saturating_mul(2);
82        if self.max_election_timeout < required_min_max {
83            return Err(ConfigError::ElectionRangeRatio {
84                min_election_timeout: self.min_election_timeout,
85                max_election_timeout: self.max_election_timeout,
86                required_min_max,
87            });
88        }
89        for (field, value) in [
90            ("max_entries_per_append", self.max_entries_per_append),
91            ("max_append_bytes", self.max_append_bytes),
92            ("snapshot_chunk_bytes", self.snapshot_chunk_bytes),
93            ("max_sessions", self.max_sessions),
94            ("max_uncommitted_entries", self.max_uncommitted_entries),
95        ] {
96            if value == 0 {
97                return Err(ConfigError::ZeroValue { field });
98            }
99        }
100        Ok(())
101    }
102}