Bully algorithm in Java

The bully algorithm is a type of Election algorithm which is mainly used for choosing a coordinate. In a distributed system, we need some election algorithms such as bully and ring to get a coordinator that performs functions needed by other processes.

Election algorithms select a single process from the processes that act as coordinator. A new process is selected when the selected coordinator process crashes due to some reasons. In order to determine the position where the new copy of coordinator should be restarted, the election algorithms are used.

It assumes that each process has a unique priority number in the system, so the highest priority process will be chosen first as a new coordinator. When the current use coordinator process crashes, it elects a new process having the highest priority number. We note that priority number and pass it to each active process in the distributed system.

The Bully election algorithm is as follows:

Let's assume that P is a process that sends a message to the coordinator.

  1. It will assume that the coordinator process is failed when it doesn't receive any response from the coordinator within the time interval T.
  2. An election message will be sent to all the active processes by process P along with the highest priority number.
  3. If it will not receive any response within the time interval T, the current process P elects itself as a coordinator.
  4. After selecting itself as a coordinator, it again sends a message that process P is elected as their new coordinator to all the processes having lower priority.
  5. If process P will receive any response from another process Q within time T:
    1. It again waits for time T to receive another response, i.e., it has been elected as coordinator from process Q.
    2. If it doesn't receive any response within time T, it is assumed to have failed, and the algorithm is restarted.

BullyAlgoExample.java

BullyAlgoExample2.java

Output:

Bully algorithm in Java