Table of Contents |
---|
...
Code Block | ||
---|---|---|
| ||
public class ClusterSample { public static void main(String[] args) { ClusterManager clusterManager = new HazelcastClusterManager("FEJNode1"); //(1) clusterManager.addLocalNodeLeaderListener(new LocalNodeLeaderListenerAdaptor() { @Override public void onGranted() { //(2) System.out.println("This node was elected as a leader"); } @Override public void onRevoked() { //(3) System.out.println("This node isn't leader any more"); } }); clusterManager.join(); //(4) clusterManager.electLeader(clusterManager.localNode().id()); //(5) //pause 1 sec try { Thread.sleep(1000); } catch (InterruptedException e) { } //print nodes clusterManager.nodes().stream().forEach(System.out::println); //(6) clusterManager.leave(); //(7) } } |
...
Each node at start receives information about available nodes in the cluster and decides whether or not it should be elected a leader in that moment. This decision is based on the presence or absence of a quorum (see Hazelcast configuration). It is considered that the cluster has a quorum (and the leader can be chosen) if
...
If the quorum size is not specified, it is considered that even one node already composes a quorum.
Info |
---|
Current implementation only automatically chooses a leader in the cluster when the cluster did not have a leader before. |
The first time leader selection algorithm is:
If the new node has decided that it is necessary to choose a leader, it sends the
LEADER_EVENT
event to the cluster and offers a new leader.Once the proposed new leader gets the
LEADER_EVENT
event with its ID, it notifies the application about its new status by callingLocalNodeLeaderListener.onGranted()
and sends theLEADER_STARTED_EVENT
event with its ID back to the cluster.When the rest of the cluster nodes receive the
LEADER_STARTED_EVENT
event , they set their state to BACKUP (mark themselves as backup), and notify their applications about the new status by calling LocalNodeBackupListener.onBackup().
Info |
---|
You can check the presence of the leader in the cluster by using |
Appointment of a new leader
...
The node that calls the
HazelcastClusterManager.electLeader()
method sendsthe sends the clusterLEADER_EVENT
event a new leader ID.When the backup node receives the
LEADER EVENT
event, it calls theLocalNodeBackupListener.offBackup()
method.The node that was elected as the new leader launches a timer and waits for the old leader to be finishedfinish. This mechanism was introduced to minimize the possibility of two leaders in the cluster working simultaneously. The old leader needs some time to complete its processes. A timer is used to protect the cluster from endless waiting when the old leader becomes inaccessible or crashes while leaders are being switched.
The old leader receives the
LEADER_EVENT
event and notifies its application about the status change by callingLocalNodeLeaderListener.onRevoked()
. After this call is completed successfully, it sends aLEADER_STOPPED_EVENT
event to the cluster.When the new leader node receives a
LEADER_STOPPED_EVENT
event, (or if it doesn’t receive this event duringtimeoutLeaderShutdown
period), it notifies the application about the new status by callingLocalNodeLeaderListener.onGranted()
and sends to the clusterLEADER_STARTED_EVENT
event with its ID.When the rest of the cluster nodes receive the
LEADER_STARTED_EVENT
event, they change their state to BACKUP (mark themselves as backup) and notify their applications about the new status by callingLocalNodeBackupListener.onBackup()
.
...
Since version 1.4.0, by default, the new leader can be automatically elected when the existing leader is gone. Configuration options are described here.