A computation is drawn as a dataflow graph: a horizontal line for each agent, a dot for each event, and an arrow for each message (see Computations and Data Flow). Channels are first-in-first-out. A cut splits the events into a past and a future, with every agent's past events before its future events. A cut is consistent when every message received in the past is sent in the past, and then the states at the cut form a global snapshot (see Global Snapshots).
The problem is to assign timestamps to events so that
[past(T), future(T)] is a consistent cut, where
past(T) is the set of events with timestamps T or less
and future(T) is the set of events with timestamps greater than T.
For a number T, the events with timestamps at most
T are in the past at T, and the others are in
the future.
A simple solution, which is easily proved correct is:
When an agent sends a message in an event it puts the event timestamp on the message. The timestamp t(e) of an event e is larger than the timestamp of the previous event ep at that agent and the timestamp t(es) of the message (if any) that it receives in the event.
A common choice is
t(e) = max(t(ep), t(es)) + 1.
In Figure 1, t(b3) = 4 and t(c5) = 6.
Which of these are possible
timestamps for b4? Pick all that apply.
t(b4) > max(t(b3), t(c5)) = max(4, 6) = 6, so 7, 8 and 9 are
all possible. Adding 1 is a convention, not a requirement. Figure 1 uses
it and gives t(b4) = 7.[past(T), future(T)]: past events are dark, future events
are green, and the messages that cross the cut are blue. Every crossing
message goes from the past to the future, for every T.
Each agent records its local state at the boundary between
its past and its future. An agent v starts recording the
messages it receives on channel (u, v) when it records its
local state, and stops when it receives on that channel a message with
timestamp greater than T. That message is not recorded. The
state of channel (u, v) is the sequence of messages
recorded.
The message with timestamp greater than T plays the role of
the marker in the marker algorithm. The messages v records are
exactly those sent in the past and received in the future: they are
received after v's snapshot, and, because the channel is
first-in-first-out, they are sent before the first message from
u with a timestamp greater than T.
The algorithm requires every agent to send messages on all its output channels
with timestamps greater than T.
Assign timestamps to events so that t(e) is less
than t(e′) exactly when there is a path
from e to e′ in the dataflow graph.
The algorithm is straightforward.
Each agent j keeps a vector tj of
length n where n is the number of agents.
t(e) = [n0, n1, …] exactly when
nk is the number of events
at agent k with a path to e, and
nj is the number of events at agent j
with a path to e, counting e itself.
e (orange ring), then an event
e′ (blue ring). The figure draws a path from
e to e′ if there is one, and compares
the timestamps.
With agents A, B, C, event b3 of
Figure 2 sends m5 with timestamp [2, 4, 0].
Event a3, the previous event of A, has
timestamp [4, 0, 2]. What is the timestamp of
a4, which receives m5?
max([4, 0, 2], [2, 4, 0]) + [1, 0, 0] = [4, 4, 2] + [1, 0, 0] = [5, 4, 2].
Check it in Figure 2.
Two vectors can be unordered:
neither [1, 0] ≤ [0, 1] nor
[0, 1] ≤ [1, 0].
For an event e of agent j,
t(e) is the element-wise maximum of the timestamp of
j's previous event ep and, if
e receives a message, the timestamp t(es)
carried by the message; plus uj, the vector with a
positive value (usually assumed to be 1) in element j and 0 elsewhere.
For example, with four agents: if event e of agent 1 has
previous event with timestamp [3, 8, 2, 4] and receives a
message carrying [10, 5, 3, 2], then
t(e) = [10, 8, 3, 4] + [0, 1, 0, 0] = [10, 9, 3, 4].
e ≠ e′,
t(e) < t(e′) exactly when there is a path from
e to e′.
Induction on the sequence of events in the order of any computation.
A detection algorithm is run by the operating system (OS) to find out whether the client computation has some property -- for example, the client computation has terminated. These are properties that continue to hold until the OS takes action. For example, if a client computation is deadlocked then it remains deadlocked until the OS breaks the deadlock. In this section we focus on how the OS detects the condition and not about how the OS deals with the condition after it is detected. So, we assume that the condition is stable - if the condition holds in a state then it continues to hold for all succeeding states of all computations.
Detection algorithms are like snapshot algorithms.
A detection algorithm takes time: its agents exchange messages while the
client computation keeps executing. An algorithm to detect a property P
has to deal with the possibility that ¬P holds when the
detection algorithm starts, and while the detection algorithm is running
the client computation makes P true. For example, the client computation
may not be deadlocked when the deadlock detection algorithm starts but
becomes deadlocked before the deadlock detection algorithm terminates.
¬P and red
means P. Because P is stable, a red state is
never followed by a green one, so there are only four cases. In the last
two, P becomes true while the detector runs; the detector
returns what the detect state shows, and either answer meets the
specification.
¬P holds, where
¬P holds while the detection algorithm executes
then the algorithm returns that ¬P holds.
¬P holds when the detection algorithm starts
and P holds when the detection algorithm finishes then
then the algorithm returns either that P holds or
that ¬P holds.
For example, to detect deadlock, the OS runs the deadlock detection algorithm repeatedly. This is because the client computation may have deadlocked after the last run of the detection algorithm determined that it hadn't deadlocked.
The generic solution
Take a global snapshot. Each agent sends its local state and the states
of its incoming channels to a detector agent. The detector puts them
together and checks whether P holds in the snapshot.
Correctness proof
If P holds in the detect state then P holds in
all states reachable from the detect state because P is stable.
There is a computation from the detect state to the end state and so
the end state is reachable from the detect state. Therefore P holds
in the end state.
If ¬P holds in the detect state then ¬P
holds in all states that can reach the detect state because
P is stable. There is a computation from the start state
to the detect state and so the start state can reach the
detect state. Therefore ¬P holds
in the start state.
Consider a distributed system D in which each agent waits to receive messages on any of its incoming channels. (This type of system is different from the office used in DisSysLab where an agent can wait to receive messages on only one of its inputs.) Each agent in D is either active or idle. An active agent may send messages, and may become idle at any time. An idle agent sends no messages. An idle agent becomes active when it receives a message. Initially every agent is active and every channel is empty. A computation of D has terminated when every agent is idle and every channel is empty. Termination is stable -- after the computation terminates no idle agent can become active because all channels are empty, and messages cannot be added to channels because all agents are idle.
Specification The detection algorithm is part of the OS. Its specification is
that the detection algorithm must finish, and the algorithm has a
variable terminated, which is initially false, and must satisfy:
terminated ⟹
the client computation has terminated. The detector sets terminated
to true only if the computation has terminated.
terminated is set to true when the
detection algorithm finishes.
If the client computation has not terminated when the detection algorithm
starts, but the client computation terminates before the detection algorithm
finishes, then the value of terminated is unspecified -- it may
be true or false.
For each channel X, the sending agent keeps a count
Xs of the messages it has sent on X,
and the receiving agent keeps a count Xr of the
number of messages it has received on the channel.
The number of messages in transit on X is
Xs − Xr. The problem is
that no agent has both numbers at the same instant. The sending agent
cannot read Xr and the receiving agent cannot read
Xs.
We will show that consistent cuts and global snapshots allow us to use old
values of Xs and Xr.
Agent actions When an agent A becomes idle it sends a message to an OS agent called the
detector. This message contains the value of Xs for
every output channel X of agent A, and the value of
Yr for every input channel Y of A. The
message from A to the detector may be delayed by an arbitrary amount.
Detector actions The detector has local variables
X′s and X′r
for every channel X. Let X be a channel from
agent A to agent B. The detector's variable X′s
is the value of Xs in the most recent message that the
detector has received from A. Likewise, the
detector's variable X′r
is the value of Xr in the most recent message that the
detector has received from B. Because message delays are arbitrary, it is
possible that X′s ≠ Xs
and X′r ≠ Xr.
Nevertheless, the algorithm claims that the computation has terminated when,
for all channels X, X′s = X′r.
Initially Set X′s, X′r to values
so that they are unequal and remain unequal until the detector gets a message
from each agent; this is to ensure that the detector doesn't claim
termination even before the computation starts. For example,
X′s = - 1, X′r = - 2.
Safety We will first prove that if for all channels, X,
X′s = X′r then
the computation has terminated.
Consider the point at each agent's computaton at which the
agent sent the messages with the values in X′
X′s. Because
X′s ≥ X′r for every
channel X, those points form a consistent cut.
At that cut every agent is idle because agents send counts when they
become idle, and every channel is empty.
So the computation had terminated at the cut.
Progress. Every agent is active initially, so it becomes idle at
least once and sends counts. After the computation terminates, each
agent's last message contains the final counts of the messages it
has sent and received. After the detector receives the
final message from all agents
X′s = X′r for every channel.
Both agents have become idle and reported, and the detector
holds C′s = 3, C′r = 1,
D′s = 2, D′r = 2. Should it
conclude that the computation has terminated?
C held
3 − 1 = 2 messages. When B receives them it
becomes active again. A detector that only checked “every agent has
reported idle” would make exactly this mistake. Figure 4's
Play the example passes through this state.
A sends on channel C to B,
and B sends on channel D to A. The
top half shows the current state: counts, messages in transit, and the
detector's variables. The bottom half is the timeline: red arrows are
client messages, green arrows carry counts to the detector, and the
purple line is the cut formed by the counts the detector holds. Drive it
yourself with the buttons in the first row, or press Play the
example. Truth says whether the computation has terminated,
which no agent can see. Detector must never say
“terminated” before Truth does. Naive is a
detector that ignores the counts and checks only that both agents have
reported idle; try to make it wrong.
In a DisSysLab office an agent waits to receive a message on one
inbox, not on any inbox. Figure 5 shows an office of three agents,
A, B and C, that waits for ever.
in_0 and in_1 and
outboxes out_0 and out_1. A is
connected to B, B to C, and
C to A, out_0 to
in_0 and out_1 to in_1. Each
agent is waiting for a message on in_0, and every inbox,
outbox and connection is empty. So the agents wait for ever on the cycle
A.out_0 → B.in_0, B.out_0 → C.in_0,
C.out_0 → A.in_0.
The problem is to develop an algorithm that detects deadlock such as that shown in the figure -- a cycle of agents, each agent waiting to receive a message from the previous agent in the cycle, with empty message channels for all channels in the cycle.
The generic algorithm is take a global snapshot and inspect the snapshot state to determine if there is a deadlock in the snapshot state. The generic algorithm works to detect any stable property including deadlock.
A simple deadlock detection algorithm is a variant of the termination detection
algorithm. Instead of checking whether
X′s = X′r holds for all
channels, X the algorithm only checks for channels in a cycle.
Logical clocks are due to Lamport, “Time, clocks, and the ordering of events in a distributed system” (1978). Vector clocks were introduced independently by Fidge and by Mattern in 1988. The snapshot at a logical time, with markers replaced by messages carrying timestamps, is a variant of the Chandy–Lamport snapshot algorithm (1985). Detecting termination by counting messages is in the spirit of Mattern's counting algorithms (1987); Dijkstra and Scholten (1980) and Francez (1980) gave early termination detection algorithms.