pub enum Action {
SetElectionTimeout,
SaveCurrentTerm,
SaveVotedFor,
BroadcastMessage(Message),
AppendLogEntries(LogEntries),
SendMessage(NodeId, Message),
InstallSnapshot(NodeId),
}Variants§
SetElectionTimeout
Set an election timeout.
When the timeout expires, please call Node::handle_election_timeout().
An existing timeout is cancelled when a new one is set.
The user needs to set different timeouts for each node based on its Role as follows:
- For
Role::Leader:- When the timeout expires, the leader sends heartbeat messages to followers.
- To maintain the role of leader, the timeout should be shorter than the election timeouts of the followers.
- For
Role::Candidate:- When the timeout expires, the candidate starts a new election.
- The timeout should have some randomness to avoid conflicts with other candidates.
- For
Role::Follower:- When the timeout expires, the follower starts a new election.
- The timeout should be longer than the heartbeat timeout of the leader.
Note that the appropriate timeout values depend on the specific application.
SaveCurrentTerm
Save the current term (Node::current_term()) to persistent storage.
To guarantee properties by the Raft algorithm, the value must be saved before responding to users or sending messages to other nodes.
SaveVotedFor
Save the voted-for node ID (Node::voted_for()) to persistent storage.
To guarantee properties by the Raft algorithm, the value must be saved before responding to users or sending messages to other nodes.
BroadcastMessage(Message)
Broadcast a message to all other nodes (Node::peers()).
On the receiving side, the message is handled by Node::handle_message().
Unlike storage-related actions, this action can be executed asynchronously and can be discarded if the communication link is busy. Additionally, the reordering of messages to the same destination node is acceptable.
AppendLogEntries(LogEntries)
Append log entries to the node-local log on persistent storage.
Note that previously written log suffix entries may be overwritten by these new entries.
In other words, the LogEntries::last_position().index can be any value within the range from the start index to the end index of the local log.
To guarantee properties by the Raft algorithm, the entries must be appended before responding to users or other nodes. (However, because writing all log entries to persistent storage synchronously could be too costly, in reality, the entries are often written asynchronously.)
SendMessage(NodeId, Message)
Send a message to a specific node.
On the receiving side, the message is handled by Node::handle_message().
Unlike storage-related actions, this action can be executed asynchronously and can be discarded if the communication link is busy. Additionally, the reordering of messages to the same destination node is acceptable.
Additionally, if an AppendEntriesRPC contains too many entries to be sent in a single message,
they can be safely truncated using LogEntries::truncate() before sending the message.
InstallSnapshot(NodeId)
Install a snapshot on a specific node.
The user is responsible for managing the details of sending and installing the snapshots.
Note that once the snapshot installation is complete, the user needs to call Node::handle_snapshot_installed().