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



##########
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());

Review comment:
       Should we log this as well?




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