You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@helix.apache.org by GitBox <gi...@apache.org> on 2020/11/05 00:34:50 UTC

[GitHub] [helix] dasahcc commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock

dasahcc commented on a change in pull request #1509:
URL: https://github.com/apache/helix/pull/1509#discussion_r517712639



##########
File path: helix-lock/src/main/java/org/apache/helix/lock/helix/ZKDistributedNonblockingLock.java
##########
@@ -165,6 +165,7 @@ public ZNRecord update(ZNRecord current) {
     private String _userId;
     private long _timeout;
     private String _lockMsg;
+    private String _lockPath;

Review comment:
       What if lockPath is confilct the lockPath in lockScope.

##########
File path: helix-rest/src/main/java/org/apache/helix/rest/server/resources/lock/LockAccessor.java
##########
@@ -0,0 +1,228 @@
+package org.apache.helix.rest.server.resources.lock;
+
+/*
+ * 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.
+ */
+
+import java.util.List;
+import java.util.Map;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Response;
+
+import com.codahale.metrics.annotation.ResponseMetered;
+import com.codahale.metrics.annotation.Timed;
+import com.google.common.collect.Lists;
+import org.apache.helix.HelixException;
+import org.apache.helix.lock.LockInfo;
+import org.apache.helix.lock.LockScope;
+import org.apache.helix.lock.helix.HelixLockScope;
+import org.apache.helix.lock.helix.ZKDistributedNonblockingLock;
+import org.apache.helix.rest.common.HttpConstants;
+import org.apache.helix.rest.server.resources.helix.AbstractHelixResource;
+import org.apache.helix.zookeeper.datamodel.ZNRecord;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Path("/locks")
+public class LockAccessor extends AbstractHelixResource {
+  private static final Logger LOG = LoggerFactory.getLogger(LockAccessor.class);
+  private static final String ZK_ADDR = "zkAddress";
+  private static final String LOCK_PATH = "lockPath";
+  private static final String LOCK_TIMEOUT = "lockTimeout";
+  private static final String USER_ID = "userId";
+  private static final String LOCK_MSG = "lockMsg";
+  private static final String LOCK_SCOPE_PROPERTY = "property";
+  private static final String PATH_KEYS = "pathKeys";
+  private static final String PATH_KEY_DELIMITER = "\\|";
+
+  private enum LockScopeKey {
+    HelixLockScope,
+  }
+
+  /**
+   *  A class contains information to locate a lock znode
+   */
+  private class LockLocator {
+    private final String _zkAddress;
+    private final String _lockPath;
+
+    private LockLocator(String zkAddress, String lockPath) {
+      this._zkAddress = zkAddress;
+      this._lockPath = lockPath;
+    }
+
+    public String getZkAddress() {
+      return _zkAddress;
+    }
+
+    public String getLockPath() {
+      return _lockPath;
+    }
+  }
+
+  @ResponseMetered(name = HttpConstants.WRITE_REQUEST)
+  @Timed(name = HttpConstants.WRITE_REQUEST)
+  @POST
+  @Path("lock")
+  public Response tryLock(String content) {
+    System.out.println("Payload:\n" + content);
+    ZKDistributedNonblockingLock lock;
+    LockLocator lockLocator;
+    LockInfo lockInfo;
+    try {
+      // Parse payload data
+      ZNRecord lockContent = toZNRecord(content);
+
+      // Parse zk address and lock path
+      lockLocator = parseLockLocator(lockContent);
+
+      // Parse lock info such as user id, message, timeout
+      lockInfo = parseLockInfo(lockContent);
+
+      // Build a lock object
+      lock = new ZKDistributedNonblockingLock.Builder().setLockMsg(lockInfo.getMessage())
+          .setUserId(lockInfo.getOwner()).setTimeout(lockInfo.getTimeout())
+          .setZkAddress(lockLocator.getZkAddress()).setLockPath(lockLocator.getLockPath()).build();
+    } catch (Exception e) {
+      return badRequest(e.getMessage());
+    }
+
+    // Perform try lock
+    boolean result;
+    try {
+      result = lock.tryLock();
+    } catch (HelixException e) {
+      return badRequest(
+          "Lock path is not found or lock scope could not be generated, please examine the request.");
+    }
+
+    return result ? OK(JSONRepresentation(lock)) : serverError(String.format(

Review comment:
       You will not see the serverError part, right? Because it already returns badrequst in previous catch.

##########
File path: helix-lock/src/main/java/org/apache/helix/lock/helix/ZKDistributedNonblockingLock.java
##########
@@ -204,8 +210,20 @@ public ZKDistributedNonblockingLock build() {
       }
 
       // Return a ZKDistributedNonblockingLock instance
-      return new ZKDistributedNonblockingLock(_lockScope.getPath(), _timeout, _lockMsg, _userId,
-          baseDataAccessor);
+      String lockPath = null;
+      if (_lockScope != null) {
+        lockPath = _lockScope.getPath();
+      }
+
+      if (_lockPath != null) {
+        lockPath = _lockPath;
+      }

Review comment:
       I can understand the logic. But it seemed not be a good readable code.
   
   If we think lockPath is necessary:
   
   if (_lockScope != null) {
   _lockPath = _lockScope.getPath();
   }

##########
File path: helix-rest/src/main/java/org/apache/helix/rest/server/resources/lock/LockAccessor.java
##########
@@ -0,0 +1,228 @@
+package org.apache.helix.rest.server.resources.lock;
+
+/*
+ * 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.
+ */
+
+import java.util.List;
+import java.util.Map;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Response;
+
+import com.codahale.metrics.annotation.ResponseMetered;
+import com.codahale.metrics.annotation.Timed;
+import com.google.common.collect.Lists;
+import org.apache.helix.HelixException;
+import org.apache.helix.lock.LockInfo;
+import org.apache.helix.lock.LockScope;
+import org.apache.helix.lock.helix.HelixLockScope;
+import org.apache.helix.lock.helix.ZKDistributedNonblockingLock;
+import org.apache.helix.rest.common.HttpConstants;
+import org.apache.helix.rest.server.resources.helix.AbstractHelixResource;
+import org.apache.helix.zookeeper.datamodel.ZNRecord;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Path("/locks")
+public class LockAccessor extends AbstractHelixResource {
+  private static final Logger LOG = LoggerFactory.getLogger(LockAccessor.class);
+  private static final String ZK_ADDR = "zkAddress";
+  private static final String LOCK_PATH = "lockPath";
+  private static final String LOCK_TIMEOUT = "lockTimeout";
+  private static final String USER_ID = "userId";
+  private static final String LOCK_MSG = "lockMsg";
+  private static final String LOCK_SCOPE_PROPERTY = "property";
+  private static final String PATH_KEYS = "pathKeys";
+  private static final String PATH_KEY_DELIMITER = "\\|";
+
+  private enum LockScopeKey {
+    HelixLockScope,
+  }
+
+  /**
+   *  A class contains information to locate a lock znode
+   */
+  private class LockLocator {
+    private final String _zkAddress;
+    private final String _lockPath;
+
+    private LockLocator(String zkAddress, String lockPath) {
+      this._zkAddress = zkAddress;
+      this._lockPath = lockPath;
+    }
+
+    public String getZkAddress() {
+      return _zkAddress;
+    }
+
+    public String getLockPath() {
+      return _lockPath;
+    }
+  }
+
+  @ResponseMetered(name = HttpConstants.WRITE_REQUEST)
+  @Timed(name = HttpConstants.WRITE_REQUEST)
+  @POST
+  @Path("lock")
+  public Response tryLock(String content) {
+    System.out.println("Payload:\n" + content);

Review comment:
       Change this to log.

##########
File path: helix-rest/src/main/java/org/apache/helix/rest/server/resources/lock/LockAccessor.java
##########
@@ -0,0 +1,228 @@
+package org.apache.helix.rest.server.resources.lock;
+
+/*
+ * 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.
+ */
+
+import java.util.List;
+import java.util.Map;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Response;
+
+import com.codahale.metrics.annotation.ResponseMetered;
+import com.codahale.metrics.annotation.Timed;
+import com.google.common.collect.Lists;
+import org.apache.helix.HelixException;
+import org.apache.helix.lock.LockInfo;
+import org.apache.helix.lock.LockScope;
+import org.apache.helix.lock.helix.HelixLockScope;
+import org.apache.helix.lock.helix.ZKDistributedNonblockingLock;
+import org.apache.helix.rest.common.HttpConstants;
+import org.apache.helix.rest.server.resources.helix.AbstractHelixResource;
+import org.apache.helix.zookeeper.datamodel.ZNRecord;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Path("/locks")
+public class LockAccessor extends AbstractHelixResource {
+  private static final Logger LOG = LoggerFactory.getLogger(LockAccessor.class);
+  private static final String ZK_ADDR = "zkAddress";
+  private static final String LOCK_PATH = "lockPath";
+  private static final String LOCK_TIMEOUT = "lockTimeout";
+  private static final String USER_ID = "userId";
+  private static final String LOCK_MSG = "lockMsg";
+  private static final String LOCK_SCOPE_PROPERTY = "property";
+  private static final String PATH_KEYS = "pathKeys";
+  private static final String PATH_KEY_DELIMITER = "\\|";
+
+  private enum LockScopeKey {
+    HelixLockScope,
+  }
+
+  /**
+   *  A class contains information to locate a lock znode
+   */
+  private class LockLocator {
+    private final String _zkAddress;
+    private final String _lockPath;
+
+    private LockLocator(String zkAddress, String lockPath) {
+      this._zkAddress = zkAddress;
+      this._lockPath = lockPath;
+    }
+
+    public String getZkAddress() {
+      return _zkAddress;
+    }
+
+    public String getLockPath() {
+      return _lockPath;
+    }
+  }
+
+  @ResponseMetered(name = HttpConstants.WRITE_REQUEST)
+  @Timed(name = HttpConstants.WRITE_REQUEST)
+  @POST
+  @Path("lock")

Review comment:
       Do you mean the URI  "xxxx/locks/lock" ? Shall we call it "acquireLock"?

##########
File path: helix-rest/src/main/java/org/apache/helix/rest/server/resources/lock/LockAccessor.java
##########
@@ -0,0 +1,228 @@
+package org.apache.helix.rest.server.resources.lock;
+
+/*
+ * 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.
+ */
+
+import java.util.List;
+import java.util.Map;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Response;
+
+import com.codahale.metrics.annotation.ResponseMetered;
+import com.codahale.metrics.annotation.Timed;
+import com.google.common.collect.Lists;
+import org.apache.helix.HelixException;
+import org.apache.helix.lock.LockInfo;
+import org.apache.helix.lock.LockScope;
+import org.apache.helix.lock.helix.HelixLockScope;
+import org.apache.helix.lock.helix.ZKDistributedNonblockingLock;
+import org.apache.helix.rest.common.HttpConstants;
+import org.apache.helix.rest.server.resources.helix.AbstractHelixResource;
+import org.apache.helix.zookeeper.datamodel.ZNRecord;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Path("/locks")
+public class LockAccessor extends AbstractHelixResource {
+  private static final Logger LOG = LoggerFactory.getLogger(LockAccessor.class);
+  private static final String ZK_ADDR = "zkAddress";
+  private static final String LOCK_PATH = "lockPath";
+  private static final String LOCK_TIMEOUT = "lockTimeout";
+  private static final String USER_ID = "userId";
+  private static final String LOCK_MSG = "lockMsg";
+  private static final String LOCK_SCOPE_PROPERTY = "property";
+  private static final String PATH_KEYS = "pathKeys";
+  private static final String PATH_KEY_DELIMITER = "\\|";

Review comment:
       Let's create a constant class to hold them.

##########
File path: helix-rest/src/main/java/org/apache/helix/rest/server/resources/lock/LockAccessor.java
##########
@@ -0,0 +1,228 @@
+package org.apache.helix.rest.server.resources.lock;
+
+/*
+ * 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.
+ */
+
+import java.util.List;
+import java.util.Map;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Response;
+
+import com.codahale.metrics.annotation.ResponseMetered;
+import com.codahale.metrics.annotation.Timed;
+import com.google.common.collect.Lists;
+import org.apache.helix.HelixException;
+import org.apache.helix.lock.LockInfo;
+import org.apache.helix.lock.LockScope;
+import org.apache.helix.lock.helix.HelixLockScope;
+import org.apache.helix.lock.helix.ZKDistributedNonblockingLock;
+import org.apache.helix.rest.common.HttpConstants;
+import org.apache.helix.rest.server.resources.helix.AbstractHelixResource;
+import org.apache.helix.zookeeper.datamodel.ZNRecord;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Path("/locks")
+public class LockAccessor extends AbstractHelixResource {
+  private static final Logger LOG = LoggerFactory.getLogger(LockAccessor.class);
+  private static final String ZK_ADDR = "zkAddress";
+  private static final String LOCK_PATH = "lockPath";
+  private static final String LOCK_TIMEOUT = "lockTimeout";
+  private static final String USER_ID = "userId";
+  private static final String LOCK_MSG = "lockMsg";
+  private static final String LOCK_SCOPE_PROPERTY = "property";
+  private static final String PATH_KEYS = "pathKeys";
+  private static final String PATH_KEY_DELIMITER = "\\|";
+
+  private enum LockScopeKey {
+    HelixLockScope,
+  }
+
+  /**
+   *  A class contains information to locate a lock znode
+   */
+  private class LockLocator {
+    private final String _zkAddress;
+    private final String _lockPath;
+
+    private LockLocator(String zkAddress, String lockPath) {
+      this._zkAddress = zkAddress;
+      this._lockPath = lockPath;
+    }
+
+    public String getZkAddress() {
+      return _zkAddress;
+    }
+
+    public String getLockPath() {
+      return _lockPath;
+    }
+  }
+
+  @ResponseMetered(name = HttpConstants.WRITE_REQUEST)
+  @Timed(name = HttpConstants.WRITE_REQUEST)
+  @POST
+  @Path("lock")
+  public Response tryLock(String content) {
+    System.out.println("Payload:\n" + content);
+    ZKDistributedNonblockingLock lock;
+    LockLocator lockLocator;
+    LockInfo lockInfo;
+    try {
+      // Parse payload data
+      ZNRecord lockContent = toZNRecord(content);

Review comment:
       It's not a good idea to let input content to be ZNRecord. User has to input "simpleFields, listFields, mapFields" even they dont use it. I would suggest to use plain json format.




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



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