jiajunwang commented on a change in pull request #718: Implement Helix 
nonblocking lock
URL: https://github.com/apache/helix/pull/718#discussion_r377357255
 
 

 ##########
 File path: 
helix-lock/src/main/java/org/apache/helix/lock/ZKHelixNonblockingLock.java
 ##########
 @@ -0,0 +1,198 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.helix.lock;
+
+import java.util.Date;
+
+import org.I0Itec.zkclient.DataUpdater;
+import org.apache.helix.AccessOption;
+import org.apache.helix.BaseDataAccessor;
+import org.apache.helix.HelixException;
+import org.apache.helix.ZNRecord;
+import org.apache.helix.manager.zk.ZkBaseDataAccessor;
+import org.apache.log4j.Logger;
+
+import static 
org.apache.helix.lock.ZKHelixNonblockingLockInfo.DEFAULT_OWNER_TEXT;
+import static 
org.apache.helix.lock.ZKHelixNonblockingLockInfo.DEFAULT_TIMEOUT_LONG;
+
+
+/**
+ * Helix nonblocking lock implementation based on Zookeeper
+ */
+public class ZKHelixNonblockingLock implements HelixLock {
+
+  private static final Logger LOG = 
Logger.getLogger(ZKHelixNonblockingLock.class);
+
+  private static final String LOCK_ROOT = "/LOCK";
+  private final String _lockPath;
+  private final String _userId;
+  private final long _timeout;
+  private final String _lockMsg;
+  private final BaseDataAccessor<ZNRecord> _baseDataAccessor;
+
+  /**
+   * Initialize the lock with user provided information, e.g.,cluster, scope, 
etc.
+   * @param clusterName the cluster under which the lock will live
+   * @param scope the scope to lock
+   * @param zkAddress the zk address the cluster connects to
+   * @param timeout the timeout period of the lcok
+   * @param lockMsg the reason for having this lock
+   * @param userId a universal unique userId for lock owner identity
+   */
+  public ZKHelixNonblockingLock(String clusterName, HelixLockScope scope, 
String zkAddress,
+      Long timeout, String lockMsg, String userId) {
+    this("/" + clusterName.toUpperCase() + LOCK_ROOT + scope.getZkPath(), 
zkAddress, timeout, lockMsg, userId);
+  }
+
+  /**
+   * Initialize the lock with user provided information, e.g., lock path under 
zookeeper, etc.
+   * @param lockPath the path of the lock under Zookeeper
+   * @param zkAddress the zk address of the cluster
+   * @param timeout the timeout period of the lcok
+   * @param lockMsg the reason for having this lock
+   * @param userId a universal unique userId for lock owner identity
+   */
+  private ZKHelixNonblockingLock(String lockPath, String zkAddress, Long 
timeout, String lockMsg,
+      String userId) {
+    _lockPath = lockPath;
+    _timeout = timeout;
+    _lockMsg = lockMsg;
+    _userId = userId;
+    _baseDataAccessor = new ZkBaseDataAccessor<>(zkAddress);
+  }
+
+  @Override
+  public boolean acquireLock() {
+
+    // Set lock information fields
+    ZNRecord lockInfo = new ZNRecord(_userId);
+    lockInfo.setSimpleField(ZKHelixNonblockingLockInfo.InfoKey.OWNER.name(), 
_userId);
+    lockInfo.setSimpleField(ZKHelixNonblockingLockInfo.InfoKey.MESSAGE.name(), 
_lockMsg);
+
+    long deadline;
+    // Prevent value overflow
+    if (_timeout > Long.MAX_VALUE - System.currentTimeMillis()) {
+      deadline = Long.MAX_VALUE;
+    } else {
+      deadline = System.currentTimeMillis() + _timeout;
+    }
+    lockInfo.setLongField(ZKHelixNonblockingLockInfo.InfoKey.TIMEOUT.name(), 
deadline);
+
+    LockUpdater updater = new LockUpdater(lockInfo);
+    return _baseDataAccessor.update(_lockPath, updater, 
AccessOption.PERSISTENT);
+  }
+
+  @Override
+  public boolean releaseLock() {
+    ZNRecord newLockInfo = new ZKHelixNonblockingLockInfo<>().toZNRecord();
+    LockUpdater updater = new LockUpdater(newLockInfo);
+    return _baseDataAccessor.update(_lockPath, updater, 
AccessOption.PERSISTENT);
+  }
+
+  @Override
+  public ZKHelixNonblockingLockInfo getLockInfo() {
+    ZNRecord curLockInfo = _baseDataAccessor.get(_lockPath, null, 
AccessOption.PERSISTENT);
+    return new ZKHelixNonblockingLockInfo(curLockInfo);
+  }
+
+  @Override
+  public boolean isOwner() {
+    ZNRecord curLockInfo = _baseDataAccessor.get(_lockPath, null, 
AccessOption.PERSISTENT);
+    return userIdMatches(curLockInfo) && !hasTimedOut(curLockInfo);
+  }
+
+  /**
+   * Check if a lock has timed out
+   * @param record the current lock information in ZNRecord format
+   * @return return true if the lock has timed out, otherwise return false.
+   */
+  private boolean hasTimedOut(ZNRecord record) {
+    if (record == null) {
+      return false;
 
 Review comment:
   Should be true? If there is no such lock node, we can assume timeout == 
true, right?

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@helix.apache.org
For additional commands, e-mail: reviews-h...@helix.apache.org

Reply via email to