poorbarcode commented on code in PR #17865:
URL: https://github.com/apache/pulsar/pull/17865#discussion_r983496968


##########
pulsar-package-management/bookkeeper-storage/src/test/java/org/apache/pulsar/packages/management/storage/bookkeeper/bookkeeper/test/ZooKeeperUtil.java:
##########
@@ -16,120 +16,142 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-/**
- * This file is derived from ZooKeeperUtil from Apache BookKeeper
- * http://bookkeeper.apache.org
- */
 
 package 
org.apache.pulsar.packages.management.storage.bookkeeper.bookkeeper.test;
 
+import java.io.File;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.bookkeeper.test.ZooKeeperCluster;
+import org.apache.bookkeeper.util.IOUtils;
 import org.apache.bookkeeper.zookeeper.ZooKeeperClient;
 import org.apache.commons.io.FileUtils;
-import org.apache.zookeeper.CreateMode;
-import org.apache.zookeeper.ZooDefs.Ids;
 import org.apache.zookeeper.ZooKeeper;
 import org.apache.zookeeper.server.NIOServerCnxnFactory;
 import org.apache.zookeeper.server.ZooKeeperServer;
 import org.apache.zookeeper.test.ClientBase;
+import org.junit.Assert;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.testng.Assert;
-
-import java.io.File;
-import java.io.IOException;
-import java.net.InetSocketAddress;
-import java.util.concurrent.CountDownLatch;
-
-public class ZooKeeperUtil {
-    static final Logger LOG = 
LoggerFactory.getLogger(org.apache.bookkeeper.test.ZooKeeperUtil.class);
-
-    // ZooKeeper related variables
-    protected static final Integer zooKeeperPort = PortManager.nextFreePort();
-    private final InetSocketAddress zkaddr;
 
+public class ZooKeeperUtil implements ZooKeeperCluster {
+    static final Logger LOG;
+    protected Integer zooKeeperPort = 0;
+    private InetSocketAddress zkaddr;
     protected ZooKeeperServer zks;
-    protected ZooKeeper zkc; // zookeeper client
+    protected ZooKeeper zkc;
     protected NIOServerCnxnFactory serverFactory;
-    protected File ZkTmpDir;
-    private final String connectString;
+    protected File zkTmpDir;
+    private String connectString;
 
     public ZooKeeperUtil() {
-        zkaddr = new InetSocketAddress(zooKeeperPort);
-        connectString = "localhost:" + zooKeeperPort;
+        String loopbackIPAddr = 
InetAddress.getLoopbackAddress().getHostAddress();
+        this.zkaddr = new InetSocketAddress(loopbackIPAddr, 0);
+        this.connectString = loopbackIPAddr + ":" + this.zooKeeperPort;
     }
 
     public ZooKeeper getZooKeeperClient() {
-        return zkc;
+        return this.zkc;
     }
 
     public String getZooKeeperConnectString() {
-        return connectString;
+        return this.connectString;
+    }
+
+    public String getMetadataServiceUri() {
+        return this.getMetadataServiceUri("/ledgers");
     }
 
-    public void startServer() throws Exception {
-        // create a ZooKeeper server(dataDir, dataLogDir, port)
+    public String getMetadataServiceUri(String zkLedgersRootPath) {
+        return "zk://" + this.connectString + zkLedgersRootPath;
+    }
+
+    public String getMetadataServiceUri(String zkLedgersRootPath, String type) 
{
+        return "zk+" + type + "://" + this.connectString + zkLedgersRootPath;
+    }
+
+    public void startCluster() throws Exception {
         LOG.debug("Running ZK server");
-        // ServerStats.registerAsConcrete();
         ClientBase.setupTestEnv();
-        ZkTmpDir = File.createTempFile("zookeeper", "test");
-        ZkTmpDir.delete();
-        ZkTmpDir.mkdir();
+        this.zkTmpDir = IOUtils.createTempDir("zookeeper", "test");
+        this.restartCluster();
+        this.createBKEnsemble("/ledgers");
+    }
 
-        zks = new ZooKeeperServer(ZkTmpDir, ZkTmpDir, 
ZooKeeperServer.DEFAULT_TICK_TIME);
-        serverFactory = new NIOServerCnxnFactory();
-        serverFactory.configure(zkaddr, 100);
-        serverFactory.startup(zks);
+    public void restartCluster() throws Exception {
+        this.zks = new ZooKeeperServer(this.zkTmpDir, this.zkTmpDir, 3000);

Review Comment:
   Good suggestion



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to