9 Replies Latest reply on Jan 11, 2016 5:24 PM by vamshi appala

    [wildfly-10/clustering] How to find the master in wildfly cluster?

    Anoop Krishnakumar Newbie

      I am trying to use wildfly clustering capability in my application. There are requirements in the application where it needs to know which node is the master and listen to election events.

      1. Is there any implementation where the application code can register to listen for events?
      2. Is there an API that can be queried to find which node is the master?
        • 1. Re: [wildfly-10/clustering] How to find the master in wildfly cluster?
          Jay SenSharma Master

          WildFly provides a refined public clustering API for use by applications, which might be helpful for you in this case.

           

          There is a method "boolean isCoordinator();"  and "Node getCoordinatorNode();" which might be of your interest: https://github.com/wildfly/wildfly/blob/master/clustering/api/src/main/java/org/wildfly/clustering/group/Group.java

           

              /**
               * Indicates whether or not we are the group coordinator.
               *
               * @return true, if we are the group coordinator, false otherwise
               */
              boolean isCoordinator();
          
              /**
               * Returns the group coordinator node.
               *
               * @return the group coordinator node
               */
              Node getCoordinatorNode();
          
          

           

          org.wildfly.clustering.group.Group

          https://github.com/wildfly/wildfly/tree/master/clustering/api/src/main/java/org/wildfly/clustering/group

           

          The Group service provides a mechanism to view the cluster topology for a channel or cache, and to be notified when the topology changes.  A cache group, defined as the set of nodes in a cluster on which a given cache is deployed, is always the same as, or a subset of, the corresponding channel group.

          e.g.

           

          @Resource(lookup = "java:jboss/clustering/group/channel-name")
          private Group channelGroup;
          
          @Resource(lookup = "java:jboss/clustering/group/cache-container-name/cache-name")
          private Group cacheGroup;
          
          
          

           

          See:   https://developer.jboss.org/wiki/ClusteringChangesInWildFly8?_sscc=t

          • 2. Re: [wildfly-10/clustering] How to find the master in wildfly cluster?
            Anoop Krishnakumar Newbie

            Thanks Jay.

             

            I am using the timer service example in quickstart to learn and understand clustering. I implemented a listener that listens to channel and cache events. The listener receives a callback when the cluster group view changes but before the election. Is it possible to receive a callback after the election so that "channelGroup.getCoordinatorNode().getName()" will return the real master?

             

            Code snippets and logs

            Defined a custom singleton election policy where a particular node will always be the master. In this case "node2"

             

            
            public class MySingletonElectionPolicy implements SingletonElectionPolicy {
            
                @Override
                public Node elect(List<Node> nodes) {
                    System.out.println("=========> Inside election policy.....");
                    Node master = null;
                    if (nodes != null && nodes.size() > 1) {
                        master = nodes.stream().filter(n -> n.getName().equals("node2")).findFirst().get();
                    } else if (nodes != null) {
                        master = nodes.get(0);
                    }
                    System.out
                            .println(String.format("=========> Inside election policy - %s is the master.....", master.getName()));
                    return master;
                }
            
            }
            
            

             

            Defined a listener to receive callbacks on cluster events. Listener implementation is as follows,

            
            @Singleton
            @Startup
            public class ClusterListener implements Listener {
            
                @Resource(lookup = "java:jboss/clustering/group/ejb")
                private Group channelGroup;
            
                @Resource(lookup = "java:jboss/clustering/group/server")
                private Group cacheGroup;
            
                @PostConstruct
                public void createrListener() {
                    channelGroup.addListener(this);
                    cacheGroup.addListener(this);
                    System.out.println("====> Registered topology listener...");
                }
            
                @Override
                public void membershipChanged(List<Node> previousMembers, List<Node> members, boolean merged) {
                    System.out.println("======> Membership changed...");
                    System.out.println("======> Channel Group : Is Co-ordinator : " + channelGroup.isCoordinator());
                    System.out.println("======> Channel Group : Co-ordinator Node : " + channelGroup.getCoordinatorNode().getName());
            
                    System.out.println("======> Cache Group : Is Co-ordinator : " + cacheGroup.isCoordinator());
                    System.out.println("======> Cache Group : Co-ordinator Node : " + cacheGroup.getCoordinatorNode().getName());
                    
                }
            
            
            
            
            
            
            }
            

             

            First "node1" is started and it is elected as the master since that is the only node.

             

            "node1" logs : {{

            14:54:41,666 INFO  [stdout] (notification-thread--p17-t1) =========> Inside election policy.....

            14:54:41,666 INFO  [stdout] (notification-thread--p17-t1) =========> Inside election policy - node1 is the master.....

            14:54:41,666 INFO  [org.wildfly.clustering.server] (notification-thread--p17-t1) WFLYCLSV0003: node1 elected as the singleton provider of the jboss.quickstart.ha.singleton.timer service

            14:54:41,669 INFO  [class me.learn.singleton.HATimerService] (MSC service thread 1-1) Start HASingleton timer service 'me.learn.singleton.HATimerService'

            14:54:41,954 INFO  [stdout] (ServerService Thread Pool -- 71) ====> Registered topology listener...

            }}

             

            Then "node2" is started and that triggers an election and "node2" is elected as the master. The listener receives a callback before the election.

            "node1" logs : {{

            14:55:00,987 INFO  [org.infinispan.remoting.transport.jgroups.JGroupsTransport] (Incoming-2,ee,node1) ISPN000094: Received new cluster view for channel server: [node1|1] (2) [node1, node2]

            14:55:00,993 INFO  [stdout] (Incoming-2,ee,node1) ======> Membership changed...

            14:55:00,995 INFO  [stdout] (Incoming-2,ee,node1) ======> Channel Group : Is Co-ordinator : true

            14:55:00,996 INFO  [stdout] (Incoming-2,ee,node1) ======> Channel Group : Co-ordinator Node : node1

            14:55:00,996 INFO  [stdout] (Incoming-2,ee,node1) ======> Cache Group : Is Co-ordinator : true

            14:55:00,996 INFO  [stdout] (Incoming-2,ee,node1) ======> Cache Group : Co-ordinator Node : node1

            14:55:00,996 INFO  [stdout] (Incoming-2,ee,node1) ======> Membership changed...

            14:55:00,996 INFO  [stdout] (Incoming-2,ee,node1) ======> Channel Group : Is Co-ordinator : true

            14:55:00,997 INFO  [stdout] (Incoming-2,ee,node1) ======> Channel Group : Co-ordinator Node : node1

            14:55:00,997 INFO  [stdout] (Incoming-2,ee,node1) ======> Cache Group : Is Co-ordinator : true

            14:55:00,997 INFO  [stdout] (Incoming-2,ee,node1) ======> Cache Group : Co-ordinator Node : node1

            14:55:02,273 INFO  [org.infinispan.CLUSTER] (remote-thread--p18-t1) ISPN000310: Starting cluster-wide rebalance for cache default, topology CacheTopology{id=1, rebalanceId=1, currentCH=ReplicatedConsistentHash{ns = 60, owners = (1)[node1: 60]}, pendingCH=ReplicatedConsistentHash{ns = 60, owners = (2)[node1: 30, node2: 30]}, unionCH=null, actualMembers=[node1, node2]}

            14:55:02,495 INFO  [org.infinispan.CLUSTER] (remote-thread--p18-t1) ISPN000336: Finished cluster-wide rebalance for cache default, topology id = 1

            14:55:02,565 INFO  [stdout] (notification-thread--p17-t1) =========> Inside election policy.....

            14:55:02,567 INFO  [stdout] (notification-thread--p17-t1) =========> Inside election policy - node2 is the master.....

            14:55:02,568 INFO  [org.wildfly.clustering.server] (notification-thread--p17-t1) WFLYCLSV0003: node2 elected as the singleton provider of the jboss.quickstart.ha.singleton.timer service

            }}

            • 3. Re: [wildfly-10/clustering] How to find the master in wildfly cluster?
              tibu Newbie

              Hello,

               

              is it possible to modify Jays solution in such a way, that the bean can be deployed in a standalone configuration server as well? We have something similar, but the @Resource injection will obviously fail with an UnsatisfiyedDependencyExcpetion on standalone, because the clustering subsystem(s) is(are) not available at all.

              We tried to work around this by using JNDI Lookup to obtain the group, if is is available. The lookup is not successful, even in a cluster environment, because the group is not bound to JNDI until @Resource is used, which not only obtains the instance but also binds it in the context.

              Is it maybe possible to programmatically access the service which would normally be used to resolve the @Resource injection? Or is it somehow possible to have an optional @Resource, that may be null in the case of standalone?

               

              Best regards

              Tibu

              • 4. Re: [wildfly-10/clustering] How to find the master in wildfly cluster?
                Paul Ferraro Master

                When running in a non-clustered server profile, @Resource("java:jboss/clustering/group/default") will resolve to a local implementation of org.wildfly.clustering.group.Group.  The same is true of every clustering abstraction.  In this way, your application can use the clustering API with consistent behavior regardless of the server profile used.

                • 5. Re: [wildfly-10/clustering] How to find the master in wildfly cluster?
                  vamshi appala Newbie

                  Is it possible to dynamically lookup channel group in EJB,

                   

                  channelGroup = (org.wildfly.clustering.group.Group)ctx.lookup("java:jboss/clustering/group/ejb");

                  I am getting exception when I try to do context lookup, but work when I use @Resource. In our use case we want to do it programatically? can we lookup using JNDI?

                  • 6. Re: [wildfly-10/clustering] How to find the master in wildfly cluster?
                    Paul Ferraro Master

                    Yes - by setting up a resource-ref in your deployment descriptor.

                    e.g.

                    <resource-ref>
                      <res-ref-name>group/ejb</res-ref-name>
                     <lookup-name>java:jboss/clustering/group/ejb</lookup-name>
                    </resource-ref>
                    

                     

                    You can now lookup the object in JNDI via the referenced name within your component's namespace.

                    e.g.

                    Group group = (Group) new InitialContext().lookup("java:comp/env/group/ejb");
                    
                    • 7. Re: [wildfly-10/clustering] How to find the master in wildfly cluster?
                      vamshi appala Newbie

                      Hi Paul,

                      How can we define the config in ejb-jar.xml, I don't see lookup-name in ejb-jar.xml

                      • 9. Re: [wildfly-10/clustering] How to find the master in wildfly cluster?
                        vamshi appala Newbie

                        HI Paul,

                        Thanks for the info, your suggestion worked for me.