[jboss-cvs] JBossAS SVN: r83171 - in branches/Branch_5_x: testsuite/imports/config and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jan 21 16:00:46 EST 2009


Author: pferraro
Date: 2009-01-21 16:00:46 -0500 (Wed, 21 Jan 2009)
New Revision: 83171

Added:
   branches/Branch_5_x/testsuite/src/main/org/jboss/test/cluster/hasingleton/PreferredMasterElectionPolicyUnitTestCase.java
Modified:
   branches/Branch_5_x/cluster/src/main/org/jboss/ha/singleton/PreferredMasterElectionPolicy.java
   branches/Branch_5_x/testsuite/imports/config/tests-clustering.xml
Log:
[JBAS=6375] PreferredMasterElectionPolicy unnecessarily parses host/port every election

Modified: branches/Branch_5_x/cluster/src/main/org/jboss/ha/singleton/PreferredMasterElectionPolicy.java
===================================================================
--- branches/Branch_5_x/cluster/src/main/org/jboss/ha/singleton/PreferredMasterElectionPolicy.java	2009-01-21 20:38:28 UTC (rev 83170)
+++ branches/Branch_5_x/cluster/src/main/org/jboss/ha/singleton/PreferredMasterElectionPolicy.java	2009-01-21 21:00:46 UTC (rev 83171)
@@ -22,6 +22,7 @@
 package org.jboss.ha.singleton;
 
 import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.UnknownHostException;
@@ -44,18 +45,47 @@
    extends HASingletonElectionPolicySimple 
    implements PreferredMasterElectionPolicyMBean
 {
-   protected Logger log = Logger.getLogger(this.getClass());
+   private Logger log = Logger.getLogger(this.getClass());
    
-   private String preferredMaster;
+   private volatile InetSocketAddress preferredMaster;
    
    // -------------------------------------------------------------  Properties
    
    /**
     * @see PreferredMasterElectionPolicyMBean#setPreferredMaster(String)
     */
-   public void setPreferredMaster(String node)
+   public void setPreferredMaster(String value)
    {
-      this.preferredMaster = node;
+      String node = (value != null) ? value.trim() : "";
+      
+      if (node.length() > 0)
+      {
+         try
+         {
+            URI uri = new URI("cluster://" + node);
+            
+            String host = uri.getHost();
+            
+            if (host == null)
+            {
+               throw new IllegalArgumentException("Cannot extract host/address from " + node);
+            }
+            
+            this.preferredMaster = new InetSocketAddress(InetAddress.getByName(host), uri.getPort());
+         }
+         catch (URISyntaxException e)
+         {
+            throw new IllegalArgumentException("Cannot extract URI from " + node, e);
+         }
+         catch (UnknownHostException e)
+         {
+            throw new IllegalArgumentException("Cannot resolve host from " + node, e);
+         }
+      }
+      else
+      {
+         this.preferredMaster = null;
+      }
    }
    
    /**
@@ -63,7 +93,9 @@
     */
    public String getPreferredMaster()
    {
-      return this.preferredMaster;
+      InetSocketAddress address = this.preferredMaster;
+      
+      return (address != null) ? address.toString() : null;
    }
 
    // -----------------------------------------------------  HASingletonElector
@@ -71,102 +103,41 @@
    @Override
    public ClusterNode elect(List<ClusterNode> candidates)
    {
+      InetSocketAddress sockAddress = this.preferredMaster;
+      
+      ClusterNode master = null;
+      
       // If preferred master is defined and contained in cluster, return it
-      if (this.preferredMaster != null) 
+      if (sockAddress != null) 
       {
-         this.log.debug("Checking if " + this.preferredMaster + " is in candidate list " + candidates);
+         InetAddress address = sockAddress.getAddress();
+         int port = sockAddress.getPort();
          
-         // First just match on names
-         for (ClusterNode node : candidates)
-         {
-            if (node.getName().equals(this.preferredMaster))
-            {
-               return node;
-            }
-         }
+         // First find by address
+         master = this.find(candidates, address.getHostAddress(), port);
          
-         // 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(this.preferredMaster);
-         // See if we can parse out an InetAddress and port from preferredMaster.
-         InetAddress addr = parseInetAddress(uri);
-                 
-         if (addr != null)
+         if (master == null)
          {
-            int port = uri.getPort();
-            String rewritten = addr.getHostAddress() + ":" + port;
-            if (this.preferredMaster.equals(rewritten))
-            {
-               rewritten = addr.getHostName() + ":" + port;
-            }
-            
-            for (ClusterNode node : candidates)
-            {
-               if (node.getName().equals(rewritten))
-               {
-                  return node;
-               }
-            }
+            // Then try by hostname
+            master = this.find(candidates, address.getHostName(), port);
          }
       }
       
-      return super.elect(candidates);
+      return (master != null) ? master : super.elect(candidates);
    }
    
-   // --------------------------------------------------------------- Protected
-   
-   /**
-    * 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)
+   private ClusterNode find(List<ClusterNode> candidates, String host, int port)
    {
-      try
-      {
-         return new URI("cluster://" + this.preferredMaster);
-      }
-      catch (URISyntaxException use)
-      {
-         this.log.debug("Cannot extract URI from " + this.preferredMaster, use);
-      }
+      String node = host + ":" + port;
       
-      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)
+      this.log.debug("Checking if " + node + " is in candidate list: " + candidates);
+      
+      for (ClusterNode candidate: candidates)
       {
-         // 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)
+         if (candidate.getName().equals(node))
          {
-            try
-            {
-               InetAddress addr = InetAddress.getByName(host);
-               this.log.debug("Parsed " + this.preferredMaster + " into " + addr + " and " + port);
-               return addr;
-            }
-            catch (UnknownHostException uhe)
-            {
-               this.log.debug("Cannot extract InetAddress from " + this.preferredMaster, uhe);
-            }
-         }             
+            return candidate;
+         }
       }
       
       return null;

Modified: branches/Branch_5_x/testsuite/imports/config/tests-clustering.xml
===================================================================
--- branches/Branch_5_x/testsuite/imports/config/tests-clustering.xml	2009-01-21 20:38:28 UTC (rev 83170)
+++ branches/Branch_5_x/testsuite/imports/config/tests-clustering.xml	2009-01-21 21:00:46 UTC (rev 83171)
@@ -64,7 +64,17 @@
     <include name="org/jboss/test/cluster/defaultcfg/ejb2/ustxsticky/test/UserTransactionStickyUnitTestCase.class"/>
   </patternset>    
   <patternset id="cluster.excludes">
-    <exclude name="org/jboss/test/cluster/**/*TestCase.class"/>
+    <exclude name="org/jboss/test/cluster/apache_tomcat/**/*TestCase.class"/>
+    <exclude name="org/jboss/test/cluster/cache/**/*TestCase.class"/>
+    <exclude name="org/jboss/test/cluster/classloader/**/*TestCase.class"/>
+    <exclude name="org/jboss/test/cluster/defaultcfg/**/*TestCase.class"/>
+    <exclude name="org/jboss/test/cluster/ejb2/**/*TestCase.class"/>
+    <exclude name="org/jboss/test/cluster/hapartition/**/*TestCase.class"/>
+    <exclude name="org/jboss/test/cluster/haservice/**/*TestCase.class"/>
+    <exclude name="org/jboss/test/cluster/httpsessionreplication/**/*TestCase.class"/>
+    <exclude name="org/jboss/test/cluster/invokerha/**/*TestCase.class"/>
+    <exclude name="org/jboss/test/cluster/multicfg/**/*TestCase.class"/>
+    <exclude name="org/jboss/test/cluster/web/**/*TestCase.class"/>
   </patternset>
 
   <!--
@@ -696,5 +706,4 @@
    <server:stop name="cluster-field-${jboss-junit-configuration}-BR-1"/>
 	   
   </target>
-
 </project>
\ No newline at end of file

Added: branches/Branch_5_x/testsuite/src/main/org/jboss/test/cluster/hasingleton/PreferredMasterElectionPolicyUnitTestCase.java
===================================================================
--- branches/Branch_5_x/testsuite/src/main/org/jboss/test/cluster/hasingleton/PreferredMasterElectionPolicyUnitTestCase.java	                        (rev 0)
+++ branches/Branch_5_x/testsuite/src/main/org/jboss/test/cluster/hasingleton/PreferredMasterElectionPolicyUnitTestCase.java	2009-01-21 21:00:46 UTC (rev 83171)
@@ -0,0 +1,93 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, 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.test.cluster.hasingleton;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.jboss.ha.framework.interfaces.ClusterNode;
+import org.jboss.ha.framework.server.ClusterNodeImpl;
+import org.jboss.ha.singleton.PreferredMasterElectionPolicy;
+import org.jgroups.stack.IpAddress;
+
+/**
+ * A PreferredMasterElectionPolicyTestCase.
+ * 
+ * @author Paul Ferraro
+ */
+public class PreferredMasterElectionPolicyUnitTestCase extends TestCase
+{
+   private List<ClusterNode> candidates = new ArrayList<ClusterNode>();
+   
+   public void setUp() throws UnknownHostException
+   {
+      InetAddress localAddress = InetAddress.getLocalHost();
+      
+      this.candidates.add(new ClusterNodeImpl(new IpAddress(localAddress, 10000)));
+      this.candidates.add(new ClusterNodeImpl(new IpAddress(localAddress, 10001)));
+      this.candidates.add(new ClusterNodeImpl(new IpAddress(localAddress, 10002)));
+   }
+   
+   public void testUsePreferredMasterViaHost()
+   {
+      PreferredMasterElectionPolicy policy = new PreferredMasterElectionPolicy();
+      policy.setPreferredMaster("localhost:10001");
+      
+      ClusterNode master = policy.elect(this.candidates);
+      
+      Assert.assertSame(this.candidates.get(1), master);
+   }
+   
+   public void testUsePreferredMasterViaAddress()
+   {
+      PreferredMasterElectionPolicy policy = new PreferredMasterElectionPolicy();
+      policy.setPreferredMaster("127.0.0.1:10002");
+      
+      ClusterNode master = policy.elect(this.candidates);
+      
+      Assert.assertSame(this.candidates.get(2), master);
+   }
+   
+   public void testUseDefaultMaster()
+   {
+      PreferredMasterElectionPolicy policy = new PreferredMasterElectionPolicy();
+      policy.setPreferredMaster("localhost:10003");
+      
+      ClusterNode master = policy.elect(this.candidates);
+      
+      Assert.assertSame(this.candidates.get(0), master);
+   }
+   
+   public void testUseDefaultMasterNoPreference()
+   {
+      PreferredMasterElectionPolicy policy = new PreferredMasterElectionPolicy();
+      
+      ClusterNode master = policy.elect(this.candidates);
+      
+      Assert.assertSame(this.candidates.get(0), master);
+   }
+}




More information about the jboss-cvs-commits mailing list