A timeline diagram is a pictorial representation of system behavior as it evolves over time. Time flows from left to right in the diagram. The diagram has a horizontal line for each agent which shows the agent's state over time. Similarly, the diagram has a horizontal line for each inbox and each outbox. The line for an inbox or outbox shows its state -- i.e., the queue of messages in the box -- over time. We call the horizontal line showing an agent's state over time the agent's timeline. An inbox's or outbox's timeline has a similar meaning. The diagram below shows a timeline diagram with two agents X and Y.
A thin horizontal segment of an agent's timeline depicts an agent waiting to receive a message. A thick horizontal line or bar shows an active agent, and the length of the bar represents the time the agent spends on that action. The bar is labeled with the inbox from which the agent got the message that it is processing.
An agent's leftmost bar is its initial action, labeled
init.
An initial action by an agent sends a message (possibly to itself), but
does not get a message from an inbox.
Initial actions are merely convenient ways of representing computations
starting up at each agent.
Click the play and step buttons in the diagram below
R and two outboxes A,
B; agent Y has one outbox P and two inboxes
U, V. The connections are
A→V, B→U and P→R.
A message gets from one agent to another in three steps shown in the
diagram.
The step in which agent X places a message on its outbox A at a time t
is represented by a vertical arrow at t which runs from the X-timeline to the
A-timeline.
When that message is removed from A at a time t and reaches
inbox V at a later time t', a slanted arrow runs from
the A-timeline at t to the V-timeline at t'.
When agent Y removes the message from V at a time t, a
vertical arrow at t runs from the V-timeline to the
Y-timeline.
Each arrow is labeled with the name of its message.
The segments of an agent's timeline in which the agent is waiting
are labeled with the inboxes for which the agent is waiting. In the diagram,
after the initial action of agent Y it waits for a message from inbox
U, and that line segment is labeled waiting on U.
The message m1 from agent X arrives in inbox V, but agent Y
goes on waiting on U.
The state of a system is tuple of the states of its components -- its agents, outboxes, inboxes, and channels. The state of the system at time t can be obtained in the timeline diagram from the vertical line at t. The state of an agent or box at t is the label on its timeline at time t. The state of a channel is the sequence of message lines intersected by the vertical line at t.
Move the red vertical bar in the diagram below.
| state at t = | ||
|---|---|---|
| agent X | ||
| agent Y | ||
| X's boxes | ||
| Y's boxes | ||
| channels | ||
xk is X's state
after it has completed k actions. Slide t and watch which
elements change — nothing changes except at an event, and each event
changes exactly one agent and one of its boxes, or moves one message
from an outbox into a channel and then into an inbox.
Three moments are worth stopping at. Around t = 15 outbox
P holds two messages at once, so a single element of the
tuple is a queue rather than one message. Around t = 45
m4 is sitting in inbox R while X is still busy
with m2 — delivered, not yet received, and the cut sees it
because R has a line of its own. And if the line falls
inside a bar, the agent is part-way through an action, so there is no
state to read: the tuple is only defined between events.
In most of this course, we make no assumptions about agent clocks. An agent clock may change its rate with respect to other agent clocks by arbitrary amounts. A time recorded by one agent and a time recorded by another cannot be compared, so no agent can say when its actions happened relative to another agent's. Timeline diagrams are helpful even without clocks.
What an agent can record is its own actions, in the order it performed them. For each action it records three things: its state before the action, the message it received, and the messages it sent. Messages carry unique ids, so a message is recorded by id at both ends. We call this record the agent computation. It is a sequence, not a timeline: it says what the agent did and in what order, and says nothing about when.
A system computation is a sequence of all the actions of all the agents, in the order in which they occurred. During an execution some system computation occurred, and each agent's record is what that system computation looks like from that agent. Our problem is the reverse: given only the set of agent computations, what can we say about the system computation they came from?
Notice what is not in the records. No agent records when a message left an outbox, when it crossed to the other agent, or when it landed in an inbox. No agent records anything about the other agent at all. The boxes and the slanted arrows of the timeline diagram are invisible from the inside.
Two things survive, and they are enough to build a graph. First, each agent computation gives the order of that agent's own actions. Second, a message sent in one record and received in another can be matched by its id, and a message can be received only after it is sent, so the action that sent it must appear earlier in the system computation than the action that received it. We represent what we know from the set of agent computations by a directed, labeled graph that we call a dataflow graph.
Click the button "Derive the dataflow graph" in the diagram below. The animation starts from the whole timeline diagram, so that you can see where each edge of the graph comes from. The graph itself needs none of that: its agent edges come from each agent's own record, and its message edges come from matching ids across two records. Everything the animation discards — the boxes, the queues, the crossings — is exactly what no agent could record.
The dataflow graph obtained from sets of agent timelines
Events are represented by vertices in the dataflow graph.
There are two kinds of edges: agent edges and message edges.
An agent edge is from an event at an agent to the next event at
that agent and is labeled with the state of the agent between the events.
A message edge is from an event at which an agent sends a message to the
event in which an agent receives that message.
A message edge is labeled with the message.
Each vertex, other than the initial (init) vertices, has exactly one incoming agent edge and one incoming message edge. Edges represent the flow of information. An agent edge represents the flow of agent state from one action to the next. An event at an agent gets data flow from (1) the state of the agent prior to the event and (2) the message that is processed during the event. The graph is acyclic.
We define the interleaving of sequences to specify the problem of constructing a system computation from agent computations.
Given a set S of sequences, where each element of each sequence in S is unique, an interleaving of the sequences in S is a sequence Z consisting of elements of the sequences in S, in which: each element of each sequence in S occurs exactly once in Z, and the order of elements within each sequence in S is preserved in Z.
Interleavings of sequences [x[0], x[1], x[2]] and [y[0], y[1]] include:
Given a set of agent computations recorded during a system execution, what interleavings of the agent computations are system computations?
Below are examples of the interleavings of the agent computations [0, 2, 3] and [1, 4] in the example. Which of the interleavings are system computations?
The first two are system computations and the third is not. In the example, action 2 sends the message m3 and action 4 receives it. The third interleaving places 4 before 2, so m3 would be received before it was sent. The first two place 2 before 4, and every other message is likewise sent before it is received. Note that 3 and 4 may appear in either order: no message passes between them, and they are actions of different agents, so neither constrains the other.
Let Z = [Z[0], Z[1], Z[2], ...] be a system computation.
Observation 1 For all i, there is no edge in the dataflow graph from Z[i+1] to Z[i].
Observation 2 If there is no edge from Z[i] to Z[i+1] then the sequence Z' obtained from Z by interchanging the order of Z[i] and Z[i+1] is also a computation.
The proof of the second observation is straightforward. The vertices Z[i] to Z[i+1] represent events at different agents because there is no edge from Z[i] to Z[i+1]. Let Z[i] and Z[i+1] be at agents X and Y, respectively. The state of Y and the message received by Y in Z[i+1] are not changed by the occurrence of Z[i]. Likewise, the state of X and the message received by X in Z[i] are not changed by the occurrence of Z[i+1].
Given a set of agent computations in a system execution, an interleaving of those agent computations is a system computation if and only if each message is sent before it is received in the interleaving.
Proof
The proof is similar to the proof that a list can be sorted by
flipping out-of-order adjacent pairs.
Only if. Let D be a system computation and let a message be sent at action e and received at action e′. The dataflow graph has a message edge from e to e′, so by observation 1 e appears before e′ in D.
If. Let D be an interleaving of the agent computations in which every message is sent before it is received. First note that D respects every edge of the dataflow graph: it respects agent edges because it is an interleaving, and message edges by assumption.
The execution produced some system computation C, with the same agent computations. Let n be the number of out-of-order pairs: a pair of indices (i, j), i < j, is out of order when C[i] appears later in D than C[j]. If n > 0 then some adjacent pair (C[k], C[k+1]) is out of order. There is no edge from C[k] to C[k+1], because D respects every edge and D places C[k+1] first. So by observation 2 the sequence C′ obtained from C by flipping C[k] and C[k+1] is also a system computation, and it has n-1 out-of-order pairs.
Repeat. Each flip lowers n by one, so after n flips we have a system computation with no out-of-order pairs — that is, D itself. Hence D is a system computation. □
0, 2,
5, 6 and agent Y does 1,
3, 4, 7; 0 and
1 are the initial actions. Six messages run between them:
0→3, 0→4, 1→2,
3→5, 3→6 and 2→7.
Press Another ordering and the six later events slide to new
times. The two initial actions stay where they are: nothing precedes
them, so they always come first, and holding them still makes the rest
easier to watch. Each agent keeps its own order — that never moves —
and every message arrow keeps pointing forwards, which is the only
other thing required.
By the theorem each of these is a computation of the same system, and
the count says how many orderings there are in all.
The theorem says which interleavings are computations. This last diagram says the same thing in the picture we started with. Agent X recorded three actions and agent Y recorded two, and those recordings never change. Every timeline below is consistent with them.
init,
then an R action, then another R action, and
sent and received exactly these messages; Y did init then a
U action. Every picture is consistent with those two
records, and nothing else was assumed — least of all a clock.
Watch the third row: sometimes X's last action comes before Y's, and
sometimes after. Nothing in either record settles that, which is why
both are computations.