0% found this document useful (0 votes)
15 views23 pages

05b Elections Slides

The document discusses two algorithms for leader election in distributed systems: 1) The Bully algorithm selects the process with the highest ID as leader. If the leader fails, processes with higher IDs are contacted to initiate a new election. 2) The Ring election algorithm passes a message around a virtual ring of processes. The message returns to the initiator who selects the leader, typically the process with the highest ID in the message.
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)
15 views23 pages

05b Elections Slides

The document discusses two algorithms for leader election in distributed systems: 1) The Bully algorithm selects the process with the highest ID as leader. If the leader fails, processes with higher IDs are contacted to initiate a new election. 2) The Ring election algorithm passes a message around a virtual ring of processes. The message returns to the initiator who selects the leader, typically the process with the highest ID in the message.
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/ 23

CS 417 – DISTRIBUTED SYSTEMS

Week 5: Part 2
Leader Election

© 2022 Paul Krzyzanowski. No part of this

Paul Krzyzanowski content may be reproduced or reposted in


whole or in part in any manner without the
permission of the copyright owner.
Leader Election
• Purpose
– Need to pick one process to act as coordinator

• Assumptions
– Processes have no distinguishing characteristics
– Each process has a unique ID to identify itself
– Reliable message delivery

CS 417 © 2022 Paul Krzyzanowski 2


Bully algorithm
• Select process with largest ID as the leader

• When process P detects a dead leader:


– Send election message to all processes with higher IDs
• If nobody responds, P wins and takes over
• If any process responds, P’s job is done
– Optional: Let all nodes with lower IDs know an election is taking place

• If process receives an election message


– Send OK message back
– Hold election (unless it is already holding one)
CS 417 © 2022 Paul Krzyzanowski 3
Bully algorithm
• A process announces victory:
– Sends all processes a message telling them that it is the new leader

• If a dead process recovers


– It holds an election to find the leader

CS 417 © 2022 Paul Krzyzanowski 4


Bully algorithm

P0 P1 P2 P3 P4 P5
DEAD
hello?

Rule: highest # process is the leader

Suppose P5 dies

P2 detects P5 is not responding

CS 417 © 2022 Paul Krzyzanowski 5


Bully algorithm
ELECTION

P0 P1 P2 P3 P4 P5
DEAD
ELECTION
ELECTION

P2 starts an election

Contacts all higher-numbered systems

CS 417 © 2022 Paul Krzyzanowski 6


Bully algorithm
OK

P0 P1 P2 P3 P4 P5
DEAD
ELECTION
ELECTION

Everyone who receives an ELECTION message responds


… and holds their own election, contacting higher # processes

Example: P3 receives the message from P2


Responds to P2
Sends ELECTION messages to P4 and P5

CS 417 © 2022 Paul Krzyzanowski 7


Bully algorithm
OK
OK

P0 P1 P2 P3 P4 P5
DEAD
ELECTION

P4 responds to P3 and P2's messages


… and holds an election

CS 417 © 2022 Paul Krzyzanowski 8


Bully algorithm
Leader = P4 Leader = P4 Leader = P4
Leader = P4

P0 P1 P2 P3 P4 P5
Leader = P4
DEAD

Nobody responds to P4

After a timeout, P4 declares itself the leader

CS 417 © 2022 Paul Krzyzanowski 9


Ring election algorithm
Ring arrangement of processes
• If any process detects failure of leader
– Construct election message with process ID and send to next process
– If successor is down, skip over
– Repeat until a running process is located

• Upon receiving an election message


– Process forwards the message, adding its process ID to the body

CS 417 © 2022 Paul Krzyzanowski 10


Ring algorithm
Eventually message returns to originator
• Process sees its ID on list
• Multicasts a leader message announcing the new leader
– E.g., highest numbered process

CS 417 © 2022 Paul Krzyzanowski 11


Ring algorithm
Assume P1 discovers that the leader, P5, is dead
P1 starts an election

P5
DEAD
P4 P0

P3 P1

Election: {P1}
P2

CS 417 © 2022 Paul Krzyzanowski 12


Ring algorithm

P5
DEAD
P4 P0

P3 P1

P2
Election: {P1, P2}

CS 417 © 2022 Paul Krzyzanowski 13


Ring algorithm

P5
DEAD
P4 P0

Election: {P1, P2, P3}

P3 P1

P2

CS 417 © 2022 Paul Krzyzanowski 14


Ring algorithm

Election: {P1, P2, P3, P4}


P5
Fails: P5 is dead
P4 P0

P3 P1

P2

CS 417 © 2022 Paul Krzyzanowski 15


Ring algorithm

Election: {P1, P2, P3, P4}


P5
Skip to P0 DEAD
P4 P0

P3 P1

P2

CS 417 © 2022 Paul Krzyzanowski 16


Ring algorithm

P5
DEAD
P4 P0
Election: {P1, P2, P3, P4, P0}

P3 P1

P2

CS 417 © 2022 Paul Krzyzanowski 17


Ring algorithm
P2 receives the election message that it initiated
P2 now picks a leader (e.g., highest ID)

P5
DEAD
P4 P0
This is me! Winner!

P3 P1 Election: {P1, P2, P3, P4, P0}

P2 We might have multiple


Because P1 sees its ID at concurrent elections. Everyone
the head of the list, it needs to pick the same leader.
know that this is the Here, we agree to pick the
election that it started highest ID in the list.

CS 417 © 2022 Paul Krzyzanowski 18


Ring algorithm
P1 announces that P4 is the new leader to the group

P5
DEAD
P4 P0
P4
Many other election
P4 algorithms that target
P4
other topologies: mesh,
P4
P3 P1 torus, hypercube, trees, …

P4
P2

CS 417 © 2022 Paul Krzyzanowski 19


Chang & Roberts Ring Algorithm
Optimize the ring
– Message always contains one process ID
– Avoid multiple circulating elections

• If a process sends a message, it marks its state as a participant


– This allows it to cut off extra elections
• Assume highest # PID is the winner

CS 417 © 2022 Paul Krzyzanowski 20


Chang & Roberts Ring Algorithm
Upon receiving an election message:
If PID(message) > PID(process) – higher ID will always win over a lower one
forward the message

If PID(message) < PID(process) AND process is not a participant – we are a higher ID number; use it
replace PID in message with PID(process) and forward the new message
set the process state to participant

If PID(message) < PID(process) AND process is participant


discard the message
– we're already circulating our ID and it’s a higher number than this one

If PID(message) == PID(process)
the process is now the leader
– message fully circulated to the one who started: announce winner

CS 417 © 2022 Paul Krzyzanowski 21


Network Partitions
• Network partitions (segmentation)
– Multiple nodes may decide they’re the leader
– Leads to multiple groups, each with a leader & diverging data among them → split brain
Leader! Leader! Leader!

Switch Switch Switch

• Dealing with partitions


– Insist on a majority → if no majority, the system will not function
• Quorum = minimum # of participants required for a system to function)
– Rely on alternate communication mechanism to validate failure
• Redundant network, shared disk (but that can also fail)
CS 417 © 2022 Paul Krzyzanowski 22
The End

CS 417 © 2022 Paul Krzyzanowski 23

You might also like