diff --git a/.github/workflows/testsuite.yml b/.github/workflows/testsuite.yml new file mode 100644 index 0000000..b19be71 --- /dev/null +++ b/.github/workflows/testsuite.yml @@ -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 diff --git a/.gitignore b/.gitignore index a136337..64dec63 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.pdf +.claudeignore diff --git a/internal/raft/appendEntriesLogic.go b/internal/raft/appendEntriesLogic.go index 922cd99..4d53c03 100644 --- a/internal/raft/appendEntriesLogic.go +++ b/internal/raft/appendEntriesLogic.go @@ -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 diff --git a/internal/raft/clientAPI.go b/internal/raft/clientAPI.go index 379f1e9..359a1de 100644 --- a/internal/raft/clientAPI.go +++ b/internal/raft/clientAPI.go @@ -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, @@ -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 @@ -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, diff --git a/internal/raft/node.go b/internal/raft/node.go index 93070fa..9f3f534 100644 --- a/internal/raft/node.go +++ b/internal/raft/node.go @@ -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 @@ -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 @@ -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, @@ -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 } @@ -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 - } diff --git a/internal/raft/persistence.go b/internal/raft/persistence.go new file mode 100644 index 0000000..06bf37f --- /dev/null +++ b/internal/raft/persistence.go @@ -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 +} diff --git a/internal/raft/requestVoteLogic.go b/internal/raft/requestVoteLogic.go index eff5e78..20d25ec 100644 --- a/internal/raft/requestVoteLogic.go +++ b/internal/raft/requestVoteLogic.go @@ -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: @@ -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) @@ -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, @@ -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 } diff --git a/internal/raft/rpc.go b/internal/raft/rpc.go index b1c730c..a271650 100644 --- a/internal/raft/rpc.go +++ b/internal/raft/rpc.go @@ -1,9 +1,8 @@ -//NOTE: This file holds the gRPC server implementations - package raft import ( "context" + "github.com/git-sudo-404/Raft_KV/proto" ) @@ -11,32 +10,26 @@ import ( // 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() @@ -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{}{}: @@ -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() @@ -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{}{}: @@ -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 @@ -128,7 +112,5 @@ func (node *RaftNode) AppendEntries(ctx context.Context, req *proto.AppendEntrie } resp.Success = true - return resp, nil - } diff --git a/results.md b/results.md index 4e67aff..2edc639 100644 --- a/results.md +++ b/results.md @@ -1,18 +1,18 @@ # Raft Cluster Test Report -Generated at: 2026-07-27 16:26:14 +Generated at: 2026-07-28 03:19:11 ## 📊 Summary Metrics | Metric | Value | | :--- | :--- | -| **Total Commands Attempted** | 570 | -| **Successful Commands** | 567 | -| **Failed Commands** | 3 | -| **Success Rate** | 99.47% | -| **Nodes Killed (Chaos)** | 6 | -| **Nodes Restarted** | 5 | -| **Total Test Duration** | 53.264s | +| **Total Commands Attempted** | 530 | +| **Successful Commands** | 530 | +| **Failed Commands** | 0 | +| **Success Rate** | 100.00% | +| **Nodes Killed (Chaos)** | 3 | +| **Nodes Restarted** | 3 | +| **Total Test Duration** | 34.645s | --- *Cluster test executed successfully with simulated network node failures and state machine verification.*