FileDocCategorySizeDatePackage
HASingletonSupport.javaAPI DocJBoss 4.2.19204Fri Jul 13 20:52:36 BST 2007org.jboss.ha.singleton

HASingletonSupport

public class HASingletonSupport extends org.jboss.ha.jmx.HAServiceMBeanSupport implements HASingleton, HASingletonSupportMBean
Base class for HA-Singleton services.
author
Ivelin Ivanov
author
Scott Stark
author
Dimitris Andreadis
version
$Revision: 61770 $

Fields Summary
private boolean
isMasterNode
private HASingletonElectionPolicy
mElectionPolicyMB
private boolean
restartOnMerge
Constructors Summary
public HASingletonSupport()
Default CTOR


   // Constructors --------------------------------------------------
   
         
    
   
      // empty
   
Methods Summary
public void_stopOldMaster()
This method will be invoked twice by the local node when it stops as well as by the remote

      log.debug("_stopOldMaster, isMasterNode=" + isMasterNode);
      
      try 
      {
         // since this is a cluster call, all nodes will hear it
         // so if the node is not the master, then ignore 
         if (isMasterNode == true)
         {
            isMasterNode = false;
            
            // notify stopping
            sendLocalNotification(HASINGLETON_STOPPING_NOTIFICATION);
            
            // stop the singleton
            stopSingleton();
            
            // notify stopped
            sendLocalNotification(HASINGLETON_STOPPED_NOTIFICATION);
         }
      }
      catch (Exception ex)
      {
         log.error(
            "_stopOldMaster failed. Will still try to start new master. " +
            "You need to examine the reason why the old master wouldn't stop and resolve it. " +
            "It is bad that the old singleton may still be running while we are starting a new one, " +
            "so you need to resolve this ASAP.", ex);
      }
   
public HASingletonElectionPolicygetElectionPolicy()

see
HASingletonSupportMBean#getElectionPolicy()

      return this.mElectionPolicyMB;
   
public booleangetRestartOnMerge()
Gets whether this singleton will stop and restart itself if it is the master and a cluster merge occurs.

A restart allows the service to reset any state that may have gotten out-of-sync with the rest of the cluster while the just-merged split was in effect.

return
true if a restart will occur, false otherwise

      return restartOnMerge;
   
public booleanisMasterNode()

jmx:managed-attribute
return
true if this cluster node has the active mbean singleton, false otherwise

      return isMasterNode;
   
protected voidmakeThisNodeMaster()

      try
      {
         // stop the old master (if there is one) before starting the new one

         // ovidiu 09/02/04 - temporary solution for Case 1843, use an asynchronous
         // distributed call.
         //callMethodOnPartition("_stopOldMaster", new Object[0], new Class[0]);
         callAsyncMethodOnPartition("_stopOldMaster", new Object[0], new Class[0]);

         startNewMaster();  
      }
      catch (Exception ex)
      {
         log.error("_stopOldMaster failed. New master singleton will not start.", ex);
      }
   
public voidpartitionTopologyChanged(java.util.List newReplicants, int newViewID)
When topology changes, a new master is elected based on the result of the isDRMMasterReplica() call.

see
HAServiceMBeanSupport#partitionTopologyChanged(List, int)
see
DistributedReplicantManager#isMasterReplica(String);

   
      boolean isElectedNewMaster;
      if (this.mElectionPolicyMB != null)
         isElectedNewMaster = this.mElectionPolicyMB.isElectedMaster(this.getPartition());
      else
         isElectedNewMaster = isDRMMasterReplica();
      
      if (log.isDebugEnabled())
      {
         log.debug("partitionTopologyChanged, isElectedNewMaster=" + isElectedNewMaster
            + ", isMasterNode=" + isMasterNode + ", viewID=" + newViewID);
      }

      // if this node is already the master, don't bother electing it again
      if (isElectedNewMaster && isMasterNode)
      {
         // JBAS-4229         
         if (restartOnMerge && ClusterMergeStatus.isMergeInProcess())
         {
            restartMaster();
         }
      }
      // just becoming master
      else if (isElectedNewMaster && !isMasterNode)
      {
         makeThisNodeMaster();
      }
      // transition from master to slave
      else if (isMasterNode == true)
      {
         _stopOldMaster();
      }
   
protected voidrestartMaster()

      _stopOldMaster();
      startNewMaster();
   
private voidsendLocalNotification(java.lang.String type)

      Notification n = new Notification(type, this, getNextNotificationSequenceNumber());
      super.sendNotificationToLocalListeners(n);
   
public voidsetElectionPolicy(HASingletonElectionPolicy mb)

see
HASingletonSupportMBean#setElectionPolicy(HASingletonElectionPolicy)

      this.mElectionPolicyMB = mb;
   
public voidsetRestartOnMerge(boolean restartOnMerge)
Sets whether this singleton will stop and restart itself if it is the master and a cluster merge occurs?

A restart allows the service to reset any state that may have gotten out-of-sync with the rest of the cluster while the just-merged split was in effect.

param
restartOnMerge true if a restart should occur, false otherwise

      this.restartOnMerge = restartOnMerge;
   
protected voidstartNewMaster()

      log.debug("startNewMaster, isMasterNode=" + isMasterNode);
      
      isMasterNode = true;
      
      // notify starting
      sendLocalNotification(HASINGLETON_STARTING_NOTIFICATION);

      // start new master
      startSingleton();
      
      // notify started
      sendLocalNotification(HASINGLETON_STARTED_NOTIFICATION);
   
public voidstartSingleton()
Extending classes should override this method and implement the custom singleton logic. Only one node in the cluster is the active master. If the current node is elected for master, this method is invoked. When another node is elected for master for some reason, the stopSingleton() method is invokded.

When the extending class is a stateful singleton, it will usually use putDistributedState() and getDistributedState() to save in the cluster environment information that will be needed by the next node elected for master should the current master node fail.

see
HASingleton

      if (log.isDebugEnabled())
         log.debug("startSingleton() : elected for master singleton node");

      // Extending classes will implement the singleton logic here
   
public voidstopSingleton()
Extending classes should override this method and implement the custom singleton logic. Only one node in the cluster is the active master. If the current node is master and another node is elected for master, this method is invoked.

see
HASingleton

      if (log.isDebugEnabled())
         log.debug("stopSingleton() : another node in the partition (if any) is elected for master");
      
      // Extending classes will implement the singleton logic here