TASK
Implementation
Implement log entry commitment:
- Leader tracks matchIndex for each follower
- For each index N, count how many nodes have matchIndex >= N
- If majority have entry N, and entry N is from current term, commit N
- Advance commitIndex to highest committed N
- Notify followers of new commitIndex in next heartbeat
Important: Only commit entries from current term to satisfy the Raft safety property.
Sample Test Cases
Commit on majority replicationTimeout: 5000ms
Input
{
"src": "c0",
"dest": "n1",
"body": {
"type": "init",
"msg_id": 1,
"node_id": "n1",
"node_ids": [
"n1"
]
}
}Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}Hints
Hint 1▾
Entry committed when on majority
Hint 2▾
Use matchIndex to count replicas
Hint 3▾
Only commit entries from current term directly
OVERVIEW
Theoretical Hub
Commitment
An entry is committed when the leader knows a majority have it. Committed entries are durable - they will survive leader changes. The leader advances commitIndex when majority confirms.
Current Term Requirement
Leaders only directly commit entries from their own term. Entries from previous terms are committed indirectly when a current-term entry is committed after them. This prevents a subtle safety violation.
Key Concepts
commitmentmajorityquorum
main.py
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python3
import sys
import json
class CommitTracker:
def __init__(self, node_id, peers, log, current_term):
self.node_id = node_id
self.peers = peers
self.log = log
self.current_term = current_term
self.match_index = {p: 0 for p in peers}
self.match_index[node_id] = log.get_last_index()
def update_match(self, peer, index):
# TODO: Update peer's matchIndex
pass
def try_advance_commit(self):
# TODO: Find highest N where majority have matchIndex >= N
# TODO: Only commit if entry N is from current term
pass
def _count_replicas(self, index):
# TODO: Count nodes with matchIndex >= index
pass