Function: advancecommit_index in raft-consensus/raft.py

Date: 2026-05-29

Time: 13:50

advancecommit_index — Raft Leader Commit Advancement

Purpose

This method implements the leader's commit index advancement rule from the Raft paper (§5.3/§5.4). A leader cannot simply commit an entry the moment it writes it to its own log — it must wait until a majority of the cluster has replicated that entry. This method scans uncommitted log entries, counts how many nodes have replicated each one, and advances commitindex to the highest entry replicated by a majority.

It also enforces a critical safety property: a leader only commits entries from its own term, never from a prior leader's term. Entries from prior terms get committed indirectly — once a current-term entry at a higher index is committed, all preceding entries are implicitly committed too. This prevents the scenario in Figure 8 of the Raft paper, where a leader could incorrectly commit an old-term entry that later gets overwritten.

Contract

Preconditions:

Postconditions:

Invariant:

Parameters

None — this is a private method that reads directly from instance state:

Return Value

None. This method mutates self.commitindex as a side effect.

Algorithm

1. Iterate candidate indices: Loop from commit_index + 1 up through the last log index. Each n is a candidate for the new commit index.

2. Term filter: Skip any entry whose term doesn't match currentterm. This is the safety guard — entries from old terms are never directly committed.

3. Count replicas: Start count at 1 (the leader always has the entry). For each peer, check if matchindex[peer] >= n. If so, that peer has replicated at least up to index n, so increment count.

4. Majority check: Compute total = len(peers) + 1 (full cluster size). If count > total // 2, a strict majority has replicated index n, so advance commitindex = n.

5. No early exit: The loop continues even after finding a committable index, because a higher index might also have a majority. The final value of commitindex after the loop is the highest such index.

Side Effects

Error Handling

None. The method assumes all state is well-formed. There are no bounds checks, no exception handling, and no defensive guards. If matchindex contains a peer not in peerids, that peer is simply ignored (only peerids is iterated). If matchindex is missing a peer, dict.get(peer, 0) defaults to 0, treating that peer as having replicated nothing.

Usage Patterns

Called in two places:

1. tick() (line ~139) — on every heartbeat interval, after sending AppendEntries to all peers. This catches up the commit index periodically.

2. handleappendresponse() (line ~189) — immediately after a successful replication response updates matchindex. This allows the commit index to advance as soon as a majority is confirmed, without waiting for the next heartbeat.

Both callers gate on self._state == "leader", so this method is never invoked on followers or candidates.

Dependencies

Assumptions Not Enforced by Types

Topics to Explore

Beliefs