07-02
Chapter 07 · PlusCal: labels are atomicity
Labels are steps
00Recall
One card from 07-01 on the disjunct PlusCal adds, one from chapter 00 on states versus interleavings, and one from chapter 02 on what a primed variable means. You will need the last one to read today's translation.
07-01-a from Read the translation first
What does the PlusCal translator add to Next that a hand-written pc spec usually lacks, and why?
Show answer
A Terminating disjunct: once every process's pc is "Done", a step that changes nothing. It gives the final state a successor, so TLC does not report the normal end as a deadlock.
00-02-a from Every interleaving
What is the difference between a state and an interleaving?
Show answer
A state is a snapshot of every variable's value. An interleaving is one order of all the steps of a run; running it passes through a sequence of states. Many interleavings can pass through the same state.
02-01-a from Primes
In an action, what does a primed variable such as hour' stand for?
Show answer
The variable's value in the new state of a step. The unprimed hour is its value in the old state. The action is a true-or-false claim about that pair of states, not an instruction.
Skip recall Recall is never required.
01Commit
Two web servers count page hits in one shared counter, hits. The real code makes two database calls per request: read the counter, then write the new value. The model, specs/HitCounter.tla, gives each call its own label:
Read: while served < Requests do
seen := hits;
Write: hits := seen + 1;
served := served + 1;
end while;A label is a name followed by a colon. For the first question, each server handles one request (Requests = 1).
Commit a
Two servers, one request each, labels Read and Write as shown. Which final values of hits can a complete run end with?
- Only 2: each server adds one.
- 1 or 2.
- Only 1: two servers on one counter always lose a hit.
Answer 1 or 2.
- If you picked A
- Each server does add one to the value it read. But the read and the write are separate steps, and the other server can run in between. If both read 0, both write 1.
- If you picked C
- A race needs a bad order of steps, and only some orders are bad. If one server finishes its Write before the other does its Read, the second server reads 1 and writes 2.
Now two requests per server (Requests = 2). A step is one move from one state to the next. Count the steps of one complete run, from the start until both servers have left the loop.
Commit b
Two servers, two requests each. How many steps does one complete run take, from the initial state until both servers are done?
Your answer is a whole number.
Answer 10
- Exactly right
- Right. Each label a server executes is one step, and leaving the loop takes one more.
- Within 10 %
- Within 10 %. If you were one over, you counted states, not steps: the initial state comes before any step. If you were one under, you forgot one server's loop exit.
- Under by more than half
- Under by more than half. You probably counted one step per request, as if the read and the write were one step. Each label executed is one step.
- More than double
- More than double. You probably counted orderings of the steps, the way chapter 00 warned against counting orderings as states. Every run, whatever its order, takes the same number of steps; the orderings only change which states it passes through.
- Anything else
- If you were two under, you forgot that leaving a while loop is a step: the label in front of the loop runs once more, finds the condition false, and moves on. Otherwise count per server: steps per request, times requests, plus the exit.
02Lab
TLC found no violation
This is a teaching view. Real checking happens with TLC on your machine.
Text view: every state and step
| State | hits | seen | pc | served | Steps (action → state) |
|---|---|---|---|---|---|
| initial 1 | 0 | (s1 :> 0 @@ s2 :> 0) | (s1 :> "Read" @@ s2 :> "Read") | (s1 :> 0 @@ s2 :> 0) | Read(s1) → 2; Read(s2) → 3 |
| 2 | 0 | (s1 :> 0 @@ s2 :> 0) | (s1 :> "Write" @@ s2 :> "Read") | (s1 :> 0 @@ s2 :> 0) | Write(s1) → 4; Read(s2) → 5 |
| 3 | 0 | (s1 :> 0 @@ s2 :> 0) | (s1 :> "Read" @@ s2 :> "Write") | (s1 :> 0 @@ s2 :> 0) | Read(s1) → 5; Write(s2) → 6 |
| 4 | 1 | (s1 :> 0 @@ s2 :> 0) | (s1 :> "Read" @@ s2 :> "Read") | (s1 :> 1 @@ s2 :> 0) | Read(s1) → 7; Read(s2) → 8 |
| 5 | 0 | (s1 :> 0 @@ s2 :> 0) | (s1 :> "Write" @@ s2 :> "Write") | (s1 :> 0 @@ s2 :> 0) | Write(s1) → 9; Write(s2) → 10 |
| 6 | 1 | (s1 :> 0 @@ s2 :> 0) | (s1 :> "Read" @@ s2 :> "Read") | (s1 :> 0 @@ s2 :> 1) | Read(s1) → 11; Read(s2) → 12 |
| 7 | 1 | (s1 :> 0 @@ s2 :> 0) | (s1 :> "Done" @@ s2 :> "Read") | (s1 :> 1 @@ s2 :> 0) | Read(s2) → 13 |
| 8 | 1 | (s1 :> 0 @@ s2 :> 1) | (s1 :> "Read" @@ s2 :> "Write") | (s1 :> 1 @@ s2 :> 0) | Read(s1) → 13; Write(s2) → 14 |
| 9 | 1 | (s1 :> 0 @@ s2 :> 0) | (s1 :> "Read" @@ s2 :> "Write") | (s1 :> 1 @@ s2 :> 0) | Read(s1) → 15; Write(s2) → 16 |
| 10 | 1 | (s1 :> 0 @@ s2 :> 0) | (s1 :> "Write" @@ s2 :> "Read") | (s1 :> 0 @@ s2 :> 1) | Write(s1) → 16; Read(s2) → 17 |
| 11 | 1 | (s1 :> 1 @@ s2 :> 0) | (s1 :> "Write" @@ s2 :> "Read") | (s1 :> 0 @@ s2 :> 1) | Write(s1) → 18; Read(s2) → 19 |
| 12 | 1 | (s1 :> 0 @@ s2 :> 0) | (s1 :> "Read" @@ s2 :> "Done") | (s1 :> 0 @@ s2 :> 1) | Read(s1) → 19 |
| 13 | 1 | (s1 :> 0 @@ s2 :> 1) | (s1 :> "Done" @@ s2 :> "Write") | (s1 :> 1 @@ s2 :> 0) | Write(s2) → 20 |
| 14 | 2 | (s1 :> 0 @@ s2 :> 1) | (s1 :> "Read" @@ s2 :> "Read") | (s1 :> 1 @@ s2 :> 1) | Read(s1) → 20; Read(s2) → 21 |
| 15 | 1 | (s1 :> 0 @@ s2 :> 0) | (s1 :> "Done" @@ s2 :> "Write") | (s1 :> 1 @@ s2 :> 0) | Write(s2) → 22 |
| 16 | 1 | (s1 :> 0 @@ s2 :> 0) | (s1 :> "Read" @@ s2 :> "Read") | (s1 :> 1 @@ s2 :> 1) | Read(s1) → 22; Read(s2) → 23 |
| 17 | 1 | (s1 :> 0 @@ s2 :> 0) | (s1 :> "Write" @@ s2 :> "Done") | (s1 :> 0 @@ s2 :> 1) | Write(s1) → 23 |
| 18 | 2 | (s1 :> 1 @@ s2 :> 0) | (s1 :> "Read" @@ s2 :> "Read") | (s1 :> 1 @@ s2 :> 1) | Read(s1) → 24; Read(s2) → 25 |
| 19 | 1 | (s1 :> 1 @@ s2 :> 0) | (s1 :> "Write" @@ s2 :> "Done") | (s1 :> 0 @@ s2 :> 1) | Write(s1) → 25 |
| 20 | 2 | (s1 :> 0 @@ s2 :> 1) | (s1 :> "Done" @@ s2 :> "Read") | (s1 :> 1 @@ s2 :> 1) | Read(s2) → 26 |
| 21 | 2 | (s1 :> 0 @@ s2 :> 1) | (s1 :> "Read" @@ s2 :> "Done") | (s1 :> 1 @@ s2 :> 1) | Read(s1) → 26 |
| 22 | 1 | (s1 :> 0 @@ s2 :> 0) | (s1 :> "Done" @@ s2 :> "Read") | (s1 :> 1 @@ s2 :> 1) | Read(s2) → 27 |
| 23 | 1 | (s1 :> 0 @@ s2 :> 0) | (s1 :> "Read" @@ s2 :> "Done") | (s1 :> 1 @@ s2 :> 1) | Read(s1) → 27 |
| 24 | 2 | (s1 :> 1 @@ s2 :> 0) | (s1 :> "Done" @@ s2 :> "Read") | (s1 :> 1 @@ s2 :> 1) | Read(s2) → 28 |
| 25 | 2 | (s1 :> 1 @@ s2 :> 0) | (s1 :> "Read" @@ s2 :> "Done") | (s1 :> 1 @@ s2 :> 1) | Read(s1) → 28 |
| 26 | 2 | (s1 :> 0 @@ s2 :> 1) | (s1 :> "Done" @@ s2 :> "Done") | (s1 :> 1 @@ s2 :> 1) | Terminating → self |
| 27 | 1 | (s1 :> 0 @@ s2 :> 0) | (s1 :> "Done" @@ s2 :> "Done") | (s1 :> 1 @@ s2 :> 1) | Terminating → self |
| 28 | 2 | (s1 :> 1 @@ s2 :> 0) | (s1 :> "Done" @@ s2 :> "Done") | (s1 :> 1 @@ s2 :> 1) | Terminating → self |
Open branches until you have found a leaf with each final value of hits. Then find the first step where the two branches part ways, and say which server did what. For the second question, HitCounter.finish.cfg breaks a claim on purpose, as in 03-03: NeverFinished says no run ever finishes, so TLC prints a complete run.
./tla check chapters/07-pluscal/specs/HitCounter.finish.cfg07-pluscal/specs/HitCounter.finish07-pluscal/specs/HitCounter.finish HitCounter.finish.cfg (Servers={s1, s2}, Requests=2)
PlusCal translated (translation up to date)
parse ok
model check: invariant NeverFinished violated; trace has 11 states (10 steps); 130 distinct states
expected outcome met: violation NeverFinished
trace (changed variables only; ./tla trace --full shows all):
1 Init pc = (s1 :> "Read" @@ s2 :> "Read"), hits = 0, seen = (s1 :> 0 @@ s2 :> 0), served = (s1 :> 0 @@ s2 :> 0)
2 Read pc: (s1 :> "Read" @@ s2 :> "Read") -> (s1 :> "Write" @@ s2 :> "Read")
3 Write pc: (s1 :> "Write" @@ s2 :> "Read") -> (s1 :> "Read" @@ s2 :> "Read"), hits: 0 -> 1, served: (s1 :> 0 @@ s2 :> 0) -> (s1 :> 1 @@ s2 :> 0)
4 Read pc: (s1 :> "Read" @@ s2 :> "Read") -> (s1 :> "Write" @@ s2 :> "Read"), seen: (s1 :> 0 @@ s2 :> 0) -> (s1 :> 1 @@ s2 :> 0)
5 Write pc: (s1 :> "Write" @@ s2 :> "Read") -> (s1 :> "Read" @@ s2 :> "Read"), hits: 1 -> 2, served: (s1 :> 1 @@ s2 :> 0) -> (s1 :> 2 @@ s2 :> 0)
6 Read pc: (s1 :> "Read" @@ s2 :> "Read") -> (s1 :> "Done" @@ s2 :> "Read")
7 Read pc: (s1 :> "Done" @@ s2 :> "Read") -> (s1 :> "Done" @@ s2 :> "Write"), seen: (s1 :> 1 @@ s2 :> 0) -> (s1 :> 1 @@ s2 :> 2)
8 Write pc: (s1 :> "Done" @@ s2 :> "Write") -> (s1 :> "Done" @@ s2 :> "Read"), hits: 2 -> 3, served: (s1 :> 2 @@ s2 :> 0) -> (s1 :> 2 @@ s2 :> 1)
9 Read pc: (s1 :> "Done" @@ s2 :> "Read") -> (s1 :> "Done" @@ s2 :> "Write"), seen: (s1 :> 1 @@ s2 :> 2) -> (s1 :> 1 @@ s2 :> 3)
10 Write pc: (s1 :> "Done" @@ s2 :> "Write") -> (s1 :> "Done" @@ s2 :> "Read"), hits: 3 -> 4, served: (s1 :> 2 @@ s2 :> 1) -> (s1 :> 2 @@ s2 :> 2)
11 Read pc: (s1 :> "Done" @@ s2 :> "Read") -> (s1 :> "Done" @@ s2 :> "Done")Reveal for commit a
Both. A run ends with hits = 2 when one server finishes its Write before the other does its Read, and with hits = 1 when both servers read 0 first. With Requests = 2, HitCounter.cfg checks AllCounted and TLC reports a lost hit after a trace of 11 states.
Reveal for commit b
10 steps, in a trace of 11 states. Every complete run has that length, whatever the order, so the shortest one TLC prints is any of them.
03Reason
If you picked "only 2", you read seen := hits and hits := seen + 1 as one increment. In the code they are two database calls, and the labels say so. Here is the rule that makes PlusCal useful: a step runs from one label to the next. Everything between two labels happens in one atomic step, which no other process can interrupt. A label marks a place where another process may get a turn.
Picture each process walking down its own corridor, and the labels as doors. Between two doors you walk alone: nobody can step in, and nobody sees you halfway. At a door, anyone may cut in front of you, as often as the schedule allows. The corridor does not say how long the walk takes or who goes first. It says only where cutting in is possible, and TLC tries every way of doing it.
Read the translation of Write:
Write(self) is defined as and pc[self] = "Write"
and hitsprime = seen[self] + 1
and servedprime = [served EXCEPT ![self] = served[self] + 1]
and pcprime = [pc EXCEPT ![self] = "Read"]
and seenprime = seenTwo assignments, one action. Each primed variable is a value in the next state: that is the chapter 02 meaning of a prime, and it is why everything inside one label happens at once. Inside one label the statements still run in order, and the translator writes the result as one action. A right-hand side reads the current state, unless an earlier statement in the same label already assigned that variable; then the translator writes the primed value. L: a := a + 1; b := a; becomes a' = a + 1 /\ b' = a', so b gets the new a. In Write the two assignments touch different variables, so each one reads the current state.
Write something before openingthe while loop belongs to the Read label. What does one Read step do when served = Requests?
It tests the condition, finds it false, and moves pc to "Done". The translation of Read is an IF: the THEN branch reads hits and goes to Write, the ELSE branch goes to "Done". Both branches are the same action. That is the step you may have left out of your count: leaving the loop costs one step.
So the step count is arithmetic on labels. Each request runs Read once and Write once, and each server runs Read once more to leave the loop: per server, twice the requests plus one, and twice that for two servers. With Requests = 2 that is 10, and TLC's trace has 10.
Why must the while have a label at all? Delete Read: and see. specs/HitCounterUnlabelled.tla is HitCounter without it:
07-pluscal/specs/HitCounterUnlabelled07-pluscal/specs/HitCounterUnlabelled HitCounterUnlabelled.cfg (Servers={s1, s2}, Requests=2)
parse error: PlusCal: -- Missing label at the following location: line 20, column 10.
expected outcome met: parse-error Missing labelThe translator refuses before SANY runs. This while is also the process's first statement, which needs a label under a second rule, but a while needs one wherever it stands. A loop jumps back to its test, and the translator needs a value of pc that means "at the test". The label is that value.
Those rules are about translation. The choice that matters is yours: where you put labels is where you claim the real system can be interrupted. Put one between the read and the write, and TLC explores every run in which the other server sneaks in. Leave it out, and the model promises an atomic increment the database never gave you.
Write something before openingthe update of served sits inside the Write step. Would giving it a label of its own add a race?
No. served is the server's own local variable, and no other process reads or writes it. A race needs two processes touching the same thing, with a decision in between. A label in front of that update would add states and interleavings for TLC to explore, and no new way to go wrong. Labels around purely local work cost state space and buy nothing. Labels around every touch of shared state are where the bugs live.
Write something before openingsuppose the database offered one call that adds one to the counter in place. How would the model change, and what would TLC say?
The read and the write would sit under one label, since one call is one atomic step, and no other server could get between them. That is exactly the Core exercise's starter, LikeCounter.tla, and TLC finds no violation in it. The model is right for that database and wrong for the code you have, which makes two calls. A label is a claim about the real system, and the starter's claim is false.
Write something before openingthe lab shows more leaves than distinct final states. Is the tree the state space?
No. A leaf is the end of one interleaving, and many interleavings end in the same state. TLC stores each state once: 28 distinct states for this model. The tree is the set of runs; the state graph is the much smaller set of places those runs pass through.
04In your editor
The specs are in chapters/07-pluscal/specs/. Check this lesson's exercises with:
./tla check 07-02The lost update, with two requests per server:
07-pluscal/specs/HitCounter07-pluscal/specs/HitCounter HitCounter.cfg (Servers={s1, s2}, Requests=2)
PlusCal translated (translation up to date)
parse ok
model check: invariant AllCounted violated; trace has 11 states (10 steps); 131 distinct states
expected outcome met: violation AllCounted
trace (changed variables only; ./tla trace --full shows all):
1 Init pc = (s1 :> "Read" @@ s2 :> "Read"), hits = 0, seen = (s1 :> 0 @@ s2 :> 0), served = (s1 :> 0 @@ s2 :> 0)
2 Read pc: (s1 :> "Read" @@ s2 :> "Read") -> (s1 :> "Write" @@ s2 :> "Read")
3 Write pc: (s1 :> "Write" @@ s2 :> "Read") -> (s1 :> "Read" @@ s2 :> "Read"), hits: 0 -> 1, served: (s1 :> 0 @@ s2 :> 0) -> (s1 :> 1 @@ s2 :> 0)
4 Read pc: (s1 :> "Read" @@ s2 :> "Read") -> (s1 :> "Write" @@ s2 :> "Read"), seen: (s1 :> 0 @@ s2 :> 0) -> (s1 :> 1 @@ s2 :> 0)
5 Read pc: (s1 :> "Write" @@ s2 :> "Read") -> (s1 :> "Write" @@ s2 :> "Write"), seen: (s1 :> 1 @@ s2 :> 0) -> (s1 :> 1 @@ s2 :> 1)
6 Write pc: (s1 :> "Write" @@ s2 :> "Write") -> (s1 :> "Read" @@ s2 :> "Write"), hits: 1 -> 2, served: (s1 :> 1 @@ s2 :> 0) -> (s1 :> 2 @@ s2 :> 0)
7 Read pc: (s1 :> "Read" @@ s2 :> "Write") -> (s1 :> "Done" @@ s2 :> "Write")
8 Write pc: (s1 :> "Done" @@ s2 :> "Write") -> (s1 :> "Done" @@ s2 :> "Read"), served: (s1 :> 2 @@ s2 :> 0) -> (s1 :> 2 @@ s2 :> 1)
9 Read pc: (s1 :> "Done" @@ s2 :> "Read") -> (s1 :> "Done" @@ s2 :> "Write"), seen: (s1 :> 1 @@ s2 :> 1) -> (s1 :> 1 @@ s2 :> 2)
10 Write pc: (s1 :> "Done" @@ s2 :> "Write") -> (s1 :> "Done" @@ s2 :> "Read"), hits: 2 -> 3, served: (s1 :> 2 @@ s2 :> 1) -> (s1 :> 2 @@ s2 :> 2)
11 Read pc: (s1 :> "Done" @@ s2 :> "Read") -> (s1 :> "Done" @@ s2 :> "Done")When you are unsure where the steps of an algorithm are, do not guess from the indentation. Read the translation: find the operators whose first conjunct is a guard such as pc[self] = "Read", one per label, and read each one's update of pc. The translation is short, and it is what TLC checks.
05Practice
Warm-up. A label printer's loop that the translator refuses. Give it the label it asks for.
Core. A like counter whose model says a like is one step, while the code makes two database calls. Change the model to match the code, and let TLC show what the atomic version was hiding.
07-02-ex1Warm-up5 min
07-02-ex1 · Label the loop
Tier: warm-up. Time: about 5 minutes.
Two packing stations print shipping labels from one shared roll. The algorithm in LabelRoll.tla looks complete, but the translator refuses it.
- Run
./tla check 07-02-ex1. Read the message: which line and column does it name, and what statement is there? - Fill gap g1: give that statement the label it needs, named
Print. Delete the\* GAPmarker. - Run the check again. Then read the translation: how many actions does it have, and which statements ended up inside
Print?
Before you run it, predict: is Accounted (every label is still on the roll or printed) safe with the whole loop body in one label? Why?
The cfg's @coverage line names Print, so the label must have exactly that name.
Hints: HINTS.md, under "07-02-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/07-pluscal/exercises/02-ex1-label-the-loop/; open it after you have a passing answer of your own.
If TLC finds no violation but Print never fires or the witness RollEmpty is not reached, ./tla prints "goal not met" and exits with code 1.
Done when: ./tla check 07-02-ex1 prints "goal met: pass".
./tla check 07-02-ex1
Repo chapters/07-pluscal/exercises/02-ex1-label-the-loop/
Hints
Hint 1
The message names a line and a column. Count to that column: which keyword starts there?
Hint 2
A loop jumps back to its test, and the translator needs a value of pc that means "at the test". Only a label gives it one.
Hint 3 (nearly the answer)
Write Print: in front of the while, on the same line, and delete the \* GAP marker. Keep the indentation of the loop body as it is.
07-02-ex2Core8 min
07-02-ex2 · Two round trips
Tier: core. Time: about 8 minutes.
App servers record likes on one post in a shared database counter. The model in LikeCounter.tla says a like is one step, Like: likes := likes + 1. The code does not: it makes one call that reads likes, and a second call that writes the new value back.
- Run
./tla check 07-02-ex2. Both cfgs pass. The model is green because it promises an atomic increment. - Fill gap g1: make the model match the code, with one label per database call, named
ReadandWrite. Delete the\* GAPmarker. - Run the check again and read the counterexample. Which interleaving loses a like?
LikeCounter.solo.cfg is the course's check of your change: one server, on a post that already has Start = 2 likes. Nobody can race, so every like must still be counted, and a change that miscounts without a race fails there. Leave that file as it is.
Hints: HINTS.md, under "07-02-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/07-pluscal/exercises/02-ex2-two-round-trips/; open it after you have a passing answer of your own.
Done when: ./tla check 07-02-ex2 prints "2 of 2 checks met their expected outcome".
./tla check 07-02-ex2
Repo chapters/07-pluscal/exercises/02-ex2-two-round-trips/
Hints
Hint 1
Each label is one step. Count the database calls the real code makes for one like, and compare with the number of labels.
Hint 2
The process already has a local variable, seen, that nothing uses yet. The first step stores the counter there; the second writes the counter from it.
Hint 3 (nearly the answer)
Replace the Like line with two labelled lines, Read and Write. Each holds one assignment: Read assigns to seen, and Write assigns to likes.
06Explain it
Open NOTES.md and answer the three prompts under 07-02, about the Core exercise.
What I built, and why it is correct:
Which assumption, if false, breaks it:
What TLC did not check here:Compare your answers with "07-02 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "I split the like into Read and Write, one label per database call, because a label is where another server may run. With two servers, both can read the same value before either writes, so one like is lost, and TLC reports AllCounted violated; with one server it passes. The model assumes each call is atomic on its own: a database makes a single statement atomic, but another server can run between two separate calls unless a suitably isolated transaction wraps them. TLC checked two servers with one like each, and nothing about retries, transactions or a server that crashes between its calls."
A weak answer: "I added a label and now TLC finds a bug." It does not say why the label belongs there, or what the atomic model had promised that the code does not do.
Done?