DisSysLab · Distributed algorithms

Diffusing Computations

How the agent that started a computation learns that it is over

One agent starts a computation by sending messages. The agents that receive them do some work and send more messages, and so on: the computation spreads, or diffuses, through the system. How does the agent that started it find out that it has ended? This class develops the answer that Dijkstra and Scholten gave in 1980. As before, the arguments are informal, and the animations show why they hold.

1. Diffusing computations

Agents send messages to each other along channels. Each agent is active or idle.

Terminated

The computation has terminated when every agent, the initiator included, is idle, and every channel is empty.

Once terminated, the computation stays terminated: no agent is active to send a message, and no message is left to wake an agent up. Note that every agent idle is not enough.

Figure 1. A diffusing computation. Purple agents are active, green agents idle; the initiator has a double ring. Purple dots are messages, travelling on the side of the line that leads to their destination. Watch step 8: every agent is idle, but a message is still on its way to u, so the computation has not terminated. When the message arrives, u becomes active again.

2. The problem

The initiator has a variable claim, initially false. Devise an algorithm, running alongside the computation, that sets claim to true when the computation has terminated.

Specification

Safety. If claim is true, the computation has terminated.

Progress. If the computation terminates, claim eventually becomes true.

3. The idea: a tree rooted at the initiator

Keep a structure that contains everything that could still keep the computation going: every active agent, and every agent that has sent a message still in transit. If the structure shrinks to just the initiator, and the initiator is idle, nothing is left to keep the computation going. Dijkstra and Scholten used a tree rooted at the initiator. Each agent v has a variable v.parent: null if v is not on the tree, and otherwise its parent on the tree.

The invariant
  1. Following parent links from any agent on the tree leads to the initiator.
  2. Every active agent is on the tree.
  3. If a message is in transit from w, then w is on the tree.

An idle agent may stay on the tree, and an idle agent may have active descendants. The tree changes in only two ways:

4. Acknowledgements

How does an agent know that it has no children and no messages in transit? Every message is acknowledged: its receiver eventually sends back an ack. Each agent counts its messages that have not yet been acknowledged.

The algorithm
  1. Receiving a message. The receiver becomes active. If it is not on the tree, it joins with the sender as its parent, and holds on to the ack for that message. Otherwise it sends the ack at once.
  2. Leaving the tree. When an agent off the root is idle and has no unacknowledged messages, it sends the held ack to its parent and sets its parent to null.
  3. Claiming termination. When the initiator is idle and has no unacknowledged messages, it sets claim to true.

The held ack is what makes the counts tell the truth. While v is a child of w, v has not acked the message that made it join, so w has at least one unacknowledged message. And a message in transit has not been acked either. So when all of an agent's messages are acknowledged, it has no children and no messages in transit.

Figure 2. The algorithm, step by step. Thick orange arrows are parent links: the tree. Teal squares are acks. Below each agent is its number of unacknowledged messages. The checks on the right hold at every step. Watch r receive a second message, from s: it is already on the tree, so it acks at once. Then the tree shrinks from its leaves, each leaving agent sending its held ack to its parent, until the initiator claims termination.

Why hold the ack?

Suppose an agent acked every message at once, including the one that made it join. Then its parent's count could reach zero while it is still a child, and the parent could leave the tree, or the initiator could claim termination, while the child is still active.

Figure 3. Acking at once is wrong. The initiator sends a message to p, and p sends one to r. With Ack at once, both acks come straight back, so when p becomes idle it leaves the tree while its child r is still active, and when the initiator becomes idle it claims termination. The checks on the right turn red. With Hold the ack, the initiator must wait until r and then p have left the tree.

5. Why it works

Safety, informally

Each step keeps the invariant. A receiver becomes active and is on the tree, either already or by joining under the sender, which is on the tree. A sender is active, so it is on the tree. An agent that leaves is idle and has no unacknowledged messages, so it has no children and no messages in transit: removing it breaks nothing. Now, when the initiator claims termination, it is idle and has no unacknowledged messages, so it has no children: the tree is just the initiator. By the invariant, no agent is active and no message is in transit. So the computation has terminated.

Progress, informally

Suppose the computation has terminated. Then no agent sends another message, and every message still in transit is an ack. Acks arrive. Every leaf of the tree is idle and, once its acks have arrived, has no unacknowledged messages, so it leaves, sending an ack to its parent. The tree loses an agent each time, so it shrinks to the initiator, which then claims termination.

Figure 4. The tree shrinks. All six agents are on the tree, and no more messages will be sent. The agents become idle; then the leaves leave the tree one at a time, each sending its held ack up. The number of agents on the tree falls from 6 to 1, and the initiator claims termination.
Figure 5. The whole algorithm, at random. Agents send messages, become idle and become active again, in random order, until 30 messages have been sent; after that the computation can only wind down. The checks on the right hold at every step, and claim becomes true only after the computation has terminated. Press Reset for a different run.

6. Notes

The algorithm is due to E. W. Dijkstra and C. S. Scholten, “Termination detection for diffusing computations” (Information Processing Letters, 1980). They call an ack a signal, and an agent's number of unacknowledged messages its deficit. The cost is one ack for every message. The tree is another example of a distributed data structure maintained by the actions of all the agents, none of which sees all of it.