11-01
Chapter 11 · Refinement is implication
One system, three granularities
00Recall
One card from chapter 04 on the hour hand as the shadow of the minute clock, one on what TLC's distinct count measures, and one from chapter 08 on a network modelled as a set. All three come back today.
04-04-b from The frame-rate test and the shadow
The minute clock satisfies the hour clock's spec. What exactly was checked?
Show answer
That every behavior of the minute clock, with only hr kept (its shadow), is a behavior the hour clock's spec allows, stuttering included: Spec => HourSpec. Nothing about how hr lines up with min.
03-04-a from Reading the numbers
TLC reports more states generated than distinct states. Did it count something twice by mistake?
Show answer
No. Generated counts every successor it computed, including repeats; distinct counts each state once.
08-01-a from The network is a set
Why does a set of messages in flight model a network that reorders, without any reorder action?
Show answer
A set has no order, and the receive step may take any element of it, so every arrival order is a behavior of the spec.
Skip recall Recall is never required.
01Commit
Your team runs a key-value store. A write takes three messages: the client's put to a primary server, the primary's sync to a backup, and the backup's ack to the client. Someone asks: "Is that the same thing as store' = [store EXCEPT ![k] = v]?" Before you can answer, you need models at more than one level of detail. Here is the coarsest, specs/AtomicKV.tla, with two keys, two values and a model value NoVal for "never written":
Write(k, v) is defined as
and storeprime = [store EXCEPT ![k] = v]
Next is defined as there exists k in Keys, v in Vals : Write(k, v)Commit a
With Keys = {k1, k2} and Vals = {v1, v2}, how many distinct states does TLC report for AtomicKV?
Your answer is a whole number.
Answer 9
- Exactly right
- Right. Each key holds one of three things, NoVal, v1 or v2, and the two keys choose independently.
- Within 10 %
- Right. Each key holds one of three things, NoVal, v1 or v2, and the two keys choose independently.
- Under by more than half
- Under by more than half. You probably counted only the values a write can store and forgot the value each key starts with, NoVal.
- More than double
- More than double. You may have counted orders of writes, or behaviors. TLC counts each assignment of values to the variables once, however many paths reach it.
- Anything else
- If you said six, you added the two keys' choices instead of multiplying them: each choice for k1 can sit next to each choice for k2. Otherwise, write every state out; there are few enough.
Three models of the same store sit side by side in this chapter. AtomicKV: one step per write, no servers. TwoCopyKV, which you build in this lesson's Core exercise: a primary and a backup, a write step and a copy step, no messages and no client. ReplicatedKV: the three messages, a client that waits for its ack, and a backup that can crash and come back empty.
Commit b
Can the client be told "done" while the backup is behind the primary? Which of the three models can even state that question?
- All three: they model the same store.
- TwoCopyKV and ReplicatedKV: both have a backup.
- Only ReplicatedKV.
Answer Only ReplicatedKV.
- If you picked A
- A model can only answer questions about things it contains. AtomicKV has no client, no backup and no ack, so "the client was told done" is not a sentence about any of its states.
- If you picked B
- TwoCopyKV has a backup, but nobody in it is ever told anything. There is no client and no ack, so "told done" has no meaning there.
02Lab
Run the coarsest and the finest model:
./tla check chapters/11-refinement/specs/AtomicKV.cfg
./tla check chapters/11-refinement/specs/ReplicatedKV.cfg11-refinement/specs/ReplicatedKV11-refinement/specs/ReplicatedKV ReplicatedKV.cfg (Keys={k1, k2}, Vals={v1, v2}, NoVal=NoVal)
parse ok
model check: no violation found in 137 distinct states (265 generated, depth 11)
expected outcome met: pass
coverage: all 5 actions fired
witness BackupFull reached (trace of 8 states)Reveal for commit a
TLC reports 9 distinct states for AtomicKV: three choices for k1 times three for k2. The three-message model has 137, and the reference TwoCopyKV from the Core exercise has 49.
Reveal for commit b
Only ReplicatedKV has a client that is told anything. Its ack message is the "done". TwoCopyKV has a backup but no ack, and AtomicKV has neither. In the Warm-up you ask ReplicatedKV this question as an invariant, and it answers.
03Reason
If you picked "all three", you treated the coarse model as a smaller copy of the real one. It is not a smaller copy. It is a different set of questions. A model can answer a question only if the question is a sentence about the model's variables. AtomicKV has one variable, store, so every question it can answer is a question about a map from keys to values. "Can a key ever hold a value no client could write?" is such a question. "Was the client told?" is not.
Think of three maps of one city. A subway map shows which stations connect and nothing else: no streets, no distances. A street map shows every street but no traffic. A live traffic map shows every car. Ask the subway map how long the walk is, and it has no answer, not a wrong one. Each map is right about what it shows, and each one costs more to draw than the last.
Write something before openingwhich question can AtomicKV answer that ReplicatedKV answers only with more work?
"Does the store ever change two keys in one step?" In AtomicKV the answer is visible in the one action: never. In ReplicatedKV you have to look at every action and every message, because the store there is spread over a primary, a backup and the network. The coarse model answers its questions more cheaply, and its answers are easier to trust.
The cost is states. TLC stores every reachable state once, so the distinct count measures what it must keep; "states generated", which counts every successor TLC computed, duplicates included, is closer to the time it spends. Going from AtomicKV to TwoCopyKV adds a second map; going to ReplicatedKV adds a network and a client. Each level multiplies the count, because a variable that takes values independently of the others multiplies it. A variable tied to the others multiplies it by less, and the card further down shows a new action that adds no state at all. Chapter 12 measures that growth. Today, notice what the extra states buy: a question you could not ask before.
Which one would you write first? Usually the coarsest one that can state your question, and for a design review, usually the intent: it is short, everyone in the room can read it, and an argument about it is an argument about what the system is for. The fine model comes second, when someone asks "but what happens if the backup crashes between the sync and the ack?" That question names a backup, a sync and an ack, so only a model with all three can answer it. The opposite habit is a first model that copies every class in the code. It answers every question slowly and none of them clearly.
So the models are not rivals. The coarse one states the intent: what the store does, with every mechanism left out. The fine one states the design: how it does it. The chapter's question is whether the design keeps the intent. Lesson 11-02 turns that into a check TLC can run.
Write something before openingwhy does the crash add no states to TwoCopyKV?
A crash leaves an empty backup next to whatever the primary holds. In TwoCopyKV you reach every such state without crashing, by writing and never copying. The crash reaches no new state; it reaches old states by a new route. It adds behaviors, not states. In ReplicatedKV it does reach new states, such as an ack on its way to the client while the backup is empty, which no crash-free run can produce. A distinct count measures states. It does not measure how many different things a system can do.
04In your editor
The models are in chapters/11-refinement/specs/. Check this lesson's exercises with:
./tla check 11-01The three-message model, with every action firing and its witness reached:
11-refinement/specs/ReplicatedKV11-refinement/specs/ReplicatedKV ReplicatedKV.cfg (Keys={k1, k2}, Vals={v1, v2}, NoVal=NoVal)
parse ok
model check: no violation found in 137 distinct states (265 generated, depth 11)
expected outcome met: pass
coverage: all 5 actions fired
witness BackupFull reached (trace of 8 states)When you choose a granularity for your own system, start from the question, not from the code. Write the question as a sentence, list the variables the sentence mentions, and model those, plus whatever the actions need to change them. If two questions need two very different granularities, write two models. This chapter shows how to check that the finer one keeps the promises of the coarser one.
05Practice
Warm-up. Ask ReplicatedKV the question from the second COMMIT, as an invariant, and read TLC's answer.
Core. Build the middle granularity, TwoCopyKV, from a skeleton, and compare its distinct counts with and without the crash.
11-01-ex1Warm-up5 min
11-01-ex1 · Ask the fine model
Tier: warm-up. Time: about 5 minutes.
The lesson's question was: can the client be told "done" while the backup is behind the primary? Only ReplicatedKV can even state it. Now ask it.
- Run
./tla check 11-01-ex1. All three cfgs pass, because the starter'sAckMeansCopiedisTRUE. - Fill gap g1 in
ReplicatedKV.tla: whenever an ack is in flight to the client, the backup holds exactly what the primary holds. An ack is a message innetwhosetypeis"ack". - Run the check. Before you read the trace, write down which action you expect in its last step.
The check also runs two course tests of your claim, both in MCReplicatedKV.tla. In MCReplicatedKV.cfg the backup crashes only while no ack is in flight. There the answer is "never", so a claim that states the question must pass; a stricter claim, such as "the backup always equals the primary", fails. In MCReplicatedKV.early.cfg the backup never crashes, both copies start with every key written, and the backup acks a sync before it installs the copy. There the client is told "done" while the backup is behind but not empty, so a claim that states the question must fail; a weaker claim, such as "some key agrees" or "the backup is not empty", passes. The two tests catch those mistakes; they do not catch every claim that differs from the question. Leave the three MCReplicatedKV files as they are.
Hints: HINTS.md, under "11-01-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/11-refinement/exercises/01-ex1-ask-the-fine-model/; open it after you have an answer of your own.
All three cfgs must meet their goals: "goal met: violation AckMeansCopied" for ReplicatedKV.cfg and MCReplicatedKV.early.cfg, and "goal met: pass" for MCReplicatedKV.cfg. You are done when every cfg prints its "goal met" line and none prints "goal not met" or "starter state". The run's last line, "3 of 3 checks met their expected outcome", is no sign of that: the untouched starter prints it too. 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 11-01-ex1 prints "goal met: violation AckMeansCopied".
./tla check 11-01-ex1
Repo chapters/11-refinement/exercises/01-ex1-ask-the-fine-model/
Hints
Hint 1
The question has two parts: "the client is being told done" and "the backup holds nothing, or less than the primary". Which variable holds the first part?
Hint 2
"Told done" is an ack on its way: some message in net has type equal to "ack". The claim is an implication: if that is true, then the backup is a copy of the primary.
Hint 3 (nearly the answer)
Write AckMeansCopied as (\E m \in net : …) => …, with the message's type on the left and an equality between the two maps on the right. "The backup is not empty" on the right meets both goals too; "the backup always equals the primary", with no left side, fails the course's check.
11-01-ex2Core8 min
11-01-ex2 · Two copies
Tier: core. Time: about 8 minutes.
Build the middle granularity of the store: a primary and a backup, with no messages and no client. TwoCopyKV.tla names the variables, the three actions and a witness; the three action bodies are placeholders.
- Run
./tla check 11-01-ex2. Two cfgs report a deadlock in the initial state;MCTwoCopyKV.shape.cfgreports that the initial state violatesShapeEnabled, because no write can fire. Why does a spec whose every action isFALSEdeadlock at once? - Fill gaps g1 to g3. In
Write(k, v), the primary sets keyktovand the backup does not change. InCopy, when the two maps differ, the backup becomes a copy of the primary's whole map. InBackupCrash, a backup that holds something restarts empty. - Run the check until all three cfgs pass with every action firing and
BackupFullreached. - Compare the two distinct counts.
TwoCopyKV.cfghas the crash;MCTwoCopyKV.cfgruns the same model without it. The lesson's "TLC is lying to you" card says they are equal. Before you open its fold, write down which states a crash reaches and how a run with no crash reaches each of them.
Leave the three MCTwoCopyKV files as they are. MCTwoCopyKV.shape.cfg is the course's check that each of your actions changes exactly what step 2 says, and can fire whenever step 2 says it may.
Hints: HINTS.md, under "11-01-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/11-refinement/exercises/01-ex2-two-copies/; 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. All three cfgs must meet their goal, "goal met: pass". You are done when every cfg prints its "goal met" line and none prints "goal not met" or "starter state". The run's last line, "3 of 3 checks met their expected outcome", is no sign of that: the untouched starter prints it too.
Done when: ./tla check 11-01-ex2 prints "goal met: pass".
./tla check 11-01-ex2
Repo chapters/11-refinement/exercises/01-ex2-two-copies/
Hints
Hint 1
Each action changes one of the two maps and leaves the other alone. Write the change first, then the UNCHANGED, then the guard.
Hint 2
Write uses EXCEPT on the primary, as in AtomicKV. Copy sets the backup to the primary's whole map, not one key of it. BackupCrash sets the backup to Empty, which the module defines.
Hint 3 (nearly the answer)
The guards stop steps that change nothing: Copy only when backup # primary, BackupCrash only when backup # Empty. Without them every action still fires, but a stuttering step is not a copy or a crash.
06Explain it
Open NOTES.md and answer the three prompts under 11-01, 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 "11-01 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "TwoCopyKV has a write that changes one key of the primary, a copy that replaces the backup with the whole primary map when they differ, and a crash that empties a non-empty backup. It relies on a copy being one indivisible step; a real copy that can be interrupted halfway would need more states. TLC checked two keys and two values, with every action firing and BackupFull reached; it said nothing about messages, clients or acks, so it cannot answer whether a client is told 'done' too early."
A weak answer: "I built the middle model and the counts were the same." It does not say why the actions are right, and it reports the counts without the reason, behaviors versus states, that makes them interesting.
Done?