13-04

Chapter 13 · Distributed systems, honestly

Leases without wall clocks

  • 44 min core
  • Core track
  • Lab: Trace Theater
  • Skills: hostile-env, abstraction

00Recall

One card from 13-03 (why two leaders can exist at once), one from 13-01 (what fairness cannot fix) and one from 10-02 (weak fairness, written out). The first one is the problem this lesson solves; the other two come back in the Mixed item.

13-03-a from Leader election, simplified

Why does a leader election over a real network allow two leaders at the same time?

Show answer

A leader of an old term keeps acting until a message from the newer term reaches it, and the network can delay that message. Without a bound on delay or clock drift, a protocol that can replace a leader it cannot reach can promise only "at most one leader per term".

13-01-b from 2PC and the coordinator crash

Under weak fairness on every step but the crash, TLC still finds a lasso where a participant never decides. Why can't fairness fix it?

Show answer

Weak fairness only forces steps that stay enabled, and in the blocked state no step that would help is enabled at all, so strong fairness would not help either. Liveness needs an assumption about recovery, stated as fairness on a Recover step.

10-02-a from Weak fairness from a starvation trace

Write WF_v(A) in terms of ENABLED, <<A>>_v, [] and <>, and say it in words.

Show answer

<>[](ENABLED <<A>>_v) => []<><<A>>_v. If an A step that changes v is possible at every moment from some point on, such steps happen infinitely often.

Skip recall Recall is never required.

01Commit

Lesson 13-03 ended with a leader that does not know it has been replaced. If two writers on one disk is a disaster, you need a stronger rule: nobody acts until the old holder has certainly stopped. The usual tool is a lease. A lock server grants one client the right to act alone for a fixed time. The server will not grant again until that time is up, and the client stops acting when its own copy of the time runs out.

TLA+ has no clock. Time is whatever variables you give it. specs/LeaseLock.tla gives each clock a countdown: serverLeft is how many ticks the server thinks are left, and clientLeft[c] is how many client c thinks it has. Tick is one tick of real time, seen by every clock. SlowTick is a tick that the server's clock sees and the clients' clocks miss, at most MaxDrift times per lease.

TLA+
SlowTick is defined as
    and serverLeft > 0
    and drift < MaxDrift
    and serverLeftprime = serverLeft - 1
    and driftprime = drift + 1
    and UNCHANGED tuple of holder, clientLeft end tuple

When a client gets the lease, it starts its own count Margin ticks short, to stay on the safe side. The claim is NoOverlap: never two clients with time left on their own counts.

Commit a

LeaseLock.cfg has two clients, Duration = 3, MaxDrift = 1 and Margin = 0. The server never grants while its own count is above zero. What does TLC report for NoOverlap?

  1. No violation. The server waits for the old lease to run out before it grants a new one.
  2. A violation: a client whose clock missed a tick is still acting when the other client gets the lease.
  3. Nothing useful: time goes on for ever, so TLC never finishes the search.

Answer A violation: a client whose clock missed a tick is still acting when the other client gets the lease.

Commit b

LeaseLock.margin.cfg is the same model with Margin = 1: one tick of margin for at most one tick of drift. What does ./tla report?

  1. No violation, every action fires, and ActingAfterDrift is reached: a client acts after its clock lost a tick and still stops in time.
  2. Still a violation. The margin has to be bigger than the drift, not equal to it.
  3. No violation, but ActingAfterDrift is not reached: with a margin, a client whose clock lost a tick never gets to act.

Answer No violation, every action fires, and ActingAfterDrift is reached: a client acts after its clock lost a tick and still stops in time.

02Lab

Lab · Trace TheaterWhat you’re looking at: the trace in which two clients act at once. Watch serverLeft and clientLeft side by side, and find the step where they stop agreeing.

NoOverlap violated

● 1● 23● 4567● 8910111213● 141516● 17

This is a teaching view. Real checking happens with TLC on your machine.

Trace

  1. State 1 Initial state holder = "none" clientLeft = (c1 :> 0 @@ c2 :> 0) drift = 0 serverLeft = 0
  2. State 2 Acquire(c1) holder "none" becomes c1 clientLeft (c1 :> 0 @@ c2 :> 0) becomes (c1 :> 3 @@ c2 :> 0) serverLeft 0 becomes 3
  3. State 3 Tick clientLeft (c1 :> 3 @@ c2 :> 0) becomes (c1 :> 2 @@ c2 :> 0) serverLeft 3 becomes 2
  4. State 4 Tick clientLeft (c1 :> 2 @@ c2 :> 0) becomes (c1 :> 1 @@ c2 :> 0) serverLeft 2 becomes 1
  5. State 5 SlowTick drift 0 becomes 1 serverLeft 1 becomes 0
  6. State 6 Acquire(c2) holder c1 becomes c2 clientLeft (c1 :> 1 @@ c2 :> 0) becomes (c1 :> 1 @@ c2 :> 3) drift 1 becomes 0 serverLeft 0 becomes 3
Text view: every state and step
States and their successors
StateholderserverLeftclientLeftdriftSteps (action → state)
initial 1"none"0(c1 :> 0 @@ c2 :> 0)0Acquire(c1) → 2; Acquire(c2) → 3
2c13(c1 :> 3 @@ c2 :> 0)0Tick → 4; SlowTick → 5; Release(c1) → 1
3c23(c1 :> 0 @@ c2 :> 3)0Tick → 6; SlowTick → 7; Release(c2) → 1
4c12(c1 :> 2 @@ c2 :> 0)0Tick → 8; SlowTick → 9; Release(c1) → 1
5c12(c1 :> 3 @@ c2 :> 0)1Tick → 9; Release(c1) → 10
6c22(c1 :> 0 @@ c2 :> 2)0Tick → 11; SlowTick → 12; Release(c2) → 1
7c22(c1 :> 0 @@ c2 :> 3)1Tick → 12; Release(c2) → 10
8c11(c1 :> 1 @@ c2 :> 0)0Tick → 13; SlowTick → 14; Release(c1) → 1
9c11(c1 :> 2 @@ c2 :> 0)1Tick → 14; Release(c1) → 10
10"none"0(c1 :> 0 @@ c2 :> 0)1Acquire(c1) → 2; Acquire(c2) → 3
11c21(c1 :> 0 @@ c2 :> 1)0Tick → 15; SlowTick → 16; Release(c2) → 1
12c21(c1 :> 0 @@ c2 :> 2)1Tick → 16; Release(c2) → 10
13c10(c1 :> 0 @@ c2 :> 0)0Acquire(c1) → 2; Acquire(c2) → 3
14c10(c1 :> 1 @@ c2 :> 0)1Acquire(c1) → 2; Acquire(c2) → 17
15c20(c1 :> 0 @@ c2 :> 0)0
16c20(c1 :> 0 @@ c2 :> 1)1
17c23(c1 :> 1 @@ c2 :> 3)0

Or in the terminal:

shell
./tla check chapters/13-distributed-systems/specs/LeaseLock.cfg
Expected output 13-distributed-systems/specs/LeaseLock
13-distributed-systems/specs/LeaseLock  LeaseLock.cfg  (Clients={c1, c2}, Duration=3, MaxDrift=1, Margin=0)
parse ok
model check: invariant NoOverlap violated; trace has 6 states (5 steps); 17 distinct states
expected outcome met: violation NoOverlap
trace (changed variables only; ./tla trace --full shows all):
  1  Init      holder = "none", serverLeft = 0, clientLeft = (c1 :> 0 @@ c2 :> 0), drift = 0
  2  Acquire   holder: "none" -> c1, serverLeft: 0 -> 3, clientLeft: (c1 :> 0 @@ c2 :> 0) -> (c1 :> 3 @@ c2 :> 0)
  3  Tick      serverLeft: 3 -> 2, clientLeft: (c1 :> 3 @@ c2 :> 0) -> (c1 :> 2 @@ c2 :> 0)
  4  Tick      serverLeft: 2 -> 1, clientLeft: (c1 :> 2 @@ c2 :> 0) -> (c1 :> 1 @@ c2 :> 0)
  5  SlowTick  serverLeft: 1 -> 0, drift: 0 -> 1
  6  Acquire   holder: c1 -> c2, serverLeft: 0 -> 3, clientLeft: (c1 :> 1 @@ c2 :> 0) -> (c1 :> 1 @@ c2 :> 3), drift: 1 -> 0

Reveal for commit a

NoOverlap is violated, with a trace of 6 states. In the last state the second client has a fresh lease, and the first one still has time left on its own count. Somewhere during the first lease, one tick reached the server's clock and missed the client's. The server's count hit zero while the client still believed it had time.

Then the same model with a margin:

shell
./tla check chapters/13-distributed-systems/specs/LeaseLock.margin.cfg
Expected output 13-distributed-systems/specs/LeaseLock.margin
13-distributed-systems/specs/LeaseLock.margin  LeaseLock.margin.cfg  (Clients={c1, c2}, Duration=3, MaxDrift=1, Margin=1)
parse ok
model check: no violation found in 16 distinct states (45 generated, depth 5)
expected outcome met: pass
coverage: all 4 actions fired
witness ActingAfterDrift reached (trace of 3 states)

Reveal for commit b

No violation of NoOverlap in 16 distinct states. Every action fires, and the witness ActingAfterDrift is reached: a client really did act after its clock lost a tick, and still stopped before the server granted the lease again.

03Reason

If you picked "no violation", you checked the server, and the server is fine. It never grants a lease while its own count is running. The trouble is that its count is not the one the client obeys.

Write something before openingthe server has a clock, and so does the client. Which clock decides when the client stops acting?

The client's. The client cannot ask the server "may I still act?" before every write; if it could, it would not need a lease. So a lease is a promise made in the server's time and kept in the client's time. Exclusivity needs the client's count to reach zero no later than the server's, whatever the clocks did in between.

Now derive the margin instead of guessing it. Say the client's clock loses d ticks during a lease, with d at most MaxDrift. When the server's count reaches zero, Duration ticks of real time have passed since the grant, and the client has seen only Duration - d of them. It started at Duration - Margin, so its count stands at (Duration - Margin) - (Duration - d), which is d - Margin. The client has stopped exactly when that is zero or less. The worst case is d = MaxDrift, so the rule is Margin >= MaxDrift. That is why the first model fails and the second passes: Margin = 0 against MaxDrift = 1, then Margin = 1 against the same drift.

Look at what SlowTick stands for. A cheap clock that runs slow is one cause. Another is a client that stops running for a moment: a long garbage-collection pause, a virtual machine that was suspended, a laptop lid that was closed. A paused client sees no ticks at all: it checked its count before the pause, and when it wakes it acts on that check. To the protocol, a pause is drift.

Write something before openingwhat if a pause can last any length of time? Which margin is enough then?

None. If the drift has no bound, d - Margin can be positive for any Margin you pick. A lease alone cannot keep a client that does not know it has stopped from acting. The fix moves the check to the resource being protected: every grant carries a number that grows, and the resource refuses a request whose number is older than one it has already seen. That is a fencing token. It is the same move as 13-03's terms, and 16-05 builds it.

Next, the choice of countdowns. It was a choice. You could give the spec a variable now that starts at zero and grows by one each tick, and stamp each lease with an expiry time. That reads more like code. It is also a different model.

That is the general trick for time in TLC: keep only the time that some decision depends on, measured relative to the event it depends on, and bounded. A timeout becomes "ticks left", a deadline becomes a countdown, and a clock that may be wrong becomes a second countdown that may lag.

04In your editor

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

shell
./tla check 13-04

When a lease trace fails, put the clocks side by side. Write each countdown in its own column and mark the step where they first disagree. That step is the assumption you made about time.

Then turn it on your own system. Name one timeout in it. What is it measured against, and what bounds the drift?

05Practice

Warm-up. A lease in a world without time. Tick is a placeholder, and nothing ever runs out. Write it.

Core. Leases that can be renewed. Write Renew and the model the cfg's checks run on, and find out which shortcut TLC catches.

The Mixed item, 13-mx, is on the mastery path: a lease server whose clocks are perfect, and a client that never gets a turn.

The AI item 13-ai2 is a renewable lease an assistant wrote, checked and described. Decide whether its claim is true and scoped to what was checked, starting with ./tla check 13-ai2. For every mutant that survives, you will have to say why.

13-04-ex1Warm-up5 min

13-04-ex1 · Make time pass

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

LeaseLock.tla is the lesson's lease with a one-tick margin, and with no time. Tick is a placeholder: only the slow server tick and a release can move anything.

  1. Run ./tla check 13-04-ex1. The starter passes with a coverage gap. A lease in a world without time never runs out, so another client gets the lock only when the holder hands it back; NoOverlap holds for the dullest reason there is.
  2. Fill gap g1 in LeaseLock.tla: one tick of real time passes, and every clock sees it. It is possible while any counter is above zero, and no counter goes below zero. The module defines Dec for that.
  3. Run the check until it passes with full coverage and both witnesses reached.

Then try two wrong versions, one at a time: tick only the server's counter, then only the clients'. For each one, predict what ./tla will report, and which line tells you.

The lesson's specs/LeaseLock.tla has a Tick of its own; write yours before you open it.

Hints: HINTS.md, under "13-04-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/13-distributed-systems/exercises/04-ex1-make-time-pass/; 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-04-ex1 prints "goal met: pass".

./tla check 13-04-ex1

Repo chapters/13-distributed-systems/exercises/04-ex1-make-time-pass/

Hints

Hint 1

Read the comment on Tick: one tick of real time, seen by every clock. Which counters are clocks, and what does Dec do at zero?

Hint 2

Every countdown goes down by one and stops at zero. The holder and the drift budget do not change. The tick is possible while at least one countdown is above zero.

Hint 3 (nearly the answer)

The guard is a disjunction over the server's count and the clients' counts. The new values use Dec: once for the server, and inside a function over Clients for the clients. Two variables stay unchanged.

13-04-ex2Core8 min

13-04-ex2 · Renew in time

Tier: core. Time: about 8 minutes.

A lease that cannot be renewed makes every holder start over from Acquire, and risks losing the lock to someone else in between. LeaseLock.tla has a Renew(c) action, and it is a placeholder.

  1. Run ./tla check 13-04-ex2. The grader passes with a coverage gap: Renew never fires. Your cfg stops with an error: a constant has no value.
  2. Fill gap g1 in LeaseLock.tla. The server agrees to a renewal only while its own count says that c holds a lease that has not run out. Both sides then start a fresh lease, the client Margin ticks short as before, and the drift budget starts again.
  3. Fill gap g1 in LeaseLock.cfg. Its checks are fixed below the gap; you write the model: the behavior spec, two clients, Duration = 3, MaxDrift = 1, and the smallest Margin that drift needs. The lesson derived it.
  4. Run the check until both cfgs pass with full coverage and ActingAfterDrift reached.

LeaseLock.grader.cfg is the course's check of your Renew. Beside NoOverlap it checks RenewIsAGrant, an action property about Renew itself. Leave it as it is. Then try two shortcuts and read what TLC says about each: a client that extends its own count without telling the server, and a renewal that forgets the margin.

Some mistakes no check here can see, so read your work against the steps above yourself. A Renew that keeps the used drift budget only takes slow ticks away from the renewed lease, and fewer slow ticks cannot break NoOverlap. Leave out the guard on serverLeft and no behavior changes, because a renewal at zero is the same step as an Acquire; only RenewIsAGrant, which reads your definition, notices. A Renew guarded only on c's own count also passes: with a big enough margin, a client with time left is always the holder of a running lease, but the server cannot see that count. And ./tla does not compare your constants with the ones asked for above: one client, or a margin bigger than needed, passes too. The redundant guards are the pattern 13-ai2 asks you to explain.

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

Both cfgs must meet their goal, "goal met: pass". 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-04-ex2 prints "goal met: pass".

./tla check 13-04-ex2

Repo chapters/13-distributed-systems/exercises/04-ex2-renew-in-time/

Hints

Hint 1

Renew looks a lot like Acquire. What is different about when the server may agree, and about what it does to holder?

Hint 2

The server agrees only if its own records say that c holds the lease and its own count is above zero. Then both countdowns start again, as in a grant, and the drift budget restarts.

Hint 3 (nearly the answer)

Guard on holder = c and on serverLeft being above zero. Set serverLeft to Duration and c's count to Duration - Margin, restart the drift budget, and leave holder unchanged. In the cfg, Margin is the one the lesson derived for MaxDrift = 1.

13-mxMixed15 min · mastery path

13-mx · Lease starvation

Tier: mixed. Time: about 15 minutes. Off the core clock: this is on the mastery path.

A lock server leases one lock to two clients, one at a time. Every clock keeps perfect time, and nothing in FairLease.tla can go wrong for safety. The complaint from production is different: "client c2 has not held the lock since Tuesday."

  1. Run ./tla check 13-mx. Both cfgs report that EveryClientGetsATurn is violated. Read the lasso in the FairLease.cfg block and say what the scheduler did forever, and why WF_vars(Next) let it.
  2. Fill gap g1 in FairLease.tla: the weakest fairness you can defend under which every client keeps getting a turn. You may not force a holder to release early: real clients release when they are done, or never.
  3. Run the check until both cfgs pass with AboutToExpire reached.

FairLease.grader.cfg is the course's check of your Fairness. It runs the same server with Releasers = {}: clients that never hand the lock back early. A fairness that gets turns going only by forcing a release has nothing to force there, and fails. Leave it as it is. Two things it cannot see, so check them yourself against the rule above: a conjunct on Release, which never fires there, and strong fairness where weak fairness would do.

For each conjunct of your Fairness, write one line in NOTES.md, under 13-04: what real mechanism makes that assumption true. A server that grants "to whoever asks first" does not make it true. What would?

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

Both cfgs must meet their goal, "goal met: pass". 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-mx prints "goal met: pass".

./tla check 13-mx

Repo chapters/13-distributed-systems/exercises/mx-lease-starvation/

Hints

Hint 1

Read the lasso. Who holds the lock in the loop? In which states of the loop could c2 acquire it, and does it stay possible?

Hint 2

Weak fairness forces an action only if it stays enabled. Acquire(c2) is enabled only at the moments when the lease is free, and somebody else takes it at once. Which kind of fairness is about an action that is enabled again and again?

Hint 3 (nearly the answer)

Strong fairness, for each client, on one of the three actions; and a weaker assumption that makes a held lease always run out. Decide which action gets which. Nothing about Release: that would force clients to give up early, and the grader's clients never do.

06Explain it

Open NOTES.md and answer the three prompts under 13-04, 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-04 check your explanation" in HINTS.md.

After you writeCheck your explanation

A good answer: "Renew is a grant to the current holder, allowed only while the server's own count says the lease is still running, and it restarts both countdowns, the client's Margin ticks short, and the drift budget. It relies on drift of at most MaxDrift ticks per lease, and on Margin >= MaxDrift; a client pause longer than the margin breaks it. TLC found no violation of NoOverlap with two clients, Duration = 3, MaxDrift = 1 and Margin = 1, with every action firing and ActingAfterDrift reached; message delay and fast clocks were not modelled."

A weak answer: "Renew gives the client more time, and TLC passed." It does not say when the server may agree, what the margin is for, or which timing assumption the pass depends on.

Done?