The bully algorithm is one of methods in distributed computing for implementation of the leader election process for dynamically electing a coordinator by process ID number. The process with the highest number is selected as the coordinator.
To run the algorithm you need a working installation of Erlang R15BXX (or later).
$ make get-deps
$ make compile-all
Configure timeout and nodes in the bully.config.
$ ./start.sh 1@127.0.0.1
$ ./start.sh 2@127.0.0.1
$ ./start.sh 3@127.0.0.1
$ ./start.sh 4@127.0.0.1
Then you can kill a node and see logs.
Implementation consists of monitoring of the current leader and election a new one
when the current is unavailable. The election of a new leader should be done as soon as possible.
You have a distributed network from N nodes where each node described by an unique identity number.
Each node sends ping every T seconds to the current leader and waits 4*T seconds for pong reply.
On the waiting timeout the leader election process should be started.
- A node started the election starts sending
announce electionto all nodes with a higher identity number and waits forok.- If no one
okafterTseconds the node starts leading and sendsnew leaderto all nodes. - If received
okthe node starts waiting fornew leadernextTseconds. Onnew leaderwaiting timeout the node starts announcing election again.
- If no one
- When receiving
announce electiona node replies withokand starts the election process itself.- If the node received
announce electionis with the highest identity number it starts leading with sendingnew leaderto all nodes.
- If the node received
- When receiving
new leadera node saves a new leader and starts monitoring it.
Inspired by an article about a test task from aboutecho.com.
Copyright (C) 2014 Andrew Djoga, released under the MIT license.
