Skip to main content

RaftConfig

Struct RaftConfig 

Source
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

Source

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);
Source

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());
Source

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));
Source

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);
Source

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);
Source

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);
Source

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);
Source

pub fn id(&self) -> NodeId

Returns this node’s id.

Source

pub fn peers(&self) -> &[NodeId]

Returns this node’s peers (every other node in the cluster).

Source

pub fn election_timeout(&self) -> (u32, u32)

Returns the election-timeout bounds as (min, max) ticks.

Source

pub fn heartbeat_interval(&self) -> u32

Returns the heartbeat interval in ticks.

Source

pub fn max_batch(&self) -> usize

Returns the maximum entries carried by a single AppendEntries.

Source

pub fn snapshot_threshold(&self) -> usize

Returns the snapshot threshold (0 if snapshots are disabled).

Source

pub fn seed(&self) -> u64

Returns the election-timeout RNG seed.

Trait Implementations§

Source§

impl Clone for RaftConfig

Source§

fn clone(&self) -> RaftConfig

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RaftConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error