You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by jx...@apache.org on 2019/01/28 19:54:03 UTC

[2/3] helix git commit: Add ZkConnection.reconnect to avoid NPE when reset ZkConnection.

Add ZkConnection.reconnect to avoid NPE when reset ZkConnection.

In the old version, reconnect was done by closing and then connecting. In between, the zookeeper ref is null. This may cause NPE which terminate ZkClient operation retry earlier than expected.
This change copy the existing ZkConnection and add reconnect. The new method ensures reconnecting without leaving the field empty.


Project: http://git-wip-us.apache.org/repos/asf/helix/repo
Commit: http://git-wip-us.apache.org/repos/asf/helix/commit/867aa5f4
Tree: http://git-wip-us.apache.org/repos/asf/helix/tree/867aa5f4
Diff: http://git-wip-us.apache.org/repos/asf/helix/diff/867aa5f4

Branch: refs/heads/master
Commit: 867aa5f4593ea303a8cd15475550ef86ace61d1d
Parents: 060ef36
Author: Jiajun Wang <jj...@linkedin.com>
Authored: Tue Jan 8 15:27:21 2019 -0800
Committer: jiajunwang <er...@gmail.com>
Committed: Thu Jan 24 14:30:51 2019 -0800

----------------------------------------------------------------------
 .../helix/manager/zk/zookeeper/ZkClient.java    |  5 ++---
 .../manager/zk/zookeeper/ZkConnection.java      | 20 +++++++++++++++++++-
 2 files changed, 21 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/helix/blob/867aa5f4/helix-core/src/main/java/org/apache/helix/manager/zk/zookeeper/ZkClient.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/manager/zk/zookeeper/ZkClient.java b/helix-core/src/main/java/org/apache/helix/manager/zk/zookeeper/ZkClient.java
index 5fa2f91..03de880 100644
--- a/helix-core/src/main/java/org/apache/helix/manager/zk/zookeeper/ZkClient.java
+++ b/helix-core/src/main/java/org/apache/helix/manager/zk/zookeeper/ZkClient.java
@@ -823,9 +823,8 @@ public class ZkClient implements Watcher {
   private void reconnect() {
     getEventLock().lock();
     try {
-      IZkConnection connection = getConnection();
-      connection.close();
-      connection.connect(this);
+      ZkConnection connection = ((ZkConnection) getConnection());
+      connection.reconnect(this);
     } catch (InterruptedException e) {
       throw new ZkInterruptedException(e);
     } finally {

http://git-wip-us.apache.org/repos/asf/helix/blob/867aa5f4/helix-core/src/main/java/org/apache/helix/manager/zk/zookeeper/ZkConnection.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/manager/zk/zookeeper/ZkConnection.java b/helix-core/src/main/java/org/apache/helix/manager/zk/zookeeper/ZkConnection.java
index b4a78bb..6f1e880 100644
--- a/helix-core/src/main/java/org/apache/helix/manager/zk/zookeeper/ZkConnection.java
+++ b/helix-core/src/main/java/org/apache/helix/manager/zk/zookeeper/ZkConnection.java
@@ -31,7 +31,6 @@ import org.apache.zookeeper.data.ACL;
 import org.apache.zookeeper.data.Stat;
 
 public class ZkConnection implements IZkConnection {
-
   private static final Logger LOG = Logger.getLogger(ZkConnection.class);
 
   /** It is recommended to use quite large sessions timeouts for ZooKeeper. */
@@ -84,6 +83,25 @@ public class ZkConnection implements IZkConnection {
     }
   }
 
+  protected void reconnect(Watcher watcher) throws InterruptedException {
+    _zookeeperLock.lock();
+    try {
+      if (_zk == null) {
+        throw new IllegalStateException("zk client has not been connected or already been closed");
+      }
+      ZooKeeper prevZk = _zk;
+      try {
+        LOG.debug("Creating new ZookKeeper instance to reconnect to " + _servers + ".");
+        _zk = new ZooKeeper(_servers, _sessionTimeOut, watcher);
+        prevZk.close();
+      } catch (IOException e) {
+        throw new ZkException("Unable to connect to " + _servers, e);
+      }
+    } finally {
+      _zookeeperLock.unlock();
+    }
+  }
+
   @Override
   public String create(String path, byte[] data, CreateMode mode) throws KeeperException, InterruptedException {
     return _zk.create(path, data, Ids.OPEN_ACL_UNSAFE, mode);