pkuwm commented on a change in pull request #642: Fix handleNewSession creating 
ephemeral node with expired session
URL: https://github.com/apache/helix/pull/642#discussion_r361219969
 
 

 ##########
 File path: 
helix-core/src/main/java/org/apache/helix/manager/zk/zookeeper/ZkClient.java
 ##########
 @@ -556,15 +634,55 @@ public String create(final String path, Object datat, 
final List<ACL> acl, final
     }
     long startT = System.currentTimeMillis();
     try {
-      final byte[] data = datat == null ? null : serialize(datat, path);
-      checkDataSizeLimit(data);
-      String actualPath = retryUntilConnected(new Callable<String>() {
-        @Override
-        public String call() throws Exception {
-          return getConnection().create(path, data, acl, mode);
+      final byte[] dataBytes = dataObject == null ? null : 
serialize(dataObject, path);
+      checkDataSizeLimit(dataBytes);
+
+      /*
+       * We pass different implementation of callable.
+       * 1. If the session id is null, it means the operation is NOT session 
aware.
+       * In this case, we will implement a regular callable to create a node.
+       * 2. Otherwise, the operation is session aware. We will use a  to
+       * create a node which is guaranteed to be created in the expected 
session.
+       */
+      final String actualPath = retryUntilConnected(() -> {
+        ZooKeeper zooKeeper = ((ZkConnection) getConnection()).getZookeeper();
+
+        /*
+         * 1. If the session id is NOT null and create mode is EPHEMERAL or 
EPHEMERAL_SEQUENTIAL,
+         * the operation is session aware. We have to check the whether or not 
the
+         * passed-in(expected) session id matches actual session's id.
+         * If not, ephemeral node creation is failed. This validation is
+         * critical to guarantee the ephemeral node created by the expected ZK 
session.
+         *
+         * 2. Otherwise, the operation is NOT session aware.
+         * In this case, we will use the actual zookeeper session to create 
the node.
+         */
+        if (expectedSessionId != null && !expectedSessionId.isEmpty()
+            && (mode == CreateMode.EPHEMERAL || mode == 
CreateMode.EPHEMERAL_SEQUENTIAL)) {
+          acquireEventLock();
 
 Review comment:
   Comments added.
   Regarding the optimization you mentioned, I don't think we can make it. 
Because in retryUntilConnect, we don't know if the operation is session aware 
or not. If it is NOT session aware, we still have to retry even the session is 
expired. So we can only check session is matched or not after retry and 
connected in callable.call()

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


With regards,
Apache Git Services

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

Reply via email to