Chapter 13 Leader Election Leader Election n Breaking
Chapter 13 Leader Election
Leader Election n Breaking the symmetry in system Similar to distributed mutual exclusion problems, the first process to enter the CS can be the leader Lamport’s algorithm is not efficient for leader election in a ring
Ring based leader election n No deterministic algorithm for leader election in an anonymous ring q q Initial state is completely symmetric All processes take the same action The new state is also symmetric This can be repeated forever
Chang-Robert’s algorithm n n Every process sends an election message with its id to the left process if it has not seen a message from a higher process 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 then declares itself to be the leader by sending a leader message
//Chang-Roberts Leader Election public class Ring. Leader extends Process implements Election { int number; int leader. Id = -1; int next; boolean awake = false; … public synchronized int get. Leader(){ while (leader. Id == -1) my. Wait(); return leader. Id; } public synchronized void handle. Msg(Msg m, int src, String tag) { int j = m. get. Message. Int(); // get the number if (tag. equals("election")) { if (j > number) send. Msg(next, "election", j); // forward the message else if (j == number) // I won! send. Msg(next, "leader", my. Id); else if ((j < number) && !awake) start. Election(); } else if (tag. equals("leader")) { leader. Id = j; notify(); if (j != my. Id) send. Msg(next, "leader", j); } } public synchronized void start. Election() { awake = true; send. Msg(next, "election", number); } }
Chang Roberts Leader Election n Worst case message complexity Best case Worst case
Hirschberg-Sinclair algorithm n n n Assume ring is bidirectional Carry out elections on increasingly larger sets Algorithm works in asynchronous rounds Only processes that win the election in round r can proceed to round r+1 Algorithm: Pi is the leader in round r iff it has the largest id of all nodes that are at a distance 2 r or less from Pi
Hirschberg-Sinclair algorithm n Initially: q n Round 0: q n 7, 8 are leaders Round 2: q n 6 , 7 and 8 are leaders Round 1: q n All processes are leaders 8 is the only leader At most log(N) rounds
Election on general graphs n n Totally connected graph – use Lamport’s mutex algorithm If the graph is not completely connected q Construct a spanning tree
Spanning tree construction n n Assume a distinguished process root (we will remove this assumption later) root initiates the algorithm by sending an invite to all neighbors When Pi receives an invite for the first time, (say from Pj) it sends the invite to all processes except Pj and sends accept to Pj (i. e. Pj is now the parent of P i) Pi replies with a reject to all invites received later All processes maintain the # of nodes from which they have received messages
public class Span. Tree extends Process { public int parent = -1; // no parent yet public Int. Linked. List children = new Int. Linked. List(); int num. Reports = 0; boolean done = false; public Span. Tree(Linker init. Comm, boolean is. Root) { if (is. Root) { parent = my. Id; if (init. Comm. neighbors. size() == 0) done = true; else send. To. Neighbors( "invite", my. Id); } }. . . public synchronized void handle. Msg(Message m, int source, String tag) { if (tag. equals("invite")) { if (parent == -1) { num. Reports++; parent = source; send. Msg(source, "accept") for (int i = 0; i < N; i++) if ((i != my. Id) && (i != source) && is. Neighbor(i)) send. Msg(i, "invite"); } else send. Msg(source, "reject"); } else if ((tag. equals("accept")) || (tag. equals("reject"))) { if (tag. equals("accept")) children. add(source); num. Reports++; if (num. Reports == comm. neighbors. size()) { done = true; notify(); } }
Spanning tree n This algorithm can be also used for broadcast by flooding when there is no predefined spanning tree n If there is no distinguished process q q q each process can start the spanning tree construction All messages in the spanning tree started by Pi contain the id of Pi Only the instance started by the largest process succeeds
Application: Computing global functions n If we have variable xi at process Pi: q n Compute a function f(x 1, x 2, x 3 , …, xn) Assume we have a predefined spanning tree and use convergecast and broadcast to compute the global function
Convergecast Broadcast
Computing a global function n n Assume the global function is commutative and associative Eg. min, max, sum, product Each node of the tree computes the function using its value and the values received from its children This partial answer is sent to the parent When the root computes the final value, it broadcasts the answer
- Slides: 15