ACCUMULO-1365 move getRandomFreePort() out of MAC API git-svn-id: https://svn.apache.org/repos/asf/accumulo/branches/1.4@1478018 13f79535-47bb-0310-9956-ffa450edef68
Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/7f5d0179 Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/7f5d0179 Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/7f5d0179 Branch: refs/heads/1.5.1-SNAPSHOT Commit: 7f5d017940dbd7bd73d278d766549b0f729b9d79 Parents: 73c3423 Author: Keith Turner <[email protected]> Authored: Wed May 1 14:25:11 2013 +0000 Committer: Keith Turner <[email protected]> Committed: Wed May 1 14:25:11 2013 +0000 ---------------------------------------------------------------------- .../org/apache/accumulo/proxy/SimpleTest.java | 3 +- .../apache/accumulo/server/util/PortUtils.java | 51 ++++++++++++++++++++ .../accumulo/test/MiniAccumuloCluster.java | 37 ++------------ 3 files changed, 58 insertions(+), 33 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/accumulo/blob/7f5d0179/src/proxy/src/test/java/org/apache/accumulo/proxy/SimpleTest.java ---------------------------------------------------------------------- diff --git a/src/proxy/src/test/java/org/apache/accumulo/proxy/SimpleTest.java b/src/proxy/src/test/java/org/apache/accumulo/proxy/SimpleTest.java index 5474947..a841bd2 100644 --- a/src/proxy/src/test/java/org/apache/accumulo/proxy/SimpleTest.java +++ b/src/proxy/src/test/java/org/apache/accumulo/proxy/SimpleTest.java @@ -75,6 +75,7 @@ import org.apache.accumulo.proxy.thrift.UnknownScanner; import org.apache.accumulo.proxy.thrift.UnknownWriter; import org.apache.accumulo.proxy.thrift.WriterOptions; import org.apache.accumulo.server.test.functional.SlowIterator; +import org.apache.accumulo.server.util.PortUtils; import org.apache.accumulo.test.MiniAccumuloCluster; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; @@ -136,7 +137,7 @@ public class SimpleTest { protocolClass = getRandomProtocol(); System.out.println(protocolClass.getName()); - proxyPort = MiniAccumuloCluster.getRandomFreePort(); + proxyPort = PortUtils.getRandomFreePort(); proxyServer = Proxy.createProxyServer(org.apache.accumulo.proxy.thrift.AccumuloProxy.class, org.apache.accumulo.proxy.ProxyServer.class, proxyPort, protocolClass, props); thread = new Thread() { http://git-wip-us.apache.org/repos/asf/accumulo/blob/7f5d0179/src/server/src/main/java/org/apache/accumulo/server/util/PortUtils.java ---------------------------------------------------------------------- diff --git a/src/server/src/main/java/org/apache/accumulo/server/util/PortUtils.java b/src/server/src/main/java/org/apache/accumulo/server/util/PortUtils.java new file mode 100644 index 0000000..f710b0f --- /dev/null +++ b/src/server/src/main/java/org/apache/accumulo/server/util/PortUtils.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.accumulo.server.util; + +import java.io.IOException; +import java.net.ServerSocket; +import java.util.Random; + +public class PortUtils { + + public static int getRandomFreePort() { + Random r = new Random(); + int count = 0; + + while (count < 13) { + int port = r.nextInt((1 << 16) - 1024) + 1024; + + ServerSocket so = null; + try { + so = new ServerSocket(port); + so.setReuseAddress(true); + return port; + } catch (IOException ioe) { + + } finally { + if (so != null) + try { + so.close(); + } catch (IOException e) {} + } + + } + + throw new RuntimeException("Unable to find port"); + } + +} http://git-wip-us.apache.org/repos/asf/accumulo/blob/7f5d0179/src/test/src/main/java/org/apache/accumulo/test/MiniAccumuloCluster.java ---------------------------------------------------------------------- diff --git a/src/test/src/main/java/org/apache/accumulo/test/MiniAccumuloCluster.java b/src/test/src/main/java/org/apache/accumulo/test/MiniAccumuloCluster.java index e490a3b..e0e49d4 100644 --- a/src/test/src/main/java/org/apache/accumulo/test/MiniAccumuloCluster.java +++ b/src/test/src/main/java/org/apache/accumulo/test/MiniAccumuloCluster.java @@ -23,7 +23,6 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.net.ServerSocket; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -31,7 +30,6 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; -import java.util.Random; import java.util.TimerTask; import org.apache.accumulo.core.conf.Property; @@ -40,6 +38,7 @@ import org.apache.accumulo.server.logger.LogService; import org.apache.accumulo.server.master.Master; import org.apache.accumulo.server.tabletserver.TabletServer; import org.apache.accumulo.server.util.Initialize; +import org.apache.accumulo.server.util.PortUtils; import org.apache.accumulo.server.util.time.SimpleTimer; import org.apache.accumulo.start.Main; import org.apache.zookeeper.server.ZooKeeperServerMain; @@ -124,32 +123,6 @@ public class MiniAccumuloCluster { private MiniAccumuloConfig config; private Process[] tabletServerProcesses; - static public int getRandomFreePort() { - Random r = new Random(); - int count = 0; - - while (count < 13) { - int port = r.nextInt((1 << 16) - 1024) + 1024; - - ServerSocket so = null; - try { - so = new ServerSocket(port); - so.setReuseAddress(true); - return port; - } catch (IOException ioe) { - - } finally { - if (so != null) - try { - so.close(); - } catch (IOException e) {} - } - - } - - throw new RuntimeException("Unable to find port"); - } - Process exec(Class<? extends Object> clazz, String... args) throws IOException { String javaHome = System.getProperty("java.home"); String javaBin = javaHome + File.separator + "bin" + File.separator + "java"; @@ -243,7 +216,7 @@ public class MiniAccumuloCluster { walogDir.mkdirs(); libDir.mkdirs(); - zooKeeperPort = getRandomFreePort(); + zooKeeperPort = PortUtils.getRandomFreePort(); File siteFile = new File(confDir, "accumulo-site.xml"); @@ -256,8 +229,8 @@ public class MiniAccumuloCluster { appendProp(fileWriter, Property.INSTANCE_DFS_DIR, accumuloDir.getAbsolutePath(), siteConfig); appendProp(fileWriter, Property.INSTANCE_ZK_HOST, "localhost:" + zooKeeperPort, siteConfig); appendProp(fileWriter, Property.INSTANCE_SECRET, INSTANCE_SECRET, siteConfig); - appendProp(fileWriter, Property.MASTER_CLIENTPORT, "" + getRandomFreePort(), siteConfig); - appendProp(fileWriter, Property.TSERV_CLIENTPORT, "" + getRandomFreePort(), siteConfig); + appendProp(fileWriter, Property.MASTER_CLIENTPORT, "" + PortUtils.getRandomFreePort(), siteConfig); + appendProp(fileWriter, Property.TSERV_CLIENTPORT, "" + PortUtils.getRandomFreePort(), siteConfig); appendProp(fileWriter, Property.TSERV_PORTSEARCH, "true", siteConfig); appendProp(fileWriter, Property.LOGGER_DIR, walogDir.getAbsolutePath(), siteConfig); appendProp(fileWriter, Property.TSERV_DATACACHE_SIZE, "10M", siteConfig); @@ -265,7 +238,7 @@ public class MiniAccumuloCluster { appendProp(fileWriter, Property.TSERV_MAXMEM, "50M", siteConfig); appendProp(fileWriter, Property.TSERV_WALOG_MAX_SIZE, "100M", siteConfig); appendProp(fileWriter, Property.TSERV_NATIVEMAP_ENABLED, "false", siteConfig); - appendProp(fileWriter, Property.TRACE_PORT, "" + getRandomFreePort(), siteConfig); + appendProp(fileWriter, Property.TRACE_PORT, "" + PortUtils.getRandomFreePort(), siteConfig); appendProp(fileWriter, Property.LOGGER_SORT_BUFFER_SIZE, "50M", siteConfig); appendProp(fileWriter, Property.LOGGER_PORTSEARCH, "true", siteConfig);
