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

Election Algorithms

Shah Khalid

Shah.khalid@seecs.edu.pk

SEECS- A 210

1
Today…

❖ Last Session:
❖ Mutual Exclusion Algorithms
❖ Today’s Session:
❖ Why we need Election Algorithms?
❖ Election Algorithms
Why Election?
 Example 1: Your Bank maintains multiple servers in their cloud, but for
each customer, one of the servers is responsible, i.e., is the leader
 What if there are two leaders per customer?
 Inconsistency

 What if servers disagree about who the leader is?


 Inconsistency

 What if the leader crashes?


 Unavailability
Why Election?

 In a group of processes, elect a Leader to undertake special tasks.


 What happens when a leader fails (crashes)
 Some (at least one) process detects this (how?)
 Then what?
 Focus of this lecture: Election algorithm
1. Elect one leader only among the non-faulty processes
2. All non-faulty processes agree on who is the leader
System Model/Assumptions
 Any process can call for an election.
 A process can call for at most one election at a time.
 Multiple processes can call an election simultaneously.
All of them together must yield a single leader only
 The result of an election should not depend on which process calls for it.
 Messages are eventually delivered.
Election Algorithms
❖ Many algorithms require one process to act as coordinator, initiator or
otherwise perform a special role.
❖ There are different algorithms for coordinator selection
❖ The Bully Algorithm
❖ A Ring Algorithm

6
The Bully Algorithm
❖ Process notices that coordinator is no longer responding to
requests.
❖ It initiate an election as follows
I. P send an ELECTION message to all processes with higher
number
II. If no one responds, P wins the election and becomes coordinator
III. If one of the higher-ups answers, it takes over. P’s job is done
The biggest guy in town always win –
bully algorithm

7
The Bully Algo.

8
The Bully Algo.

9
Example: Bully Election
answer=OK

P1 P1 P1

P0 Election P2 P0 P2 P0 P2
answer
Election Election answer Election
P5 P3 P5 P3 P5 P3
Election
Election
P4 P4 P4

1. P2 initiates election 2. P2 receives answers 3. P3 & P4 initiate election

P1 P1 P1

P0 P2 P0 P2 P0 P2

P5 P5 coordina
P5 P3 P3 P3
answer tor

P4 P4 P4

4. P3 receives reply 5. P4 receives no reply 5. P4 announces itself


The Bully Algorithm with Failures
election

election
C
election
The coordinator p4 fails and p1 detects this Stage 1
p answer p p p
1 2 3 4
answer
election

election election C
Stage 2
answer
p1 timeout p p p
2 3 4

p3 fails
Stage 3
p p p p
1 2 3 4

Eventually.....
coordinator
C
Stage 4
p p p p
1 2 3 4
A Token Ring Algorithm

12
A Ring Alog.
❖ The processes are logically arranged in a ring.
❖ When any process notices that the coordinator is nor responding, it builds an
ELECTION message containing its own process number and sends it to its
successor
❖ At each step, the sender adds its own number to the list in the message thus
making itself be a candidate.
❖ the message reaches the process that started it all. Then it looks through the
message and decides which process has the highest number and that becomes
the coordinator
❖ Then the message type is changed to COORDINATOR and the message
circulates once again so everyone knows the new coordinator and the new ring
configuration

13
A Ring Alog.

14
Chang-Roberts algorithm
 Ring-based coordinator election algorithm, employed in distributed
computing
Algorithms
 Every process sends an election message with its id to the left if it has
not seen a message with a higher id
 Forward any message with an id greater than own id to the left
 If a process receives its own election message it is the leader
 It is uniform: number of processors does not need to be known to the
algorithm
Chang-Roberts algorithm
1
8 8
Each node sends 1
a message with 5
2
its id
to the left 2
5
neighbor

6
3
6
3
4
7 7
16 4
If: message received id  current node id
Then: forward message
5
8
1
2

5 8

6
7

3
4
7
17 6
If: message received id  current node id
Then: forward message

8
1
7 2

3 8
4
7
18
If: message received id  current node id
Then: forward message

7
8
1
2

3
4
7
19 8
If: message received id  current node id
Then: forward message

8
1
2

3
4
8 7
20
If: a node receives its own message

Then: it elects itself a leader


8
8
1
2

3
4
7
21
If: a node receives its own message

Then: it elects itself a leader

8
1
leader
2

3
4
7
22
Hirschberg-Sinclair algorithm
• Assume ring is bidirectional
• Carry out elections on increasingly larger sets
• Pi is a leader in phase r=0,1,2,… iff it has the largest id
of all nodes that are at a distance 2r or less from it; to
establish that, it sends probing messages on both sides
Phase 0: send(id,step counter)
to 1-neighborhood

1
8 8
1 8
5
2 2
1
2
5
6

3 5 6
4
6
3 3
7
4
7 7
4
25
If: received id  current id
Then: send a reply(OK)

8
1
2

3
4
7
26
If: a node receives both replies
Then: it becomes a temporal leader
and proceed to next phase

8
1
2

3
4
7
27
Phase 1: send(id,1,1) to left and right
adjacent in the 2-neighborhood

8
8
1
8 2
5
5 6

5 6
6
3 7
4
7
7
28
If: received id 
current id
Then: forward(id,1,2)

8 6
1
8 5
2

5 8

7 6

3 7
6
4
5 7
29
At second step: since step counter=2, I’m on
the boundary of the 2-neighborood

If: received id > current id


Then: send a reply(id)
8
1
2

3
4
7
30
If: a node receives a reply with another id
Then: forward it
If: a node receives both replies
Then: it becomes a temporal leader

8
1
2

3
4
7 31
Phase 2: send id to 2 -neighborhood
2

8
1
8 2

5
8
7
7
6

3
4
7
32
If: received id current id
Then: send a reply

8
1
2

3
4
7
33
If: a node receives both replies
Then: it becomes the leader

8
1
2

3
4
7
34
Phase 3: send id to 8-neighborhood
 The node with id 8 will receive its own probe message, and
then becomes leader!

8
1
leader
2

3
4
7 35
36

You might also like