DisSysLab

Global Snapshots

Prose to come.

Distributed operating system algorithms

An operating system (OS) carries out actions on behalf of its client without changes to the execution of the client algorithm. For example, an OS algorithm takes repeated checkpoints of the client's execution. The execution of the client algorithm remains unchanged (except that it may run slower because the OS and client use the same resources) when the checkpoint algorithm runs.

The OS may use the same processes, threads, and queues that the client does. We build systems with an OS agent for every client agent. OS messages are trapped by the OS agent and not passed on to the client agents. Client agents never receive or send OS messages.

In the figure below, the client agents are depicted as blue circles. The OS manager of a client is shown as a red circle surrounding the client's blue circle. An OS message is shown in red and a client message in blue. OS and client messages are sent in the same way. Click the buttons to see OS messages getting trapped by the OS manager and client messages going through.

Figure 1. An OS agent around every client agent. Press the buttons and watch what happens when a message arrives. The client message is passed straight on to the client inside. The OS message is trapped at the OS boundary and goes no further: the client never sees it. Both messages travel the same channel at the same speed; the only difference is what the OS agent does with them on arrival.

OS Managers

We will use OS agents that are not associated with any specific client. In many distributed OS algorithms we assume an OS systems-manager agent or a set of such agents that manage the entire client system. OS messages are exchanged between the OS systems-manager and the OS agents responsible for each client.

We begin with a simple algorithm that we will refine to make more efficient. In the simple global snapshot algorithm, the OS systems-manager sends messages to the OS client-managers to take local snapshots of their clients. When a client-manager completes taking its local snapshot it sends its local snapshot information to the systems manager. The systems manager then collates information from each client manager to form a global snapshot.

For brevity, when we describe OS algorithms we will use agent X instead of the longer, and correct, OS client-manager of agent X. The OS algorithm is independent of client computations, so while describing OS algorithms we need not distinguish the manager of an agent from the agent itself.

A global snapshot at an instant in time

If we could, we would get a global snapshot by having all agents record their states at the same instant in time. The diagram below shows the state at different points in a computation.

Click on Take the snapshot in the diagram below.

The vertical dotted red line represents an instant t in time. The state of an agent at t is given by the label on the agent timeline at t. Define past events at t as the events that occur before t and future events at t as the events that occur after t.

Messages in transit The messages in transit at t are the messages sent in the past and not received in the past.

In the diagram, messages in transit at t are represented by the message lines that cross the vertical line at t. Message m2 is not in transit in the snapshot because it is sent and received in the past. Likewise, message m7 is not in the snapshot because it is not sent in the past. Messages m4 and m5 are in the snapshot because they were sent in the past and not received in the past. And because messages are received only after they are sent, there is no message to the past from the future.

Recorded at
In the channels
Figure 2. A snapshot at an instant in time.

Messages that agents send to the systems manager

Assume that agents could take their local snapshots (i.e., record their own states) at the same instant. What messages do agents send to the systems manager?

Each agent sends a message to the systems manager with the agent's own local state and the sequence of messages it has placed on each of its outboxes and the sequence of messages it has received from each of its inboxes -- these are the messages that the agent sent and received in the past. For each connected outbox-inbox pair the systems manager determines the messages in transit: the messages sent in the past on that outbox, with the messages received in the past from the inbox removed. (We will see later that the entire sequence of messages need not be stored.)

The Global Snapshot

Each agent's clock can deviate by arbitrary amounts from the clocks of other agents. So if all agents take their local snapshots when their local clocks are at t the snapshots may not be taken at the same instant. In the simple algorithm, the systems manager sends messages to the agents. If each agent takes its local snapshot when it receives the message from the systems manager, the agents may still not take their local snapshots at the same instant.

An algorithm to determine a global snapshot is not instantaneous: it starts at some point and finishes at a later point.

The global snapshot Let S_init and S_fini be the states of the system at which the global snapshot algorithm starts and finishes, respectively. The global snapshot is a state S_snap of the system such that there is a computation from S_init to S_snap to S_fini.

Why is the global snapshot useful?

Checkpoint, rollback and recovery. In the event of a transient error the system can restart from the most recent checkpoint (a global snapshot) and continue execution from there.

Detecting stable properties. A stable property is one that if it holds at any point in a computation then it continues to hold thereafter. Examples of stable properties are that the system has terminated, or that a set of agents is deadlocked. Let P be a stable property. The problem is to develop an algorithm that detects whether P holds. A global snapshot answers it because of two facts:

  1. If P holds when the algorithm starts then P holds in the snapshot.
  2. If P holds in the snapshot then P holds when the algorithm terminates.

Both facts follow from the theorem below. By the theorem there is a computation from S_init to S_snap to S_fini, and P is stable in every computation of the system. So if P holds at S_init it still holds at S_snap, which is the first fact; and if P holds at S_snap it still holds at S_fini, which is the second.

Stability is doing real work here. The inbox of agent X is empty is not a stable property: it can be true at S_snap and false at S_fini, or false at S_init and true at S_snap. Of a stable property, a snapshot tells you something about the system now; of an unstable one, it tells you only that it held in some state the system may never have been in.

One way of detecting a stable property P is to take global snapshots repeatedly and check whether P holds in the snapshot.

Agents take local snapshots at different moments

The diagram below shows a timeline with agents X, Y, and Z. Click the button Take the snapshot.

The animation shows Y taking its local snapshot first (shown as a red circle), then X and then Z. The global snapshot algorithm in this case starts when Y takes its local snapshot and terminates when Z takes its local snapshot. So S_init is the global state at the point at which Y takes its snapshot; diagrammatically it is determined by the agent timelines and message lines that intersect the vertical line at that point. Likewise, S_fini is the global state at the point at which Z takes its snapshot. The dotted red lines connect the points at which agents take local snapshots. The dotted red lines connect the points at which agents take local snapshots. Whether a particular set of points will do — and how the agents can choose such points without a common clock — is what the rest of this lecture answers.

Recorded at
Recorded so far
In the channels
Impossible
Figure 3. Agents take local snapshots at different points.

Past and Future

When there are perfect clocks, the past at time t is obvious. Because agents don't take local snapshots at the same instant, we define past and future with respect to the points at which local snapshots are taken.

Past and future events An event at an agent is a past event exactly when it occurs before the agent takes its local snapshot. Events that are not past events are future events.

Example 1: Can this state be a global snapshot?

Recall that past events are events at an agent before the agent takes its local snapshot, and the other events are future events. Let's align the points at which agents take local snapshots so that past events are before the aligned points and future events are after the aligned points. Click the button: Line them up

The alignment shows a problem: there is a message from the future to the past, shown as a blue arrow. There is no global state in which a channel delivers a message that has not been sent. So, in the figure, the points at which the agents take their local snapshots do not correspond to a global snapshot.

Recorded at
In the channels
Impossible
Figure 4. An infeasible global snapshot: message from future to past

Example 2: Can this state be a global snapshot?

The next diagram shows a timeline with a different set of points at which agents take local snapshots. Can these points correspond to a global snapshot? Click the button: Line them up

We see that every message received in the past is sent in the past. Equivalently, no message is sent from the future to the past. The global snapshot, S_snap, is the state shown when the times of the agent snapshots are aligned. This is the state at the dotted green vertical line. The past is before this line and the future is after the line. The timeline shows a computation from S_init to S_snap to S_fini. The state of an agent in S_snap is its local snapshot, and the messages in transit in S_snap are the messages that are sent in the past and not received in the past. Messages m4 and m5 are in transit in S_snap.

Recorded at
In the channels
Impossible
Figure 5. A global snapshot: messages received in the past are sent in the past.

The Global Snapshot Theorem

The global snapshot theorem The points of local snapshots correspond to a global snapshot exactly when
every message received in the past is sent in the past.

Proof

The proof uses the theorem of Computations and Data Flow: Given a set of agent computations recorded in a system computation, all interleavings of agent computations in which each message is received only after it is sent are system computations.

Let agent computations be recorded during a system computation C. Construct an interleaving C' of agent computations as follows: in C' the relative ordering of past events is the same as in C, and the relative ordering of future events is also the same as in C, but every past event appears before every future one.

C' is a computation

Consider any message m, and the two events that send and receive it. There are four cases, and the hypothesis of the theorem rules out the only bad one.

  1. m is sent in the past and received in the past. Both events are past events, and in C' the past events keep the order they had in C — in which m was sent before it was received.
  2. m is sent in the past and received in the future. The send is a past event and the receive is a future event, and in C' every past event comes before every future one.
  3. m is sent in the future and received in the future. Both events are future events, and in C' the future events keep the order they had in C.
  4. m is sent in the future and received in the past. This is the only case in which C' would put a receive before its send — and it is exactly the case the hypothesis forbids, so it does not arise.

So every message in C' is received only after it is sent, and by the theorem of Computations and Data Flow, C' is a computation of the system.

C' passes through the recorded state

C' is a reordering of the events recorded during the algorithm, so C' starts at S_init and ends at S_fini. Consider the state of the system in C' after every past event and before every future one. Each agent has performed exactly its past events, so each agent's state is the state it recorded in its local snapshot. And a message is in transit in that state exactly when it was sent and not received among the past events — which is exactly what the algorithm records as in transit. So that state is S_snap, and C' is a computation from S_init to S_snap to S_fini.

The other direction

Conversely, suppose some message m is received in the past and sent in the future. Then S_snap is a state in which X has already received m and X' has not yet sent it. A message is received only after it is sent, so no computation of the system reaches such a state; in particular there is no computation from S_init to S_snap. The points of local snapshots therefore do not correspond to a global snapshot — which is the situation in Example 1.

Example illustrating the proof idea

Click Align the records. The three records slide onto one line, and the events slide with them: every past event moves to the left of that line and every future event to the right, which is the interleaving C'. The strip below shows the same thing as a sequence — the order the events happened in, becoming the snapshot's order. Watch y2 move in front of x2, and x3 move behind z3 and y3: no agent's own order changes, and no message ends up received before it was sent.

Before its own record
After its own record
Still a computation
Figure 6. Illustration of the proof

A Global Snapshot Algorithm

Local snapshots

An agent takes its local snapshot by recording (1) its own state, (2) for each outbox, the sequence of messages it has placed on that outbox, and (3) for each inbox, the sequence of messages it has received from that inbox.

When an agent takes its local snapshot it sends this information to the OS systems-manager.

Global snapshot algorithms are based on this principle: select points at which agents take local snapshots so that messages received in the past are sent in the past.

When agents take local snapshots

  1. When an agent takes its local snapshot the agent puts a special message that we call a marker on each of its outboxes.
  2. When an agent receives a marker from any inbox, if the agent has already taken its local snapshot then the agent does nothing. If the agent has not taken its local snapshot then it does so and (from the previous rule) puts a marker on each of its outboxes.

A marker is an OS message. As in Figure 1, it is trapped by the OS agent that receives it and is never passed on to the client inside, so no client ever sees a marker and no client's computation is changed by the algorithm. Markers travel along the same connections as the clients' own messages — which is exactly why the queue argument below works.

Snapshots taken
Markers in transit
Figure 7. The markers. The computation of Figure 6 again, and this time the algorithm is shown running on it. Nothing is marked to begin with: no snapshots, no S_init, no S_fini. Press Next step. X takes its local snapshot — the red ring — and that moment is S_init, because X is the first to record. By the first rule X puts a marker on each of its outboxes, and both are now on their way: one along the connection to Z, one along the connection to Y. Neither has arrived. Watch where each is headed. The marker to Z lands exactly where Z takes its own snapshot, because that is what the arrival will make Z do. The marker to Y lands to the right of where Y records. Step 2. The marker reaches Z, and Z records — that is rule 2: an agent that has not yet recorded does so when a marker arrives. By rule 1 Z then puts a marker on each of its own outboxes, one heading for Y and one for X. X has recorded already, so that one will change nothing when it lands. Step 3. Z's marker reaches Y, Y records, and that moment is S_fini — the last agent to record is where the algorithm finishes. Y sends markers of its own in turn, and both of them will arrive at agents that have already recorded. The four markers that land to the right of S_fini are drawn all the way to the timeline they land on, at no particular point — when they arrive changes nothing, because the agent has recorded already, so the picture does not pretend to know when that was. By the end, every agent has recorded and every connection has carried a marker. Notice what the markers did not do: no client computation changed, and no message of the computation moved. The snapshot was taken around it.

Proof that messages received in the past are sent in the past

Let m be a message that agent X receives from inbox b in the past, and let X' be the agent that placed m on outbox b', where b' is connected to b. We show that m is sent in the past.

X receives m in the past, that is, before X takes its local snapshot. When an agent receives a marker it takes its local snapshot if it has not already done so, so X takes its local snapshot no later than the arrival of the marker on b. Therefore X receives m before the marker on b.

Outboxes, connections, and inboxes are queues — so messages are received from an inbox in the order in which they were placed in the outbox to which it is connected. Therefore X' placed m on outbox b' before X' placed the marker on b'. And X' puts a marker on each of its outboxes when it takes its local snapshot. So X' placed m on b' before it took its own snapshot, that is, m is sent in the past.

Starting the algorithm

The algorithm requires every agent to send its snapshot to the systems manager. The proof that messages received in the past are sent in the past requires that each agent takes its snapshot when or before it receives a marker.

The following mechanism can be used to start the algorithm: each agent takes its snapshot when its local clock reaches T, if the agent has not already taken its snapshot. This only requires that clock times increase forever; it does not require that clocks be synchronized.

Logical clocks

The snapshot algorithm never needed a clock. But the pictures in this class keep asking a question a clock would answer: given a computation, can we put a number on every event so that the numbers only ever increase along the arrows? Not real time — there is none to be had — but a logical time.

Logical timestamps Assign a number t(e) to every event e so that for every edge (e, e′) of the dataflow graph — every agent step and every message — t(e) < t(e′).

One consequence is the property this class has been circling: for any number t, every message received before t was sent before t. A cut taken at a logical time is a consistent cut, by construction.

Assigned so far
Still unlabelled
t(e) < t(e′) on every edge?
Figure 8. Logical time on the same computation. The computation of the previous two figures, with no snapshot and no markers — just the events and the messages. The three initial events start at 0. Press Next timestamp and the events with logical time 1 are labelled, then 2, and so on. An event's timestamp is one more than the largest of its inputs, and it has two kinds of input: the previous step of its own agent, and the message it took. So x2 is 1 because it follows x1 and takes m3 from z1, both at 0. The values do not come out in tidy rows. x3 and z3 are 2, but y3 is 3 — it takes m8 from z3, so it cannot share a number with it — and x4 is 4, because it takes m7 from y3. A logical timestamp measures the longest chain of causes behind an event, not its position in a queue. Two steps follow. First, a dotted red timestamp of 3 on X's line, between x3 and x4: no event of X carries time 3, but the time is there all the same, and it is a point on X's line like any other. Every logical time is such a point on every agent's line, and the three of them together are a cut. Then the cut at t = 2.5, drawn through the three gaps between the events with t ≤ 2 and those with t ≥ 3. It is a consistent cut, and not by luck: any message it crosses was sent at a time below 2.5 and received at a time above, because t increases along every arrow. m6 and m8 are the two it crosses. So every logical time gives a consistent cut, for nothing. That is the next class.

Clocks that drift

Where do the numbers come from? Each agent has a clock, and no two clocks agree. Here X and Y have accurate ones, so for them a timestamp is simply the physical distance from the start. Z's clock is slow — it runs at half speed — and a slow clock would break the rule that time increases along every message: Z would stamp an event with a time earlier than the message that caused it.

So an agent does not merely read its clock. It sets it forward. If a message arrives carrying time 3.0 while Z's clock reads 2.3, Z moves its own clock to 3.1 and ticks on from there, slowly, until the next message pushes it again. The timestamp of an event is then always just what the agent's own clock reads; the clock is simply nudged whenever a message finds it behind. And the nudge is continuous — the clock sweeps up through 2.4, 2.5, 2.6 — which matters in a moment: it means that for every time t there is a point on every agent's line where that agent's clock reads t.

Latest event
Its logical time
Z’s clock
Figure 9. Physical time, and a clock that drifts. The same computation, now laid along an axis of physical time, and the three first events aligned on one vertical: they happen together, at 0. Press Next event and the events appear from left to right, each with its logical timestamp. X and Y keep accurate clocks, so for their events the timestamp is the distance from 0 — x3 at 5.0, y3 at 5.5. Z's clock runs at half speed, and twice a message catches it out. At z3, physical time 4.6, Z's clock reads 2.3; the message m4 it takes there carries 3.0, so Z sets its clock forward to 3.1 and stamps the event 3.1. It then ticks on at half speed, reaching 4.3 by physical time 7 (3.1 plus 1.2), where z4 takes m6 from x3; that message carries 5.0, so the clock is set forward again, to 5.1. The orange line under those two boxes is the jump: what Z's clock read an instant before, and what Z set it to. That is the whole mechanism: a clock ticks along on its own, and a message drags it forward. Nobody knows the physical time, and nothing needs to.

A snapshot at a logical time

Now put the two ideas together. Name a time, and every agent records its state at the moment its own clock passes that time. There is always such a moment, because a clock moves continuously, even while it is being set forward. No coordination, no markers — and what comes out is a consistent cut, because time only ever increases along a message.

In the past
In the channels
A consistent cut?
Figure 10. Snapshots at a logical time. The computation of the previous figure, every timestamp in place. Press either button and the cut for that logical time is drawn: down through X and Y, where the clocks are accurate and the cut is vertical, then sideways to meet Z later in physical time, because Z's clock is slow and reaches that reading afterwards. At 1.5 the past is x1, y1, z1, z2 and three messages are in the channels. At 2.5 x2 joins the past and m3 has been delivered. Both are consistent cuts, and not by accident: a message leaves at one time and arrives at a greater one, so no cut at a fixed time can have a message arriving in its past that was sent in its future. The second one is worth a second look. Z's clock ticks up to 2.3 and is then set forward to 3.1, so it reads 2.5 during that adjustment, at the instant m4 arrives — and that is exactly where Z records, a hair before z3. Nothing is missing and nothing is special-cased: because a clock sweeps through the readings it skips, every time has its point on every line, and each agent finds its own by watching one number of its own.

Deadlock, which is termination of a part

A deadlocked system is usually not a system that has stopped. It is one where most of the machinery is humming along and some subset of it has quietly died: a set of agents each waiting for a message that only another member of the set could send. Nothing in any one agent's own state says so. Each of them sees only that it is waiting, which is the most ordinary thing in the world.

Here is the smallest example worth drawing. Three agents in a ring, each with two inboxes and two outboxes, each running this program:

loop:
    t = receive(in1)     # a token, from the agent behind me in the ring
    m = receive(in2)     # a data item, from the pipeline
    send(out1, t)        # pass the token on, round the ring
    send(out2, m)        # pass the data on, down the pipeline

Each in1 is fed by the previous agent's out1, so channel 1 closes into a ring: u → v → w → u. Each in2 is fed by the previous agent's out2, so channel 2 is a straight pipeline: src1 → u → v → w → snk1. A second strand, src2 → x → snk2, runs alongside and shares nothing with the first, so that there is always something on the screen that the trouble does not touch.

● a token, on channel 1 ■ a data item, on channel 2 green can still take a step red will never take another step
Waiting
In the channels
Verdict
Figure 11. Deadlock, and two things it is not. The graph is the same in all three cases. Only the global state differs. Case 1. No token exists anywhere, so all three agents block at receive(in1) and the three ring channels stay empty for ever. src1 keeps sending, and its messages pile up in u:in2: u has work in hand and cannot look at it, because it is waiting on the wrong inbox. u, v, w and snk1 turn red — nothing can ever reach them again — while src2, x and snk2 run on, untouched. That is the sentence worth keeping: a deadlock is not a system stopping, it is a part of a system terminating while the rest works normally. And the part is exactly what a predicate over a subset names: these agents, all waiting, with every channel between them empty. Case 2. Add one edge, src1 → u:in1, and the deadlock is gone: tokens arrive, everything moves, data reaches snk1. But a token only ever leaves an agent on out1, back into the ring, so every token src1 injects stays in the ring for ever and their number grows without bound. Nothing is stuck, nothing has terminated, and the system is still wrong. The repair is to break the cycle: let w send every N th token on out2 instead, and the population stops growing. Notice that no single snapshot can show this, because no single state is bad. It takes two, and the observation that the count went up in between. Case 3. Both sources have finished. The figure winds down: the last data items flow through to snk1 and snk2, each agent ends up holding a token and blocking at receive(in2), and every channel empties. Now everything is red. That is true termination — every agent waiting, every channel empty — and unlike case 2 it is stable: once true it stays true, which is exactly why one snapshot can settle it. The colours are not asserted here. The figure computes them, turning a component red only when it can prove that no message will ever reach it again. Look at what that proof needs: the contents of every channel and the state of every agent, at one instant. No agent has that, and that is the whole argument for taking a snapshot.

Each of the three verdicts — a dead subset, an unbounded population, a terminated system — is a property of a global state, and none of the eight components can evaluate any of them alone. That is what the next figures are for.