--------------------------- MODULE SpillWriteBehind ---------------------------
\* The hand-off of an evicted start point's blocks to the spill writer thread
\* (`src/serve/block_cache.rs`: `evict_start_inner`, `queue_spill`, `poll_spill_writes`,
\* `on_spill_block_written`, `commit_pending_spill`, `fail_pending_spill`, `abort_spill_claims`;
\* `src/spill/writer.rs`: `SpillWriter::push`, `cancel`, the writer loop).
\*
\* Two threads share the pool's blocks. The scheduler evicts a start, pins the blocks its spill has to
\* write (`pin`, one pool reference each) and queues one job per block. The writer takes a job, reads
\* the block and writes it; its result goes back to the scheduler, which only then drops the pin. A
\* spill becomes a spilled start when every block it names is on disk; a spill that cannot land is
\* dropped and counted. The scheduler never waits for the writer: every one of its actions below is
\* enabled whatever the writer is doing.
\*
\* Start `s` holds blocks `s` and `s + 1`, so neighbouring starts share a block: the second spill
\* waits for the first one's write instead of queueing its own, as `entry_spill_keys` arranges.
\*
\* Safety invariants:
\* - `NoReuseWhileRead`: the block the writer holds is the block that was evicted. Its slot has not
\* been taken again since the write was queued, so the bytes being read are the start's.
\* - `WritePinned`: every block with a write queued, in the writer's hands or landed and not yet
\* collected holds its pin. This is what makes `NoReuseWhileRead` true.
\* - `AccountingExact`: the pool's pins and the scheduler's record of pending writes are the same set.
\* No block is free while a write of it is pending.
\* - `SpilledIsSound`: a spilled start's blocks are all on disk and each file holds the start's bytes.
\* - `NoSilentLoss`: every evicted start is pending, spilled, or counted as dropped.
\* - `QueueBounded`: at most `QueueCap` block writes are pending.
\* - `SchedulerNeverWaits`: the scheduler thread is never parked on the writer.
\* - `ColdGivesWayOnlyToLanded`: a spilled start already on disk is trimmed only for a spill that has
\* landed, never for one that is later dropped.
\*
\* Liveness (`LiveSpec`, no fairness for the writer, whose disk may hang for ever):
\* - `PendingSettles`: a pending spill is eventually spilled or dropped. Its deadline does it.
\*
\* Reversed witnesses (`Bug`):
\* - `"early_unpin"` (`SpillWriteBehindEarlyUnpin.cfg`): the eviction frees the blocks at once and
\* queues the write unpinned; a lane takes the slot while the writer reads it. `NoReuseWhileRead`.
\* - `"flag_cancel"` (`SpillWriteBehindFlagCancel.cfg`): dropping a spill unpins every block nobody
\* else waits for, including the one the writer holds, and leaves the jobs queued. `NoReuseWhileRead`.
\* - `"silent_drop"` (`SpillWriteBehindSilentDrop.cfg`): a dropped spill is not counted. `NoSilentLoss`.
\* - `"sync_write"` (`SpillWriteBehindSyncWrite.cfg`): the eviction waits for its writes, as the code
\* did before the spill writer. `SchedulerNeverWaits`.
\* - `"trim_at_queue"` (`SpillWriteBehindTrimAtQueue.cfg`): the oldest spilled start is trimmed when the
\* spill is queued, as the code did before. `ColdGivesWayOnlyToLanded`.
\* - `"unbounded"` (`SpillWriteBehindUnbounded.cfg`): the queue takes every spill. `QueueBounded`.
\* - `"no_deadline"` (`SpillWriteBehindNoDeadline.cfg`): a spill waits for its writes for ever.
\* `PendingSettles`.
EXTENDS Integers, FiniteSets
CONSTANTS
NStarts, \* start points resident at the beginning
MaxGen, \* how many times one slot can be taken again
QueueCap, \* most block writes pending at once
OldCold, \* spilled starts already on disk; each commit trims one while any is left
Bug
ASSUME /\ NStarts \in Nat \ {0}
/\ MaxGen \in Nat \ {0}
/\ QueueCap \in Nat \ {0}
/\ OldCold \in Nat
/\ Bug \in {"none", "early_unpin", "flag_cancel", "silent_drop", "sync_write",
"trim_at_queue", "unbounded", "no_deadline"}
Starts == 1..NStarts
Blocks == 1..(NStarts + 1)
Blk(s) == {s, s + 1}
NoBlock == 0
VARIABLES
st, \* [Starts -> "resident" | "pending" | "spilled" | "dropped"]
pin, \* [Blocks -> BOOLEAN]: the write pin, a pool reference held for the writer
pw, \* SUBSET Blocks: the scheduler's pending writes (`pending_writes`)
lane, \* [Blocks -> BOOLEAN]: a lane has taken the slot
gen, \* [Blocks -> 0..MaxGen]: the slot's generation; taking it again bumps it
wantGen, \* [Blocks -> 0..MaxGen]: the generation the queued write is to read
queued, \* SUBSET Blocks: jobs in the writer's queue
writing, \* the block the writer holds, or NoBlock
done, \* SUBSET (Blocks \X BOOLEAN): results sent and not yet collected
onDisk, \* SUBSET Blocks: blocks in the store's index
good, \* [Blocks -> BOOLEAN]: the block's file holds the bytes its start had
nEvicted,
nSpilled,
nDropped,
cold, \* old spilled starts still on disk
gaveWay, \* SUBSET Starts: spills an old spilled start was trimmed for
sched \* "run" | "wait": the scheduler thread
vars == <<st, pin, pw, lane, gen, wantGen, queued, writing, done, onDisk, good,
nEvicted, nSpilled, nDropped, cold, gaveWay, sched>>
Held(b) == \E s \in Starts : st[s] = "resident" /\ b \in Blk(s)
Free(b) == ~Held(b) /\ ~pin[b] /\ ~lane[b]
Pending == {s \in Starts : st[s] = "pending"}
Init ==
/\ st = [s \in Starts |-> "resident"]
/\ pin = [b \in Blocks |-> FALSE]
/\ pw = {}
/\ lane = [b \in Blocks |-> FALSE]
/\ gen = [b \in Blocks |-> 0]
/\ wantGen = [b \in Blocks |-> 0]
/\ queued = {}
/\ writing = NoBlock
/\ done = {}
/\ onDisk = {}
/\ good = [b \in Blocks |-> FALSE]
/\ nEvicted = 0
/\ nSpilled = 0
/\ nDropped = 0
/\ cold = OldCold
/\ gaveWay = {}
/\ sched = "run"
\* `evict_start_inner` with a spill: `queue_spill` pins and queues the blocks not yet on disk or on
\* their way, or drops the spill when the queue is full. No file is touched and nothing is waited for.
Evict(s) ==
/\ sched = "run"
/\ st[s] = "resident"
/\ nEvicted' = nEvicted + 1
/\ LET new == {b \in Blk(s) : b \notin onDisk /\ b \notin pw}
fits == Bug = "unbounded" \/ Cardinality(pw) + Cardinality(new) <= QueueCap
IN IF fits
THEN /\ st' = [st EXCEPT ![s] = "pending"]
/\ pw' = pw \cup new
/\ pin' = IF Bug = "early_unpin" THEN pin ELSE [b \in Blocks |-> pin[b] \/ b \in new]
/\ queued' = queued \cup new
/\ wantGen' = [b \in Blocks |-> IF b \in new THEN gen[b] ELSE wantGen[b]]
/\ sched' = IF Bug = "sync_write" /\ new # {} THEN "wait" ELSE "run"
/\ IF Bug = "trim_at_queue" /\ cold > 0
THEN cold' = cold - 1 /\ gaveWay' = gaveWay \cup {s}
ELSE UNCHANGED <<cold, gaveWay>>
/\ UNCHANGED nDropped
ELSE /\ st' = [st EXCEPT ![s] = "dropped"]
/\ nDropped' = IF Bug = "silent_drop" THEN nDropped ELSE nDropped + 1
/\ UNCHANGED <<pw, pin, queued, wantGen, sched, cold, gaveWay>>
/\ UNCHANGED <<lane, gen, writing, done, onDisk, good, nSpilled>>
\* The writer takes the next job. Under the queue lock: a job is either taken or cancelled, never both.
WriterTake(b) ==
/\ writing = NoBlock
/\ b \in queued
/\ queued' = queued \ {b}
/\ writing' = b
/\ UNCHANGED <<st, pin, pw, lane, gen, wantGen, done, onDisk, good,
nEvicted, nSpilled, nDropped, cold, gaveWay, sched>>
\* The write ends, well or badly. The file is good when the slot was not taken again under it.
WriterFinish(ok) ==
/\ writing # NoBlock
/\ done' = done \cup {<<writing, ok>>}
/\ good' = [good EXCEPT ![writing] = ok /\ gen[writing] = wantGen[writing]]
/\ writing' = NoBlock
/\ UNCHANGED <<st, pin, pw, lane, gen, wantGen, queued, onDisk,
nEvicted, nSpilled, nDropped, cold, gaveWay, sched>>
\* What dropping the pending spills `S` leaves behind (`fail_pending_spill`, `abort_spill_claims`).
\* A block no pending spill waits for any more comes back out of the queue, under its lock, and only
\* then loses its pin; one the writer holds keeps its pin until its result is collected. A block on
\* disk that no spill names any more is unlinked.
DropSpills(S) ==
LET st2 == [s \in Starts |-> IF s \in S THEN "dropped" ELSE st[s]]
wanted(b) == \E t \in Starts : st2[t] = "pending" /\ b \in Blk(t)
owned(b) == \E t \in Starts : st2[t] \in {"pending", "spilled"} /\ b \in Blk(t)
cancelled == {b \in queued : ~wanted(b)}
unpinned == IF Bug = "flag_cancel" THEN {b \in pw : ~wanted(b)} ELSE cancelled
IN /\ st' = st2
/\ queued' = IF Bug = "flag_cancel" THEN queued ELSE queued \ cancelled
/\ pw' = pw \ unpinned
/\ pin' = [b \in Blocks |-> pin[b] /\ b \notin unpinned]
/\ onDisk' = {b \in onDisk : owned(b)}
/\ nDropped' = IF Bug = "silent_drop" THEN nDropped ELSE nDropped + Cardinality(S)
\* `poll_spill_writes` collects one result: the pin is dropped now, and not before. A landed block
\* joins the index if a spill still wants it; a failed write drops every spill that waited for it.
Collect(b, ok) ==
/\ <<b, ok>> \in done
/\ done' = done \ {<<b, ok>>}
/\ LET waiters == {s \in Pending : b \in Blk(s)}
IN IF b \notin pw
THEN UNCHANGED <<st, pin, pw, queued, onDisk, nDropped>>
ELSE IF ok
THEN /\ pin' = [pin EXCEPT ![b] = FALSE]
/\ pw' = pw \ {b}
/\ onDisk' = IF waiters # {} THEN onDisk \cup {b} ELSE onDisk
/\ UNCHANGED <<st, queued, nDropped>>
ELSE LET st2 == [s \in Starts |-> IF s \in waiters THEN "dropped" ELSE st[s]]
wanted(c) == \E t \in Starts : st2[t] = "pending" /\ c \in Blk(t)
owned(c) == \E t \in Starts : st2[t] \in {"pending", "spilled"} /\ c \in Blk(t)
cancelled == {c \in queued : ~wanted(c)}
IN /\ st' = st2
/\ queued' = queued \ cancelled
/\ pw' = (pw \ cancelled) \ {b}
/\ pin' = [c \in Blocks |-> pin[c] /\ c \notin cancelled /\ c # b]
/\ onDisk' = {c \in onDisk : owned(c)}
/\ nDropped' = IF Bug = "silent_drop" THEN nDropped ELSE nDropped + Cardinality(waiters)
/\ sched' = IF sched = "wait" /\ pw' = {} THEN "run" ELSE sched
/\ UNCHANGED <<lane, gen, wantGen, writing, good, nEvicted, nSpilled, cold, gaveWay>>
\* `commit_pending_spill`: every block the spill names is on disk. Only here does an older spilled
\* start give way to it.
Commit(s) ==
/\ sched = "run"
/\ st[s] = "pending"
/\ Blk(s) \subseteq onDisk
/\ st' = [st EXCEPT ![s] = "spilled"]
/\ nSpilled' = nSpilled + 1
/\ IF Bug # "trim_at_queue" /\ cold > 0
THEN cold' = cold - 1 /\ gaveWay' = gaveWay \cup {s}
ELSE UNCHANGED <<cold, gaveWay>>
/\ UNCHANGED <<pin, pw, lane, gen, wantGen, queued, writing, done, onDisk, good,
nEvicted, nDropped, sched>>
\* The spill's deadline passes (`poll_spill_writes`), at any moment the model likes.
Expire(s) ==
/\ Bug # "no_deadline"
/\ sched = "run"
/\ st[s] = "pending"
/\ DropSpills({s})
/\ UNCHANGED <<lane, gen, wantGen, writing, done, good, nEvicted, nSpilled, cold, gaveWay, sched>>
\* A lane takes a free slot (`take_unzeroed`): nothing holds it, and its generation moves.
Reuse(b) ==
/\ sched = "run"
/\ Free(b)
/\ gen[b] < MaxGen
/\ gen' = [gen EXCEPT ![b] = gen[b] + 1]
/\ lane' = [lane EXCEPT ![b] = TRUE]
/\ UNCHANGED <<st, pin, pw, wantGen, queued, writing, done, onDisk, good,
nEvicted, nSpilled, nDropped, cold, gaveWay, sched>>
LaneRelease(b) ==
/\ sched = "run"
/\ lane[b]
/\ lane' = [lane EXCEPT ![b] = FALSE]
/\ UNCHANGED <<st, pin, pw, gen, wantGen, queued, writing, done, onDisk, good,
nEvicted, nSpilled, nDropped, cold, gaveWay, sched>>
Scheduler ==
\/ \E s \in Starts : Evict(s) \/ Commit(s) \/ Expire(s)
\/ \E b \in Blocks : Reuse(b) \/ LaneRelease(b)
\/ \E b \in Blocks, ok \in BOOLEAN : Collect(b, ok)
Writer ==
\/ \E b \in Blocks : WriterTake(b)
\/ \E ok \in BOOLEAN : WriterFinish(ok)
Next == Scheduler \/ Writer
Spec == Init /\ [][Next]_vars
\* The scheduler's own steps are fair. The writer's are not: a disk may hang for ever.
LiveSpec ==
/\ Spec
/\ \A s \in Starts : WF_vars(Expire(s)) /\ WF_vars(Commit(s))
/\ WF_vars(\E b \in Blocks, ok \in BOOLEAN : Collect(b, ok))
\* ---- Invariants -----------------------------------------------------------
TypeOK ==
/\ st \in [Starts -> {"resident", "pending", "spilled", "dropped"}]
/\ pin \in [Blocks -> BOOLEAN]
/\ pw \subseteq Blocks
/\ lane \in [Blocks -> BOOLEAN]
/\ gen \in [Blocks -> 0..MaxGen]
/\ wantGen \in [Blocks -> 0..MaxGen]
/\ queued \subseteq Blocks
/\ writing \in Blocks \cup {NoBlock}
/\ done \subseteq (Blocks \X BOOLEAN)
/\ onDisk \subseteq Blocks
/\ good \in [Blocks -> BOOLEAN]
/\ nEvicted \in 0..NStarts
/\ nSpilled \in 0..NStarts
/\ nDropped \in 0..NStarts
/\ cold \in 0..OldCold
/\ gaveWay \subseteq Starts
/\ sched \in {"run", "wait"}
NoReuseWhileRead == writing # NoBlock => gen[writing] = wantGen[writing]
WritePinned ==
\A b \in Blocks : (b \in queued \/ b = writing \/ (\E ok \in BOOLEAN : <<b, ok>> \in done)) => pin[b]
AccountingExact == \A b \in Blocks : pin[b] <=> b \in pw
SpilledIsSound == \A s \in Starts : st[s] = "spilled" => \A b \in Blk(s) : b \in onDisk /\ good[b]
NoSilentLoss == nEvicted = nSpilled + nDropped + Cardinality(Pending)
QueueBounded == Cardinality(pw) <= QueueCap
SchedulerNeverWaits == sched = "run"
\* The loss itself: an old spilled start trimmed for a spill that then never landed.
ColdGivesWayOnlyToLanded == \A s \in gaveWay : st[s] # "dropped"
\* ---- Liveness -------------------------------------------------------------
PendingSettles == \A s \in Starts : (st[s] = "pending") ~> (st[s] \in {"spilled", "dropped"})
=============================================================================