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

jiajunwang 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 6617d91  Clarify error for ZkSessionMismatchedException in 
ZkAsyncCallbacks (#1718)
6617d91 is described below

commit 6617d91f0ad6dcf5f909bed73bdb80a7a3c6b995
Author: Ramin Bashizade <[email protected]>
AuthorDate: Wed May 5 11:39:09 2021 -0700

    Clarify error for ZkSessionMismatchedException in ZkAsyncCallbacks (#1718)
    
    Change the error message for ZkSessionMismatchedException in 
ZkAsyncCallbacks to be clearer instead of throwing a NullPointerException.
---
 .../java/org/apache/helix/zookeeper/zkclient/ZkClient.java     | 10 ++++++++++
 .../helix/zookeeper/zkclient/callback/ZkAsyncCallbacks.java    | 10 ++++++++++
 .../apache/helix/zookeeper/impl/client/TestRawZkClient.java    |  4 ++--
 3 files changed, 22 insertions(+), 2 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 268910b..8b742da 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
@@ -1960,6 +1960,11 @@ public class ZkClient implements Watcher {
                 });
         return null;
       });
+    } catch (ZkSessionMismatchedException e) {
+      // Process callback to release caller from waiting
+      cb.processResult(ZkAsyncCallbacks.ZK_SESSION_MISMATCHED_CODE, path,
+          new ZkAsyncCallMonitorContext(_monitor, startT, 0, false), null);
+      throw e;
     } catch (RuntimeException e) {
       // Process callback to release caller from waiting
       cb.processResult(ZkAsyncCallbacks.UNKNOWN_RET_CODE, path,
@@ -1998,6 +2003,11 @@ public class ZkClient implements Watcher {
             });
         return null;
       });
+    } catch (ZkSessionMismatchedException e) {
+      // Process callback to release caller from waiting
+      cb.processResult(ZkAsyncCallbacks.ZK_SESSION_MISMATCHED_CODE, path,
+          new ZkAsyncCallMonitorContext(_monitor, startT, 0, false), null);
+      throw e;
     } catch (RuntimeException e) {
       // Process callback to release caller from waiting
       cb.processResult(ZkAsyncCallbacks.UNKNOWN_RET_CODE, path,
diff --git 
a/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/callback/ZkAsyncCallbacks.java
 
b/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/callback/ZkAsyncCallbacks.java
index fc1d14d..c188e7e 100644
--- 
a/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/callback/ZkAsyncCallbacks.java
+++ 
b/zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/callback/ZkAsyncCallbacks.java
@@ -37,6 +37,12 @@ public class ZkAsyncCallbacks {
   private static Logger LOG = LoggerFactory.getLogger(ZkAsyncCallbacks.class);
   public static final int UNKNOWN_RET_CODE = 255;
 
+  // Whenever there's a mismatch between the expected Zookeeper session ID and 
the actual
+  // Zookeeper session ID for the async operation, the ZkClient which performs 
the async
+  // operation passes this return code to the async callback indicate that it 
has caught
+  // a ZkSessionMismatchedException.
+  public static final int ZK_SESSION_MISMATCHED_CODE = 127;
+
   public static class GetDataCallbackHandler extends DefaultCallback 
implements DataCallback {
     public byte[] _data;
     public Stat _stat;
@@ -276,6 +282,10 @@ public class ZkAsyncCallbacks {
      * @return true if the error is transient and the operation may succeed 
when being retried.
      */
     protected boolean needRetry(int rc) {
+      if (rc == ZK_SESSION_MISMATCHED_CODE) {
+        LOG.error("Actual session ID doesn't match with expected session ID. 
Skip retrying.");
+        return false;
+      }
       try {
         switch (Code.get(rc)) {
         /** Connection to the server has been lost */
diff --git 
a/zookeeper-api/src/test/java/org/apache/helix/zookeeper/impl/client/TestRawZkClient.java
 
b/zookeeper-api/src/test/java/org/apache/helix/zookeeper/impl/client/TestRawZkClient.java
index 22be865..e078367 100644
--- 
a/zookeeper-api/src/test/java/org/apache/helix/zookeeper/impl/client/TestRawZkClient.java
+++ 
b/zookeeper-api/src/test/java/org/apache/helix/zookeeper/impl/client/TestRawZkClient.java
@@ -887,7 +887,7 @@ public class TestRawZkClient extends ZkTestBase {
 
       // Ensure the async callback is cancelled because of the exception
       Assert.assertTrue(createCallback.waitForSuccess(), "Callback operation 
should be done");
-      Assert.assertEquals(createCallback.getRc(), 
ZkAsyncCallbacks.UNKNOWN_RET_CODE);
+      Assert.assertEquals(createCallback.getRc(), 
ZkAsyncCallbacks.ZK_SESSION_MISMATCHED_CODE);
     }
 
     Assert.assertFalse(zkClient.exists(path));
@@ -915,7 +915,7 @@ public class TestRawZkClient extends ZkTestBase {
 
       // Ensure the async callback is cancelled because of the exception
       Assert.assertTrue(setDataCallback.waitForSuccess(), "Callback operation 
should be done");
-      Assert.assertEquals(setDataCallback.getRc(), 
ZkAsyncCallbacks.UNKNOWN_RET_CODE);
+      Assert.assertEquals(setDataCallback.getRc(), 
ZkAsyncCallbacks.ZK_SESSION_MISMATCHED_CODE);
     }
 
     TestHelper.verify(() -> zkClient.delete(path), TestHelper.WAIT_DURATION);

Reply via email to