Skip to main content

RaftNode

Struct RaftNode 

Source
pub struct RaftNode<S: LogStorage> { /* private fields */ }
Expand description

A single Raft group’s state machine.

This is a deterministic, event-driven core. It does NOT own any threads or timers — the caller drives it via tick() and RPC handler methods, and reads output via take_ready().

Implementations§

Source§

impl<S: LogStorage> RaftNode<S>

Source

pub fn new(config: RaftConfig, storage: S) -> Self

Create a new Raft node. Call restore() before ticking.

If config.starts_as_learner is true, the node boots in the Learner role and will never run an election timeout or become a leader until it is promoted via promote_self_to_voter.

Source

pub fn restore(&mut self) -> Result<()>

Restore state from persistent storage. Must be called before ticking.

Source

pub fn node_id(&self) -> u64

Source

pub fn group_id(&self) -> u64

Source

pub fn role(&self) -> NodeRole

Source

pub fn leader_id(&self) -> u64

Source

pub fn current_term(&self) -> u64

Source

pub fn commit_index(&self) -> u64

Source

pub fn last_applied(&self) -> u64

Source

pub fn election_deadline_override(&mut self, deadline: Instant)

Override election deadline (for testing).

Source

pub fn take_ready(&mut self) -> Ready

Take the pending Ready output. Caller must execute messages, persist hard state, and apply committed entries.

Source

pub fn advance_applied(&mut self, applied_to: u64)

Advance last_applied after the caller has applied entries.

Source

pub fn match_index_for(&self, peer: u64) -> Option<u64>

Query a peer’s match_index from the leader’s replication state. Returns None if this node is not the leader or the peer is unknown.

Source

pub fn log_snapshot_index(&self) -> u64

Source

pub fn log_snapshot_term(&self) -> u64

Source

pub fn log_entries_range(&self, lo: u64, hi: u64) -> Result<&[LogEntry]>

Return committed log entries in the inclusive range [lo, hi].

Clamps hi to commit_index so callers that pass u64::MAX never read uncommitted entries. Returns Err(RaftError::LogCompacted) if lo has already been compacted into a snapshot.

Source

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

Current voter peer list (excluding self).

Source

pub fn voters(&self) -> &[u64]

Current voter peer list — alias for peers(), clearer at call sites that need to distinguish voters from learners.

Source

pub fn learners(&self) -> &[u64]

Current learner peer list (excluding self).

Source

pub fn observers(&self) -> &[u64]

Current observer peer list tracked by this leader (excluding self).

Source

pub fn is_learner_peer(&self, peer: u64) -> bool

Whether peer is currently tracked as a learner in this group.

Source

pub fn tick(&mut self)

Drive time-based events: election timeout, heartbeat.

Source

pub fn propose(&mut self, data: Vec<u8>) -> Result<u64>

Propose a new entry (leader only). Returns the log index.

Source§

impl<S: LogStorage> RaftNode<S>

Source

pub fn add_peer(&mut self, peer: u64)

Add a single voter peer to this group.

No-op if peer is self, already a voter, or currently a learner (use promote_learner to convert a learner into a voter).

Source

pub fn remove_peer(&mut self, peer: u64)

Remove a voter peer from this group.

Source

pub fn add_learner(&mut self, peer: u64)

Add a non-voting learner peer.

Learners receive replicated log entries but do not vote and do not count toward the commit quorum. If this node is currently the leader, the learner is immediately added to LeaderState replication tracking so the next heartbeat ships entries to it.

No-op if peer is self, already a voter, or already a learner.

Source

pub fn remove_learner(&mut self, peer: u64)

Remove a learner peer (e.g., join was rolled back before promotion).

Source

pub fn promote_learner(&mut self, peer: u64) -> bool

Promote an existing learner to a full voter.

Called on the leader after observing the learner has caught up (its match_index >= the group’s commit_index). The LeaderState entry is left in place — it already tracks the peer’s next/match index — but the peer now counts toward the commit quorum.

Returns true if the promotion happened, false if peer was not a learner.

Source

pub fn promote_self_to_voter(&mut self)

Promote this node from learner to voter role.

Used when a follow-up conf change committed the local node’s promotion — the node transitions out of Learner role so its subsequent ticks will run election timeouts like a normal follower.

Source§

impl<S: LogStorage> RaftNode<S>

Source

pub fn handle_append_entries( &mut self, req: &AppendEntriesRequest, ) -> AppendEntriesResponse

Handle incoming AppendEntries RPC.

Source

pub fn handle_append_entries_response( &mut self, peer: u64, resp: &AppendEntriesResponse, )

Handle AppendEntries response from a peer (leader only).

For voter peers: update match/next index and attempt commit advancement. For learner peers: update match/next index only (no quorum contribution). For observer peers: update observer state advisorily — no quorum contribution, no commit advancement. Observer acks release backpressure so the leader resumes sending to that observer.

Source§

impl<S: LogStorage> RaftNode<S>

Source

pub fn handle_install_snapshot( &mut self, req: &InstallSnapshotRequest, ) -> InstallSnapshotResponse

Handle incoming InstallSnapshot RPC (Raft paper Figure 13).

Called on followers (and learners) that are too far behind for log-based catch-up. The leader sends its snapshot; the receiver replaces its log and state.

Source§

impl<S: LogStorage> RaftNode<S>

Source

pub fn handle_request_vote( &mut self, req: &RequestVoteRequest, ) -> RequestVoteResponse

Handle incoming RequestVote RPC.

Learners and observers never grant votes: by definition they are not members of the voting set for this term, and granting a vote could let an incorrect quorum form.

Source

pub fn handle_request_vote_response( &mut self, peer: u64, resp: &RequestVoteResponse, )

Handle RequestVote response (candidate only).

Auto Trait Implementations§

§

impl<S> Freeze for RaftNode<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for RaftNode<S>
where S: RefUnwindSafe,

§

impl<S> Send for RaftNode<S>

§

impl<S> Sync for RaftNode<S>
where S: Sync,

§

impl<S> Unpin for RaftNode<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for RaftNode<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for RaftNode<S>
where S: UnwindSafe,

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

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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, 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> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
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