kaisun2000 commented on a change in pull request #1119:
URL: https://github.com/apache/helix/pull/1119#discussion_r454809275



##########
File path: 
zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ZkClient.java
##########
@@ -1221,20 +1237,109 @@ private void reconnect() {
     }
   }
 
+  private class SyncCallbackHandler extends ZkAsyncCallbacks.DefaultCallback 
implements AsyncCallback.VoidCallback {
+    private String _sessionId;
+
+    SyncCallbackHandler(String sessionId) {
+      _sessionId = sessionId;
+    }
+
+    @Override
+    public void processResult(int rc, String path, Object ctx) {
+      LOG.info("sycnOnNewSession with sessionID {} async return code: {}", 
_sessionId, rc);
+      callback(rc, path, ctx);
+    }
+
+    @Override
+    public void handle() {
+      // Make compiler happy, not used.
+    }
+
+    @Override
+    protected boolean needRetry(int rc) {
+      try {
+        switch (KeeperException.Code.get(rc)) {
+          /** Connection to the server has been lost */
+          case CONNECTIONLOSS:
+            return true;
+          default:
+            return false;
+        }
+      } catch (ClassCastException | NullPointerException ex) {
+        LOG.error("Session {} failed to handle unknown return code {}. Skip 
retrying. ex {}",
+            _sessionId, rc, ex);
+        return false;
+      }
+    }
+  }
+
+  private void doAsyncSync(final ZooKeeper zk, final String path, final long 
startT,
+      final SyncCallbackHandler cb) {
+    zk.sync(path, cb,
+        new ZkAsyncRetryCallContext(_asyncCallRetryThread, cb, _monitor, 
startT, 0, false) {
+          @Override
+          protected void doRetry() throws Exception {
+            doAsyncSync(zk, path, startT, cb);
+          }
+        });
+  }
+
+  private boolean retrySync(String sessionId) throws ZkInterruptedException {
+    if (!_syncOnNewSession) {
+      return true;
+    }
+
+    SyncCallbackHandler callbackHandler = new SyncCallbackHandler(sessionId);
+
+    final ZkConnection zkConnection = (ZkConnection) getConnection();

Review comment:
       This is how I look at it. 
   
   Let us say the code is like this 
   
   ```
   void doAsyncSync final String path, final long startT,
         final ZkAsyncCallbacks.SyncCallbackHandler cb) {
   
        final ZooKeeper zk = (ZkConnection) getConnection()).getZookeeper();
        zk.sync(path, cb,
           new ZkAsyncRetryCallContext(_asyncCallRetryThread, cb, _monitor, 
startT, 0, false) {
             @Override
             protected void doRetry() throws Exception {
               doAsyncSync(zk, path, System.currentTimeMillis(), cb);
             }
           });
     }
   
   ```
   
   Then, there is chance that when connection_loss, we have retry request from 
the asyncThread of ZkClient. The retry request is finally doAsyncSync(). Then, 
it would retrieve the ZooKeeper object from connection again. Note, by this 
time, the session can be changed (thus ZooKeeper object with a new session ID) 
is used. 
   
   This is not what we want. At least this sync() call is wasted for the reason 
the new session would invoke another fireAllEvent().
   




----------------------------------------------------------------
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.

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



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to