![Page 1: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/1.jpg)
Distributed Systems09. Consensus: Mutual Exclusion & Election Algorithms
Paul Krzyzanowski
Rutgers University
Spring 2020
1March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 2: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/2.jpg)
Process Synchronization
Techniques to coordinate execution among processes– One process may have to wait for another– Shared resource (e.g. critical section) may require exclusive access
Mutual exclusion– Examples
• Update a fields in database tables• Modify a file• Modify file contents that are replicated on multiple servers
– Easy to handle if the entire request is atomic • Contained in a single message; server can manage mutual exclusion
– Needs to be coordinated if the request comprises multiple messages or spans multiple systems
2March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 3: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/3.jpg)
Centralized Systems
Achieve mutual exclusion via:– Test & set in hardware– Semaphores– Messages (inter-process)– Condition variables
3March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 4: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/4.jpg)
Distributed Mutual Exclusion
Goal:Create an algorithm to allow a process to request and obtain exclusive access to a resource that is available on the network.
Required properties:Safety: At any instant, only one process may hold the resource
Liveness: The algorithm should make progress; processes should not wait forever for messages that will never arrive
Fairness: Each process gets a fair chance to hold the resource: bounded wait time & in-order processing
4March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 5: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/5.jpg)
Assumption
Assume there is agreement on how a resource is identified– Pass the identifier with requests– e.g., lock(“printer”)
lock(“table:employees”),lock(“table:employees;row:15”)
…and every process can identify itself uniquely
We’ll just use request(R) to request exclusive access to resource R.
5March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 6: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/6.jpg)
Categories of algorithms
• Centralized– A process can access a resource because a central coordinator
allowed it to do so
• Token-based– A process can access a resource if it is holding a token permitting it
to do so
• Contention-based– A process can access a resource via distributed agreement
6March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 7: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/7.jpg)
Centralized algorithm
• Mimic single processor system
• One process elected as coordinator
P
Crequest(R)
grant(R)
1. Request resource2. Wait for response3. Receive grant4. access resource5. Release resource
release(R)
7March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 8: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/8.jpg)
Centralized algorithm
If another process claimed resource:– Coordinator does not reply until release– Maintain queue
• Service requests in FIFO order
P0
Crequest(R)
grant(R)
release(R) P1
P2
request(R)
Queue
P1
request(R)
P2
grant(R)
8
![Page 9: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/9.jpg)
Centralized algorithm
Benefits
• Fair: All requests processed in order
• Easy to implement, understand, verify
• Processes do not need to know group members – just the coordinator
Problems
• Process cannot distinguish being blocked from a dead coordinator – single point of failure
• Centralized server can be a bottleneck
9March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 10: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/10.jpg)
Token Ring algorithm
Assume known group of processes– Some ordering can be imposed on group (unique process IDs)– Construct logical ring in software– Process communicates with its neighbor
P0
P1
P2
P3
P4
P5
10March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 11: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/11.jpg)
Token Ring algorithm
• Initialization– Process 0 creates a token for resource R
• Token circulates around ring– From Pi to P(i+1)mod N
• When process acquires token– Checks to see if it needs to enter critical section– If no, send ring to neighbor– If yes, access resource
• Hold token until done
P0
P1
P2
P3
P4
P5
token(R)
11March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 12: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/12.jpg)
Token Ring algorithm
P0
P1
P2
P3
P4
P5
Your turn to access resource R
12March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 13: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/13.jpg)
Token Ring algorithm
P0
P1
P2
P3
P4
P5Your turn to access resource R
13March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 14: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/14.jpg)
Token Ring algorithm
P0
P1
P2
P3
P4
P5
Your turn to access resource R
14March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 15: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/15.jpg)
Token Ring algorithm
P0
P1
P2
P3
P4
P5
Your turn to access resource R
15March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 16: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/16.jpg)
Token Ring algorithm
P0
P1
P2
P3
P4
P5
Your turn to accessresource R
16March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 17: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/17.jpg)
Token Ring algorithm
P0
P1
P2
P3
P4
P5Your turn to accessresource R
17March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 18: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/18.jpg)
Token Ring algorithm
P0
P1
P2
P3
P4
P5
Your turn to access resource R
18March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 19: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/19.jpg)
Token Ring algorithm
P0
P1
P2
P3
P4
P5Your turn to accessresource R
19March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 20: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/20.jpg)
Token Ring algorithm summary
• Only one process at a time has token– Mutual exclusion guaranteed
• Order well-defined (but not necessarily first-come, first-served)– Starvation cannot occur– Lack of FCFS ordering may be undesirable sometimes
• Problems– Token loss (e.g., process died)
• It will have to be regenerated• Detecting loss may be a problem
(is the token lost or in just use by someone?)
– Process loss: what if you can't talk to your neighbor?
20March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 21: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/21.jpg)
Lamport’s Mutual ExclusionDistributed algorithm using reliable multicast and logical clocks
• Each process maintains request queue– Queue contains mutual exclusion requests
• Messages are sent reliably and in FIFO order– Each message is time stamped with totally ordered Lamport
timestamps• Ensures that each timestamp is unique• Every node can make the same decision by comparing timestamps
• Queues are sorted by message timestamps
21March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 22: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/22.jpg)
1. Request a Resource
Request a critical section:• Process Pi sends request(i, Ti) to all nodes
It also places the same request onto its own queue
• When a process Pj receives a request:– It returns a timestamped ack– Places the request on its request queue
• Every process will have an identical queue– Same contents in the same order
Lamport time
22
Process Time stamp
P4 1021
P8 1022
P1 3944
P6 8201
P12 9638
Sample request queueIdentical at each process
![Page 23: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/23.jpg)
2. Use the Resource
Enter a critical section (accessing resource):
• Pi has received ACKs from everyone
• Pi’s request has the earliest timestamp in its queue
If your request is at the head of the queue AND you received ACKs for that request… you can access the critical section
23
Process Time stamp
P4 1021
P8 1022
P1 3944
P6 8201
P12 9638
Sample request queueIdentical at each process
![Page 24: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/24.jpg)
3. Release the resource
Release a critical section:• Process Pi removes its request from its queue
• Sends release(i, Ti) to all nodes
• Each process now checks if its request is the earliest in its queue
• If so, that process now has the critical section
24
![Page 25: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/25.jpg)
Lamport’s Mutual Exclusion
• Performance– 3(N-1) messages per critical section
– (N-1) Request msgs + (N-1) Reply msgs + (N-1) Release msgs
• N points of failure
• A lot of messaging traffic – Requests & releases are sent to the entire group
• Not great … but demonstrates that a fully distributed algorithm is possible
25March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 26: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/26.jpg)
Ricart & Agrawala algorithmAnother distributed algorithm
using reliable multicast and logical clocks
When a process wants to enter critical section:1. Compose message containing:
• Identifier (machine ID, process ID)• Name of resource• Timestamp (e.g., totally-ordered Lamport)
2. Reliably multicast request to all processes in group
3. Wait until everyone gives permission
4. Enter critical section / use resource
26March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 27: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/27.jpg)
Ricart & Agrawala algorithm
When process receives request:– If receiver not interested:
• Send OK to sender– If receiver is in critical section
• Do not reply; add request to queue– If receiver just sent a request as well: (potential race condition)
• Compare timestamps on received & sent messages• Earliest wins• If receiver is the loser: send OK• If receiver is the winner: do not reply – queue the request
• When done with critical section– Send OK to all queued requests
27
![Page 28: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/28.jpg)
Ricart & Agrawala algorithm
• Performance– 2(N-1) messages per critical section
– (N-1) Request msgs + (N-1) Reply messages
• Not great either– N points of failure
– A lot of messaging traffic
– Also demonstrates that a fully distributed algorithm is possible
28March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 29: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/29.jpg)
Lamport vs. Ricart & Agrawala
Lamport– Everyone responds (acks) … always – no hold-back– 3(N-1) messages
• Request – ACK – Release– Process decides to go based on whether its request is the earliest in
its queue
Ricart & Agrawala– If you are in the critical section (or won a tie)
• Don’t respond with an ACK until you are done with the critical section– 2(N-1) messages
• Request – ACK– Process decides to go if it gets ACKs from everyone
29March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 30: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/30.jpg)
Election algorithms
30March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 31: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/31.jpg)
Elections
• Purpose– Need to pick one process to act as coordinator
• Processes have no distinguishing characteristics
• Each process has a unique ID to identify itself
31March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 32: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/32.jpg)
Bully algorithm
• Select process with largest ID as coordinator
• When process P detects dead coordinator:– 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)
32March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 33: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/33.jpg)
Bully algorithm
• A process announces victory:– Sends all processes a message telling them that it is the new
coordinator
• If a dead process recovers– It holds an election to find the coordinator
33March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 34: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/34.jpg)
Bully algorithm
34
P0 P1 P2 P3 P4 P5
DEAD
Rule: highest # process is the leader
Suppose P5 dies
P2 detects P5 is not responding
hello?
March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 35: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/35.jpg)
Bully algorithm
35
P0 P1 P2 P3 P4 P5
DEAD
P2 starts an election
Contacts all higher-numbered systems
ELECTION
ELECTION
ELECTION
March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 36: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/36.jpg)
Bully algorithm
36
P0 P1 P2 P3 P4 P5
DEAD
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
ELECTION
ELECTION
ACK
March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 37: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/37.jpg)
Bully algorithm
37
P0 P1 P2 P3 P4 P5
DEAD
P4 responds to P3 and P2's messages… and holds an election
ELECTION
ELECTION
ACK
ACK
March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 38: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/38.jpg)
Bully algorithm
38
P0 P1 P2 P3 P4 P5
DEAD
Nobody responds to P4
After a timeout, P4 declares itself the leader
Leader = P4Leader = P4 Leader = P4
Leader = P4
Leader = P4
![Page 39: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/39.jpg)
Ring algorithm
Ring arrangement of processes• If any process detects failure of coordinator
– 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
39
![Page 40: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/40.jpg)
Ring algorithm
Eventually message returns to originator
• Process sees its ID on list
• Circulates (or multicasts) a coordinator message announcing coordinator– E.g. highest numbered process
40March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 41: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/41.jpg)
Ring algorithmAssume P1 discovers that the coordinator, P5, is deadP1 starts an election
P5
P0
P1
P2
P3
P4
Election: {P1}
DEAD
41March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 42: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/42.jpg)
Ring algorithm
P5
P0
P1
P2
P3
P4
Election: {P1, P2}
DEAD
42March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 43: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/43.jpg)
Ring algorithm
P5
P0
P1
P2
P3
P4DEAD
Election: {P1, P2, P3}
43March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 44: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/44.jpg)
Ring algorithm
P5
P0
P1
P2
P3
P4DEAD
Election: {P1, P2, P3, P4}
Fails: P5 is dead
44March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 45: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/45.jpg)
Ring algorithm
P5
P0
P1
P2
P3
P4DEAD
Election: {P1, P2, P3, P4}
Skip to P0
45March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 46: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/46.jpg)
Ring algorithm
P5
P0
P1
P2
P3
P4DEAD
Election: {P1, P2, P3, P4, P0}
46March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 47: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/47.jpg)
Ring algorithmP2 receives the election message that it initiated
P2 now picks a leader (e.g., lowest or highest ID)
P5
P0
P1
P2
P3
P4DEAD
Election: {P1, P2, P3, P4, P0}
Winner!This is me!
47
Because P1 sees its ID at the head of the list, it know that this is the election that it started
We might have multiple concurrent elections. Everyone needs to pick the same leader. Here, we agree to pick the highest ID in the list.
March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 48: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/48.jpg)
Ring algorithmP1 announces that P4 the new coordinator to the group
48March 25, 2020 © 2014-2020 Paul Krzyzanowski
P5
P0
P1
P2
P3
P4DEAD
P4P4
P4
P4
P4
![Page 49: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/49.jpg)
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
• Assume highest # PID is the winner
49
![Page 50: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/50.jpg)
Chang & Roberts Ring Algorithm
Upon receiving an election message:If PID(message) > PID(process)
forward the message – higher ID will always win over a lower one
If PID(message) < PID(process)replace PID in message with PID(process)forward the new message – we now see a higher ID number; use it
If PID(message) < PID(process) AND process is participantdiscard 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
50
![Page 51: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/51.jpg)
Network Partitions: Split Brain• Network partitioning (segmentation)
– Split brain– Multiple nodes may decide they’re the leader
• Dealing with partitioning– Insist on a majority → if no majority, the system will not function– Rely on alternate communication mechanism to validate failure
• Redundant network, shared disk, serial line, SCSI
• We will visit this problem later!51
Router Router Router
Leader!Leader!Leader!
March 25, 2020 © 2014-2020 Paul Krzyzanowski
![Page 52: Distributed Systems - Rutgers Universitypxk/417/notes/pdf/09-mutex-elections-slides.pdfToken Ring algorithm •Initialization –Process 0 creates a token for resource R •Token circulates](https://reader036.vdocuments.us/reader036/viewer/2022062923/5f0ad0da7e708231d42d7b0f/html5/thumbnails/52.jpg)
The End
March 25, 2020 52© 2014-2020 Paul Krzyzanowski