Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/testsuite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Raft CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build-and-test:
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.22" # Adjust to match your local Go version if needed

- name: Run Cluster Tests
run: make test_cluster
env:
CI: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.pdf
.claudeignore
2 changes: 1 addition & 1 deletion internal/raft/appendEntriesLogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ func (node *RaftNode) handlePeerClientAppendEntries(peer *PeerClient) {
node.role = Follower
node.currentTerm = resp.Term
node.votedFor = ""
_ = node.savePersistentState() // PERSIST: Term and VotedFor updated
return
}

if !resp.Success {
node.nextIndex[peer.ID] = node.nextIndex[peer.ID] - 1
} else {
// FIXED: Properly update matchIndex first
matchVal := int(appentEntriesRequest.PrevLogIndex) + len(appentEntriesRequest.Entries)
if matchVal > node.matchIndex[peer.ID] {
node.matchIndex[peer.ID] = matchVal
Expand Down
6 changes: 4 additions & 2 deletions internal/raft/clientAPI.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (node *RaftNode) Execute(ctx context.Context, req *proto.ClientRequest) (*p

// 1. Redirection check: Reject if this node is not the Leader
if node.role != Leader {
leaderID := node.votedFor // Or track the current known leader ID
leaderID := node.leaderId // Return the actual tracked leader ID
node.mu.Unlock()
return &proto.ClientResponse{
Success: false,
Expand Down Expand Up @@ -47,6 +47,8 @@ func (node *RaftNode) Execute(ctx context.Context, req *proto.ClientRequest) (*p
}
node.log = append(node.log, entry)
targetIndex := entry.Index

_ = node.savePersistentState()
node.mu.Unlock()

// 4. Trigger replication immediately across followers
Expand Down Expand Up @@ -82,7 +84,7 @@ func (node *RaftNode) Execute(ctx context.Context, req *proto.ClientRequest) (*p
func (node *RaftNode) Get(ctx context.Context, req *proto.GetRequest) (*proto.GetResponse, error) {
node.mu.Lock()
if node.role != Leader {
leaderID := node.votedFor
leaderID := node.leaderId // Return the actual tracked leader ID
node.mu.Unlock()
return &proto.GetResponse{
Success: false,
Expand Down
19 changes: 12 additions & 7 deletions internal/raft/node.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//NOTE: This file holds the implementations for the server states , constructor function , and the main event looo

package raft

import (
"github.com/git-sudo-404/Raft_KV/proto"
"log"
"sync"

"github.com/git-sudo-404/Raft_KV/proto"
)

type Role int
Expand Down Expand Up @@ -37,6 +37,7 @@ type RaftNode struct {
role Role
commitIndex uint64
lastApplied uint64
leaderId string

peers map[string]*PeerClient
resetTimerChan chan struct{} // election starts in case nothing was put in this channel for the electionTimeout amount of time
Expand All @@ -53,9 +54,8 @@ type RaftNode struct {
}

// NewRaftNode initializes and returns a new Raft node starting as a Follower.

func NewRaftNode(id string, peers map[string]*PeerClient) *RaftNode {
return &RaftNode{
node := &RaftNode{
id: id,
role: Follower,
currentTerm: 0,
Expand All @@ -67,10 +67,16 @@ func NewRaftNode(id string, peers map[string]*PeerClient) *RaftNode {
commitChan: make(chan struct{}, 1),
KVStore: make(map[string]string),
}

// Load persistent state before joining the cluster
if err := node.loadPersistentState(); err != nil {
log.Printf("[Node %s] Failed to load persistent state: %v", id, err)
}

return node
}

func (node *RaftNode) getLastLogIndexAndTerm() (uint64, uint64) {

if len(node.log) == 0 {
return 0, 0
}
Expand All @@ -79,5 +85,4 @@ func (node *RaftNode) getLastLogIndexAndTerm() (uint64, uint64) {
var lastLogTerm = node.log[lastLogIndex-1].Term // Go index is 0 - based

return lastLogIndex, lastLogTerm

}
88 changes: 88 additions & 0 deletions internal/raft/persistence.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package raft

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)

type PersistentState struct {
CurrentTerm uint64 `json:"current_term"`
VotedFor string `json:"voted_for"`
Log []json.RawMessage `json:"log"`
}

func (n *RaftNode) persistPath() string {
_ = os.MkdirAll("raft_data", 0755)
return filepath.Join("raft_data", fmt.Sprintf("node_%v.json", n.id))
}

func (n *RaftNode) savePersistentState() error {
// LOCK REMOVED: Caller is expected to hold n.mu.Lock()

logData, err := json.Marshal(n.log)
if err != nil {
return err
}

var rawLog []json.RawMessage
if err := json.Unmarshal(logData, &rawLog); err != nil {
return err
}

state := PersistentState{
CurrentTerm: n.currentTerm,
VotedFor: n.votedFor,
Log: rawLog,
}

data, err := json.MarshalIndent(state, "", " ")
if err != nil {
return err
}

path := n.persistPath()
tmpPath := path + ".tmp"
if err := os.WriteFile(tmpPath, data, 0644); err != nil {
return err
}

return os.Rename(tmpPath, path)
}

func (n *RaftNode) loadPersistentState() error {
// LOCK REMOVED: Caller is expected to hold n.mu.Lock() (or be initializing)

path := n.persistPath()
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil
}

data, err := os.ReadFile(path)
if err != nil {
return err
}

var state PersistentState
if err := json.Unmarshal(data, &state); err != nil {
return err
}

n.currentTerm = state.CurrentTerm
n.votedFor = state.VotedFor

if len(state.Log) > 0 {
logData, err := json.Marshal(state.Log)
if err != nil {
return err
}
if err := json.Unmarshal(logData, &n.log); err != nil {
return err
}
} else {
n.log = nil
}

return nil
}
10 changes: 4 additions & 6 deletions internal/raft/requestVoteLogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func (node *RaftNode) handlePeerClientRequestVote(peer *PeerClient, votes *int,
node.role = Follower
node.currentTerm = resp.Term
node.votedFor = ""
_ = node.savePersistentState() // PERSIST: Term and VotedFor updated

// Wake up the event loop as the node steps down from the Leader role
select {
case node.resetTimerChan <- struct{}{}:
default:
Expand All @@ -34,18 +34,15 @@ func (node *RaftNode) handlePeerClientRequestVote(peer *PeerClient, votes *int,
if resp.VoteGranted && resp.Term == node.currentTerm {
*votes += 1

// peers does not include the current node, so total nodes = len(peers) + 1
totalNodes := len(node.peers) + 1
if *votes > totalNodes/2 && node.role == Candidate {
node.role = Leader

// Trigger the appendEntries heartbeat loop
select {
case node.leaderChan <- struct{}{}:
default:
}

// Make the volatile maps to store nextIndex & matchIndex
node.nextIndex = make(map[string]int)
node.matchIndex = make(map[string]int)

Expand All @@ -63,9 +60,10 @@ func (node *RaftNode) startElection() {
node.role = Candidate
node.currentTerm += 1
node.votedFor = node.id
_ = node.savePersistentState() // PERSIST: Term and VotedFor updated

lastLogIndex, lastLogTerm := node.getLastLogIndexAndTerm()
votes := 1 // starts at 1 accounting for self
votes := 1

requestVoteRequest := &proto.RequestVoteRequest{
Term: node.currentTerm,
Expand All @@ -87,7 +85,7 @@ func (node *RaftNode) RunElectionTimer() {
node.mu.Unlock()

if role == Leader {
<-node.resetTimerChan // Blocks with 0% CPU usage while Leader
<-node.resetTimerChan
continue
}

Expand Down
36 changes: 9 additions & 27 deletions internal/raft/rpc.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
//NOTE: This file holds the gRPC server implementations

package raft

import (
"context"

"github.com/git-sudo-404/Raft_KV/proto"
)

//NOTE: RequestVote() will be run on the other nodes except the candidate node
// The candidate node gets the results from all these nodes ... and the orchestration of these will be managed by another function say startElection().

func (node *RaftNode) RequestVote(ctx context.Context, req *proto.RequestVoteRequest) (*proto.RequestVoteResponse, error) {

// mutexing locking the node state so that other go routines (say other RequestVote rpc's or the appendEntries don't do dirty reads n writes)
node.mu.Lock()
defer node.mu.Unlock()

// a default response of rejecting the vote request
resp := &proto.RequestVoteResponse{
Term: node.currentTerm,
VoteGranted: false,
}

// reject the request if the candidate is requesting vote for an outdated term
// NOTE: here we are accepting & processing if the candidate's requestVote term is equal to the node's current term in order to account for the retries

// 1.
if req.Term < node.currentTerm {
return resp, nil // return a nil in the error field to communicate that the server correctly got the request n processed it without any netwrok failures or any crashes
return resp, nil
}

// 2. MUST update term & step down if the candidate is found to be having a higher term than the follower ndoe
if req.Term > node.currentTerm {
node.currentTerm = req.Term
// update the resp.term as well since it was earlier initialized with the node.currentTerm
resp.Term = node.currentTerm
node.votedFor = ""
node.role = Follower
_ = node.savePersistentState() // PERSIST: Term and VotedFor updated
}

followerLastLogIndex, followerLastLogTerm := node.getLastLogIndexAndTerm()
Expand All @@ -52,8 +45,7 @@ func (node *RaftNode) RequestVote(ctx context.Context, req *proto.RequestVoteReq
if node.votedFor == "" || node.votedFor == req.CandidateId {
node.votedFor = req.CandidateId
resp.VoteGranted = true

// reset the election timer when we vote for someone
_ = node.savePersistentState() // PERSIST: VotedFor updated

select {
case node.resetTimerChan <- struct{}{}:
Expand All @@ -64,11 +56,9 @@ func (node *RaftNode) RequestVote(ctx context.Context, req *proto.RequestVoteReq
}

return resp, nil

}

func (node *RaftNode) AppendEntries(ctx context.Context, req *proto.AppendEntriesRequest) (*proto.AppendEntriesResponse, error) {

node.mu.Lock()
defer node.mu.Unlock()

Expand All @@ -81,7 +71,7 @@ func (node *RaftNode) AppendEntries(ctx context.Context, req *proto.AppendEntrie
return resp, nil
}

// moved the election timer reset after the term check to avoid considering the AppendEntries() from stale leaders who come back after a network partition
node.leaderId = req.LeaderId

select {
case node.resetTimerChan <- struct{}{}:
Expand All @@ -96,29 +86,23 @@ func (node *RaftNode) AppendEntries(ctx context.Context, req *proto.AppendEntrie
node.currentTerm = req.Term
node.votedFor = ""
resp.Term = node.currentTerm
_ = node.savePersistentState() // PERSIST: Term and VotedFor updated
}

if len(node.log) < int(req.PrevLogIndex) { // since raft's log index 0 means no log yet , and 1 means there's one log
// so which means , raft's log index 1 -> Go's log index 0
// basically , raft's logIndex represents the len
if len(node.log) < int(req.PrevLogIndex) {
return resp, nil
}

if req.PrevLogIndex > 0 && node.log[req.PrevLogIndex-1].Term != req.PrevLogTerm {
// truncation will be handled when the logIndex matches correctly
return resp, nil
}

// the above cases ensure the exising last log entry's logIndex & termNumber matches with the req

// truncate the log at the PrevLogIndex to ensure that we don't append the entries again n again for the retries as well

// also only truncate and append if there are new Entries , don't truncate for heartbeats anol

// truncate the log at the PrevLogIndex
if len(req.Entries) > 0 {
afterDeletionLog := node.log[:req.PrevLogIndex]
afterDeletionLog = append(afterDeletionLog, req.Entries...)
node.log = afterDeletionLog
_ = node.savePersistentState() // PERSIST: Log updated
}

// commitIndex should always only move forward
Expand All @@ -128,7 +112,5 @@ func (node *RaftNode) AppendEntries(ctx context.Context, req *proto.AppendEntrie
}

resp.Success = true

return resp, nil

}
Loading
Loading