ClusterConfig

Struct ClusterConfig 

Source
pub struct ClusterConfig {
    pub voters: BTreeSet<NodeId>,
    pub new_voters: BTreeSet<NodeId>,
    pub non_voters: BTreeSet<NodeId>,
}
Expand description

Cluster configuration (membership).

§Examples

use raftbare::{ClusterConfig, NodeId};

// Makes a new cluster configuration with two voting nodes.
let mut config = ClusterConfig::new();
config.voters.insert(NodeId::new(0));
config.voters.insert(NodeId::new(1));
assert!(!config.is_joint_consensus());

// Adds a new non-voting node.
config.non_voters.insert(NodeId::new(2));
assert!(!config.is_joint_consensus());

// Updates the configuration to add a new voting node.
config.new_voters = config.voters.clone();
config.new_voters.insert(NodeId::new(3));
assert!(config.is_joint_consensus());

The config value in the example above can be applied to a cluster via Node::propose_config().

Fields§

§voters: BTreeSet<NodeId>

Voting nodes.

For a cluster to be available, the majority of the voters must be alive and able to communicate with each other.

§new_voters: BTreeSet<NodeId>

New voting nodes during joint consensus.

When a cluster changes its configuration, it enters a joint consensus state. In that state, voters and new_voters represent the old and new configurations, respectively.

During joint consensus, the cluster requires the majority of both the old and new voters to be available to proceed with leader election and log entry commit.

Once the log entry for this joint configuration is committed, voters takes the value of new_voters, and the cluster exits the joint consensus state.

If new_voters is empty, it means there are no configuration changes in progress.

§non_voters: BTreeSet<NodeId>

Non-voting nodes.

Non-voting nodes do not participate in the leader election and log entry commit quorum, but they do replicate log entries from the leader. Additionally, non-voting nodes never transition to candidate or leader.

When adding more than half of the current number of nodes to a cluster that has a large snapshot or many log entries, it is recommended to first add the new nodes as non-voters to avoid blocking subsequent log commits. Allow these nodes to catch up with the leader before promoting them to voters.

Note that adding or removing non-voters does not require a joint consensus.

Implementations§

Source§

impl ClusterConfig

Source

pub fn new() -> Self

Makes a new empty ClusterConfig instance.

Source

pub fn contains(&self, id: NodeId) -> bool

Returns true if the given node is in this configuration.

Source

pub fn is_joint_consensus(&self) -> bool

Returns true if this configuration represents a joint consensus state.

Source

pub fn unique_nodes(&self) -> impl '_ + Iterator<Item = NodeId>

Gets an iterator over all unique NodeIds in this configuration, in sorted order.

Source

pub fn to_joint_consensus( &self, adding_voters: &[NodeId], removing_voters: &[NodeId], ) -> Self

Converts this configuration to a joint consensus by adding and removing voters.

§Examples
use raftbare::{Node, NodeId};

fn add_node(node: &mut Node, adding_node_id: NodeId) {
    let new_config = node.config().to_joint_consensus(&[adding_node_id], &[]);
    assert_eq!(new_config.voters.len() + 1, new_config.new_voters.len());

    node.propose_config(new_config);
}

fn remove_node(node: &mut Node, removing_id: NodeId) {
    let new_config = node.config().to_joint_consensus(&[], &[removing_id]);
    assert_eq!(new_config.voters.len() - 1, new_config.new_voters.len());

    node.propose_config(new_config);
}

Trait Implementations§

Source§

impl Clone for ClusterConfig

Source§

fn clone(&self) -> ClusterConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ClusterConfig

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for ClusterConfig

Source§

fn default() -> ClusterConfig

Returns the “default value” for a type. Read more
Source§

impl Hash for ClusterConfig

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for ClusterConfig

Source§

fn eq(&self, other: &ClusterConfig) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for ClusterConfig

Source§

impl StructuralPartialEq for ClusterConfig

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.