Sunday, October 18, 2009

Determining the master node in a JBoss Cluster


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.
  1. public class MyHAService extends HASingletonSupport implements
  2. MyHAServiceMBean {

  3. private static Logger logger =
  4. Logger.getLogger(MyHAService.class);

  5. public void startService() throws Exception {
  6. logger.info(" *** STARTED MY SINGLETON SERVICE *** ");
  7. super.startService();
  8. }

  9. public void stopService() throws Exception {
  10. logger.info(" *** STOPPED MY SINGLETON SERVICE *** ");
  11. super.stopService();
  12. }

  13. public boolean isMasterNode() {
  14. return super.isMasterNode();
  15. }


  16. public void startSingleton() {
  17. logger.info(" *** CURRENT NODE IP:"
  18. + this.getPartition().getClusterNode()
  19. .getIpAddress().getHostAddress() +
  20. " ELECTED AS A MASTER NODE *** ");
  21. }


  22. public void stopSingleton() {
  23. logger.info(" *** CURRENT NODE IP:"
  24. + this.getPartition().getClusterNode()
  25. .getIpAddress().getHostAddress()
  26. + " STOPPED ACTING AS A MASTER NODE *** ");
  27. }


  28. public void partitionTopologyChanged(List newReplicants, int newViewID) {
  29. logger.info(" *** TOPOLOGY CHANGE STARTING *** ");
  30. super.partitionTopologyChanged(newReplicants, newViewID);

  31. }
  32. }
startSingleton() - invoked when the current node elected as a master.
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:
  1. private String getMasterSocket() {

  2. HAPartition partition = this.getPartition();

  3. if (partition != null) {

  4. if (partition.getCurrentView() != null) {
  5. return partition.getCurrentView().get(0).toString();

  6. } else {
  7. return null;
  8. }
  9. } else {
  10. return null;
  11. }
  12. }
The method above will return me a string contains port and ip of the master node, for example:
192.168.62.12:1099
The 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