Repository: hbase
Updated Branches:
  refs/heads/branch-1 0b081303b -> 303ef340d


HBASE-14293 TestStochasticBalancerJmxMetrics intermittently fails due to port 
conflict


Project: http://git-wip-us.apache.org/repos/asf/hbase/repo
Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/303ef340
Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/303ef340
Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/303ef340

Branch: refs/heads/branch-1
Commit: 303ef340dba6ae3c8715e4dee0fbbe72a7d280e8
Parents: 0b08130
Author: tedyu <[email protected]>
Authored: Mon Aug 24 06:27:22 2015 -0700
Committer: tedyu <[email protected]>
Committed: Mon Aug 24 06:27:22 2015 -0700

----------------------------------------------------------------------
 .../hadoop/hbase/HBaseTestingUtility.java       | 35 ++++++++++++++++++++
 .../hbase/TestStochasticBalancerJmxMetrics.java | 15 +++++++--
 2 files changed, 48 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hbase/blob/303ef340/hbase-server/src/test/java/org/apache/hadoop/hbase/HBaseTestingUtility.java
----------------------------------------------------------------------
diff --git 
a/hbase-server/src/test/java/org/apache/hadoop/hbase/HBaseTestingUtility.java 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/HBaseTestingUtility.java
index b3e80fd..c59b62b 100644
--- 
a/hbase-server/src/test/java/org/apache/hadoop/hbase/HBaseTestingUtility.java
+++ 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/HBaseTestingUtility.java
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
+import java.net.DatagramSocket;
 import java.net.InetAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
@@ -221,6 +222,40 @@ public class HBaseTestingUtility extends 
HBaseCommonTestingUtility {
     };
 
   /**
+   * Checks to see if a specific port is available.
+   *
+   * @param port the port number to check for availability
+   * @return <tt>true</tt> if the port is available, or <tt>false</tt> if not
+   */
+  public static boolean available(int port) {
+    ServerSocket ss = null;
+    DatagramSocket ds = null;
+    try {
+      ss = new ServerSocket(port);
+      ss.setReuseAddress(true);
+      ds = new DatagramSocket(port);
+      ds.setReuseAddress(true);
+      return true;
+    } catch (IOException e) {
+      // Do nothing
+    } finally {
+      if (ds != null) {
+        ds.close();
+      }
+
+      if (ss != null) {
+        try {
+          ss.close();
+        } catch (IOException e) {
+          /* should not be thrown */
+        }
+      }
+    }
+
+    return false;
+  }
+
+  /**
    * Create all combinations of Bloom filters and compression algorithms for
    * testing.
    */

http://git-wip-us.apache.org/repos/asf/hbase/blob/303ef340/hbase-server/src/test/java/org/apache/hadoop/hbase/TestStochasticBalancerJmxMetrics.java
----------------------------------------------------------------------
diff --git 
a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestStochasticBalancerJmxMetrics.java
 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestStochasticBalancerJmxMetrics.java
index 828da99..26b3d07 100644
--- 
a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestStochasticBalancerJmxMetrics.java
+++ 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestStochasticBalancerJmxMetrics.java
@@ -24,6 +24,7 @@ import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
+import java.util.Random;
 import java.util.Set;
 
 import javax.management.MBeanAttributeInfo;
@@ -82,15 +83,25 @@ public class TestStochasticBalancerJmxMetrics extends 
BalancerTestBase {
     conf.setFloat("hbase.regions.slop", 0.0f);
     conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY, 
JMXListener.class.getName());
 
-    for (int i = 0; i < 5; i++) {
+    Random rand = new Random();
+    for (int i = 0; i < 10; i++) {
+      do {
+        int sign = i % 2 == 0 ? 1 : -1;
+        connectorPort += sign * rand.nextInt(100);
+      } while (!HBaseTestingUtility.available(connectorPort));
       try {
         conf.setInt("regionserver.rmi.registry.port", connectorPort);
         UTIL.startMiniCluster();
         break;
       } catch (Exception e) {
-        connectorPort++;
         LOG.debug("Encountered exception when starting mini cluster. Trying 
port " + connectorPort,
           e);
+        try {
+          // this is to avoid "IllegalStateException: A mini-cluster is 
already running"
+          UTIL.shutdownMiniCluster();
+        } catch (Exception ex) {
+          LOG.debug("Encountered exception shutting down cluster", ex);
+        }
       }
     }
   }

Reply via email to