16
9. IMPLEMENTATION OF ELECTION ALGORITHMS In distributed computing, leader election is the process of designating a single process as the organizer of some task distributed among several computers (nodes). Before the task is begun, all network nodes are unaware which node will serve as the "leader," or coordinator, of the task. After a leader election algorithm has been run, however, each node throughout the network recognizes a particular, unique node as the task leader. The network nodes communicate among themselves in order to decide which of them will get into the "leader" state. For that, they need some method in order to break the symmetry among them. For example, if each node has unique identities, then the nodes can compare their identities, and decide that the node with the highest identity is the leader. The definition of this problem is often attributed to LeLann, who formalized it as a method to create a new token in a token ring network in which the token has been lost. Leader election algorithms are designed to be economical in terms of total bytes transmitted, and time. The algorithm suggested by Gallager, Humblet, and Spira for general undirected graphs has had a strong impact on the design of distributed algorithms in general, and won

Implementation of Election Algorithms

Embed Size (px)

Citation preview

Page 1: Implementation of Election Algorithms

9. IMPLEMENTATION OF ELECTION ALGORITHMS

In distributed   computing, leader   election is   the   process   of   designating   a   single process as   the   organizer   of   some   task   distributed   among   several   computers (nodes). Before the task is begun, all network nodes are unaware which node will serve as the "leader," or coordinator, of the task. After a leader election algorithm has been run, however, each node throughout the network recognizes a particular, unique node as the task leader.

The network nodes communicate among themselves in order to decide which of them will get into the "leader" state. For that, they need some method in order to break the symmetry among them. For example, if each node has unique identities, then the nodes can compare their  identities, and decide that the node with the highest identity is the leader.

The definition of this problem is often attributed to LeLann, who formalized it as a method to create a new token in a token ring network in which the token has been lost.

Leader   election   algorithms   are   designed   to   be   economical   in   terms   of total bytes transmitted, and time. The algorithm suggested by Gallager, Humblet, and Spira for general undirected graphs has had a strong impact on the design of distributed algorithms in general, and won the Dijkstra Prize for an influential paper in distributed computing.

a. RING ELECTION ALGORITHM:

The algorithm assumes that each process has a Unique Identification (UID) and that the   processes   can   arrange   themselves   in   a unidirectional   ring with   a communication channel going to the clockwise and anticlockwise neighbour. The 2 part algorithm can be described as follows:

1. Initially each process in the ring is marked as non-participant.

Page 2: Implementation of Election Algorithms

2. A  process   that  notices  a   lack  of   leader   starts  an  election.   It  marks   itself as participant and   creates   an election  message containing   its  UID.   It   then sends this message clockwise to its neighbour.

3. When a process receives an election message it compares the UID with its own,   if   the   current   process   has   a   larger   UID   it   replaces   the   one   in the election   message with   its   UID.   The   process   then   marks   itself as participant and   again   forwards   the election   message in   a   clockwise direction.

4. If the process was already marked as participant when it receives an election message the procedure is different. In this case it will compare the UID as before but only forward the election message if it has needed to replace the UID.

The algorithm finishes when a process receives an election message containing its own UID. Then the second stage of the algorithm takes place

1. This process marks itself as non-participant and sends an elected message to its neighbour announcing its election and UID.

2. When   a   process   receives   an elected   message it   marks   itself   as non-participant records the elected UID and again forwards the elected message.

3. When the elected message reaches the newly elected process the election is over.

Assuming there are no failures this algorithm will finish.

For Example:Let’s assume that we have 7 nodes:

                         

Page 3: Implementation of Election Algorithms

At   this   current  moment   in  time,  node   ‘7’   is   the  coordinator,  because   it   is   the highest numbered node. But then node ‘7’ leaves, so who is now the coordinator? Lets say node ‘4’ decides it wants to be the coordinator, then we have:

Here, node ‘4’ has sent a ELECTION to nodes ‘5’, ‘6’ and ‘7’, this is because none of the nodes know that node ‘7’ has left yet. In this case, node ‘7’ cannot reply, yet, nodes ‘5’, and ‘6’ are still here, so they reply to ‘4’ that they are still here:

Now we have nodes ‘5’ and ‘6’ sending out elections. In this case, node ‘5’ sends an election to node ‘6’ and ‘7’. Whereas node ‘6’ just sends an election to node ‘7’:

Page 4: Implementation of Election Algorithms

Now, since node ‘6’ is still here, then node ‘6’ tells node ‘5’ that it is still here:

Finally,  since node  ‘7’  has not replied to node  ‘6’,  this   implies that node  ‘7’  no longer exists, meaning that node ‘6’ is the new coordinator. So node ‘6’ tells all the other nodes:

Program:#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<time.h>int main(){   clrscr();    int node_state[6]={1,1,1,1,1,1};    int ring_nodes[6]={9};    int cord = 6;    int req_node;    int new_req;    int new_del;    int new_act_node;

Page 5: Implementation of Election Algorithms

    int fin_req=0;    int oneTimeFlag=0;    int i, k, x=99, m=0, n_req=0;        //destroying the coordinator    node_state[5]=0;    cord=9;    printf("The node 6 has broke down, Have to relect a new coordinator using ring algorithm.......\n");    getch();    while(fin_req!=999)    {                                  if(cord==9)            {                       //getting request for node                       srand ( time(NULL) );                  req_node = (rand() % 7);                   if(req_node==0)                      req_node=1;                    if(node_state[req_node-1]==0)                  {                        //take the node to the right or if node6 is there then take 5                        if(req_node==6)                              req_node=5;                        else                              req_node+=1;                  }                  printf("\nThe random node requesting permission is, Node : %d",req_node);                  getch();                  //electing the new coordinator                  cord=req_node;                  printf("\nThe cordinator node is : %d",cord);                  getch();                  ring_nodes[0]=req_node;                  printf("\nThe ring  nodes is : %d", ring_nodes[0]);                  getch();                  k=1;                  n_req = (req_node+1);                  while(x!=req_node)

Page 6: Implementation of Election Algorithms

                  {                        printf("\nEntered loop with x : %d and n_req : %d",x, n_req);                        getch();                        if(n_req==7)                        {                            n_req=1;                            printf("\nEntered node 6 loop with n_req is : %d",cord);                            getch();                        }                        if(node_state[n_req-1]==1)                        {                            printf("\nEntered node stated checking and ring_nodes setting loop, k : %d",k);                            getch();                                                  ring_nodes[k]=n_req;                        }                        else                        {                            n_req++;                            continue;                        }                        x=n_req;                        n_req++;                          k++;                                             }                 for(m=0;m<5;m++)                 {                     if(ring_nodes[m+1]>ring_nodes[m])                     {                                             cord=m+1;                     }                 }                  printf("\nThe new relected coordinator is, Node : %d", cord);                  //bringing up a new node                  srand ( time(NULL) );                  new_req = (rand() % 7);                  if(new_req==0)                       new_req=1;                  node_state[new_req-1]=1;

Page 7: Implementation of Election Algorithms

                  printf("\nThe node : %d is up and running....\n", new_req);                  getch();                  if(new_req>cord)                  {                         printf("\nThe subsequent coordinator is, Node : %d",new_req);                         getch();                         cord=new_req;                  }            }            //else            //{                  //bringing up a new node                  srand ( time(NULL) );                  new_req = (rand() % 7);                  if(new_req==0)                         new_req=1;                  node_state[new_req-1]=1;                  printf("\nThe node : %d is up and running....\n", new_req);                  getch();                  if(new_req>cord)                  {                         printf("\nThe subsequent coordinator is, Node : %d",new_req);                         getch();                         cord=new_req;                  }                  else                  {                         printf("\nThe coordinator for this process is, Node : %d",cord);                         getch();                  }            //}            //Destroying a new node            srand ( time(NULL) );            new_del = (rand() % 7);            if(new_del==0)                  new_del=1;            printf("\nThe node : %d has broke down......", new_del);            node_state[new_del-1]=0;

Page 8: Implementation of Election Algorithms

            if(new_del==cord)            {                   printf("\nSubsequently, the coordinator is also down and a new coordinator must be elected\n");                   cord=9;                   getch();            }                                                                             printf("\nPress 999 to exit or any other key to continue.....");            scanf("%d",&fin_req);     }    getch();    return 0;}

OUTPUT

                     

              

Page 9: Implementation of Election Algorithms

b. BULLY ELECTION ALGORITHM:

The bully algorithm is a method in distributed computing for dynamically selecting a coordinator by process ID number.

When a process P determines  that  the current  coordinator   is  down because of message timeouts or failure of the coordinator to initiate a handshake, it performs the following sequence of actions:

1. P broadcasts an election message (inquiry) to all other processes with higher process IDs.

2. If  P  hears   from no  process  with  a  higher  process   ID   than   it,   it  wins   the election and broadcasts victory.

3. If P hears from a process with a higher ID, P waits a certain amount of time for that process to broadcast itself as the leader. If it does not receive this message in time, it re-broadcasts the election message.

Note that if P receives a victory message from a process with a lower ID number, it immediately initiates a new election. This is how the algorithm gets its name - a process with a higher ID number will bully a lower ID process out of the coordinator position as soon as it comes online.

Program:

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<time.h>

Page 10: Implementation of Election Algorithms

int main(){    int node_state[6]={1,1,1,1,1,1};    int cord = 6;    int req_node;    int new_req;    int new_del;    int new_act_node;    int fin_req=0;    int oneTimeFlag=0;    int i;        //destroying the coordinator    node_state[5]=0;    cord=9;    printf("The node 6 has broke down, Have to relect a new coordinator .......\n");    getch();    while(fin_req!=999)    {                                  if(cord==9)            {                       //getting request for node                       srand ( time(NULL) );                  req_node = (rand() % 7);                   if(req_node==0)                      req_node=1;                    if(node_state[req_node-1]==0)                  {                        //take the node to the right or if node6 is there then take 5                        if(req_node==6)                              req_node=5;                        else                              req_node+=1;                  }                  printf("\nThe random node requesting permission is, Node : %d",req_node);                  getch();                  //electing the new coordinator                  cord=req_node;                  for(i=req_node; i<6; i++)                  {                         //check state                         if(node_state[i]==1)                            cord=i+1;                         else                             continue;   

Page 11: Implementation of Election Algorithms

                  }                  printf("\nThe new relected coordinator is, Node : %d", cord);                  //bringing up a new node                  srand ( time(NULL) );                  new_req = (rand() % 7);                  if(new_req==0)                       new_req=1;                  node_state[new_req-1]=1;                  printf("\nThe node : %d is up and running....\n", new_req);                  getch();                  if(new_req>cord)                  {                         printf("\nThe subsequent coordinator is, Node : %d",new_req);                         getch();                         cord=new_req;                  }            }            //else            //{                  //bringing up a new node                  srand ( time(NULL) );                  new_req = (rand() % 7);                  if(new_req==0)                         new_req=1;                  node_state[new_req-1]=1;                  printf("\nThe node : %d is up and running....\n", new_req);                  getch();                  if(new_req>cord)                  {                         printf("\nThe subsequent coordinator is, Node : %d",new_req);                         getch();                         cord=new_req;                  }                  else                  {                         printf("\nThe coordinator for this process is, Node : %d",cord);                         getch();                  }            //}            //Destroying a new node            srand ( time(NULL) );            new_del = (rand() % 7);            if(new_del==0)                  new_del=1;

Page 12: Implementation of Election Algorithms

            printf("\nThe node : %d has broke down......", new_del);            node_state[new_del-1]=0;            if(new_del==cord)            {              printf("\nSubsequently, the coordinator is also down and a new coordinator must be elected\n");                                              cord=9;                   getch();            }                                                                             printf("\nPress 999 to exit or any other key to continue.....");            scanf("%d",&fin_req);     }    getch();    return 0;}

OUTPUT