0% found this document useful (0 votes)
39 views37 pages

Lecture-03. Leader Election

Uploaded by

R N Shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views37 pages

Lecture-03. Leader Election

Uploaded by

R N Shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Lecture: 03

Leader Election in Rings

Rajiv Misra
Dept. of Computer Science & Engineering
Indian Institute of Technology Patna
[email protected]
Preface

Recap of Previous Lecture:


• In the previous lecture, we have discussed the formal model of a
distributed message passing system i.e. synchronous and
asynchronous timing models with no-failures.
• Few simple algorithms for message-passing systems were
demonstrated to understand their concepts and complexity
measures.
• The algorithms solve the problems of broadcast, convergecast,
DFS and used as a basic building blocks of distributed algorithm.
Preface

Content of this Lecture:


• In this lecture, we will discuss the leader election problem in
message-passing systems for a ring topology, in which a group
of processors must choose one among them to be a leader.
• We will present the different algorithms for leader election
problem by taking the cases like anonymous/ non-anonymous
rings, uniform/ non-uniform rings and synchronous/
asynchronous rings etc.
Leader Election (LE) Problem: Introduction

• The leader election problem has several variants.


• LE problem is for each processor to decide that either it is the leader or
non-leader, subject to the constraint that exactly one processor decides
to be the leader.
• LE problem represents a general class of symmetry-breaking problems.
• For example, when a deadlock is created, because of processors waiting
in a cycle for each other, the deadlock can be broken by electing one of
the processor as a leader and removing it from the cycle.
Leader Election: Definition

• Each processor has a set of elected (won) and not-elected (lost) states.
• Once an elected state is entered, processor is always in an elected state
(and similarly for not-elected): i.e., irreversible decision
• In every admissible execution:
• every processor eventually enters either an elected or a not-elected
state
• exactly one processor (the leader) enters an elected state
Uses of Leader Election

• A leader can be used to coordinate activities of the system:


• find a spanning tree using the leader as the root
• reconstruct a lost token in a token-ring network

• In this lecture, we will study the leader election in rings.


Ring Networks

• In an oriented ring, processors have a consistent notion of left and right

• For example, if messages are always forwarded on channel 1, they will


cycle clockwise around the ring
Why Study Rings?

• simple starting point, easy to analyze


• abstraction of a token ring
• lower bounds and impossibility results for ring topology also apply to
arbitrary topologies
Anonymous Rings

• How to model situation when processors do not have unique


identifiers?
• First attempt: require each processor to have the same state machine
• Subtle point: does algorithm rely on knowing the ring size (number of
processors)?
Uniform (Anonymous) Algorithms

• A uniform algorithm does not use the ring size (same algorithm for each
size ring)
• Formally, every processor in every size ring is modeled with the same
state machine
• A non-uniform algorithm uses the ring size (different algorithm for each
size ring)
• Formally, for each value of n, every processor in a ring of size n is
modeled with the same state machine An .
• Note the lack of unique ids.
Leader Election in Anonymous Rings
Theorem: There is no leader election algorithm for anonymous rings, even if
algorithm knows the ring size (non-uniform) and synchronous model
Proof Sketch:
• Every processor begins in same state with same outgoing messages (since
anonymous)
• Every processor receives same messages, does same state transition, and
sends same messages in round 1
• Ditto for rounds 2, 3, …
• Eventually some processor is supposed to enter an elected state.
But then they all would.
Leader Election in Anonymous Rings

• Proof sketch shows that either safety (never elect more than one
leader) or liveness (eventually elect at least one leader) is violated.
• Since the theorem was proved for non-uniform and synchronous rings,
the same result holds for weaker (less well-behaved) models:
• uniform
• asynchronous
Rings with Identifiers

• Assume each processor has a unique id.


• Don't confuse indices and ids:
• indices are 0 to n - 1; used only for analysis, not available to the
processors
• ids are arbitrary nonnegative integers; are available to the
processors through local variable id.
Specifying a Ring
• Start with the smallest id and list ids in clockwise order.

id = 37
id = 3
p4 p0

p1 id = 19
p3
id = 25 p2
id = 4

• Example: 3, 37, 19, 4, 25


Uniform (Non-anonymous) Algorithms

• Uniform algorithm: there is one state machine for every id, no matter
what size ring
• Non-uniform algorithm: there is one state machine for every id and
every different ring size
• These definitions are tailored for leader election in a ring.
O(n2) Messages LE Algorithm:
LeLann-Chang-Roberts (LCR) algorithm

• send value of own id to the left


• when receive an id j (from the right):
• if j > id then
• forward j to the left (this processor has lost)
• if j = id then
• elect self (this processor has won)
• if j < id then
• do nothing
Analysis of O(n2) Algorithm

Correctness: Elects processor with largest id.


• message containing largest id passes through every processor
Time: O(n)
Message complexity: Depends how the ids are arranged.
• largest id travels all around the ring (n messages)
• 2nd largest id travels until reaching largest
• 3rd largest id travels until reaching largest or second largest etc.
Analysis of O(n2) Algorithm
Analysis of O(n2) Algorithm

• Worst way to arrange the ids is in decreasing order:


• 2nd largest causes n - 1 messages
• 3rd largest causes n - 2 messages etc.

• Total number of messages is n + (n-1) + (n-2) + … + 1 = (n2)


Analysis of O(n2) Algorithm

• Clearly, the algorithm never sends more than


O(n2) messages in any admissible execution.
Moreover, there is an admissible execution in
which the algorithm sends (n2) messages;
Consider the ring where the identifiers of the
processor are 0,……, n-1 and they are ordered as
in Figure 3.2. In this configuration, the message
of processor with identifier i is send exactly i+1
times, Thus the total number of messages,
including the n termination messages, is

Clockwise Unidirectional Ring


Can We Use Fewer Messages?

• The O(n2) algorithm is simple and works in both synchronous and


asynchronous model.
• But can we solve the problem with fewer messages?
Idea:
• Try to have messages containing smaller ids travel smaller distance in
the ring
O(nlogn) Messages LE Algorithm:
The Hirschberg and Sinclair (HS) algorithm

• To describe the algorithm, we first define the k-neighbourhood of a


processor pi in the ring to be the set of processors that are at distance
at most k from pi in the ring (either to the left or to the right). Note that
the k-neighbourhood of a processor includes exactly 2k+1 processors.
• The algorithm operates in phases; it is convenient to start numbering the
phases with 0. In the kth phase a processor tries to become a winner for
that phase; to be a winner, it must have the largest id in its
2k-neighborhood. Only processors that are winners in the kth phase
continue to compete in the (k+1)-st phase, Thus fewer processors
proceed to higher phases, until at the end, only one processor is a winner
and it is elected as the leader of the whole ring.
The HS Algorithm: Sending Messages
Phase 0
• In more detail, in phase 0, each processor attempts to become a
phase 0 winner and sends a <probe> message containing its
identifier to its 1-neighborhood, that is, to each of its two
neighbors.

• If the identifier of the neighbor receiving the probe is greater than


the identifier in the probe, it swallows the probe; otherwise, it
sends back a <reply> message.

• If a processor receives a reply from both its neighbors, then the


processor becomes a phase 0 winner and continues to phase 1.
The HS Algorithm: Sending Messages
Phase k
• In general, in phase k, a processor pi that is a phase k-1 winner sends
<probe> messages with its identifier to its 2k-neighborhood (one in
each direction). Each such message traverses 2k processors one by
one, A probe is swallowed by a processor if it contains an identifier
that is smaller than its own identifier.
• If the probe arrives at the last processor on the neighbourhood
without being swallowed, then that last processor sends back a
<reply> message to pi. If pi receives replies from both directions, it
becomes a phase k winner, and it continues to phase k+1. A processor
that receives its own <probe> message terminates the algorithm as
the leader and sends a termination message around the ring.
The HS Algorithm

• The pseudocode appears in Algorithm 5. Phase k for a processor corresponds to


the period between its sending of a <probe> message in line 4 or 15 with third
parameter k and its sending of a <probe> message in line 4 or 15 with third
parameter k+1. The details of sending the termination message around the ring
have been left out in the code, and only the leader terminates.

• The correctness of the algorithm follows in the same manner as in the simple
algorithm, because they have the same swallowing rules.

• It is clear that the probes of the processor with the maximal identifier are
never swallowed; therefore, this processor will terminate the algorithm as a
leader. On the other hand, it is also clear that no other <probe> can traverse
the whole ring without being swallowed. Therefore, the processor with the
maximal identifier is the only leader elected by the algorithm.
O(n log n) Leader Election Algorithm

• Each processor tries to probe successively larger neighborhoods in both


directions
• size of neighborhood doubles in each phase

• If probe reaches a node with a larger id, the probe stops


• If probe reaches end of its neighborhood, then a reply is sent back to
initiator
• If initiator gets back replies from both directions, then go to next phase
• If processor receives a probe with its own id, it elects itself
O(n log n) Leader Election Algorithm

pi
probe probe
reply reply

probe probe probe probe


reply reply reply reply

probe probe probe probe probe probe probe probe


reply reply reply reply reply reply reply reply
Analysis of O(n log n) Leader Election Algorithm

Correctness:
• Similar to O(n2) algorithm.
Message Complexity:
• Each message belongs to a particular phase and is initiated by a
particular processor
• Probe distance in phase k is 2k
• Number of messages initiated by a processor in phase k is at most
4*2k (probes and replies in both directions)
Analysis of O(n log n) Leader Election Algorithm

• How many processors initiate probes in phase k ?


• For k = 0, every processor does
• For k > 0, every processor that is a "winner" in phase k - 1 does
• "winner" means has largest id in its 2k-1 neighborhood
Analysis of O(n log n) Leader Election Algorithm
• Maximum number of phase k - 1 winners occurs when they are packed
as densely as possible:

2k-1 processors

… a phase
k-1 winner … a phase
k-1 winner

• total number of phase k - 1 winners is at most


n/(2k-1 + 1)
Analysis of O(n log n) Leader Election Algorithm

• How many phases are there?


• At each phase the number of (phase) winners is cut approx. in half
• from n/(2k-1 + 1) to n/(2k + 1)

• So after approx. log2 n phases, only one winner is left.


• more precisely, max phase is log(n–1)+1
Analysis of O(n log n) Leader Election Algorithm

• Total number of messages is sum, over all phases, of number of winners


at that phase times number of messages originated by that winner:

log(n–1)+1

phase 0 msgs
≤ 4n + n +  4•2k•n/(2k-1+1)
k=1

termination
msgs < 8n(log n + 2) + 5n
msgs for
phases 1 to
= O(n log n) log(n–1)+1
Can We Do Better?

• The O(n log n) algorithm is more complicated than the O(n2) algorithm
but uses fewer messages in worst case.
• Works in both synchronous and asynchronous case.
• Can we reduce the number of messages even more?
• Not in the asynchronous model…
Lower bound for LE algorithm
But, can we do better than O(n log n)?
Theorem: Any leader election algorithm for asynchronous rings
whose size is not known a priori has ῼ(n log n) message
complexity (holds also for unidirectional rings).
• Both LCR and HS are comparison-based algorithms, i.e. they
use the identifiers only for comparisons (<; >;=).
• In synchronous networks, O(n) message complexity can be
achieved if general arithmetic operations are permitted (non-
comparison based) and if time complexity is unbounded.
Overview of LE in Rings with Ids

• There exist algorithms when nodes have unique ids.


• We have evaluated them according to their message complexity.
• asynchronous ring:
• (n log n) messages

• synchronous ring:
• (n) messages under certain conditions
• otherwise (n log n) messages

• All bounds are asymptotically tight.


Conclusion
• This lecture provided an in-depth study of the leader
election problem in message-passing systems for a ring
topology.
• We have presented the different algorithms for leader
election problem by taking the cases like anonymous/non-
anonymous rings, uniform/non-uniform rings and
synchronous/ asynchronous rings
• In upcoming lecture, we will discuss about causality and
time.

You might also like