What is the advised approach to ensuring a particular process is running exactly once within the grid?
Usecase would be subscription of a single multiplexed stream from a remote source in order to update data across the grid. We need to elect the node to subscribe and elect a new node to subscribe when that node fails.
Is there any pre-built pattern for this in gridgain or is it solved by a combination of listening to grid lifecycle events and a distributed CAS operation?
Another usecase would be a singleton job that must run forever, migrating to a new node on failure.
Thanks.
You can simply take the oldest node in the cluster and start your operation on that node (Grid.nodes() will return all the nodes in the grid). You should also subscribe a discovery event listener on other nodes and have the next oldest node take over in case if oldest node fails.
To check if a node is oldest or node you can use GridNode.order() method. Node with smallest order will be the oldest.
To listen to discovery events, you can use this code:
Starting with GridGain 6.2.0-rc2 release, GridGain has several ways for leader election:
GridProjection.oldest()
will return you a dynamic projection over the oldest node in the cluster. If oldest node leaves cluster for whatever reason, then the next oldest node is automatically picked, so user can continue using the oldest node without interruption.cluster-singleton-service
,per-node-singleton-service
, orper-cache-key-singleton-service
. This is not leader election per se, but it may remove the need for leader election altogether as GridGain, for example, will guarantee forcluster-singleton-service
that there is only one instance of that service available in the grid at any time.For more information on distributed services refer to Distributed Services documentation.
Please, check the code bellow. I tested it using GridGain 6.1.8.