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 a3a1e61..d8d9522 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
@@ -23,6 +23,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;
@@ -223,6 +224,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 true if the port is available, or false 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.
*/
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 c8cb665..dbf666e 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,24 @@ public class TestStochasticBalancerJmxMetrics extends BalancerTestBase {
conf.setFloat("hbase.master.balancer.stochastic.maxMovePercent", 0.75f);
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 {
+ connectorPort += 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 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);
+ }
}
}
}