incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amccu...@apache.org
Subject [2/4] git commit: Setup ZooKeeper MiniCluster to run on random port determined at use time.
Date Tue, 23 Oct 2012 16:20:24 GMT
Setup ZooKeeper MiniCluster to run on random port determined at use time.


Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/9638a1c9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/9638a1c9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/9638a1c9

Branch: refs/heads/lucene-4.0.0
Commit: 9638a1c94c178608cf16853f239f4fa7cef6bd97
Parents: ac77082
Author: Aaron McCurry <amccurry@gmail.com>
Authored: Tue Oct 23 12:17:07 2012 -0400
Committer: Aaron McCurry <amccurry@gmail.com>
Committed: Tue Oct 23 12:17:07 2012 -0400

----------------------------------------------------------------------
 .../src/test/java/org/apache/blur/MiniCluster.java |   55 +++++++++-----
 1 files changed, 35 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/9638a1c9/src/blur-core/src/test/java/org/apache/blur/MiniCluster.java
----------------------------------------------------------------------
diff --git a/src/blur-core/src/test/java/org/apache/blur/MiniCluster.java b/src/blur-core/src/test/java/org/apache/blur/MiniCluster.java
index 1d03932..5b35cf1 100644
--- a/src/blur-core/src/test/java/org/apache/blur/MiniCluster.java
+++ b/src/blur-core/src/test/java/org/apache/blur/MiniCluster.java
@@ -29,11 +29,13 @@ import static org.apache.blur.utils.BlurConstants.BLUR_ZOOKEEPER_CONNECTION;
 import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.Field;
+import java.net.ServerSocket;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.Random;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -74,31 +76,24 @@ import org.apache.zookeeper.server.ZooKeeperServerMain;
 import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
 import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
 
-
 public abstract class MiniCluster {
 
   private static Log LOG = LogFactory.getLog(MiniCluster.class);
   private static MiniDFSCluster cluster;
   private static Thread serverThread;
-  private static String zkConnectionString = "localhost:21810";
+  // private static String zkConnectionString = "localhost:21810";
   private static ZooKeeperServerMainEmbedded zooKeeperServerMain;
   private static List<ThriftServer> controllers = new ArrayList<ThriftServer>();
   private static List<ThriftServer> shards = new ArrayList<ThriftServer>();
   private static String controllerConnectionStr;
+  private static int zkPort;
 
   public static void main(String[] args) throws IOException, InterruptedException, KeeperException,
BlurException, TException {
     startDfs("./tmp");
     startZooKeeper("./tmp");
     startControllers(1);
     startShards(1);
-    
-    //Run the controllers/shards on custom ports.  
-    //BlurConfiguration conf = new BlurConfiguration(false);
-    //conf.setInt(BLUR_CONTROLLER_BIND_PORT, 40001);
-    //conf.setInt(BLUR_SHARD_BIND_PORT, 40002);
-    //startControllers(conf, 1);
-    //startShards(conf, 1);
-    
+
     try {
       Iface client = BlurClient.getClient(getControllerConnectionStr());
       createTable("test", client);
@@ -195,13 +190,13 @@ public abstract class MiniCluster {
 
   private static BlurConfiguration getBlurConfiguration(BlurConfiguration overrides) {
     BlurConfiguration conf = getBlurConfiguration();
-	
-    for(Map.Entry<String, String> over: overrides.getProperties().entrySet()) {
+
+    for (Map.Entry<String, String> over : overrides.getProperties().entrySet()) {
       conf.set(over.getKey().toString(), over.getValue().toString());
     }
     return conf;
   }
-  
+
   private static BlurConfiguration getBlurConfiguration() {
     BlurConfiguration configuration;
     try {
@@ -305,7 +300,7 @@ public abstract class MiniCluster {
   }
 
   public static String getZkConnectionString() {
-    return zkConnectionString;
+    return "localhost:" + zkPort;
   }
 
   public static void startZooKeeper(String path) {
@@ -317,14 +312,32 @@ public abstract class MiniCluster {
     properties.setProperty("tickTime", "2000");
     properties.setProperty("initLimit", "10");
     properties.setProperty("syncLimit", "5");
+    startZooKeeper(properties, format, path, getRandomPort());
+  }
 
-    properties.setProperty("clientPort", "21810");
+  public static void startZooKeeper(Properties properties, String path) {
+    startZooKeeper(properties, true, path, getRandomPort());
+  }
 
-    startZooKeeper(properties, format, path);
+  private static int getRandomPort() {
+    Random random = new Random();
+    for (int i = 0; i < 100; i++) {
+      int port = random.nextInt(50000) + 10000;
+      if (testPort(port)) {
+        return port;
+      }
+    }
+    throw new RuntimeException("Random port for zookeeper could not be found.");
   }
 
-  public static void startZooKeeper(Properties properties, String path) {
-    startZooKeeper(properties, true, path);
+  private static boolean testPort(int port) {
+    try {
+      ServerSocket serverSocket = new ServerSocket(port);
+      serverSocket.close();
+      return true;
+    } catch (IOException e) {
+      return false;
+    }
   }
 
   private static class ZooKeeperServerMainEmbedded extends ZooKeeperServerMain {
@@ -334,7 +347,9 @@ public abstract class MiniCluster {
     }
   }
 
-  public static void startZooKeeper(final Properties properties, boolean format, String path)
{
+  public static void startZooKeeper(final Properties properties, boolean format, String path,
int port) {
+    zkPort = port;
+    properties.setProperty("clientPort", Integer.toString(port));
     String realPath = path + "/zk_test";
     properties.setProperty("dataDir", realPath);
     final ServerConfig serverConfig = new ServerConfig();
@@ -372,7 +387,7 @@ public abstract class MiniCluster {
         throw new RuntimeException(e);
       }
       try {
-        ZooKeeper zk = new ZooKeeper(zkConnectionString, 30000, new Watcher() {
+        ZooKeeper zk = new ZooKeeper(getZkConnectionString(), 30000, new Watcher() {
           @Override
           public void process(WatchedEvent event) {
 


Mime
View raw message