This project is my attempt to implement Raft consensus algorithm in Rust for educational purposes. It focuses on demonstrating the core concepts of leader election, log replication, and basic state machine application within a simulated network environment.
The project is organized into several key modules:
src/main.rs: The entry point for the simulation. It sets up the nodes, the simulated network, runs the node tasks, and orchestrates a basic test scenario (leader election, command submission, state verification).config.rs: Defines configuration parameters for the Raft nodes, such as timeouts and intervals.state_machine.rs:- Defines the
StateMachinetrait, representing the application logic that Raft replicates. - Provides a simple default implementation (
StateMachineDefault).
- Defines the
consensus/: Contains the core Raft implementation.mod.rs: Exports the public interface of the consensus module.core/: Implements the fundamental Raft state and logic, independent of networking or timers.mod.rs: DefinesNodeCore(holdingcurrentTerm,votedFor,log,commitIndex,lastApplied, state-specific fields likenextIndex,matchIndex) andNodeStateenum. Implements state transitions, voting logic, log consistency checks, and commit index calculation.tests.rs: Unit tests forNodeCore.
server/: Bridges the core logic with the outside world (messaging, timers).mod.rs: DefinesNodeServerwhich ownsNodeCore, aStateMachine, aNodeMessenger, and handles RPC processing by callingNodeCoremethods. Contains the main message handling logic (process_message,handle_append_entries,handle_request_vote, etc.) and timer event handling.tests.rs: Integration-style tests forNodeServer.
timer.rs: Implements the election and heartbeat timers usingtokio::time. DefinesNodeTimerandTimerType.log_entry.rs: Defines theLogEntrystruct stored in the Raft log.error.rs: DefinesConsensusErrorfor Raft-specific errors.event.rs: DefinesConsensusEvent(e.g.,LeaderElected,EntryCommitted) for signaling significant occurrences, used by the simulation.
messaging/: Simulates the network layer for node communication.mod.rs: Exports the public interface of the messaging module.network.rs: Defines theNetworkstruct, which acts as a central router holding channels to each node.messenger.rs: DefinesNodeMessenger, providing an API for a node to send messages (send_to,broadcast,send_self) via theNetwork.receiver.rs: DefinesNodeReceiver, used by each node's task to receive messages from its dedicated channel.message.rs: Defines theMessageenum, containing variants for Raft RPCs and internal commands.error.rs: DefinesMessagingErrorfor network simulation errors.
Key Design Principles:
- Separation of Concerns: The core Raft state (
NodeCore) is separated from the server logic (NodeServer) which handles I/O (timers, messages). The network simulation (messaging) is also distinct. - Asynchronous: Leverages
tokiofor non-blocking timers and message passing between nodes. - State Machine Abstraction: Uses a
StateMachinetrait to decouple the Raft logic from the specific application being replicated. - Simulation Focus: The current networking layer is designed for in-memory simulation rather than real network sockets.
Cargo generated documentation is hosted on github pages
The implementation currently includes:
- Core Raft Logic (
consensus::core):- Node states (Follower, Candidate, Leader).
- Term management and voting logic (
VoteRequest,VoteResponse). - Basic log replication (
AppendEntries,AppendResponse). - Leader election triggered by timeouts.
- Commit index calculation based on majority match.
- Log consistency checks and basic conflict handling (truncation).
- RPC Handling (
consensus::server):- Handles incoming RPC messages (
VoteRequest,VoteResponse,AppendEntries,AppendResponse). - Manages state transitions based on RPCs and timer events.
- Applies committed entries to a simple state machine.
- Handles incoming RPC messages (
- Timers (
consensus::timer):- Election timer with randomized timeout.
- Heartbeat timer for leaders.
- Messaging Simulation (
messaging):- In-memory network simulation using
tokio::mpscchannels. - Ability to broadcast and send messages between simulated nodes.
- In-memory network simulation using
- State Machine (
state_machine):- A basic trait (
StateMachine) and a default implementation (StateMachineDefault) that increments a counter.
- A basic trait (
- Simulation (
main.rs):- Initializes a cluster of nodes.
- Runs node tasks concurrently.
- Allows nodes to elect a leader naturally via timeouts.
- Sends one or more commands to the elected leader.
- Verifies that commands are eventually applied consistently across all nodes' state machines.
- Testing:
- Unit tests for core logic (
NodeCore), timers (NodeTimer), and most of server interactions (NodeServer).
- Unit tests for core logic (
- Prerequisites: Ensure you have Rust and Cargo installed (
rustup). - Clone: Clone this repository.
- Build:
cargo build - Run Simulation:
cargo run- You can adjust logging levels using the
RUST_LOGenvironment variable (e.g.,RUST_LOG=debug cargo run). - The simulation in
main.rscurrently sets up a small cluster, waits for an election, sends commands to the leader, and verifies state convergence.
- You can adjust logging levels using the
This implementation provides a foundation, but few critical features are needed for a complete and robust Raft:
- Persistence layer: Allow nodes to recover their state after crashing.
- Log Compaction (Snapshotting): Prevent the log from growing indefinitely.
- Client Interaction Guarantees: Provide correct semantics for external clients submitting commands (including complete commit-wait mechanism)
- Cluster Membership Changes: Allow adding/removing nodes from the cluster safely.
- More Robust Error Handling: Implement retry logic for RPCs where appropriate, handle channel closure and other task failures more gracefully.
- Optimizations: Implement batching of log entries in
AppendEntries, optimize log persistence. - Enhanced Simulation: Test more complex scenarios including network partitions, message loss and delays
- Comprehensive Testing: More tests for edge cases