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
impl RaftNode
Sourcepub fn new(id: impl Into<NodeId>, config: RaftConfig) -> Self
pub fn new(id: impl Into<NodeId>, config: RaftConfig) -> Self
Create a new Raft node with the default in-memory state machine.
Sourcepub fn with_state_machine(
id: impl Into<NodeId>,
config: RaftConfig,
state_machine: Arc<dyn StateMachineBackend>,
) -> Self
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.
Sourcepub fn current_term(&self) -> Term
pub fn current_term(&self) -> Term
Get the current term.
Sourcepub fn is_leader_with_lease(&self) -> bool
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.
Sourcepub fn extend_lease(&self)
pub fn extend_lease(&self)
Extend the leader lease to now + lease_duration. Called when the leader receives successful acks from a majority.
Sourcepub fn has_valid_lease(&self) -> bool
pub fn has_valid_lease(&self) -> bool
Check if the leader lease is still valid.
Sourcepub fn check_lease(&self)
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.
Sourcepub fn remove_peer(&self, peer_id: &NodeId)
pub fn remove_peer(&self, peer_id: &NodeId)
Remove a peer from the cluster.
Sourcepub fn cluster_size(&self) -> usize
pub fn cluster_size(&self) -> usize
Get the cluster size (including self).
Sourcepub fn quorum_size(&self) -> usize
pub fn quorum_size(&self) -> usize
Get the quorum size.
Sourcepub fn reset_heartbeat(&self)
pub fn reset_heartbeat(&self)
Reset the heartbeat timer.
Sourcepub fn election_timeout_elapsed(&self) -> bool
pub fn election_timeout_elapsed(&self) -> bool
Check if the election timeout has elapsed.
Sourcepub fn start_election(&self) -> VoteRequest
pub fn start_election(&self) -> VoteRequest
Start an election as a candidate.
Sourcepub fn handle_vote_request(&self, request: &VoteRequest) -> VoteResponse
pub fn handle_vote_request(&self, request: &VoteRequest) -> VoteResponse
Handle a vote request.
Sourcepub fn handle_vote_response(&self, response: &VoteResponse) -> bool
pub fn handle_vote_response(&self, response: &VoteResponse) -> bool
Handle a vote response.
Sourcepub fn propose(&self, command: Command) -> Result<LogIndex, String>
pub fn propose(&self, command: Command) -> Result<LogIndex, String>
Propose a command (leader only).
Sourcepub fn create_append_entries(
&self,
peer_id: &NodeId,
) -> Option<AppendEntriesRequest>
pub fn create_append_entries( &self, peer_id: &NodeId, ) -> Option<AppendEntriesRequest>
Create an append entries request for a peer.
Sourcepub fn handle_append_entries(
&self,
request: &AppendEntriesRequest,
) -> AppendEntriesResponse
pub fn handle_append_entries( &self, request: &AppendEntriesRequest, ) -> AppendEntriesResponse
Handle an append entries request.
Sourcepub fn handle_append_entries_response(
&self,
peer_id: &NodeId,
response: &AppendEntriesResponse,
)
pub fn handle_append_entries_response( &self, peer_id: &NodeId, response: &AppendEntriesResponse, )
Handle an append entries response.
Sourcepub fn apply_committed(&self) -> Vec<CommandResult>
pub fn apply_committed(&self) -> Vec<CommandResult>
Apply committed entries to the state machine.
Sourcepub fn log(&self) -> &ReplicatedLog
pub fn log(&self) -> &ReplicatedLog
Get the log.
Sourcepub fn state_machine(&self) -> &dyn StateMachineBackend
pub fn state_machine(&self) -> &dyn StateMachineBackend
Get the state machine.
Sourcepub fn should_snapshot(&self) -> bool
pub fn should_snapshot(&self) -> bool
Check if a snapshot should be taken based on log size.
Sourcepub fn take_snapshot(&self) -> Option<SnapshotMetadata>
pub fn take_snapshot(&self) -> Option<SnapshotMetadata>
Take a snapshot of the current state and compact the log. Returns the snapshot metadata if successful.
Sourcepub fn get_snapshot_data(&self) -> Option<(SnapshotMetadata, Vec<u8>)>
pub fn get_snapshot_data(&self) -> Option<(SnapshotMetadata, Vec<u8>)>
Get the current snapshot data (for sending to followers).
Sourcepub fn create_install_snapshot(
&self,
peer_id: &NodeId,
offset: u64,
) -> Option<InstallSnapshotRequest>
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.
Sourcepub fn handle_install_snapshot(
&self,
request: &InstallSnapshotRequest,
) -> InstallSnapshotResponse
pub fn handle_install_snapshot( &self, request: &InstallSnapshotRequest, ) -> InstallSnapshotResponse
Handle an InstallSnapshot request from the leader.
Sourcepub fn handle_install_snapshot_response(
&self,
peer_id: &NodeId,
response: &InstallSnapshotResponse,
_last_chunk_offset: u64,
was_last_chunk: bool,
)
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.
Sourcepub fn peer_needs_snapshot(&self, peer_id: &NodeId) -> bool
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).
Sourcepub fn snapshot_metadata(&self) -> Option<SnapshotMetadata>
pub fn snapshot_metadata(&self) -> Option<SnapshotMetadata>
Get current snapshot metadata.