Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Distributed Algorithms (CAS 769) : Dr. Borzoo Bonakdarpour

Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

Distributed Algorithms (CAS 769)

Week 4: Leader Election

Dr. Borzoo Bonakdarpour

Department of Computing and Software


McMaster University

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 1/43
Acknowledgments
Most of the contents of these slides are obtained from the
following:

I Distributed Algorithms: An Intuitive Approach - Wan Fokkink

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 2/43
Presentation outline

Leader Election

Election in Anonymous Networks

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 3/43
Leader Election

Goal
In an election algorithm, each computation should terminate in a
configuration where one process is the leader.

The leader usually acts as the organizer of something or as an initiator of a


centralized algorithm

Assumptions
I All processes have the same local algorithm.
I The algorithm is decentralized: The initiators can be any non-empty set
of processes.
I Process id’s are unique, and from a totally ordered set.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 4/43
Election in Rings

We start with election algorithms in rings. In all these algorithms, the initiators
determine among themselves which one has the highest ID.

Initially, the initiators are active and all nominators are passive.

Passive processes do not participate in the race to become a leader and


simply pass the messages.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 5/43
Chang-Roberts Algorithm

Consider a directed ring. The idea of the algorithm is that only the message
with the highest id completes one round in the ring.
I Each initiator sends a message, tagged with its id.
I When p receives q with q < p, it purges the message.
I When p receives q with q > p, it becomes passive, and passes on the
message.
I When p receives p, it becomes the leader.

Passive processes (including all non-initiators) pass on messages.

Worst-case message complexity: O(N 2 )

Average-case message complexity: O(N log N)

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 6/43
Chang-Roberts Algorithm - Example

2 1

I Clockwise:
0 N(N + 1)
2
I Counter clockwise: 2N − 1

N −2 N −1

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 7/43
Franklin’s Algorithm

Franklin’s algorithm aims at improving the message complexity of


Chang-Roberts algorithm.

Each active process p repeatedly compares its own id with the id’s of its
nearest active neighbors on both sides.

If such a neighbor has a larger id, then p becomes passive.

This will avoid messages to travel unnecessarily.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 8/43
Franklin’s Algorithm

Consider an undirected ring.

I Initially, initiators are active, and non-initiators are passive. Each active
process sends its id to its neighbors on either side.
I Let active process p receive q and r
I if max{q, r } < p, then p sends its id again
I if max{q, r } > p, then p becomes passive
I if max{q, r } = p, then p becomes the leader

Passive processes pass on incoming messages.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 9/43
Franklin’s Algorithm

Worst-case message complexity: O(N log N).

In each round, at least half of the active processes become passive. So there
are at most log2 N rounds.

Each round takes 2N messages.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 10/43
Franklin’s Algorithm - Example

0 4

5 2

1 3
Franklin’s Algorithm - Example

0 4

5 2

1 3
Franklin’s Algorithm - Example

0 4

5 2

1 3
Franklin’s Algorithm - Example

0 4

5 2

1 3

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 11/43
Franklin’s Algorithm - Example

1
I after 1 round only node N − 1 is
2
active
I after 2 rounds node N − 1 is the
leader
0 Suppose this ring is directed with a
clockwise orientation.

If a process would only compare its


id with the one of its predecessor,
N −2 N −1 then it would take N rounds to
complete.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 12/43
Dolev-Klawe-Rodeh Algorithm

Dolev-Klawe-Rodeh algorithm transposes the idea Franklin’s algorithm to a


directed ring.

Each active process p cannot easily compar its id with its nearest active
neighbors q and r .

This is resolved by performing this comparison not at p, but at its next (in the
direction of the ring) active neighbor r . I.e., the ids of p, q, and r are collected
at r :
I If p is larger than q and r , then r remains active and progresses to the
next round, where it assumes the id of r .
I If p is smaller than q or r , then r becomes passive.
I If p equals q and r , then r becomes the leader.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 13/43
Dolev-Klawe-Rodeh’s Algorithm - Details

Consider an directed ring.

The comparison of id’s of an active process p and its nearest active


neighbors q and r is performed at r .

−→ s −→ q −→ p −→ r −→ t −→

I If max{q, r } < p, then r changes its id to p, and sends out p.


I If max{q, r } > p, then r becomes passive.
I If max{q, r } = p, then r announces this id to all processes. The process
that originally had the id p becomes the leader.

Worst-case message complexity: O(N log N).

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 14/43
Dolev-Klawe-Rodeh Algorithm - Example

1 3

5 2

0 4
Dolev-Klawe-Rodeh Algorithm - Example

5
1 3

5 3
2

0
4 4
Dolev-Klawe-Rodeh Algorithm - Example

5
1 3

5 5
3
2

0
4 4
Dolev-Klawe-Rodeh Algorithm - Example

5
1 3

5 5
3
2

0
4 4

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 15/43
Leader Election in Trees

Given an undirected acyclic network.

The tree algorithm (from Week 2 graph algorithms) can be used as an


election algorithm for undirected, acyclic networks

Let the two deciding processes determine which one of them becomes the
leader (e.g. the one with the largest id).

The Idea
Each process p collects ids from its children, computes the maximum of
these ids and its own, and passes on this maximum to its parent.

Later, p receives the overall maximum of the ids in the network from its
parent, which p passes on to its children.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 16/43
Leader Election in Trees - Wake-up phase

Start with a wake-up phase, driven by the initiators.


I Initially, initiators send a wake-up message to all neighbors.
I When a non-initiator receive a first wake-up message, it sends a
wake-up message to all neighbors.
I A process wakes up when it has received wake-up messages from all
neighbors.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 17/43
Leader Election in Trees - The Algorithm

The local algorithm at an awake process p:


I p waits until it has received id’s from all neighbors except one,
which becomes its parent.
I p computes the largest id maxp among the received id’s and its own id.
I p sends a parent request to its parent, tagged with maxp .
I If p receives a parent request from its parent, tagged with q, it computes
max0p , being the maximum of maxp and q.
I Next, p sends an information message to all neighbors except its parent,
tagged with max0p .
I This information message is forwarded through the network.
I The process with id max0p becomes the leader.

Message complexity: 2N − 2 messages.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 18/43
Leader Election in Trees - Example

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 19/43
Echo Algorithm with Extinction

This is an election algorithm that works for any topology.

Idea
We let each process to start a run of the echo algorithm tagged with it id.

Only the wave started by the initiator with the largest id complete, after which
the initiator become the leader.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 20/43
Echo Algorithm with Extinction - Details

Election for undirected networks, based on the echo algorithm:


I Each initiator starts a wave, tagged with its id.
I At any time, each process takes part in at most one wave.
I Suppose a process p in wave q is hit by a wave r :
I if q < r , then p changes to wave r (it abandons all earlier messages);
I if q > r , then p continues with wave q (it purges the incoming message);
I if q = r , then the incoming message is treated according to the echo
algorithm of wave q.
I If wave p executes a decide event (at p), p becomes leader.
Non-initiators join the first wave that hits them.

Worst-case message complexity: O(N.E)

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 21/43
Presentation outline

Leader Election

Election in Anonymous Networks

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 22/43
Anonymous Networks

Processes may be anonymous for several reasons:


I Absence of unique hardware id’s.
I Processes don’t want to reveal their id (security protocols).
I Transmitting/storing id’s is too expensive (IEEE 1394 bus).

Assumptions for election in anonymous networks:


I All processes have the same local algorithm.
I The initiators can be any non-empty set of processes.
I Processes (and channels) don’t have a unique id.
If there is one leader, all processes can be given a unique id (using a
traversal algorithm).

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 23/43
Election in Anonymous Networks

Impossibility Result
Theorem: There is no leader election algorithm for anonymous rings that
always terminates.

A configuration is symmetric if all processes are in the same state and all
channels carry the same messages.

Notice that, in a symmetric configuration there isn’t one leader.


Proof: Consider an anonymous ring of size N. By contradiction, suppose
there exists such an election algorithm.
I There is a symmetric initial configuration (all processes are in their initial
configuration and all channels are empty).
I If γ0 is symmetric and γ0 → γ1 , then there is an execution
γ1 → γ2 · · · → γN , where γN is symmetric.
Hence, the algorithm exhibits an infinite execution, where a symmetric
configuration is reached infinitely often.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 24/43
Election in Anonymous Networks

Fairness
An execution is strongly fair, if each event that is applicable in infinitely many
configurations, occurs infinitely often in the execution.

An execution is weakly fair, if each event that is applicable continuously,


eventually occurs in the execution.

Election algorithm for anonymous rings have fair infinite executions. This is
because in transition γ0 → γ1 , it does not matter which event is used; we can
always build the transition sequence to the symmetric configuration γN . This
in turn means no event is ignored.

Hence, leader election in a fair anonymous ring is also impossible.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 25/43
Election in Anonymous Networks

Question: What does the transition system of an election algorithm in an


anonymous network look like?

It contains livelocks; i.e., cycles that prevent the algorithm to reach a


terminating configuration.

Question: Is there any way to overcome this problem?

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 26/43
Probabilistic Algorithms

In a probabilistic algorithm, a process may flip a coin, and perform an event


based on the outcome of this coin flip.

Probabilistic algorithms where all computations terminate in a correct


configuration are not interesting: Letting the coin e.g. always flip heads yields
a correct non-probabilistic algorithm.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 27/43
Probabilistic Algorithms

Las Vegas Algorithms


A probabilistic algorithm is Las Vegas if:
I the probability that it terminates is greater than zero, and
I all terminal configurations are correct.

Question: When in a Las Vegas algorithm the probability of termination is


one?

Monte Carlo Algorithms


An algorithm is Monte Carlo if:
I it always terminates, and
I the probability that a terminal configuration is correct is greater than zero.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 28/43
Itai-Rodeh Election Algorithm

Assumptions
An anonymous, directed ring, where all processes know the ring size N.

Itai-Rodeh Election Algorithms is a Las Vegas algorithm that terminates with


probability one. We adapt the Chang-Roberts algorithm: each initiator sends
out an id, and the largest id is the only one making a round trip.

The Idea
Each initiator selects a random id from {1, . . . , N}.
Challenge: Different processes may select the same id.

Solution: Each message is supplied with a hop count. A message arriving at


its source has hop count N.

If several processes select the same largest id, they will start a new election
round, with a higher number.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 29/43
Itai-Rodeh Election Algorithm

Initially, initiators are active in round 0, and non-initiators are passive.

Let p be active. At the start of an election round n, p selects a random idp ,


sends (n, idp , 1, false), and waits for a message (n0 , i, h, b). The 3rd value is
the hop count. The 4th value signals if another
process with the same id was encountered during the round trip.
I p gets (n0 , i, h, b) with n0 > n, or n0 = n and i > idp : it becomes passive
and sends (n0 , i, h + 1, b).
I p gets (n0 , i, h, b) with n0 < n, or n0 = n and i < idp : it purges the
message.
I p gets (n, idp , h, b) with h < N: it sends (n, idp ; h + 1, true).
I p gets (n, idp , N, true): it proceeds to round n + 1.
I p gets (n, idp , N, false): it becomes the leader.

Passive processes pass on messages, increasing their hop count by one.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 30/43
Itai-Rodeh Election Algorithm

Correctness: The Itai-Rodeh election algorithm is Las Vegas, where


eventually one leader is elected, with probability 1.
Why?

Average-case message complexity: O(N log N).

Lower bound: Ω(N log N).

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 31/43
Itai-Rodeh Election Algorithm - Example

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 32/43
Election in Arbitrary Anonymous Networks

The echo algorithm with extinction, with random selection of id’s, can be used
for election in anonymous undirected networks in which all processes know
the network size.

The Idea
I The algorithm works in rounds and round number is used to recognize
messages from previous rounds.
I Each process randomly selects an id and runs the echo algorithm.
I When a process is hit by a wave with a higher round number than it
current wave, or with the same round number but a higher id, the
process becomes passive and moves to the other wave.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 33/43
Election in Arbitrary Anonymous Networks

Algorithm Details
Initially, initiators are active in round 0, and non-initiators are passive.

Each active process selects a random id ∈ {0, . . . , N}, and starts a wave,
tagged with its id and round number 0.
Let process p in wave i of round n be hit by wave j of round n0 :
I if n0 > n, or n0 = n and j > i, then p adopts wave j of round n0 , and treats
the message according to the echo algorithm
I if n0 < n, or n0 = n and j < i, then p purges the message
I if n0 = n and j = i , then p treats the message according to the echo
algorithm

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 34/43
Election in Arbitrary Anonymous Networks

Processes need to know the size of the network to be able to determine at


completion of their wave whether it has covered the entire network. This is
because different wave with different ids can collide which cause pre-mature
completion.

Thus, each message sent upwards in the constructed tree reports the size
of its subtree.

All other messages report 0.

When a process decides, it computes the size of the constructed tree.

If the constructed tree covers the network, it becomes the leader.

Else, it selects a new id, and initiates a new wave, in the next round.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 35/43
Election in Arbitrary Anonymous Networks

The echo algorithm with extinction, with random selection of id’s is a Las
Vegas algorithm that terminates with probability one.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 36/43
Election in Arbitrary Anonymous Networks - Example

Only waves that complete are shown.

i >j >k >l >m

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 37/43
Election in Arbitrary Anonymous Networks - Example

The process at the left computes size 6, and becomes the leader.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 38/43
Computing the Size of an Anonymous Network

Impossibility Result
Theorem: There is no Las Vegas algorithm to compute the size
of an anonymous ring.

This implies that there is no Las Vegas algorithm for election in an


anonymous ring if processes don’t know the ring size.

Because when a leader is known, the network size can be computed using a
wave algorithm.

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 39/43
Computing the Size of an Anonymous Network

Proof: Consider an anonymous, directed ring p0 , . . . , pN−1 .

Assume a probabilistic algorithm with a computation C that terminates with


the correct outcome N.

Consider the ring p0 , . . . , p2N−1 build as follows where each pi0 is a copy of pi :

p1 p0

0
pN−1 pN−1

p00 p10

Let each event at a process pi in C be executed concurrently at pi and pi+N .


This computation terminates with the incorrect outcome N.
Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 40/43
Itai-Rodeh Ring Size Algorithm

Each process p maintains an estimate estp of the ring size. Initially estp = 2.
(Always estp ≤ N.)

p initiates an estimate round (1) at the start of the algorithm, and (2) at each
update of estp .

Each round, p selects a random idp in {1, . . . , R}, sends (estp , idp , 1), and
waits for a message (est, id, h). (Always h ≤ est.)
I If est < estp , then p purges the message.
I Let est > estp .
I If h < est, then p sends (est, id, h + 1), and estp ← est
I If h = est, then estp ← est + 1.
I Let est = estp .
I If h < est, then p sends (est, id, h + 1).
I If h = est
I 6 idp , then estp ← est + 1.
If id =
I If id = idp , then p purges the message (possibly its own message returned).

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 41/43
Itai-Rodeh Ring Size Algorithm
When the algorithm terminates, estp ≤ N for all p. Also, espp converges to
the same value for all p. Why?

Question Under what condition the Itai-Rodeh algorithm terminates with an


estimate smaller than N?

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 42/43
Itai-Rodeh Ring Size Algorithm
When the algorithm terminates, estp ≤ N for all p. Also, espp converges to
the same value for all p. Why?

Question Under what condition the Itai-Rodeh algorithm terminates with an


estimate smaller than N?

This happens when in a round with an estimate est < N, all processes at
distance est from each other happen to select the same id.
j, 2

The probability that the algo-


i, 2 i, 2 rithm terminates with an in-
correct outcome tends to zero
when R tends to infinity. (recall
j, 2 that {1, . . . , R} was the domain
of ids)
The algorithm will terminate
with estimate of 2.
Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 42/43
Itai-Rodeh Ring Size Algorithm - Example

j, 2

i, 2 i, 2

k, 2
Itai-Rodeh Ring Size Algorithm - Example

j, 3
l, 2

i, 2 i, 2

k,
l, 32
Itai-Rodeh Ring Size Algorithm - Example

j, 3
l, 2

i, 3
l, 2 m,
i, 23

k,
l, 32
Itai-Rodeh Ring Size Algorithm - Example

j, 4
i,
l, 2
3

j,
i, 4
l, 2
3 m,
j, 23
i, 4

n,
l, 34
k, 2

Dr. Borzoo Bonakdarpour Distributed Algorithms (CAS 769) - McMaster University 43/43

You might also like