13-01

Chapter 13 · Distributed systems, honestly

2PC and the coordinator crash

  • 44 min core
  • Core track
  • Skills: hostile-env, intent

00Recall

One card from 12-02 (why each participant multiplies the state count), one from 10-05 (where fairness comes from) and one from 08-02 (loss and duplication for free). All three come back in this lesson.

12-02-b from Predict the growth curve

A model with two participants has a few dozen states and one with three has about a hundred. Why is "a few hundred for four" a better guess than "about a hundred and seventy"?

Show answer

Each participant multiplies the number of reachable combinations by roughly the same factor. Growth per component is multiplicative, not additive.

10-05-a from Where fairness comes from

Give a real mechanism behind each: weak fairness on a worker's retry, and strong fairness on receiving a message that is resent.

Show answer

A timer that keeps firing while the task is open (broken by a crash or a retry budget). A network that drops copies at random rather than all of them for ever (random loss gives delivery with probability 1, which SF records as certain; broken by a partition that never heals).

08-02-a from Loss and duplication for free

In a spec whose network is a set, how do you allow duplicate delivery?

Show answer

Leave the message in the set when it is received. Nothing then forbids receiving it again.

Skip recall Recall is never required.

01Commit

A trip booking needs an airline seat and a hotel room, or neither. Two-phase commit is the textbook answer. Each participant votes. A participant that can do its share votes yes; one that cannot aborts on the spot. The coordinator counts the votes, writes its decision to disk, and announces it. Every message goes into a set (chapter 08), so it can arrive late, twice, or not yet.

specs/TwoPhaseCommit.tla adds one thing the textbook diagram leaves out: the coordinator can crash, and in this module it never comes back. The claim to check is Consistent: no participant commits while another aborts.

TLA+
Consistent is defined as
    for all p, q in Participants : not(part[p] = "committed" and part[q] = "aborted")

Commit a

TLC checks TwoPhaseCommit.cfg with two participants, invariants TypeOK and Consistent, and deadlock checking on. The coordinator may crash at any moment. What does it report?

  1. No error. The crash only delays things, and every booking still ends.
  2. Consistent is violated: the crash leaves one participant committed and the other aborted.
  3. A deadlock: somebody is waiting, and nothing can ever answer them.

Answer A deadlock: somebody is waiting, and nothing can ever answer them.

Commit b

TwoPhaseCommit.safety.cfg runs the same model with deadlock checking off. Does Consistent hold in every reachable state, the blocked ones included?

  1. Yes. Blocked participants are stuck, but none of them disagrees.
  2. No. A blocked participant eventually times out and aborts, while the other one commits.
  3. TLC cannot say: with deadlock checking off it does not visit the blocked states.

Answer Yes. Blocked participants are stuck, but none of them disagrees.

02Lab

Run the chapter's first model:

shell
./tla check chapters/13-distributed-systems/specs/TwoPhaseCommit.cfg
Expected output 13-distributed-systems/specs/TwoPhaseCommit
13-distributed-systems/specs/TwoPhaseCommit  TwoPhaseCommit.cfg  (Participants={flight, hotel})
parse ok
model check: deadlock reached; trace has 4 states (3 steps); 48 distinct states
expected outcome met: deadlock
trace (changed variables only; ./tla trace --full shows all):
  1  Init     part = (flight :> "working" @@ hotel :> "working"), coord = "collecting", yes = {}, up = TRUE, net = {}
  2  VoteYes  part: (flight :> "working" @@ hotel :> "working") -> (flight :> "prepared" @@ hotel :> "working"), net: {} -> {[kind |-> "yes", from |-> flight]}
  3  VoteYes  part: (flight :> "prepared" @@ hotel :> "working") -> (flight :> "prepared" @@ hotel :> "prepared"), net: {[kind |-> "yes", from |-> flight]} -> {[kind |-> "yes", from |-> flight], [kind |-> "yes", from |-> hotel]}
  4  Crash    up: TRUE -> FALSE

Reveal for commit a

TLC reports a deadlock: a trace of 4 states that ends where no step is possible. In the last state the coordinator is down, no decision is on its disk, and at least one participant has voted yes and is waiting. Finished, a step that changes nothing once everyone has decided, makes a completed booking a designed end, so a deadlock here means somebody is still waiting.

Then the same model with deadlock checking off:

shell
./tla check chapters/13-distributed-systems/specs/TwoPhaseCommit.safety.cfg
Expected output 13-distributed-systems/specs/TwoPhaseCommit.safety
13-distributed-systems/specs/TwoPhaseCommit.safety  TwoPhaseCommit.safety.cfg  (Participants={flight, hotel})
parse ok
model check: no violation found in 85 distinct states (227 generated, depth 9)
expected outcome met: pass
coverage: all 9 actions fired
witness AllCommitted reached (trace of 8 states)
witness StuckPrepared reached (trace of 3 states)

Reveal for commit b

No violation of Consistent in 85 distinct states, and the witness StuckPrepared is reached: TLC did visit the blocked states, and in none of them do two participants disagree. Two-phase commit is safe when its coordinator crashes. It is not live.

03Reason

If you picked "Consistent is violated", you expected a crash to do damage. It does something quieter, and for the person holding the hotel room, worse: nothing, for ever.

Read the deadlock as a story. Both participants are working. One votes yes, and maybe the other does too. The coordinator crashes before it has written a decision. Now look at a participant that voted yes. It may not commit: it has no idea whether the other participant voted yes. It may not abort either.

Write something before openingthe flight voted yes and the coordinator is down. Why can't the flight give up after a while and abort?

Because it cannot tell a dead coordinator from a slow one. A slow coordinator may already have counted every yes, written "committed", and sent the commit message, which the hotel has read. If the flight aborts now, the hotel holds a room for a trip with no seat. The yes vote was a promise to do whatever the coordinator decides, and a promise you can withdraw when it gets inconvenient is not a promise.

Write something before openingthe spec has an action Finished that changes nothing at all. Why does it need one?

Without it, a booking where everyone has decided would end, sooner or later, in a state with no successor, and TLC would report it as a deadlock, as it reports the blocked state. You could not tell the happy ending from the stuck one. TLC's deadlock check asks one question of each state: does Next allow a step out of it? Finished gives a decided state such a step, so the check no longer fires there. The behavior still ends by stuttering, as every finished behavior does (chapter 09). Now every deadlock TLC reports is a state where somebody is still waiting.

So the participant waits. This is the sense in which two-phase commit blocks: a participant that voted yes can neither finish nor give up until the coordinator answers. Nothing in the protocol is broken. The blocked state is the price of the promise.

Now separate two claims that people say in one breath. Consistent is a safety property: a finite trace can break it, by reaching a bad state. The safety cfg finds no such state, crash or no crash. "Every participant eventually decides" is a liveness property: no finite trace can break it, because the decision might always come later. TwoPhaseCommit.live.cfg checks it, as EveryoneDecides, under fairness for every step except the crash.

shell
./tla check chapters/13-distributed-systems/specs/TwoPhaseCommit.live.cfg
Output 13-distributed-systems/specs/TwoPhaseCommit.live
13-distributed-systems/specs/TwoPhaseCommit.live  TwoPhaseCommit.live.cfg  (Participants={flight, hotel})
parse ok
model check: temporal property EveryoneDecides violated; trace of 4 states, then stuttering forever; 85 distinct states
expected outcome met: property EveryoneDecides
trace (changed variables only; ./tla trace --full shows all):
  1  Init     part = (flight :> "working" @@ hotel :> "working"), coord = "collecting", yes = {}, up = TRUE, net = {}
  2  Refuse   part: (flight :> "working" @@ hotel :> "working") -> (flight :> "working" @@ hotel :> "aborted")
  3  Crash    up: TRUE -> FALSE
  4  VoteYes  part: (flight :> "working" @@ hotel :> "aborted") -> (flight :> "prepared" @@ hotel :> "aborted"), net: {} -> {[kind |-> "yes", from |-> flight]}
     (stutter)  nothing changes from here on, forever

The counterexample is a lasso that ends in stuttering: a few steps, then nothing changes, for ever.

Write something before openingthe spec has weak fairness on LearnCommit and LearnAbort. Why doesn't fairness force the waiting participant to learn something?

Weak fairness says: if an action stays enabled, it eventually happens. In the blocked state, LearnCommit and LearnAbort are not enabled at all, because no decision was ever sent. Fairness cannot make a step happen that the spec does not allow. The only action that could unblock the participant is a coordinator step, and the coordinator is down for good.

Is the crash really the cause? Run the control experiment, TwoPhaseCommit.nocrash.cfg: the same protocol and the same fairness, with no Crash in its next-state relation. TLC finds no violation of Consistent or EveryoneDecides there, in 56 distinct states.

So here is the honest sentence about two-phase commit, the one you can defend in a design review. With these two participants and these steps, TLC found no state where participants disagree, even when the coordinator crashes. If the coordinator can crash and stay down, some behaviors leave a participant waiting for ever. The Core exercise shows that everyone decides when a coordinator that crashes at most once is always restarted. "This 2PC model with two participants is safe" is a claim you checked. "2PC finishes" is a claim about the operations team that restarts the coordinator. It goes in the design doc as an assumption, with a name.

That is the chapter's habit in one move: check the safety you can check, and name the assumption the liveness rests on. Protocols that avoid this blocking exist. Three-phase commit adds a round and assumes bounded delays; commit built on consensus replaces the single coordinator with a group that agrees, which is the next lesson.

04In your editor

The models are in chapters/13-distributed-systems/specs/. Check this lesson's exercises with:

shell
./tla check 13-01

./tla check 13-01 also runs every model this page quotes. Read the exercise blocks first; the others are the runs above.

When a deadlock trace is long, read it from the end. The last state names who is waiting; walk up until you find the step that made the wait permanent. In this model it is the Crash that happens while the coordinator is still collecting, or a yes vote cast after it.

05Practice

Warm-up. A copy without crashes, and without any way for the coordinator to give up. One refusal blocks everybody. Write Abort.

Core. Bring the coordinator back. Write Recover, decide what survives a crash, and choose the model on which the cfg's fixed safety and liveness checks run.

13-01-ex1Warm-up5 min

13-01-ex1 · Let the coordinator give up

Tier: warm-up. Time: about 5 minutes.

This copy of two-phase commit has no crash: the coordinator never fails. It also has no way to abort. Abort is a placeholder that never fires.

  1. Run ./tla check 13-01-ex1. The starter reports a deadlock. Read the trace and say, in one sentence, who is waiting for what. Nobody crashed.
  2. Fill gap g1 in TwoPhaseCommit.tla: until it has decided, the coordinator may give up. It records "aborted" and announces the abort to everyone.
  3. Run the check again until it passes, with every action firing and every witness reached.

Before you run the check again, decide what your Abort must not do. Then break it on purpose: drop one guard, run, and read what TLC says.

The lesson's specs/TwoPhaseCommit.tla has an Abort of its own; write yours before you open it.

Hints: HINTS.md, under "13-01-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/13-distributed-systems/exercises/01-ex1-let-the-coordinator-give-up/; open it after you have a passing answer of your own.

If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.

Done when: ./tla check 13-01-ex1 prints "goal met: pass".

./tla check 13-01-ex1

Repo chapters/13-distributed-systems/exercises/01-ex1-let-the-coordinator-give-up/

Hints

Hint 1

Read the deadlock trace from the end. Which participant refused, which one voted yes, and what is the coordinator still doing? Nobody can tell the yes voter anything.

Hint 2

Abort is the coordinator's step, like Commit. It needs a guard that says the coordinator has not decided yet, a new value for its decision, and a message to everyone. Look at Commit for the shape of each part.

Hint 3 (nearly the answer)

Guard on the coordinator's decision still being "collecting", set the decision to "aborted", and add one abort record to net, with the same field name that Commit's record uses. The participants and the counted votes stay as they are.

13-01-ex2Core8 min

13-01-ex2 · Bring the coordinator back

Tier: core. Time: about 8 minutes.

In the lesson the coordinator crashes and never returns, and the participants that voted yes wait forever. Real coordinators get restarted. CoordinatorRecovery.tla lets the coordinator crash once; your job is to bring it back, and to write the cfg that checks the result.

  1. Run ./tla check 13-01-ex2. The grader reports a deadlock: read its trace, and say what state the coordinator is in at the end. Your cfg stops earlier, with an error: a constant has no value.
  2. Fill gap g1 in CoordinatorRecovery.tla. Recover restarts the coordinator. Before you write it, decide which variables were on disk, and so survive, and which were in memory, and so are lost. The comments on the variables say which is which.
  3. Fill gap g1 in CoordinatorRecovery.cfg. Its checks are fixed below the gap; you write the model they run on. The module defines two behavior specs. Decide which one a liveness check needs, and why, then give the participants. Leave deadlock checking on.
  4. Run the check until both cfgs pass.

CoordinatorRecovery.grader.cfg is the course's check of your Recover: it runs the same properties whatever your cfg says. Leave it as it is. Nothing checks the participants you give your own cfg: one participant passes too, and checks nothing, since Consistent needs two to disagree.

Then try the tempting wrong answer: let Recover start over with coord back at "collecting". Predict what TLC will say before you run it.

Hints: HINTS.md, under "13-01-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/13-distributed-systems/exercises/01-ex2-bring-the-coordinator-back/; open it after you have a passing answer of your own.

Both cfgs must meet their goal, "goal met: pass", with every action firing and CommittedAfterCrash reached. If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.

Done when: ./tla check 13-01-ex2 prints "goal met: pass".

./tla check 13-01-ex2

Repo chapters/13-distributed-systems/exercises/01-ex2-bring-the-coordinator-back/

Hints

Hint 1

Read Crash first. Which variables does it change, and which does it leave alone? A restart undoes only part of a crash.

Hint 2

The comments on the variables say that the decision coord is on disk and the counted votes yes are in memory only, so Crash already emptied them. crashed records that the model's one crash has happened; a restart does not undo history.

Hint 3 (nearly the answer)

Recover is enabled while the coordinator is down and sets up back to TRUE. The decision on disk and the messages stay as they are; decide for yourself what crashed should say after a restart. In the cfg, a liveness check needs the spec that includes the fairness, and the same two participants as the grader.

06Explain it

Open NOTES.md and answer the three prompts under 13-01, about the Core exercise.

output
What I built, and why it is correct:
Which assumption, if false, breaks it:
What TLC did not check here:

Compare your answers with "13-01 check your explanation" in HINTS.md.

After you writeCheck your explanation

A good answer: "Recover brings the coordinator back with the decision it wrote to disk and without the votes it held in memory, so a coordinator that had already decided has nothing to redo, because its announcement is still in the network, and one that had not decided counts the votes again from the messages. It assumes the decision really is durable before it is announced, and that somebody restarts the coordinator, which is what WF_vars(Recover) states. TLC found no violation of Consistent or EveryoneDecides with two participants and at most one crash; it did not check repeated crashes, participant crashes, or a disk that loses the decision."

A weak answer: "I made the coordinator come back up, and now everyone decides." It says nothing about what survives the crash, which is the whole exercise, and does not name the fairness assumption the liveness result rests on.

Done?