How does it work ?
- Peer is assigned a uuid based on timestamp.
- On joining the network, the peer broadcasts its ID.
- If the peer doesn't receive any response, it broadcasts itself as the winner( Winner message )
- If there are other peers in the network, they add the new peer in their peers list.
- If there is a leader in the network, the leader sends the entire list of peers along with a secret value to the new peer
- Whereas, if there is no leader in the network, every peer will send its identity to the new peer and announce election
- If the new peer receives the peers list from the leader, it updates its own peer list and registers the leader id as the leader along with the secret value.
- If 5 doesnt happen, then each peer broadcasts a fresh election in a randomized timeout fashion.
- Whichever election id gets annouced first will be held and the rest will be cancelled
- The election occurs in a bully algorithm style.
- When the leader wins, it adds a secret value field to its "winner" announcement message. Reason for the secret message will be mentioned next.
- The peers will then register their new leader and keep pinging the leader in randomized intervals.
- The leader will respond with a pong message along with the secret value.
- This scenario might occur where leader crashes/exits and comes back to network again before the peers realize that the leader was dead for sometime. The "leader" will have no idea what secret value to send back to peer's ping request.
- When the leader sends the wrong secret value as response in the pong message, the peers again announce re-election in randomized timeout fashion. Since, the leader crashed, it will have no idea about the peers in the network and behave like a new peer in the network.
- On registering the leader, we will inform our local server daemon about the new leader.
The oldest peer (peer ID) will win the election. The peer ID is generated from the system timestamp.
Split brain is a situation where there are multiple clusters in the same network but they dont really know about their presence. But there are situations where clusters can detect their presence. One such situation is broadcasting the winner of the election which reaches a separate cluster. Well, in these kind of situations, one of the leaders will send a face off message to the leader of other cluster and will broadcast its peers list. The leader of the other cluster will respond by broadcasting its own peers list. All the peers in the network then combines the peers list shared by the leaders of different clusters and holds another fair re-election.
When the leader quits and joins back before the peers even detect that the leader was unavailable for sometime?
Well, why should the peers be bothered even if the leader died for a second?
All the metadata regarding files shared by peers is available only with the leader, till its alive. If the leader dies in between and resurrects within a second, the leader has still lost all the data shared by the peers.
We tend to solve this problem by pinging the leader with a secret value that is shared at the time when leader wins the election. The leader appends a secret value to the the winner message, so that when the peers ping their leader, the leader will reply back with that secret value to prove its authenticity. Its like maintaining a session. When the leader dies and resurrects, it will have no idea about the secret value shared with the peers. This leads to the confirmation that the leader has resurrected and has no data, therefore there will be a re-election. This means, when a new peer joins, the leader will send the peer list along with the secret value. Can we make it more secure ?? Sure, we can set a time interval, where the leader updates the secret value among peers.
When should the leader announce the secret value?
- When a new peer joins, the leader sends a list of peers along with the secret value to the new peer.
- The leader broadcasts the secret value when it wins the election.
When is the secret value invalidated?
We set the secret value to
None when the leader is dead or when the leader pongs back the incorrect secret value.