This Java project implements the Raft consensus algorithm, aimed at achieving consensus efficiently in distributed systems. Raft is known for its simplicity and understandability compared to older consensus algorithms like Paxos.
TODO: Need to add the architecture diagram.
The project is modular, focusing on clarity and simplicity:
- Server Node: Manages connections from clients and other nodes within a Raft cluster.
- Socket Layer: Facilitates communication across cluster nodes.
- Log Module: Handles log operations such as appending and compaction.
- Consensus Module: Implements Raft's core functionalities including log replication, leader election, and state management.
Components interact via well-defined interfaces for ease of testing and development.
A CLI tool is provided for manual simulation and testing:
- Build the package:
mvn package - Start a Node:
java -jar target/raft-java-1.0-SNAPSHOT.jar <server_name>. Theserver_namecan be from 1, 2, 3, 4, 5 - Help:
helpwill show all the options available in the CLI.
Available commands:
log - Show the state of the Raft log on this server
state - Show other Raft state on this server
command <cmd> - Add a new command on the Raft leader
heartbeat - Manually issue a heartbeat to update followers
leader - Become a leader
follower - Become a follower
help - Show this list of commands
exit - Exit the CLI
Unit tests, crucial for verifying component behavior, are conducted using JUnit:
- Run Tests: Execute
./gradlew testfrom the project root to run all unit tests. - Coverage: Focus on leader election, log replication, and fault tolerance to ensure robustness and correctness.
- Basic setup with maven. The aim is to make sure that the jar is created successfully with all the third party dependencies. Not just build but also be able to run it successfully.
- Unit testing setup with JUnit
- Implement both inbound socket processor & outbound socket processor .
- Implement both inbound queue & outbound queue.
- Implement the core logic of
Log - Thread which listens to the UDP packets from the internet. This thread holds inbound socket processor. This thread grabs the message from outside, converts the message into events and puts it into the inbound queue
- Thread which polls the inbound queue and forwards the request to
RaftServerLogic - Thread which sends the UDP package to the internet. This thread polls the outbound queue
- Main thread which runs the CLI application.
- Decide on serialization & de-serialization strategy. Initially, I felt that both inbound queue & outbound queue should hold the
Eventtype, but this strategy make the deserialization more complicated. Hence decided to create both the queues with different types - inbound queue withstringtype & outbound queue witheventtype. - Implement
handleAppendEntryResponse. This function is responsible for handling thehandleAppendEntryresponse from the followers. - After
nextIndexis backtracked, we need a way to update thenextIndexto the right position, once thehandleAppendentry becomes successful. - Implement a timer thread which sends
TICKevents
This section provides a clear view of what has been achieved and what is still on the horizon, keeping both the project team and external contributors informed about the project's status and direction.
- Currently,
nextIndexis decreased by 1 if the response we receive is false. But what happens if we get duplicate messages ? Then we will end up decreasing the nextIndex multiple times.
This README outlines the essentials for developing, testing, and understanding the Java Raft implementation. It includes the architecture, testing instructions using a CLI tool, and guidelines for unit testing, providing a foundation for effective project development and debugging.