Coordination Algorithms
Coordination Algorithms
Engineering (RA)
COURSE NAME: PARALLEL &DISTRIBUTED COMPUTING
COURSE CODE: 22CS4106 R
CO-4 Session -
21
MUTUAL EXCLUSION
Mutual exclusion is frequently used to secure shared resources inside a distributed system.
It could be important, for example, to guarantee that no other process modifies a shared resource while
another is working on it.
There are three mutual exclusion algorithms in distributed systems are follows:
Centralized
Distributed
Token Ring
• Let us first understand the primitive concept of a centralized algorithm before we dive into the modern
distributed and token ring systems.
2
3
4
Advantages of a centralized mutually exclusive algorithm:
Simple to carry out. It consists of only three messages associated with each critical region: request, grant and
release.
The processes do not undergo starvation. Starvation is when a process waits for ever for it’s turn to arrive.
Using the FIFO (first in first out) order for permission the algorithm remains judicious in its ways by offering a fair
chance it every process.
Disadvantages of a centralized mutually exclusive algorithm:
The problem of a single coordinator being the only source of management and progress in the system could result
into blockage and a single point of failure.
The processes that remain blocked cannot determine whether the purpose for blockage is a dead coordinator or an
occupied critical region.
If it is a large system, it is prone to performance bottlenecks.
5
With this algorithm, mutual exclusion is guaranteed without deadlock or starvation.
If the total number of processes is N, then the number of messages that a process needs to send and
receive before it can enter its critical region is 2· (N −1): N−1 request messages to all other processes, and
subsequently N−1 OK messages, one from each other process.
A critical region is a series of instructions to be executed by a process, which requires mutually exclusive
access.
Unfortunately, this algorithm has N points of failure. If any process crashes, it will fail to respond to
requests.
This silence will be interpreted (incorrectly) as denial of permission, thus blocking all subsequent
attempts by all processes to enter any of their respective critical regions.
The algorithm can be patched up as follows. When a request comes in, the receiver always sends a reply,
either granting or denying permission.
After a request is denied, the sender should block, waiting for a subsequent OK message.
6
Another problem with this algorithm is that either a multicast communication primitive
must be used, or each process must maintain the group membership list itself, including
processes entering the group, leaving the group, and crashing.
The method works best with small groups of processes that never change their group
memberships. Finally, note that all processes are involved in all decisions concerning
accessing the shared resource, which may impose a burden on processes running on
resource-constrained machines.
Various minor improvements are possible to this algorithm.
For example, getting permission from everyone is overkill. All that is needed is a method
to prevent two processes from accessing the resource at the same time.
The algorithm can be modified to grant permission when it has collected permission
from a simple majority of the other processes, rather than from all of them
7
8
9
MUTUAL EXCLUSION: TOKEN RING ALGORITHM
Essence
Organize processes in a logical ring, and let a token be passed between
them. The one that holds the token is allowed to enter the critical region (if it
wants to).
A token-ring algorithm
When the ring is initialized, process P0 is given a token. The token circulates around the ring. Assuming there are N
processes, the token is passed from process Pk to process P(k+1)modN in point-to-point messages.
When a process acquires the token from its neighbour, it checks to see if it needs to access the shared resource.
If so, the process goes ahead, does all the work it needs to, and releases the resources.
After it has finished, it passes the token along the ring.
It is not permitted to enter the resource immediately again using the same token.
If a process is handed the token by its neighbour and is not interested in the resource, it just passes the token along.
As a consequence, when no processes need the resource, the token just circulates around the ring.
The correctness of this algorithm is easy to see.
Only one process has the token at any instant, so only one process can actually get to the resource. Since the token
circulates among the processes in a well-defined order, starvation cannot occur.
This algorithm has its own problems. If the token is ever lost, for example because its holder crashes or due to a lost
message containing the token, it must be regenerated.
11
In fact, detecting that it is lost may be difficult, since the amount of time between
successive appearances of the token on the network is unbounded.
The fact that the token has not been spotted for an hour does not mean that it has
been lost; somebody may still be using it.
The algorithm also runs into trouble if a process crashes, but recovery is relatively easy.
If we require a process receiving the token to acknowledge receipt, a dead process will
be detected when its neighbour tries to give it the token and fails.
At that point, the dead process can be removed from the group, and the token holder
can throw the token over the head of the dead process to the next member down the
line, or the one after that, if necessary.
12
DECENTRALIZED MUTUAL EXCLUSION
Principle
Assume every resource is replicated N times, with each replica having its own
coordinator ⇒ access requires a majority vote from m > N/2 coordinators.
A coordinator always responds immediately to a request.
Assumption
When a coordinator crashes, it will recover quickly, but will have forgotten
about permissions it had granted.
A decentralized algorithm
DECENTRALIZED MUTUAL EXCLUSION
A decentralized algorithm
15
The condition f ≥ 2m - N sets a threshold for when failures become critical. If
the number of failed coordinators reaches this level, the decentralized mutual
exclusion system can no longer guarantee correctness.
16
Mutual exclusion: comparison
A decentralized algorithm
EXAMPLE: ZOOKEEPER
Note
ZooKeeper allows a client to be notified when a node, or a branch in the tree, changes. This may easily
lead to race conditions.
Solution
Use version numbers
Example: Simple locking with ZooKeeper
Coordination
ZooKeeper versioning
Notations
• W (n, k )a: request to write a to node n, assuming current version is k
.
• R(n, k ): current version of node n is k .
• R(n): client wants to know the current value of node n
• R(n, k )a: value a from node n is returned with its current version k .
Principle
An algorithm requires that some process acts as a coordinator. The question
is how to select this special process dynamically.
Note
In many systems, the coordinator is chosen manually (e.g., file servers).
This leads to centralized solutions ⇒ single point of failure.
Teasers
1. If a coordinator is chosen dynamically, to what extent can we speak
about a centralized or distributed solution?
2. Is a fully distributed solution, i.e. one without a coordinator, always more
robust than any centralized/coordinated solution?
Basic assumptions
Principle
Consider N processes {P0 , . . . , PN−1} and let id (Pk ) = k . When a
process Pk notices that the coordinator is no longer responding to requests, it
initiates an election:
1. Pk sends an ELECTION message to all processes with higher
identifiers:
Pk + 1 , Pk + 2 , . . . , PN−1.
Principle
Process priority is obtained by organizing processes into a (logical) ring. The process with
the highest priority should be elected as coordinator.
• Any process can start an election by sending an election message to its successor. If a
successor is down, the message is passed on to the next successor.
• If a message is passed on, the sender adds itself to the list. When it gets back to the
initiator, everyone had a chance to make its presence known.
• The initiator sends a coordinator message around the ring containing a list of all living
processes. The one with the highest priority is elected as coordinator.
A ring algorithm
Coordination
Election in a ring
Election algorithm using a ring
A ring algorithm
EXAMPLE: LEADER ELECTION IN ZOOKEEPER SERVER GRO
Basics
• Each server s in the server group has an identifier id(s)
• Each server has a monotonically increasing counter tx(s) of the latest transaction it handled (i.e.,
series of operations on the namespace).
• When follower s suspects leader crashed, it broadcasts an ELECTION
message, along with the pair (voteID,voteTX ). Initially,
• voteID ← id(s)
• voteTX ← tx(s)
• Each server s maintains two variables:
• leader(s): records the server that s believes may be final leader. Initially, leader(s) ← id(s).
• lastTX(s): what s knows to be the most recent transaction. Initially, lastTX(s) ← tx(s).
Note
When s∗ believes it should be the leader, it broadcasts ⟨id(s∗), tx(s∗)⟩. Essentially, we’re bullying.