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

  1. 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.
  2. Count acks: start at 1 (leader), then +1 for each up follower whose log.len() >= leader_idx.
  3. If acks >= 2 (majority of 3):
    • Set leader.commit_index = leader_idx; call leader.apply_committed.
    • For each follower whose log.len() >= leader_idx, set its commit_index to leader_idx and call apply_committed.

Acceptance

  • put_then_del_replicates test passes in all three languages.
  • After three submits in a row to a fresh cluster, every node has log.len() == 3 and commit_index == 3.

Pitfalls

  • Don't advance commit_index on 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_committed must be called after commit_index is bumped, not before.