Skip to main content

ConsensusNode

Struct ConsensusNode 

Source
pub struct ConsensusNode<C>
where C: RaftTypeConfig<NodeId = NodeId, Node = BasicNode, Responder = OneshotResponder<C>>,
{ /* private fields */ }
Expand description

A high-level wrapper around openraft::Raft.

Provides ergonomic methods for common Raft operations: proposing writes, linearizable reads, cluster membership changes, and metrics observation.

Construct via ConsensusNodeBuilder.

Note: this requires C::Responder = OneshotResponder<C> which is the default when using declare_raft_types!. This enables the blocking client_write, add_learner, and change_membership APIs.

Implementations§

Source§

impl<C> ConsensusNode<C>
where C: RaftTypeConfig<NodeId = NodeId, Node = BasicNode, Responder = OneshotResponder<C>>,

Source

pub fn from_raft(raft: Raft<C>, node_id: NodeId, address: String) -> Self

Create a ConsensusNode directly from an already-constructed Raft instance.

Source

pub fn node_id(&self) -> NodeId

This node’s ID.

Source

pub fn address(&self) -> &str

This node’s address.

Source

pub fn raft(&self) -> &Raft<C>

Get a reference to the inner Raft instance for advanced usage.

Source

pub fn raft_clone(&self) -> Raft<C>

Get a clone of the inner Raft instance (cheap, Arc-based).

Source

pub async fn propose(&self, request: C::D) -> Result<C::R>

Propose a client write to the Raft cluster.

This node must be the leader. The write is replicated to a quorum before the response is returned.

§Errors

Returns ConsensusError::Write if the write fails (e.g., not the leader).

Source

pub async fn ensure_linearizable(&self) -> Result<()>

Ensure linearizable reads by confirming this node is still the leader.

Call this before reading from the state machine to guarantee that the data is not stale. Implements the “leader lease read” pattern.

§Errors

Returns ConsensusError::Write if the linearizable check fails.

Source

pub fn is_leader(&self) -> bool

Check if this node is the current leader.

Source

pub fn leader_id(&self) -> Option<NodeId>

Get the current leader’s node ID, if known.

Source

pub fn voter_ids(&self) -> BTreeSet<NodeId>

Returns the set of current voter node IDs.

Source

pub fn voter_count(&self) -> usize

Returns the number of current voters.

Source

pub fn learner_ids(&self) -> BTreeSet<NodeId>

Returns the set of current learner node IDs (non-voters).

Source

pub fn all_member_ids(&self) -> BTreeSet<NodeId>

Returns all member node IDs (voters + learners).

Source

pub async fn bootstrap(&self) -> Result<()>

Bootstrap a new single-node cluster.

This must only be called once, on the first node, when creating a new cluster.

§Errors

Returns ConsensusError::Init if bootstrap initialization fails.

Source

pub async fn add_learner( &self, node_id: NodeId, address: String, blocking: bool, ) -> Result<()>

Add a learner to the cluster.

A learner receives log entries but does not vote. Use this to pre-sync a node before promoting it to a voter.

If blocking is true, waits until the learner has caught up with the log.

§Errors

Returns ConsensusError::Membership if the learner cannot be added.

Source

pub async fn change_membership( &self, voter_ids: BTreeSet<NodeId>, retain: bool, ) -> Result<()>

Change the cluster membership (promote learners to voters, or remove members).

Pass the complete set of voter node IDs. Nodes not in the set will be demoted or removed.

If retain is true, nodes not in voter_ids are kept as learners rather than removed entirely.

§Errors

Returns ConsensusError::Membership if the membership change fails.

Source

pub async fn add_voter(&self, node_id: NodeId, address: String) -> Result<()>

Convenience: add a node as learner then promote it to voter.

This performs the full two-step process:

  1. Add as learner (blocking, waits for log sync)
  2. Change membership to include the new voter
§Errors

Returns a ConsensusError if either the learner addition or membership change fails.

Source

pub fn metrics(&self) -> RaftMetrics<NodeId, BasicNode>

Get current Raft metrics (leader, term, log indices, etc.).

Source

pub async fn shutdown(&self) -> Result<()>

Gracefully shut down the Raft node.

§Errors

Returns ConsensusError::Fatal if shutdown fails.

Auto Trait Implementations§

§

impl<C> Freeze for ConsensusNode<C>

§

impl<C> !RefUnwindSafe for ConsensusNode<C>

§

impl<C> Send for ConsensusNode<C>

§

impl<C> Sync for ConsensusNode<C>

§

impl<C> Unpin for ConsensusNode<C>

§

impl<C> UnsafeUnpin for ConsensusNode<C>

§

impl<C> !UnwindSafe for ConsensusNode<C>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> OptionalSend for T
where T: Send + ?Sized,

Source§

impl<T> OptionalSync for T
where T: Sync + ?Sized,