Distributed Algorithms 22903 Leader election in rings Lecturer
Distributed Algorithms (22903) Leader election in rings Lecturer: Danny Hendler This presentation is based on the book “Distributed Computing” by Hagit attiya & Jennifer Welch
Message-passing systems p 0 1 1 p 1 2 1 p 3 3 2 2 1 p 2 • Processes are represented by graph nodes. • Bi-directional (unless otherwise stated) labeled communication links • Each process pi (having r links) has state consisting of: Ø Program state Ø inbufi[m], 1 ≤ m ≤ r Ø outbufi[m], 1 ≤ m ≤ r (this is inaccessible to pi) 2
Message-passing systems (cont’d) p 0 1 1 p 1 2 1 p 3 3 2 2 1 p 2 • A Configuration is a vector of process states (q 0, …, qn-1) • In the initial configuration Ø inbuf buffers are empty Ø outbuf buffers may contain messages 3
Synchronous message-passing systems p 0 1 1 p 1 2 1 p 3 3 2 2 1 p 2 • Computation proceeds in rounds • In each round: Ø All messages in the outbuf buffers are delivered Ø Each process performs a computational step and sends at most a single message on every link. 4
Asynchronous message-passing systems p 0 1 1 p 1 2 1 p 3 3 2 2 1 p 2 • No fixed upper bound on the delay of a message or the time between computation steps • An execution is a sequence of events: Ø comp(i) – a computational step by process i Ø del(i, j, m) – the delivery of message m from pi to pj 5
Leader Election in Rings 2 p 0 1 2 1 p 2 2 p 1 1 • Each process has to decide whether it is a leader or not • Termination states are partitioned to elected and not-elected. • In every execution, exactly one process enters an elected state, all others enter a non-elected state. • We assume rings are oriented. 6
Leader election in anonymous rings 2 p 1 2 1 p p 2 1 • A ring algorithm is anonymous if processes don’t have an ID they can use in the algorithm (all perform the same code) An anonymous leader election algorithm is… Impossible!! 7
A simple algorithm for (non-anonymous) asynchronous rings 1. Each process sends probe(ID) to its left neighbor (and then waits) 2. Upon receiving a message probe(i) (from the right) 3. 4. 5. 6. 7. 8. 9. if (i > ID) forward message to the left if (i=ID) send terminate(ID) and terminate as leader Upon receiving terminate(i) if (i > ID) forward message to the left and terminate as non-leader Message complexity is… Θ(n 2) 8
A more efficient asynchronous leader election algorithm • Proceeds in phases • In phase j, a process tries to become the temporary leader of its 2 j-neighborhood • Only temporary leaders of phase-j continue to phase j+1 9
An O(n log n) messages asynchronous leader election algorithm (cont’d) 1. Initially, asleep=true, participating=false 2. Upon receiving no message (spontaneous wakeup) 3. if (asleep) 4. asleep: =false, participating=true 5. send probe(ID, 0, 1) to left and right 6. Upon receiving probe(j, l, d) from left (resp. , right) 7. if (j=id) terminate as leader ; a termination message should be sent 8. if (j>id or participating) and (d<2 l) send probe(j, l, d+1) to right (resp. , left) 9. if (j>id or participating) and (d = 2 l) send reply(j, l) to left (resp. right) 10. Upon receiving reply(j, l) from left (resp. , right) 11. if (j ≠ id) send reply(j, l) to right (resp. , left) 12. else 13. 14. if already received reply(j, l) from right (resp. left) send probe(ID, l+1, 1) to left and right 10
The complexity of the asynchronous leader election algorithm is n log n 11
An (n log n) lower bound on the number of messages A ring algorithm is uniform if it has no knowledge of the size of the ring. It is non-uniform if it can use its knowledge about the size of the ring. We’ll see a proof for uniform algorithms where the process with the maximum ID must be elected and all other processes must learn its ID (write it to some local variable). 12
Pasting R 1 and R 2 into R (illustration 1. for Lemma 6) p 1 R 1 p 2 e 2 R 2 e 1 q 2 q 1 p 1 R 1 ep p 2 R 2 eq q 1 q 2 13
Induction step of lower bound (illustration 2. for Lemma 6) P p 1 ep R 1 p 2 R 2 eq q 2 q 1 Q 14
An O(n) messages Non-uniform Algorithm for Synchronous Rings 1. Initially phase: =0 and round : =0 2. if (phase = ID) 3. send Leader(ID) 4. terminate as leader 5. else 6. round: =round+1 7. if (round = n) 8. phase: =phase+1 9. round=0 10. Upon receiving Leader(j) 11. 12. 13. if (round < n-1) • Synchronized start • Only clockwise links are used Time complexity? n(min. ID+1) send Leader(j) terminate as non-leader 15
An O(n) messages Uniform Algorithm • Does not rely on synchronized start: a preliminary wake-up phase is added. • Processes that do note wake-up spontaneously act as relays • Messages from process with ID i travel at speed 1/2 i (here we assume IDs are non-negative) 16
An O(n) messages uniform Algorithm (cont’d) Initially waiting is empty and status=asleep 1. Let R be the set of messages received in this computation round 2. S : = empty set ; Set of IDs to send 3. if (status=asleep) then 4. if (R is empty) ; Woke-up spontaneously 5. status: =participating 6. min : = ID ; 7. add (ID, 1) to S ; First-phase message 8. Else status=relay, min : = ∞ ; Act as a relay 9. 10. 11. 12. 13. 14. 15. 16. 17. for each <m , h > in R do if (m < min) become not elected min: =m if (status=relay) and (h=1) add < m, 1 > to S ; m stays first-phase else add <m, 2> to waiting tagged with current round number elseif (m = ID) become elected ; if m>min message is swallowed 18. 19. 20. for each <m, 2 > in waiting do if <m, 2 > was received 2 m-1 rounds ago remove <m, 2 > from waiting and add to S 21. Send S to left 17
A proof that the algorithm has linear complexity 18
- Slides: 18