4 Replies Latest reply on Oct 31, 2007 10:15 AM by krithi

    Controlling cluster services

    krithi

      I Have setup 2 nodes in a cluster using Jboss 4.0.5 GA
      My requirement is:
      1. When the first node starts, it becomes the master and starts all my services
      2. When the second node starts, it becomes the slave. This node must join the cluster, but my services should not be started. This Slave node must wait to start the services until it becomes the master

      I am facing these problems:
      1. In the slave node the log correctly shows "MyClusterService - Waiting to acquire Master Status" message but MyClusterService gets started.
      2. In order to control MyClusterService from getting started, I am depending upon
      if ( isMasterNode()) check in startService() method. It only controls my JMS messges from getting published and MyClusterService gets started (which I am able to view in JMX-console)

      Tried to get Sample Applications with similar requirements, but unable to find. Posting this after few days without help
      1. Is using isMasterNode variable the only means to know whether MyClusterService should be started or not, if so, what is the correct design to control the service using the isMasterNode variable
      2. Is there any other means through which I could control the service from not getting started in the slave node, and start them in the master node.

      Is my understanding and approach totally wrong on this? Please help

      I have made the following code to implement this.:

       public class MyClusterService extends HAServiceMBeanSupport {
       private boolean isMasterNode = false;
       protected void startService() throws Exception {
       if (isMasterNode()) {
       log.info("MyClusterService Started Successfully");
       //Send JMS messages to indicate Service Started
       }else {
       log.info("MyClusterService - Waiting to acquire Master Status");
       }
       }
       protected void stopService() {
       log.info("Stopping MyClusterService - If Slave is waiting it will become master ");
       }
       public boolean isMasterNode() {
       return isMasterNode;
       }
       public void startSingleton() {
       isMasterNode = true;
       if (isMasterNode) {
       startService();
       }
       }
       public void stopSingleton() {
       stopService();
       isMasterNode=false;
       }
       }
      

      jboss-service.xml
       <mbean code="org.jboss.ha.singleton.HASingletonController"
       name="my.com:service=MyService-HASingletonController">
      
      <depends>jboss:service=${jboss.partition.name:DefaultPartition}</depends>
       <depends>my.com:service=MyClusterService</depends>
       <attribute
      name="PartitionName">${jboss.partition.name:DefaultPartition}</attribute>
       <attribute
      name="TargetName">my.com:service=MyClusterService</attribute>
       <attribute name="TargetStartMethod">startSingleton</attribute>
       <attribute name="TargetStopMethod">stopSingleton</attribute>
       <!-- <attribute name="TargetStopMethodArgument">true</attribute> -->
       </mbean>
       <mbean code="com.my.cluster.MyClusterService"
       name="my.com:service=MyClusterService">
       <depends>my.com:service=OtherServices</depends>
       </mbean>
      


      Thanks
      Krithi

        • 1. Re: Controlling cluster services
          krithi

          I am still digging for the solution..., Please help

          • 2. Re: Controlling cluster services
            krithi

            Please give me an opportunity to redefine my questions more clearly
            My question is

            1. Is it not true that, the singleton service (MyClusterService) will be started in the current master node only?
            2. Do I need to use if ( isMasterNode() ) check, to avoid the starting the Singleton service in the current slave node? (My worry is, Is this the right way to control the service)
            3. Is there any configuration available to control my singleton services from getting started only in the current master node
            Am I missing any of them


            MyClusterService.java

             public class MyClusterService extends HAServiceMBeanSupport {
             private boolean isMasterNode = false;
             protected void startService() throws Exception {
             if (isMasterNode()) {
             log.info("MyClusterService Started Successfully");
             //Send JMS messages to indicate Service Started
             }else {
             log.info("MyClusterService - Waiting to acquire Master Status");
             }
             }
             protected void stopService() {
             log.info("Stopping MyClusterService - If Slave is waiting it will become master ");
             }
             public boolean isMasterNode() {
             return isMasterNode;
             }
             public void startSingleton() {
             isMasterNode = true;
             if (isMasterNode) {
             startService();
             }
             }
             public void stopSingleton() {
             stopService();
             isMasterNode=false;
             }
             }
            

            jboss-service.xml
             <mbean code="org.jboss.ha.singleton.HASingletonController"
             name="my.com:service=MyService-HASingletonController">
            
            <depends>jboss:service=${jboss.partition.name:DefaultPartition}</depends>
             <depends>my.com:service=MyClusterService</depends>
             <attribute
            name="PartitionName">${jboss.partition.name:DefaultPartition}</attribute>
             <attribute
            name="TargetName">my.com:service=MyClusterService</attribute>
             <attribute name="TargetStartMethod">startSingleton</attribute>
             <attribute name="TargetStopMethod">stopSingleton</attribute>
             <!-- <attribute name="TargetStopMethodArgument">true</attribute> -->
             </mbean>
             <mbean code="com.my.cluster.MyClusterService"
             name="my.com:service=MyClusterService">
             <depends>my.com:service=OtherServices</depends>
             </mbean>
            


            Thanks
            Krithi





            • 3. Re: Controlling cluster services
              brian.stansberry

              Sorry for slow response on this.

              You've subclassed HAServiceMBeanSupport to implement MyClusterService. Do you need to do this in order for MyClusterService to do what it is supposed to? If not. don't subclass that class.

              Do you need to subclass ServiceMBeanSupport (which is a superclass of HAServiceMBeanSupport) in order for MyClusterService to do what it is supposed to? If not, consider not subclassing that class either.

              Next, decide what lifecycle steps you want MyClusterService to go through.

              If you implement any of the methods named create() start() stop() destroy() then JBoss is going to invoke those methods when it deploys MyClusterService. This is not regulated by whether you are the master or not. (This is why you shouldn't subclass ServiceMBeanSupport if you don't need to and don't want to expose create/start/stop/destroy).

              If you do implement create/start/stop/destroy, then your service really has a 6 step lifecycle:

              create -- done at deployment time
              start -- done at deployment time
              startSingleton -- when becomes master
              stopSingleton -- when stops being master
              stop -- done at undeployment time
              destroy -- done at undeployment time

              I very much advise against trying to subclass HAServiceMBeanSupport and then overriding startService to check if you are the master. Mixing together the start and startSingleton lifecycle steps is just asking for trouble. If your service needs to do some startup work when it becomes the master and not before, implement it directly in startSingleton, not in startService.

              • 4. Re: Controlling cluster services
                krithi

                Thanks for your time
                Krithi