Skip to main content

RaftNode

Struct RaftNode 

Source
pub struct RaftNode { /* private fields */ }
Expand description

A node in the Raft cluster. Supports pluggable state machine backends for flexible storage integration.

Implementations§

Source§

impl RaftNode

Source

pub fn new(id: impl Into<NodeId>, config: RaftConfig) -> Self

Create a new Raft node with the default in-memory state machine.

Source

pub fn with_state_machine( id: impl Into<NodeId>, config: RaftConfig, state_machine: Arc<dyn StateMachineBackend>, ) -> Self

Create a new Raft node with a custom state machine backend. Use this to integrate Raft with your storage layer.

Source

pub fn id(&self) -> NodeId

Get the node ID.

Source

pub fn role(&self) -> NodeRole

Get the current role.

Source

pub fn current_term(&self) -> Term

Get the current term.

Source

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

Get the current leader ID.

Source

pub fn is_leader(&self) -> bool

Check if this node is the leader.

Source

pub fn is_leader_with_lease(&self) -> bool

Check if this node is the leader and holds a valid lease. Unlike is_leader(), this also verifies that the leader has recently received acknowledgements from a majority, preventing split-brain.

Source

pub fn extend_lease(&self)

Extend the leader lease to now + lease_duration. Called when the leader receives successful acks from a majority.

Source

pub fn has_valid_lease(&self) -> bool

Check if the leader lease is still valid.

Source

pub fn check_lease(&self)

Periodic check: if this node is leader but the lease has expired, step down to follower to prevent split-brain.

Source

pub fn add_peer(&self, peer_id: NodeId)

Add a peer to the cluster.

Source

pub fn remove_peer(&self, peer_id: &NodeId)

Remove a peer from the cluster.

Source

pub fn peers(&self) -> Vec<NodeId>

Get the list of peers.

Source

pub fn cluster_size(&self) -> usize

Get the cluster size (including self).

Source

pub fn quorum_size(&self) -> usize

Get the quorum size.

Source

pub fn reset_heartbeat(&self)

Reset the heartbeat timer.

Source

pub fn election_timeout_elapsed(&self) -> bool

Check if the election timeout has elapsed.

Source

pub fn start_election(&self) -> VoteRequest

Start an election as a candidate.

Source

pub fn handle_vote_request(&self, request: &VoteRequest) -> VoteResponse

Handle a vote request.

Source

pub fn handle_vote_response(&self, response: &VoteResponse) -> bool

Handle a vote response.

Source

pub fn propose(&self, command: Command) -> Result<LogIndex, String>

Propose a command (leader only).

Source

pub fn create_append_entries( &self, peer_id: &NodeId, ) -> Option<AppendEntriesRequest>

Create an append entries request for a peer.

Source

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

Handle an append entries request.

Source

pub fn handle_append_entries_response( &self, peer_id: &NodeId, response: &AppendEntriesResponse, )

Handle an append entries response.

Source

pub fn apply_committed(&self) -> Vec<CommandResult>

Apply committed entries to the state machine.

Source

pub fn get(&self, key: &str) -> Option<Vec<u8>>

Get a value from the state machine.

Source

pub fn log(&self) -> &ReplicatedLog

Get the log.

Source

pub fn state_machine(&self) -> &dyn StateMachineBackend

Get the state machine.

Source

pub fn should_snapshot(&self) -> bool

Check if a snapshot should be taken based on log size.

Source

pub fn take_snapshot(&self) -> Option<SnapshotMetadata>

Take a snapshot of the current state and compact the log. Returns the snapshot metadata if successful.

Source

pub fn get_snapshot_data(&self) -> Option<(SnapshotMetadata, Vec<u8>)>

Get the current snapshot data (for sending to followers).

Source

pub fn create_install_snapshot( &self, peer_id: &NodeId, offset: u64, ) -> Option<InstallSnapshotRequest>

Create an InstallSnapshot request for a lagging peer. Returns None if no snapshot is available or peer doesn’t need it.

Source

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

Handle an InstallSnapshot request from the leader.

Source

pub fn handle_install_snapshot_response( &self, peer_id: &NodeId, response: &InstallSnapshotResponse, _last_chunk_offset: u64, was_last_chunk: bool, )

Handle an InstallSnapshot response from a follower.

Source

pub fn peer_needs_snapshot(&self, peer_id: &NodeId) -> bool

Check if a peer needs a snapshot (their next_index is before our first log entry).

Source

pub fn snapshot_metadata(&self) -> Option<SnapshotMetadata>

Get current snapshot metadata.

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> 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, 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<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