Algorithm complexity is better than in bully algorithm, because its worst case is
O(N^2), which happens only if hosts are organized into the network using by increasing (or decreasing, depends on the what extreme is used) order of their UIDs.Algorithm is pretty simple:
- Some process
Pnotes a lack of the leader and initiates the election. It creates election message and send it to the next process in the ring.Psends own UID in the message, so it is a candidate. - Each process that receives election message forwards the message to the next peer and marks iteself as election participant.
- When process receives the election message it compares UID in the message with own UID. If own UID is larger, it means that this host has more chances to become a leader. So it does not send the original election message with received UID, but sends own election message with own UID. Now it's a candidate.
- When candidate receives the election message with different UID, it means that it lost the election and is not a candidate but only participant.
- When candidate process receives the election message with own UID, it means that the message made a cirtle in the network ring, and there was no process with larger UID. This process can become a leader. Actually, the process starts acting as a leader.
- When process becomes a leader, it has to notify others that it's a leader now. It marks itself as non-participant and sends elected message. Every next process that receivs the message marks itself as non-participant and forwards the message to next peer. When the message reaches the leader, it discards the message, so it's removed from the network.
- Election completed.