Step 02 — Replication and commit
Goal
Wire Cluster::submit so that one Op propagates from the leader to
every live follower, advances commit_index on majority, and applies
into the local kv state.
Tasks
- In
submit(op):- Compute
leader_idx = leader.log.len() + 1. - Build
LogEntry { term: leader.term, index: leader_idx, op }. - Append on the leader (must succeed).
- For each follower id 1, 2: if
up[fid], append on that follower.
- Compute
- Count acks: start at 1 (leader), then
+1for eachupfollower whoselog.len() >= leader_idx. - If
acks >= 2(majority of 3):- Set
leader.commit_index = leader_idx; callleader.apply_committed. - For each follower whose
log.len() >= leader_idx, set itscommit_indextoleader_idxand callapply_committed.
- Set
Acceptance
put_then_del_replicatestest passes in all three languages.- After three submits in a row to a fresh cluster, every node has
log.len() == 3andcommit_index == 3.
Pitfalls
- Don't advance
commit_indexon a follower that hasn't received the entry — that's how silent divergence happens. - The leader always advances on a majority, even if a follower hasn't ack'd, because the leader itself counts.
apply_committedmust be called aftercommit_indexis bumped, not before.