[jboss-cvs] JBossAS SVN: r69598 - trunk/cluster/src/main/org/jboss/ha/singleton.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Feb 4 23:03:23 EST 2008


Author: bstansberry at jboss.com
Date: 2008-02-04 23:03:23 -0500 (Mon, 04 Feb 2008)
New Revision: 69598

Modified:
   trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicyBase.java
Log:
[JBAS-5161] Check for situation where preferred master uses a hostname and the ClusterNode uses dotted decimal

Modified: trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicyBase.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicyBase.java	2008-02-05 02:53:08 UTC (rev 69597)
+++ trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicyBase.java	2008-02-05 04:03:23 UTC (rev 69598)
@@ -21,11 +21,14 @@
  */
 package org.jboss.ha.singleton;
 
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.util.List;
 
 import org.jboss.ha.framework.interfaces.ClusterNode;
 import org.jboss.ha.framework.interfaces.HAPartition;
 import org.jboss.system.ServiceMBeanSupport;
+import org.jboss.system.server.ServerConfigUtil;
 
 /**
  * A base class for policy service that decides which node in the cluster should be 
@@ -105,13 +108,62 @@
       
       // If preferred master is defined and contained in cluster, return it
       if (null != this.mPreferredMaster) {
+       
+         log.debug("Checking if " + mPreferredMaster + " is in candidate list " + candidates);
+         
+         // First just match on names
          for (ClusterNode node : candidates)
          {
             if (node.getName().equals(this.mPreferredMaster))
             {
                return node;
-            }                 
+            }
          }
+         
+         // No match. Check for situation where preferred master uses a hostname
+         // and the ClusterNode uses dotted decimal (or vice versa)
+         
+         // See if we can parse out an InetAddress and port from mPreferredMaster.
+         InetAddress addr = null;
+         int port = -1;
+         // Look for ':' in the middle of the string
+         int idx = mPreferredMaster.indexOf(':');
+         if (idx > 0 && idx < mPreferredMaster.length() -1)
+         {
+            String portString = mPreferredMaster.substring(idx +1);
+            String hostName = mPreferredMaster.substring(0, idx);
+            try
+            {
+               port = Integer.parseInt(portString);
+               addr = InetAddress.getByName(hostName);
+               log.debug("Parsed " + mPreferredMaster + " into " + addr + " and " + port);
+            }
+            catch (NumberFormatException nfe)
+            {
+               log.debug(mPreferredMaster + " does not end in an int; cannot parse out a port", nfe);
+            }
+            catch (UnknownHostException uhe)
+            {
+               log.debug("Cannot extract InetAddress from " + mPreferredMaster, uhe);
+            }
+         } 
+         
+         if (addr != null)
+         {
+            String rewritten = addr.getHostAddress() + ":" + port;
+            if (mPreferredMaster.equals(rewritten))
+            {
+               rewritten = addr.getHostName() + ":" + port;
+            }
+            
+            for (ClusterNode node : candidates)
+            {
+               if (node.getName().equals(rewritten))
+               {
+                  return node;
+               }
+            }
+         }
       }
        
       // Preferred master is not available, election policy is in effect
@@ -137,4 +189,44 @@
     * @return {@link ClusterNode} instance.
     */
    protected abstract ClusterNode elect(List<ClusterNode> candidates);
+   
+   private AddressPort parseAddressPort(String nodeName)
+   {
+      InetAddress addr = null;
+      int port = -1;
+      // Look for ':' in the middle of the string
+      int idx = nodeName.indexOf(':');
+      if (idx > 0 && idx < nodeName.length() -1)
+      {
+         String portString = nodeName.substring(idx +1);
+         String hostName = nodeName.substring(0, idx);
+         try
+         {
+            port = Integer.parseInt(portString);
+            addr = InetAddress.getByName(hostName);
+            return new AddressPort(addr, port);
+         }
+         catch (NumberFormatException nfe)
+         {
+            log.debug(nodeName + " does not end in an int; cannot parse out a port", nfe);
+         }
+         catch (UnknownHostException uhe)
+         {
+            log.debug("Cannot extract InetAddress from " + nodeName, uhe);
+         }
+      } 
+      return null;
+   }
+   
+   private class AddressPort
+   {
+      private InetAddress address;
+      private int port;
+      
+      private AddressPort(InetAddress address, int port)
+      {
+         this.address = address;
+         this.port = port;
+      }
+   }
 }




More information about the jboss-cvs-commits mailing list