This is an automated email from the ASF dual-hosted git repository.

jxue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/helix.git


The following commit(s) were added to refs/heads/master by this push:
     new 53e583d4f Fixing the ZkServer.start() to just check for free port when 
starting ZK (#2452)
53e583d4f is described below

commit 53e583d4ff16cfafdcb06d2164cdd7a8a6a81245
Author: Naman Parakh <[email protected]>
AuthorDate: Wed Apr 19 11:24:27 2023 -0700

    Fixing the ZkServer.start() to just check for free port when starting ZK 
(#2452)
    
    Fixing the ZkServer.start() to just check for free port when starting ZK.
---
 .../apache/helix/zookeeper/zkclient/ZkServer.java  | 67 +++++++---------------
 1 file changed, 20 insertions(+), 47 deletions(-)

diff --git 
a/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkServer.java 
b/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkServer.java
index cd215f3dd..8e171dc57 100644
--- 
a/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkServer.java
+++ 
b/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkServer.java
@@ -22,10 +22,6 @@ package org.apache.helix.zookeeper.zkclient;
 import java.io.File;
 import java.io.IOException;
 import java.net.InetSocketAddress;
-import java.util.Arrays;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 
@@ -35,6 +31,8 @@ import 
org.apache.helix.zookeeper.zkclient.serialize.BasicZkSerializer;
 import org.apache.helix.zookeeper.zkclient.serialize.SerializableSerializer;
 import org.apache.zookeeper.server.NIOServerCnxnFactory;
 import org.apache.zookeeper.server.ZooKeeperServer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class ZkServer {
 
@@ -83,55 +81,30 @@ public class ZkServer {
 
     @PostConstruct
     public void start() {
-        final String[] localHostNames = NetworkUtil.getLocalHostNames();
-        String names = "";
-        for (int i = 0; i < localHostNames.length; i++) {
-            final String name = localHostNames[i];
-            names += " " + name;
-            if (i + 1 != localHostNames.length) {
-                names += ",";
-            }
-        }
-        LOG.info("Starting ZkServer on: [" + names + "] port " + _port + 
"...");
         startZooKeeperServer();
         _zkClient = new ZkClient(new ZkConnection("localhost:" + _port), 
10000, -1, new BasicZkSerializer(new SerializableSerializer()), null, null, 
null, false);
         _defaultNameSpace.createDefaultNameSpace(_zkClient);
     }
 
     private void startZooKeeperServer() {
-        final String[] localhostHostNames = NetworkUtil.getLocalHostNames();
-        final String servers = "localhost:" + _port;
-        // check if this server needs to start a _client server.
-        int pos = -1;
-        LOG.debug("check if hostNames " + servers + " is in list: " + 
Arrays.asList(localhostHostNames));
-        if ((pos = NetworkUtil.hostNamesInList(servers, localhostHostNames)) 
!= -1) {
-            // yes this server needs to start a zookeeper server
-            final String[] hosts = servers.split(",");
-            final String[] hostSplitted = hosts[pos].split(":");
-            int port = _port;
-            if (hostSplitted.length > 1) {
-                port = Integer.parseInt(hostSplitted[1]);
-            }
-            // check if this machine is already something running..
-            if (NetworkUtil.isPortFree(port)) {
-                final File dataDir = new File(_dataDir);
-                final File dataLogDir = new File(_logDir);
-                dataDir.mkdirs();
-                dataLogDir.mkdirs();
-
-                if (hosts.length > 1) {
-                    // multiple zk servers
-                    LOG.info("Start distributed zookeeper server...");
-                    throw new IllegalArgumentException("Unable to start 
distributed zookeeper server");
-                }
-                // single zk server
-                LOG.info("Start single zookeeper server...");
-                LOG.info("data dir: " + dataDir.getAbsolutePath());
-                LOG.info("data log dir: " + dataLogDir.getAbsolutePath());
-                startSingleZkServer(_tickTime, dataDir, dataLogDir, port);
-            } else {
-                throw new IllegalStateException("Zookeeper port " + port + " 
was already in use. Running in single machine mode?");
-            }
+        long startTime = System.currentTimeMillis();
+        /**
+         * Checking if the ZK hostname is present in the list of resolved 
ips/hostnames for
+         * all network interfaces on a machine is expensive. So, we start the 
ZK on any local
+         * address and just check if the port specified is free.
+         */
+        if (NetworkUtil.isPortFree(_port)) {
+            final File dataDir = new File(_dataDir);
+            final File dataLogDir = new File(_logDir);
+            dataDir.mkdirs();
+            dataLogDir.mkdirs();
+            LOG.info(String.format("Starting single zookeeper server on port 
%d...", _port));
+            LOG.info("data dir: " + dataDir.getAbsolutePath());
+            LOG.info("data log dir: " + dataLogDir.getAbsolutePath());
+            startSingleZkServer(_tickTime, dataDir, dataLogDir, _port);
+            LOG.info(String.format("Started single zookeeper server on port %d 
in %d milliseconds", _port, System.currentTimeMillis() - startTime));
+        } else {
+            throw new IllegalStateException("Zookeeper port " + _port + " was 
already in use. Running in single machine mode?");
         }
     }
 

Reply via email to