These problems practise the five safety properties: reading log states, running elections and commits by hand, and locating the step of a proof that a bad execution would break. Logs are written as lists of terms, index 1 first.
Problem 1
Problem 7.1 (Up-to-date logs and the commit rule).
- Order these five logs from most to least up-to-date:
, , , (empty), . - A leader of term 5 in a cluster of
has the log and . The matchIndex values of its four followers are . What does the commit rule allow? What changes when the follower with matchIndex 3 acknowledges index 4? - True or false, with a reason: “An entry that is stored on every server is committed.”
Solution
- Compare last (term, index) pairs lexicographically:
is (3, 2), is (2, 5), is (2, 4), is (1, 6), is (0, 0). So , then , then , then , then . The shortest non-empty log, , is the most up-to-date because its last term is the highest; the longest, , is second to last. - Index 4 is stored by the leader and one follower: 2 of 5, not a majority. Index 3 is stored by the leader and the followers with matchIndex 4, 3, 3: 4 of 5, a majority, but
, so condition 3 fails. The commit index stays 2. Once a second follower acknowledges index 4, index 4 is on 3 of 5 servers and has term 5, so the leader sets ; this commits index 3 (term 3) indirectly at the same moment. - False under the definition of Module 7: an entry is committed only when a leader directly commits it or an entry after it, and the commit rule never counts an older-term entry. Such an entry is, however, safe: no future leader can lack it (paper §5.4.2 mentions this). Raft simply does not use that observation, and the entry becomes committed as soon as the current leader commits an entry of its own term after it.
Problem 2
Problem 7.2 (Three servers after three terms).Three servers have these logs, written as (term, command): A has
; B has ; C has . (This is Kleppmann’s exercise 18 in the paper’s notation (1).)
- Describe a sequence of elections, appends and crashes that produces these logs.
- Which values can
have on each server? - Which servers can win an election for term 4, and with whose votes?
- Suppose B wins term 4. List the AppendEntries requests B sends to C until C’s log matches, and say when B can commit anything.
Solution
- Term 1: A is leader, appends
and , replicates to B and C, and crashes (or is cut off) before replicating . Term 2: B wins with its own vote and C’s (C’s log (1) equals B’s), appends , replicates to C, and is cut off. Term 3: C wins with its own vote and A’s (A’s last term 1 is below C’s 2; B would refuse since its log (2, 4) beats C’s (2, 2)), appends , and crashes before replicating it. - Only entries on a majority (2 of 3) with a current-term leader can be committed. Index 1 (term 1) is on all three and could have been committed by A in term 1. Index 2 (term 2) is on B and C and could have been committed by B in term 2. Nothing else is on two servers. So A: 0 or 1 (never 2: A’s index 2 holds
, which conflicts with the possibly committed , and a follower’s bound never certifies it). B: 0, 1 or 2. C: 0, 1 or 2 (C may have learned 2 from a later heartbeat of B). - Last (term, index): A (1, 2), B (2, 4), C (3, 3), so C beats B beats A. A gets no vote but its own: cannot win. B gets A’s vote but not C’s: 2 of 3, can win. C gets both: can win.
- B sets
. Request 1: , ; C has no index 4, fails. Request 2: ; C’s index 3 has term 3, fails. Request 3: , entries ; C’s index 2 has term 2, so the check passes; at index 3 the terms differ, C deletes and appends ; success, . Now indices 3 and 4 are on a majority, but they have term 2, not 4, so B cannot commit them by counting (this is the Figure 8 rule). B commits as soon as an entry of term 4 (a client command or the no-op appended at the start of its term) reaches C; then indices 1 to 5 are committed together.
Problem 3
Problem 7.3 (Figure 8 with three servers).Consider a variant of Raft whose commit rule omits the condition
.
- Give an execution with three servers A, B, C in which two servers apply different commands at index 2. For every election, name the voters and check the up-to-date test.
- Point to the first step of your execution that correct Raft would not perform, and explain why the rest of the execution is then harmless.
Solution
- All logs start as
, with index 1 committed. A applied
- Term 2: A is leader, appends
at index 2, and crashes before sending it. Logs: A , B , C . - Term 3: C campaigns; B grants (equal logs); C wins with B and C. C appends
at index 2 and crashes before sending it. Logs: A , B , C . - Term 4: A restarts and campaigns; B grants (A’s last term 2 beats B’s 1); A wins with A and B. A sends its index 2 to B. Now index 2 (term 2) is on A and B, a majority. The variant commits it; A applies
at index 2 and replies to its client. A crashes. - Term 5: C restarts and campaigns with last (3, 2); B’s last is (2, 2), lower, so B grants; C wins with B and C. C replicates its index 2 to B, which deletes
and stores . C appends a term-5 entry at index 3 and replicates it to B; now even the correct rule commits index 3, and with it index 2. C applies at index 2. at index 2 and C applied at index 2: State Machine Safety fails. - The first wrong step is A committing index 2 in term 4: its entry has term 2, not 4. Correct Raft would wait. Everything after that step is legal Raft: C may win term 5 because nobody with a log ending in term 4 exists yet, and it may overwrite index 2 because that entry was never committed. Had A first replicated a term-4 entry to B, B’s last term would be 4 and B would refuse C, exactly as in panel (e) of the paper’s Figure 8.
Problem 4
Problem 7.4 (What the up-to-date test does and does not say).For a log
let , with for the empty log, and write if lexicographically (” is at least as up-to-date as ”). All logs below occur in one Raft execution.
- Prove that
is reflexive, transitive and total. Give two lists of terms with the same key that are different, and explain why two logs of one Raft execution with the same key are equal (use part 4). - Prove: if
is a prefix of , then . - Give two logs with
such that is not a prefix of . Which step of the Leader Completeness proof deals with this case, and how? - Prove: if
and , then is a prefix of .
Solution
- The lexicographic order on pairs of natural numbers is a total order: compare first components, and if they are equal compare second components.
pulls it back along , so it inherits reflexivity, transitivity and totality. As bare lists, and both have key (2, 2) but differ. They cannot both occur in one execution: if , the last terms are equal, so part 4 applied in both directions makes each log a prefix of the other, hence . So on the logs of one execution, is a total order. - If
is empty, . If , trivial. Otherwise and , because terms along a log are non-decreasing (Lemma 7.2). If the inequality is strict, ; if it is an equality, the second components decide and . Either way . and : key (3, 2) beats key (2, 3), but is not a prefix of . This happens in Figure 8 (d) with S5 and S2. In the Leader Completeness proof this is case 7 ( ): the proof does not claim that the candidate contains the voter’s log; it shows that the leader of the candidate’s last term already held the committed entry (by the choice of the smallest bad term ), and Log Matching transfers it to the candidate. - Let
be the common last term, (from with equal first components). If both logs are empty. Otherwise both last entries were created by (Lemma 7.2), at indices . When created its entry at , its log held its entry at , since it appends at increasing indices and never deletes (Proposition 6.7). By Log Matching (Theorem 7.3) applied to and that snapshot at index , agrees with it on , so has term . Then and have the same term at index , and Log Matching again gives . So is a prefix of . This is case 6 of the Leader Completeness proof.
Problem 5
Problem 7.5 (Is this state possible?).Five servers S1 to S5. For each situation, say whether it can occur in correct Raft. If not, name the property of paper Figure 3 it violates (and the rule that prevents it); if yes, describe how it arises.
- S1 and S2 are both leaders of term 3 (at different times).
- S1’s log is
and S2’s log is . - S2, as leader of term 2, set
with log . Later S1 is leader of term 4 with log . - S1 is leader of term 3 with log
and ; S2 has log and . - S1 has applied the term-2 entry at index 2, and S5 has applied the term-3 entry at index 2.
Solution
- Impossible: Election Safety. “At different times” does not help; each server votes once per term (persisted
votedFor) and two majorities intersect, so two different winners of term 3 would need a double vote.- Impossible. S2’s log has term 2 after term 3, but terms along a log are non-decreasing (Lemma 7.2). Also, both logs have term 2 at index 3 but differ at index 2, which violates Log Matching (prevented by one leader per term and the consistency check).
- Impossible: Leader Completeness. Index 4 is directly committed in term 2 (term-2 entry on a majority, counted by the term-2 leader), so every later leader must hold
at indices 2 to 4. S1’s log lacks it. The vote test prevents it: S1 appended its term-4 entry only after winning, so when it campaigned its log was , ending in term 1. Its vote majority meets the commit majority, and the voter in the intersection held the term-2 entries (by the Leader Completeness proof), so its last term was at least 2 and it refused S1. - Possible. S2 kept stale entries from term 2 that were never committed; S1 won term 3 (say with votes of S3, S4 holding
) and committed its term-3 entries with the help of S3 and S4. S2 learned earlier. On S1’s next AppendEntries to S2, the walk-back reaches index 2, S2 deletes its indices 3 to 5 and copies . Note that S2’s log is longer than S1’s: length is not what counts. - Impossible: State Machine Safety. This is the Figure 8 execution with the naive commit rule; correct Raft never commits the term-2 entry at index 2 in panel (c), so S1 never applies it.