Thanks to the original author and the source
How to determine which node in the cluster is the master node?
Have you ever dealt with clustered singleton service? How to determine which cluster node is the master? Well, if I am the current node, I can simply ask whether I am the master or not. But what if I already know that the current node is not the master, and I want to determine which node among other nodes in the cluster is the master?First I would like to give brief summary about HASingleton service (HA stands for High Availability).
Summary:
HASingleton service, is a service that is deployed on every node in a cluster, but runs only on one node, while the other nodes remain passive. The node that the service runs on, is the master node.
How does JBoss selects the master node?
Well the first node in a cluster will become master node. If existing master node will leave the cluster as a result of a shutdown for example, another node is selected as master from the remaining nodes.
Master node can control which tasks will get executed, and how many times. HASingletons also have the ability to share a memory state across clustered partition. Something like caching ...
Solution:
Lets assume that I have a service bean that extends from HASingletonSupport class. HASingletonSupport in its turn extends from HAServiceMBeanSupport
and implements two interfaces: HASingletonMBean and HASingleton. All of them give me those wonderful APIs that can tell me whether the current node is the master or not, what the status of my cluster, how many nodes etc. etc.
- public class MyHAService extends HASingletonSupport implements
- MyHAServiceMBean {
-
- private static Logger logger =
- Logger.getLogger(MyHAService.class);
-
- public void startService() throws Exception {
- logger.info(" *** STARTED MY SINGLETON SERVICE *** ");
- super.startService();
- }
-
- public void stopService() throws Exception {
- logger.info(" *** STOPPED MY SINGLETON SERVICE *** ");
- super.stopService();
- }
-
- public boolean isMasterNode() {
- return super.isMasterNode();
- }
-
-
- public void startSingleton() {
- logger.info(" *** CURRENT NODE IP:"
- + this.getPartition().getClusterNode()
- .getIpAddress().getHostAddress() +
- " ELECTED AS A MASTER NODE *** ");
- }
-
-
- public void stopSingleton() {
- logger.info(" *** CURRENT NODE IP:"
- + this.getPartition().getClusterNode()
- .getIpAddress().getHostAddress()
- + " STOPPED ACTING AS A MASTER NODE *** ");
- }
-
-
- public void partitionTopologyChanged(List newReplicants, int newViewID) {
- logger.info(" *** TOPOLOGY CHANGE STARTING *** ");
- super.partitionTopologyChanged(newReplicants, newViewID);
-
- }
- }
stopSingleton() - invoked when the current node stops acting as a master.
partitionTopologyChanged() - invoked when new node joins or leaves the cluster.
As i mentioned before, I have the ability to know whether the current node is the master node, by calling isMasterNode(). The method will return true if the node is master and false if its not.
In case I already know that the current node is not the master, I can ask the clustered partition (the cluster) which node is the master. For example I can request the current view of my cluster.
The implementation can be similar to the method below which you can have inside your service bean:
- private String getMasterSocket() {
-
- HAPartition partition = this.getPartition();
-
- if (partition != null) {
-
- if (partition.getCurrentView() != null) {
- return partition.getCurrentView().get(0).toString();
-
- } else {
- return null;
- }
- } else {
- return null;
- }
- }
192.168.62.12:1099The HAPartition service maintains across cluster a registry of nodes in a view order. Now, keep in mind that an order of the nodes in the view, does not necessary reflect the order nodes have joined the cluster.
So the first node in the view as you can see beloew, will be the master node.
Simple as that.
return partition.getCurrentView().get(0).toString();Please note:
Method getPartition() may return null, if super.startService() hasnt been called. Have a look at implementation of HAServiceMBeanSupport and my other post JBoss Clustering - How many nodes in the cluster?.
No comments:
Post a Comment