pub struct RaftConfig { /* private fields */ }Expand description
Configuration for a single RaftNode.
Build one with new (or single
for a one-node cluster) and optionally tune it with the builder methods,
which consume and return self so they chain.
§Examples
use raft_io::RaftConfig;
// Node 1 in a three-node cluster, with tuned timing.
let cfg = RaftConfig::new(1, [2, 3])
.with_election_timeout(15, 30)
.with_heartbeat_interval(5);
assert_eq!(cfg.id(), 1);
assert_eq!(cfg.peers(), &[2, 3]);Implementations§
Source§impl RaftConfig
impl RaftConfig
Sourcepub fn new(id: NodeId, peers: impl IntoIterator<Item = NodeId>) -> Self
pub fn new(id: NodeId, peers: impl IntoIterator<Item = NodeId>) -> Self
Creates a configuration for node id whose peers are peers.
peers is every other node in the cluster; do not include id. The
quorum the node needs to win an election or commit an entry is derived
from the total size (peers.len() + 1). Timing defaults to a 10..=20
tick election timeout and a 3 tick heartbeat, and the RNG seed defaults
to id so distinct nodes jitter differently out of the box.
§Examples
use raft_io::RaftConfig;
let cfg = RaftConfig::new(1, [2, 3, 4, 5]);
assert_eq!(cfg.peers().len(), 4);Sourcepub fn single(id: NodeId) -> Self
pub fn single(id: NodeId) -> Self
Creates a configuration for a single-node cluster.
A single node has no peers and a quorum of one, so it elects itself and commits its own proposals immediately. This is the trivial path for tests and local development.
§Examples
use raft_io::RaftConfig;
let cfg = RaftConfig::single(1);
assert!(cfg.peers().is_empty());Sourcepub fn with_election_timeout(self, min: u32, max: u32) -> Self
pub fn with_election_timeout(self, min: u32, max: u32) -> Self
Sets the randomised election timeout bounds, in ticks.
A follower that hears nothing from a leader for a randomly chosen number
of ticks in [min, max] starts an election. The spread is what breaks
split votes. The bounds are normalised so min >= 1 and max >= min,
so out-of-order or zero arguments cannot wedge the node.
§Examples
use raft_io::RaftConfig;
let cfg = RaftConfig::single(1).with_election_timeout(150, 300);
assert_eq!(cfg.election_timeout(), (150, 300));
// Arguments are normalised rather than rejected.
let fixed = RaftConfig::single(1).with_election_timeout(0, 0);
assert_eq!(fixed.election_timeout(), (1, 1));Sourcepub fn with_heartbeat_interval(self, interval: u32) -> Self
pub fn with_heartbeat_interval(self, interval: u32) -> Self
Sets the heartbeat interval, in ticks.
A leader broadcasts a heartbeat every interval ticks to suppress
elections. Keep it well below the election-timeout lower bound — a few
times smaller is typical — so a single dropped heartbeat does not unseat
a healthy leader. The value is normalised to at least 1.
§Examples
use raft_io::RaftConfig;
let cfg = RaftConfig::single(1).with_heartbeat_interval(5);
assert_eq!(cfg.heartbeat_interval(), 5);Sourcepub fn with_max_batch(self, max_batch: usize) -> Self
pub fn with_max_batch(self, max_batch: usize) -> Self
Sets the maximum number of log entries a single AppendEntries carries.
This bounds message size and the work done per replication RPC: a
follower that has fallen far behind is caught up in batches of at most
this many entries rather than in one unbounded payload. The value is
normalised to at least 1 so replication can always make progress.
§Examples
use raft_io::RaftConfig;
let cfg = RaftConfig::new(1, [2, 3]).with_max_batch(256);
assert_eq!(cfg.max_batch(), 256);Sourcepub fn with_snapshot_threshold(self, threshold: usize) -> Self
pub fn with_snapshot_threshold(self, threshold: usize) -> Self
Sets the snapshot threshold: how many applied entries may accumulate beyond the last snapshot before the node asks the application to take a new one.
When the gap between the last applied index and the snapshot index reaches
threshold, the node emits an Action::Snapshot
hint; the application snapshots its state machine and feeds the bytes back
via Event::Snapshot, and the log is compacted.
0 (the default) disables the hint entirely, so snapshots are opt-in.
§Examples
use raft_io::RaftConfig;
let cfg = RaftConfig::new(1, [2, 3]).with_snapshot_threshold(1024);
assert_eq!(cfg.snapshot_threshold(), 1024);Sourcepub fn with_seed(self, seed: u64) -> Self
pub fn with_seed(self, seed: u64) -> Self
Sets the seed for the node’s election-timeout RNG.
Determinism is the point of the core, so the jitter source is seeded rather than drawn from the OS. Equal seeds reproduce equal timeout sequences; give peers distinct seeds (the default is the node id) so they do not jitter in lockstep.
§Examples
use raft_io::RaftConfig;
let cfg = RaftConfig::single(1).with_seed(0xDEAD_BEEF);
assert_eq!(cfg.seed(), 0xDEAD_BEEF);Sourcepub fn peers(&self) -> &[NodeId] ⓘ
pub fn peers(&self) -> &[NodeId] ⓘ
Returns this node’s peers (every other node in the cluster).
Sourcepub fn election_timeout(&self) -> (u32, u32)
pub fn election_timeout(&self) -> (u32, u32)
Returns the election-timeout bounds as (min, max) ticks.
Sourcepub fn heartbeat_interval(&self) -> u32
pub fn heartbeat_interval(&self) -> u32
Returns the heartbeat interval in ticks.
Sourcepub fn max_batch(&self) -> usize
pub fn max_batch(&self) -> usize
Returns the maximum entries carried by a single AppendEntries.
Sourcepub fn snapshot_threshold(&self) -> usize
pub fn snapshot_threshold(&self) -> usize
Returns the snapshot threshold (0 if snapshots are disabled).
Trait Implementations§
Source§impl Clone for RaftConfig
impl Clone for RaftConfig
Source§fn clone(&self) -> RaftConfig
fn clone(&self) -> RaftConfig
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more