Module 2
Module 2
Module - II
Syllabus Topics
Logical time – A framework for a system of logical clocks, Scalar time; Vector
time. Leader election algorithm – Bully Algorithm, Ring Algorithm. Global
state and snapshot recording algorithms – System model and definitions.
Snapshot algorithm for FIFO channels – Chandy Lamport algorithm.
Termination detection – System model of a distributed computation.
Termination detection using distributed snapshots. Termination detection by
weight throwing, Spanning tree-based algorithm.
Logical time
▪ Causality between events - fundamental to design and analysis of
parallel and distributed computing and operating systems
▪ In distributed systems,
➢ Concurrency measure
➢ How many events are causally related ->concurrency in the computation
➢ All the events not causally related can be executed concurrently
Logical time
▪ The concept of causality is widely used by human beings, often
unconsciously, in the planning, scheduling, and execution
2. Vector time
3. Matrix Time
A framework for a system of logical
clocks : Definition
➢ A system of logical clocks consists of a time domain T and a logical clock C
➢ This relation is
➢ usually called the happened before or causal precedence.
➢ analogous to the earlier than relation provided by the physical time.
➢ The logical local clock of a process pi and its local view of the
global time are squashed into one integer variable Ci.
SCALAR TIME
Rules R1 and R2 to update the clocks are as follows:
SCALAR TIME
• Figure shows evolution of scalar time with d=1
SCALAR TIME
Basic properties
1. Consistency property
2. Total Ordering
▪ Scalar clocks can be used to totally order events in a distributed system
▪ The main problem in totally ordering events is that two or more events at
different processes may have an identical timestamp.
Scalar Time
➢ The third event of process P1 has smaller scalar Timestamp than the third
event of process P2. However, the former did not happen before the latter.
➢ The reason that scalar clocks are not strongly consistent is that the logical local
clock and logical global clock of a process are squashed into one, resulting in
the loss causal dependency information among events at different processes.
Vector Time
▪ The system of vector clocks was developed independently by Fidge ,
Mattern, and Schmuck .
▪ distributed debugging
▪ implementations of causal ordering communication
▪ causal distributed shared memory
▪ establishment of global breakpoints
▪ determining the consistency of checkpoints in optimistic
recovery
Leader election algorithm
▪ If the process that plays the role of coordinator wishes to retire then
another election is required to choose a replacement.
▪ We say that a process calls the election if it takes an action that initiates
a particular run of the election algorithm.
● Bully algorithm
1. A ring-based election algorithm
➢ Each process pi has a communication channel to the next process
in the ring, p(i + 1) mod N
➢ All messages are sent clockwise around the ring.
➢ Goal of this algorithm: To elect a single process called the
coordinator
➢ Initially, every process is marked as a non-participant in an
election.
➢ Any process can begin an election.
➢ It proceeds by marking itself as a participant
➢ placing its identifier in an election message and
➢ sending it to its clockwise neighbour.
➢ When a process receives an election message, it compares the identifier
in the message with its own.
➢ If the arrived identifier is greater, then it forwards the message to its
neighbour.
➢ If the arrived identifier is smaller and the receiver is not a participant, then
➢ it substitutes its own identifier in the message and forwards it;
➢ but it does not forward the message if it is already a participant.
➢ If, the received identifier is that of the receiver itself, then this process’s
identifier must be the greatest, and it becomes the coordinator.
➢ The coordinator marks itself as a non-participant once more and sends an
elected message to its neighbour, announcing its election and enclosing its
identity
➢ The process that knows it has the highest identifier can elect itself as the
coordinator simply by sending a coordinator message to all processes
with lower identifiers.
➢ A process with a lower identifier can begin an election by sending an
election message to those processes that have a higher identifier and
awaiting answer messages in response.
➢ If none arrives within time T, the process considers itself the
coordinator and sends a coordinator message to all processes with
lower identifiers announcing this.
electi on
1. The process begins an election by C
electi on
sending an election message to these Stag e1
processes that have a higher ID and p answe r
p p p
1 2 3 4
awaits an answer in response. answe r
2. If none arrives within time T, the election
P5
P9
P10
P3
P18
P3
Bully Algorithm – Work out
• Pid’s 0,4,2,1,5,6,3,7
P7 was the initial coordinator and crashed
Illustrate Bully algorithm, if P4 initiates election , Calculate total
number of election messages and coordinator messages
Global state and snapshot recording algorithms
▪ Recording the global state of a distributed system on-the-fly is an important
paradigm when one is interested in analyzing, testing, or verifying properties
associated with distributed execution
▪ Unfortunately, the lack of both a globally shared memory and a global clock in a
distributed system, added to the fact that message transfer delays in these systems are
finite but unpredictable, makes this problem non-trivial.
▪ The state of a process is characterized by the state of its local memory and a history
of its activity.
▪ The state of a channel is characterized by the set of messages sent along the channel
Global state and snapshot recording algorithms
▪ The global state of a distributed system is a collection of the local states of its
components
System model
▪ The system consists of a collection of n processes, p1, p2, , pn, that are
connected by channels.
▪ There is no globally shared memory and processes communicate solely by
passing messages.
▪ There is no physical global clock in the system. Message send and receive is
asynchronous.
▪ Messages are delivered reliably with finite but arbitrary time delay.
▪ The system can be described as a directed graph in which vertices represent
the processes and edges represent unidirectional communication channels.
Global state and snapshot recording algorithms
System model
▪ Let Cij denote the channel from process pi to process pj
▪ The state of a process at any time is defined by the contents of processor registers,
stacks, local memory
▪ The actions performed by a process are modeled as three types of events, namely,
internal events, message send events, and message receive events.
▪ For a message mij that is sent by process pi to process pj, let send(mij) and rec(mij)
denote its send and receive events, respectively.
▪ A send event (or a receive event) changes the state of the process that sends
(or receives) the message and the state of the channel on which the message
is sent (or received).
▪ At any instant, the state of process pi, denoted by LSi, is a result of the
sequence of all the events executed by pi up to that instant
Global state and snapshot recording algorithms
▪ A cut is a line joining an arbitrary point on each process line that slices the
space–time diagram into a PAST and a FUTURE.
▪ All the messages that cross the cut from the PAST to the FUTURE are captured
in the corresponding channel state.
Global state and snapshot recording algorithms
Interpretation in terms of cuts
▪ If a global physical clock were available, the following simple procedure could be
used to record a consistent global snapshot of a distributed system.
▪ The initiator of the snapshot collection decides a future time at which the
snapshot is to be taken and broadcasts this time to every process.
▪ All processes take their local snapshots at that instant in the global time.
▪ However, a global physical clock is not available in a distributed system and the
following two issues need to be addressed in recording of a consistent global
snapshot of a distributed system
• After a site has recorded its snapshot, it sends a marker along all of its
outgoing channels before sending out any more messages.
A process initiates snapshot collection by executing the marker sending rule by which
it records its local state and sends a marker on each outgoing channel
The algorithm can be initiated by any process by executing the marker sending rule.
The algorithm terminates after each process has received a marker on all of its
incoming channels.
The recorded local snapshots can be put together to create the global snapshot
Snapshot algorithms for FIFO channels
Chandy–Lamport algorithm
Chandy-Lamport Algorithm
The essential idea of the algorithm is as follows:
• Each process records its state and also, for each incoming channel, a set of
messages sent to it.
• The process records, for each channel, any messages that arrived after it
recorded its state and before the sender recorded its own state.
• The algorithm proceeds through use of special marker messages,
– Marker msgs are distinct from any other messages the processes send
– the processes may send and receive markers while they proceed with their
normal execution.
• The marker has a dual role:
– as a prompt for the receiver to save its own state, if it has not already
done so; and
– as a means of determining which messages to include in the channel state.
Termination Detection
▪ In distributed processing systems, a problem is typically solved in a distributed
manner with the cooperation of a number of processes.
▪ All messages are received correctly after an arbitrary but finite delay.
▪ Messages sent over the same communication channel may not obey the FIFO
ordering.
Termination Detection
System model of a distributed computation
1. At any given time during execution of the distributed computation, a process can
be in only one of the two states: active, and idle.
3. An idle process can become active only on the receipt of a message from another
process
5. A message can be received by a process when the process is in either of the two
states.
6. The sending of a message and the receipt of a message occur as atomic actions.
Termination Detection
System model of a distributed computation
1. Termination detection using distributed
snapshots
▪ When a computation terminates, there must exist a unique process which became
idle last.
▪ When a process goes from active to idle, it issues a request to all other processes
to take a local snapshot, and also requests itself to take a local snapshot.
▪ When a process receives the request, if it agrees that the requester became idle
before itself, it grants the request by taking a local snapshot for the request.
▪ A request is said to be successful if all processes have taken a local snapshot for it.
▪ The requester or any external agent may collect all the local snapshots of a
request.
Termination detection using distributed snapshots
Informal description
▪ In the recorded snapshot, all the processes are idle and there is no message in
transit to any of the processes
2. Termination detection by weight throwing
• Rule R3: when a process becomes idle, it updates its local clock,
sends a request for snapshot R(x, k) to every other process, and
takes a local snapshot for this request.
(x = logical clock value, k = process id, R(x, k) = control message)
2. Termination detection by weight throwing
• Rule R4 (most interesting): On the receipt of a message R(x′ , k′),
1. The process takes a local snapshot if it is idle and (x′, k′) > (x, k), i.e., timing
in the message is later than the local time at the process, implying that the
sender of R(x′, k′) terminated after this process.
In this case, it is likely that the sender is the last process to terminate and thus,
the receiving process takes a snapshot for it. Because of this action, every
process will eventually take a local snapshot for the last request when the
computation has terminated.
2. In the second case, (x′, k′) ≤ (x, k), implying that the sender of R(x′, k′)
terminated before this process.
Hence, the sender of R(x′, k′) cannot be the last process to terminate. Thus,
the receiving process does not take a snapshot for it.
3. In the third case, the receiving process has not even terminated. Hence, the
sender of R(x′, k′) cannot be the last process to terminate and no snapshot is
taken.
• The last process to terminate will have the largest clock value. Therefore, every
process will take a snapshot for it; however, it will not take a snapshot for any
other process.
2. Termination detection by weight throwing
▪ In termination detection by weight throwing, a process called controlling
agent monitors the computation.
▪ The computation starts when the controlling agent sends a basic message to
one of the processes.
▪ When a process sends a message, it sends a part of its weight in the message.
▪ Thus, the sum of weights on all the processes and on all the messages in
transit is always 1.
▪ When a process becomes idle, it sends its weight to the controlling agent in a
control message, which the controlling agent then adds to its weight.
▪ The edges of the graph represent the communication channels, through which a
process sends messages to neighbouring processes in the graph.
▪ The algorithm uses a fixed spanning tree of the graph with process P0 at its root
which is responsible for termination detection
▪ Process P0 communicates with other processes to determine their states and the
messages used for this purpose are called signals.
▪ A parent node will similarly report to its parent when it has completed processing
and all of its immediate children have terminated, and so on.
▪ The root concludes that termination has occurred, if it has terminated and all of its
immediate children have also terminated
3. A spanning-tree-based termination detection algorithm
▪ The termination detection algorithm generates two waves of signals moving
inward and outward through the spanning tree.
▪ If this token wave reaches the root without discovering that termination has
occurred, the root initiates a second outward wave of repeat signals.
▪ As this repeat wave reaches leaves, the token wave gradually forms and starts
moving inward again.
• Each leaf process, after it has terminated, sends its token to its parent.
• When a parent process terminates and after it has received a token from
each of its children, it sends a token to its parent.
• This way, each process indicates to its parent process that the subtree
below it has become idle.
• The root of the tree concludes that termination has occurred, after it has
become idle and has received a token from each of its children.
3. A spanning-tree-based termination detection algorithm
3. A spanning-tree-based termination detection algorithm
3. A spanning-tree-based termination detection algorithm
3. A spanning-tree-based termination detection algorithm
3. A spanning-tree-based termination detection algorithm
3. A spanning-tree-based termination detection algorithm
3. A spanning-tree-based termination detection algorithm
Tutorial Questions
A. Logical Time B. Leader Election Algorithms
Q1. Lamport Clock Application Q3. Bully Algorithm Execution
A distributed system has three processes P1, A distributed system has five processes
P2, and P3. with IDs {2, 4, 6, 8, 10}.
The initial clock value of each process is 0. Process 10 (leader) fails, and
P1 executes internal event e1 and sends a process 4 detects the failure.
message to P2 Apply the Bully algorithm to:
P2 executes internal event e2 after receiving the List the messages exchanged
message Identify the new leader
P2 sends a message to P3 Q4. Ring Algorithm Execution
P3 executes internal event e3 after receiving the Consider a ring of six processes with IDs {1,
message 3, 5, 7, 9, 11}.
Apply Lamport’s scalar clock rules to assign Process 7 initiates a leader election.
timestamps to all events in the space-time Apply the Ring algorithm and show:
diagram. The order in which messages circulate
Q2. Vector Clock Application
The process elected as leader
For the same system in Question 1, apply vector
clock rules and assign vector timestamps to
each event.
Using the timestamps, identify whether any
two events are concurrent.
Spanning Tree Workout
• Apply spanning tree-based termination
detection algorithm in the following scenario.
The nodes are processes 0 to 6. Leaf nodes 3,
4, 5, and 6 are each given tokens T3, T4, T5
and T6 respectively. Leaf nodes 3, 4, 5 and 6
terminate in the order, but before terminating
node 5,it sends a message to node 1