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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Feb 11 13:49:03 EST 2008


Author: galder.zamarreno at jboss.com
Date: 2008-02-11 13:49:03 -0500 (Mon, 11 Feb 2008)
New Revision: 69774

Added:
   trunk/cluster/src/main/org/jboss/ha/singleton/PreferredMasterElectionPolicy.java
   trunk/cluster/src/main/org/jboss/ha/singleton/PreferredMasterElectionPolicyMBean.java
Modified:
   trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicy.java
   trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicyBase.java
   trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicySimple.java
   trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonSupport.java
Log:
[JBAS-5161] Preferred master election policy is now independent together with an MBean interface. It now uses URI to parse the given preferred master. Base election policy does not throw IllegalStateException when list of candidate cluster nodes is null and instead returns null (HASingletonElectionPolicy javadoc has been updated) after logging a debug message. This gets around a AS shutdown warn message.

Modified: trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicy.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicy.java	2008-02-11 18:44:34 UTC (rev 69773)
+++ trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicy.java	2008-02-11 18:49:03 UTC (rev 69774)
@@ -43,25 +43,10 @@
     * @return the HA partition.
     */
    HAPartition getHAPartition();
-
-   /**
-    * Sets the preferred master node. As long as the preferred master node
-    * presents in the cluster, it will be always selected as master node,
-    * no matter what the election policy is.
-    * @param node String format of ip_address:port_number
-    */
-   void setPreferredMaster(String node);
    
    /**
-    * Get the preferred master node.
-    * 
-    * @return preferred master node in ip_address:port_number format.
-    */
-   String getPreferredMaster();
-   
-   /**
     * Return the elected master node.
-    * @return the master node
+    * @return the master node or null if there're no possible master nodes.
     */
    ClusterNode elect();
 }
\ No newline at end of file

Modified: trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicyBase.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicyBase.java	2008-02-11 18:44:34 UTC (rev 69773)
+++ trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicyBase.java	2008-02-11 18:49:03 UTC (rev 69774)
@@ -21,14 +21,11 @@
  */
 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 
@@ -103,70 +100,10 @@
       List<ClusterNode> candidates = getCandidates();
       if (candidates == null)
       {
-         throw new IllegalStateException("list of cluster node candidates where to run the singleton cannot be null");
+         log.debug("list of cluster node candidates where to run the singleton is null");
+         return null;
       }
-      
-      // 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
+             
       return elect(candidates);    
    }
    
@@ -189,44 +126,4 @@
     * @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;
-      }
-   }
 }

Modified: trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicySimple.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicySimple.java	2008-02-11 18:44:34 UTC (rev 69773)
+++ trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonElectionPolicySimple.java	2008-02-11 18:49:03 UTC (rev 69774)
@@ -46,6 +46,10 @@
  * the current partition:
  * <attribute name="Position">2</attribute>
  * 
+ * If no election policy is defined, the oldest node in the cluster runs the 
+ * singleton. This behaivour can be achieved with this policy when "position" 
+ * is set to 0. 
+ * 
  * @author <a href="mailto:Alex.Fu at novell.com">Alex Fu</a>
  * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
  * @version $Revision$

Modified: trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonSupport.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonSupport.java	2008-02-11 18:44:34 UTC (rev 69773)
+++ trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonSupport.java	2008-02-11 18:49:03 UTC (rev 69774)
@@ -25,6 +25,7 @@
 
 import javax.management.Notification;
 
+import org.jboss.ha.framework.interfaces.ClusterNode;
 import org.jboss.ha.jmx.HAServiceMBeanSupport;
 
 /** 
@@ -166,9 +167,14 @@
    {
       boolean isElectedNewMaster;
       if (mElectionPolicyMB != null)
-         isElectedNewMaster = mElectionPolicyMB.elect().equals(getHAPartition().getClusterNode());
+      {
+         ClusterNode electedNode = mElectionPolicyMB.elect();
+         isElectedNewMaster = electedNode != null ? electedNode.equals(getHAPartition().getClusterNode()) : false;
+      }
       else
+      {
          isElectedNewMaster = isDRMMasterReplica();
+      }
       
       log.debug("partitionTopologyChanged, isElectedNewMaster=" + isElectedNewMaster
             + ", isMasterNode=" + isMasterNode + ", viewID=" + newViewID);

Added: trunk/cluster/src/main/org/jboss/ha/singleton/PreferredMasterElectionPolicy.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/singleton/PreferredMasterElectionPolicy.java	                        (rev 0)
+++ trunk/cluster/src/main/org/jboss/ha/singleton/PreferredMasterElectionPolicy.java	2008-02-11 18:49:03 UTC (rev 69774)
@@ -0,0 +1,163 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ha.singleton;
+
+import java.net.InetAddress;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.UnknownHostException;
+import java.util.List;
+
+import org.jboss.ha.framework.interfaces.ClusterNode;
+
+/**
+ * Election policy that chooses the node where the singleton should run based on 
+ * the given preferred master node in ip_address:port_number or 
+ * host_name:port_number format. If the preferred master is null, or its 
+ * ip_address does not resolve to a valid host name, or the port number is 
+ * invalid, it delegates to the standard policy.  
+ * 
+ * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
+ */
+public class PreferredMasterElectionPolicy extends HASingletonElectionPolicySimple 
+   implements PreferredMasterElectionPolicyMBean
+{
+   private String preferredMaster;
+   
+   /**
+    * @see PreferredMasterElectionPolicyMBean#setPreferredMaster(String)
+    */
+   public void setPreferredMaster(String node)
+   {
+      preferredMaster = node;
+   }
+   
+   /**
+    * @see PreferredMasterElectionPolicyMBean#getPreferredMaster()
+    */
+   public String getPreferredMaster()
+   {
+      return preferredMaster;
+   }
+
+   @Override
+   protected ClusterNode elect(List<ClusterNode> candidates)
+   {
+      // If preferred master is defined and contained in cluster, return it
+      if (preferredMaster != null) 
+      {
+         log.debug("Checking if " + preferredMaster + " is in candidate list " + candidates);
+         
+         // First just match on names
+         for (ClusterNode node : candidates)
+         {
+            if (node.getName().equals(preferredMaster))
+            {
+               return node;
+            }
+         }
+         
+         // No match. Check for situation where preferred master uses a hostname
+         // and the ClusterNode uses dotted decimal (or vice versa)
+         
+         // Create a URI out of the preferred master and try retrieving host 
+         // and port.
+         URI uri = createUri(preferredMaster);
+         // See if we can parse out an InetAddress and port from preferredMaster.
+         InetAddress addr = parseInetAddress(uri);
+                 
+         if (addr != null)
+         {
+            int port = uri.getPort();
+            String rewritten = addr.getHostAddress() + ":" + port;
+            if (preferredMaster.equals(rewritten))
+            {
+               rewritten = addr.getHostName() + ":" + port;
+            }
+            
+            for (ClusterNode node : candidates)
+            {
+               if (node.getName().equals(rewritten))
+               {
+                  return node;
+               }
+            }
+         }
+      }
+      
+      return super.elect(candidates);
+   }
+   
+   /**
+    * Create a URI instance from the given preferred master. 
+    * 
+    * @param str contains the String format of the preferred master.
+    * @return an instance of URI, or null if the str cannot be parsed.
+    */
+   protected URI createUri(String str)
+   {
+      try
+      {
+         return new URI("cluster://" + preferredMaster);
+      }
+      catch (URISyntaxException use)
+      {
+         log.debug("Cannot extract URI from " + preferredMaster, use);
+      }
+      
+      return null;
+   }
+   
+   /**
+    * Parse an URI into an InetAddress
+    * 
+    * @param uri URI representation of the address. It can be null.
+    * @return InetAddress representation or null if the host in the URI is 
+    * unknown or given URI is null. 
+    */
+   protected InetAddress parseInetAddress(URI uri)
+   {
+      if (uri != null)
+      {
+         // Returns either the host, or null if host undefined
+         String host = uri.getHost();
+         // Returns either the port, or -1 if port undefined or not numeric
+         int port = uri.getPort();         
+         
+         if (host != null && port != -1)
+         {
+            try
+            {
+               InetAddress addr = InetAddress.getByName(host);
+               log.debug("Parsed " + preferredMaster + " into " + addr + " and " + port);
+               return addr;
+            }
+            catch (UnknownHostException uhe)
+            {
+               log.debug("Cannot extract InetAddress from " + preferredMaster, uhe);
+            }
+         }             
+      }
+      
+      return null;
+   }
+}
\ No newline at end of file

Added: trunk/cluster/src/main/org/jboss/ha/singleton/PreferredMasterElectionPolicyMBean.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/singleton/PreferredMasterElectionPolicyMBean.java	                        (rev 0)
+++ trunk/cluster/src/main/org/jboss/ha/singleton/PreferredMasterElectionPolicyMBean.java	2008-02-11 18:49:03 UTC (rev 69774)
@@ -0,0 +1,48 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ha.singleton;
+
+/**
+ * MBean interface for the preferred master election policy that allows 
+ * preferred master to be changed at runtime. 
+ * 
+ * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
+ */
+public interface PreferredMasterElectionPolicyMBean extends HASingletonElectionPolicySimpleMBean
+{
+   /**
+    * Sets the preferred master node. As long as the preferred master node
+    * presents in the cluster, it will be always selected as master node,
+    * no matter what the election policy is.
+    * @param node String format of ip_address:port_number or 
+    * host_name:port_number.
+    */
+   void setPreferredMaster(String node);
+   
+   /**
+    * Get the preferred master node.
+    * 
+    * @return preferred master node in ip_address:port_number or 
+    * host_name:port_number format.
+    */
+   String getPreferredMaster();
+}




More information about the jboss-cvs-commits mailing list