You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by xy...@apache.org on 2023/04/27 20:36:34 UTC

[helix] 25/37: Add check in ZkClient.connect so it only called once.

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

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

commit eac70d0c0ddd90ec5ab96ddb66a76862d854b699
Author: xyuanlu <xy...@gmail.com>
AuthorDate: Thu Mar 9 15:48:51 2023 -0800

    Add check in ZkClient.connect so it only called once.
    
    This change adds an extra check in ZkClient.connect so that it will only be invoked once.
    Also avoid close called in between of isclosed check and connection establishment in connect().
---
 .../org/apache/helix/zookeeper/zkclient/ZkClient.java  | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkClient.java b/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkClient.java
index 1c00f9768..31fe4e97a 100644
--- a/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkClient.java
+++ b/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkClient.java
@@ -33,6 +33,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
 import javax.management.JMException;
+
 import org.apache.helix.zookeeper.api.client.ChildrenSubscribeResult;
 import org.apache.helix.zookeeper.constant.ZkSystemPropertyKeys;
 import org.apache.helix.zookeeper.datamodel.SessionAwareZNRecord;
@@ -2508,12 +2509,18 @@ public class ZkClient implements Watcher {
    */
   public void connect(final long maxMsToWaitUntilConnected, Watcher watcher)
       throws ZkInterruptedException, ZkTimeoutException, IllegalStateException {
-    if (isClosed()) {
-      throw new IllegalStateException("ZkClient already closed!");
-    }
     boolean started = false;
-    acquireEventLock();
+
     try {
+      acquireEventLock();
+
+      if (isClosed()) {
+        throw new IllegalStateException("ZkClient already closed!");
+      }
+      if (_currentState != null) {
+        throw new IllegalStateException(
+            "ZkClient is not in init state. connect() has already been called.");
+      }
       setShutdownTrigger(false);
 
       IZkConnection zkConnection = getConnection();
@@ -2534,8 +2541,7 @@ public class ZkClient implements Watcher {
         zkConnection.connect(watcher);
         LOG.debug("zkclient{} Awaiting connection to Zookeeper server", _uid);
         if (!waitUntilConnected(maxMsToWaitUntilConnected, TimeUnit.MILLISECONDS)) {
-          throw new ZkTimeoutException(
-              "Unable to connect to zookeeper server within timeout: " + maxMsToWaitUntilConnected);
+          throw new ZkTimeoutException("Unable to connect to zookeeper server within timeout: " + maxMsToWaitUntilConnected);
         }
       } else {
         // if the client is not managing connection, the input connection is supposed to connect.