When working in distributed environments, sometimes you need to have a guarantee that you will always pick the same node, regardless of the cluster topology changes. Such nodes are usually called leaders.
In many systems, electing cluster leaders usually has to do with data consistency and is handled via collecting votes from cluster members. Since in Ignite the data consistency is handled by data grid affinity function (e.g. Rendezvous Hashing), picking leaders in the traditional sense for data consistency outside of the data grid is not needed.
However, you may still wish to have a coordinator node for certain tasks. For this purpose, Ignite lets you automatically always pick either the oldest or the youngest node in the cluster.
Use Service Grid
Note that for most leader or singleton-like use cases, you should use the Service Grid functionality, as it allows you to automatically deploy various Cluster Singleton Services and is usually easier to use.
The oldest node has a property that it remains constant whenever new nodes are added. The only time when the oldest node in the cluster changes is when it leaves the cluster or crashes.
Here is an example of how to select Cluster Group with only the oldest node in it.
IgniteCluster cluster = ignite.cluster(); // Dynamic cluster group representing the oldest cluster node. // Will automatically shift to the next oldest, if the oldest // node crashes. ClusterGroup oldestNode = cluster.forOldest();
The youngest node, unlike the oldest node, constantly changes every time a new node joins a cluster. However, sometimes it may still be useful, especially if you need to execute some task only on the newly joined node.
Here is an example of how to select Cluster Group with only the youngest node in it.
IgniteCluster cluster = ignite.cluster(); // Dynamic cluster group representing the youngest cluster node. // Will automatically shift to the next youngest, if the youngest // node crashes. ClusterGroup youngestNode = cluster.forYoungest();
Once the cluster group is obtained, you can use it for executing tasks, deploying services, sending messages, and more.
Updated about a year ago