TASK
Implementation
Implement the three states from Raft: Leader, Follower, and Candidate. Each node starts as a Follower. Candidates request votes. Leaders coordinate the cluster.
Sample Test Cases
Node starts as followerTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1","n2","n3"]}}
{"src":"c0","dest":"n1","body":{"type":"get_state","msg_id":2}}
Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}
{"src":"n1","dest":"c0","body":{"type":"state_reply","in_reply_to":2,"msg_id":1,"state":"follower","term":0}}
Hints
Hint 1▾
Define an enum for states
Hint 2▾
All nodes start as followers
Hint 3▾
State transitions happen on specific events
OVERVIEW
Theoretical Hub
Raft Roles
In Raft, every node is in one of three states: Follower (passive, responds to leaders), Candidate (seeking to become leader), or Leader (handles all client requests). This clear state machine simplifies reasoning about the protocol.
Key Concepts
state machineleader electionRaft roles
main.py
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python3
import sys
import json
from enum import Enum
class State(Enum):
FOLLOWER = "follower"
CANDIDATE = "candidate"
LEADER = "leader"
class Node:
def __init__(self):
self.state = State.FOLLOWER
self.current_term = 0
self.voted_for = None
# TODO: Implement state transitions
if __name__ == "__main__":
pass