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/03 22:52:15 UTC
[GitHub] [helix] mgao0 opened a new pull request #1509: [WIP]Add tryLock REST API for distributed lock
mgao0 opened a new pull request #1509:
URL: https://github.com/apache/helix/pull/1509
### Issues
- [ ] My PR addresses the following Helix issues and references them in the PR description:
(#200 - Link your issue number here: You can write "Fixes #XXX". Please use the proper keyword so that the issue gets closed automatically. See https://docs.github.com/en/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue
Any of the following keywords can be used: close, closes, closed, fix, fixes, fixed, resolve, resolves, resolved)
### Description
- [ ] Here are some details about my PR, including screenshots of any UI changes:
(Write a concise description including what, why, how)
### Tests
- [ ] The following tests are written for this issue:
(List the names of added unit/integration tests)
- [ ] The following is the result of the "mvn test" command on the appropriate module:
(Before CI test pass, please copy & paste the result of "mvn test")
### Documentation (Optional)
- In case of new functionality, my PR adds documentation in the following wiki page:
(Link the GitHub wiki you added)
### Commits
- My commits all reference appropriate Apache Helix GitHub issues in their subject lines. In addition, my commits follow the guidelines from "[How to write a good git commit message](http://chris.beams.io/posts/git-commit/)":
1. Subject is separated from body by a blank line
1. Subject is limited to 50 characters (not including Jira issue reference)
1. Subject does not end with a period
1. Subject uses the imperative mood ("add", not "adding")
1. Body wraps at 72 characters
1. Body explains "what" and "why", not "how"
### Code Quality
- My diff has been formatted using helix-style.xml
(helix-style-intellij.xml if IntelliJ IDE is used)
----------------------------------------------------------------
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
[GitHub] [helix] zhangmeng916 commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
zhangmeng916 commented on a change in pull request #1509:
URL: https://github.com/apache/helix/pull/1509#discussion_r517721902
##########
File path: helix-rest/src/main/java/org/apache/helix/rest/common/ServletType.java
##########
@@ -20,32 +20,28 @@
*/
import org.apache.helix.rest.server.resources.helix.AbstractHelixResource;
+import org.apache.helix.rest.server.resources.lock.LockAccessor;
import org.apache.helix.rest.server.resources.metadata.NamespacesAccessor;
import org.apache.helix.rest.server.resources.metadatastore.MetadataStoreDirectoryAccessor;
import org.apache.helix.rest.server.resources.zookeeper.ZooKeeperAccessor;
-
public enum ServletType {
/**
* Servlet serving default API endpoints (/admin/v2/clusters/...)
*/
DEFAULT_SERVLET(HelixRestNamespace.DEFAULT_NAMESPACE_PATH_SPEC,
- new String[] {
- AbstractHelixResource.class.getPackage().getName(),
- NamespacesAccessor.class.getPackage().getName(),
- ZooKeeperAccessor.class.getPackage().getName(),
- MetadataStoreDirectoryAccessor.class.getPackage().getName()
- }),
+ new String[]{AbstractHelixResource.class.getPackage().getName(), NamespacesAccessor.class
Review comment:
The format is a bit weird. Can you revert them to previous one?
##########
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(
+ "Failed to lock the path %s using zk address %s, please check if this lock is held by someone else.",
+ lockLocator.getLockPath(), lockLocator.getZkAddress()));
+ }
+
+ private LockLocator parseLockLocator(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+
+ // Parse zk address from simple field
+ String zkAddr = znRecord.getSimpleField(ZK_ADDR);
+ if (zkAddr == null) {
+ throw new Exception("No ZK address is provided, could not create a lock.");
+ }
+
+ // Parse lock path from simple field
+ String lockPath = znRecord.getSimpleField(LOCK_PATH);
+
+ // If lock path is not provided, generate a lock path from lock scope
+ if (lockPath == null) {
+ Map<String, Map<String, String>> mapFields = znRecord.getMapFields();
+ LockScope lockScope = null;
+ for (LockScopeKey lockScopeKey : LockScopeKey.values()) {
+ // If a known lock scope is found, currently only HelixLockScope is available
+ if (mapFields.containsKey(lockScopeKey.name())) {
+ // Helix Lock Scope
+ if (lockScopeKey.name().equals(LockScopeKey.HelixLockScope.name())) {
+ lockScope = generateHelixLockScope(mapFields.get(lockScopeKey.name()));
+ }
+ }
+ }
+ if (lockScope == null) {
+ throw new Exception(
+ "Failed to parse a lock scope. Please check the lock scope property and path keys and make sure they are correct.");
+ }
+ return new LockLocator(zkAddr, lockScope.getPath());
+ }
+
+ return new LockLocator(zkAddr, lockPath);
+ }
+
+ private LockInfo parseLockInfo(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+ // Parse user iod
+ String userId = znRecord.getSimpleField(USER_ID);
+
+ // Parse lock timeout
+ long lockTimeout;
+ try {
+ lockTimeout = Long.parseLong(znRecord.getSimpleField(LOCK_TIMEOUT));
+ } catch (NumberFormatException e) {
+ throw new Exception("Please check the timeout value input, could not convert to long.");
+ }
+
+ // Parse lock message
+ String lockMsg = znRecord.getSimpleField(LOCK_MSG);
+ return new LockInfo(userId, lockMsg, lockTimeout);
+ }
+
+ private LockScope generateHelixLockScope(Map<String, String> lockScopeVal) throws Exception {
+ // Get the lock scope property: cluster, participant, resource, etc.
+ String CLUSTER = "cluster";
+ String RESOURCE = "resource";
+ String PARTICIPANT = "participant";
Review comment:
These should be defined as final static in the beginning too.
##########
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(
+ "Failed to lock the path %s using zk address %s, please check if this lock is held by someone else.",
+ lockLocator.getLockPath(), lockLocator.getZkAddress()));
+ }
+
+ private LockLocator parseLockLocator(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+
+ // Parse zk address from simple field
+ String zkAddr = znRecord.getSimpleField(ZK_ADDR);
+ if (zkAddr == null) {
+ throw new Exception("No ZK address is provided, could not create a lock.");
+ }
+
+ // Parse lock path from simple field
+ String lockPath = znRecord.getSimpleField(LOCK_PATH);
+
+ // If lock path is not provided, generate a lock path from lock scope
+ if (lockPath == null) {
+ Map<String, Map<String, String>> mapFields = znRecord.getMapFields();
+ LockScope lockScope = null;
+ for (LockScopeKey lockScopeKey : LockScopeKey.values()) {
+ // If a known lock scope is found, currently only HelixLockScope is available
+ if (mapFields.containsKey(lockScopeKey.name())) {
+ // Helix Lock Scope
+ if (lockScopeKey.name().equals(LockScopeKey.HelixLockScope.name())) {
+ lockScope = generateHelixLockScope(mapFields.get(lockScopeKey.name()));
+ }
+ }
+ }
+ if (lockScope == null) {
+ throw new Exception(
+ "Failed to parse a lock scope. Please check the lock scope property and path keys and make sure they are correct.");
+ }
+ return new LockLocator(zkAddr, lockScope.getPath());
+ }
+
+ return new LockLocator(zkAddr, lockPath);
+ }
+
+ private LockInfo parseLockInfo(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+ // Parse user iod
+ String userId = znRecord.getSimpleField(USER_ID);
+
+ // Parse lock timeout
+ long lockTimeout;
+ try {
+ lockTimeout = Long.parseLong(znRecord.getSimpleField(LOCK_TIMEOUT));
+ } catch (NumberFormatException e) {
+ throw new Exception("Please check the timeout value input, could not convert to long.");
+ }
+
+ // Parse lock message
Review comment:
There are a couple comments like this, which I think the code is self explanatory. You may just remove them for concise code.
##########
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:
This definition doesn't look right. btw, it is not used anywhere.
##########
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(
+ "Failed to lock the path %s using zk address %s, please check if this lock is held by someone else.",
+ lockLocator.getLockPath(), lockLocator.getZkAddress()));
+ }
+
+ private LockLocator parseLockLocator(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+
+ // Parse zk address from simple field
+ String zkAddr = znRecord.getSimpleField(ZK_ADDR);
+ if (zkAddr == null) {
+ throw new Exception("No ZK address is provided, could not create a lock.");
+ }
+
+ // Parse lock path from simple field
+ String lockPath = znRecord.getSimpleField(LOCK_PATH);
+
+ // If lock path is not provided, generate a lock path from lock scope
+ if (lockPath == null) {
+ Map<String, Map<String, String>> mapFields = znRecord.getMapFields();
+ LockScope lockScope = null;
+ for (LockScopeKey lockScopeKey : LockScopeKey.values()) {
+ // If a known lock scope is found, currently only HelixLockScope is available
+ if (mapFields.containsKey(lockScopeKey.name())) {
+ // Helix Lock Scope
+ if (lockScopeKey.name().equals(LockScopeKey.HelixLockScope.name())) {
+ lockScope = generateHelixLockScope(mapFields.get(lockScopeKey.name()));
+ }
+ }
+ }
+ if (lockScope == null) {
+ throw new Exception(
+ "Failed to parse a lock scope. Please check the lock scope property and path keys and make sure they are correct.");
+ }
+ return new LockLocator(zkAddr, lockScope.getPath());
+ }
+
+ return new LockLocator(zkAddr, lockPath);
+ }
+
+ private LockInfo parseLockInfo(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+ // Parse user iod
Review comment:
nit: typo
##########
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();
Review comment:
This reminds me a question from our users. They wondered whether they can distinguish between zk error and lock owned by others. From our current implementation of zkclient, I think the answer is no, they are both returned as zero. could you double check that part? If that's the case, you may want to comment somewhere.
##########
File path: helix-rest/src/test/java/org/apache/helix/rest/server/TestLockAccessor.java
##########
@@ -0,0 +1,118 @@
+package org.apache.helix.rest.server;
+
+/*
+ * 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.HashMap;
+import java.util.Map;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import org.apache.helix.TestHelper;
+import org.apache.helix.zookeeper.datamodel.ZNRecord;
+import org.testng.annotations.Test;
+
+public class TestLockAccessor extends AbstractTestClass {
+ private static final String TEST_CLUSTER = "TestCluster_0";
+ private static final String TEST_USER_ID = "testUser";
+ private static final String TEST_TIMEOUT = "100000";
+ private static final String TEST_URI = "locks/lock";
+ private static final String LOCK_TIMEOUT_STR = "lockTimeout";
+ private static final String USER_ID_STR = "userId";
+ private static final String ZK_ADDR_STR = "zkAddress";
+ private static final String LOCK_PATH_STR = "lockPath";
+ private static final String CLUSTER_NAME_STR = "cluster";
+ private static final String PARTICIPANT_NAME_STR = "participant";
+
+ @Test
+ public void testLockAccessorWithHelixLockScope() throws JsonProcessingException {
+ System.out.println("Start test :" + TestHelper.getTestMethodName());
+
+ Map<String, String> validHelixLockScope = new HashMap<String, String>() {{
+ put("property", "PARTICIPANT");
+ put(CLUSTER_NAME_STR, TEST_CLUSTER);
+ put(PARTICIPANT_NAME_STR, "testParticipant");
+ }};
+ Map<String, Map<String, String>> validMapFields = new HashMap<String, Map<String, String>>() {{
+ put("HelixLockScope", validHelixLockScope);
+ }};
+
+ // A valid lock scope is provided
+ ZNRecord validPayload = new ZNRecord("validPayload");
+ validPayload.setSimpleField(LOCK_TIMEOUT_STR, TEST_TIMEOUT);
+ validPayload.setSimpleField(USER_ID_STR, TEST_USER_ID);
+ validPayload.setSimpleField(ZK_ADDR_STR, ZK_ADDR);
+ validPayload.setMapFields(validMapFields);
+ Entity validEntity = Entity
+ .entity(OBJECT_MAPPER.writeValueAsString(validPayload), MediaType.APPLICATION_JSON_TYPE);
+ post(TEST_URI, null, Entity
+ .entity(validEntity, MediaType.APPLICATION_JSON_TYPE), Response.Status.OK.getStatusCode());
+
+ // Another valid lock scope, same path as the previous one. Try to lock while the lock is held by someone else.
+ validPayload.setSimpleField(USER_ID_STR, "testUser1");
+ post(TEST_URI, null, validEntity, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
+
+ // An invalid lock scope is provided
+ ZNRecord invalidLockScopePayload = new ZNRecord("invalidLockScopePayload");
+ invalidLockScopePayload.setSimpleField(LOCK_TIMEOUT_STR, TEST_TIMEOUT);
+ invalidLockScopePayload.setSimpleField(USER_ID_STR, TEST_USER_ID);
+ invalidLockScopePayload.setSimpleField(ZK_ADDR_STR, ZK_ADDR);
+ Map<String, String> invalidHelixLockScope = new HashMap<String, String>() {{
+ put("property", "PARTICIPANT");
+ put(CLUSTER_NAME_STR, TEST_CLUSTER);
+ }};
+ Map<String, Map<String, String>> invalidMapFields = new HashMap<String, Map<String, String>>() {{
+ put("HelixLockScope", invalidHelixLockScope);
+ }};
+ invalidLockScopePayload.setMapFields(invalidMapFields);
+ Entity invalidLockScopeEntity = Entity
+ .entity(OBJECT_MAPPER.writeValueAsString(invalidLockScopePayload),
+ MediaType.APPLICATION_JSON_TYPE);
+ post(TEST_URI, null, invalidLockScopeEntity, Response.Status.BAD_REQUEST.getStatusCode());
+
+ // No ZK address is provided
+ ZNRecord noZkAddrPayload = new ZNRecord("noZkAddrPayload");
+ noZkAddrPayload.setSimpleField(LOCK_TIMEOUT_STR, TEST_TIMEOUT);
+ noZkAddrPayload.setSimpleField(USER_ID_STR, TEST_USER_ID);
+ noZkAddrPayload.setMapFields(validMapFields);
+ Entity noZkAddrEntity = Entity
+ .entity(OBJECT_MAPPER.writeValueAsString(noZkAddrPayload), MediaType.APPLICATION_JSON_TYPE);
+ post(TEST_URI, null, noZkAddrEntity, Response.Status.BAD_REQUEST.getStatusCode());
+
+ System.out.println("End test :" + TestHelper.getTestMethodName());
+ }
+
+ @Test(dependsOnMethods = "testLockAccessorWithHelixLockScope")
+ public void testLockAccessorWithUnknownScope() throws JsonProcessingException {
Review comment:
nit: maybe rename it to general scope etc.
##########
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(
+ "Failed to lock the path %s using zk address %s, please check if this lock is held by someone else.",
+ lockLocator.getLockPath(), lockLocator.getZkAddress()));
+ }
+
+ private LockLocator parseLockLocator(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+
+ // Parse zk address from simple field
+ String zkAddr = znRecord.getSimpleField(ZK_ADDR);
+ if (zkAddr == null) {
+ throw new Exception("No ZK address is provided, could not create a lock.");
+ }
+
+ // Parse lock path from simple field
+ String lockPath = znRecord.getSimpleField(LOCK_PATH);
+
+ // If lock path is not provided, generate a lock path from lock scope
+ if (lockPath == null) {
+ Map<String, Map<String, String>> mapFields = znRecord.getMapFields();
+ LockScope lockScope = null;
+ for (LockScopeKey lockScopeKey : LockScopeKey.values()) {
+ // If a known lock scope is found, currently only HelixLockScope is available
+ if (mapFields.containsKey(lockScopeKey.name())) {
+ // Helix Lock Scope
+ if (lockScopeKey.name().equals(LockScopeKey.HelixLockScope.name())) {
+ lockScope = generateHelixLockScope(mapFields.get(lockScopeKey.name()));
+ }
+ }
+ }
+ if (lockScope == null) {
+ throw new Exception(
+ "Failed to parse a lock scope. Please check the lock scope property and path keys and make sure they are correct.");
+ }
+ return new LockLocator(zkAddr, lockScope.getPath());
+ }
+
+ return new LockLocator(zkAddr, lockPath);
+ }
+
+ private LockInfo parseLockInfo(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+ // Parse user iod
+ String userId = znRecord.getSimpleField(USER_ID);
+
+ // Parse lock timeout
+ long lockTimeout;
+ try {
+ lockTimeout = Long.parseLong(znRecord.getSimpleField(LOCK_TIMEOUT));
+ } catch (NumberFormatException e) {
+ throw new Exception("Please check the timeout value input, could not convert to long.");
+ }
+
+ // Parse lock message
+ String lockMsg = znRecord.getSimpleField(LOCK_MSG);
+ return new LockInfo(userId, lockMsg, lockTimeout);
+ }
+
+ private LockScope generateHelixLockScope(Map<String, String> lockScopeVal) throws Exception {
+ // Get the lock scope property: cluster, participant, resource, etc.
+ String CLUSTER = "cluster";
+ String RESOURCE = "resource";
+ String PARTICIPANT = "participant";
+ String lockScopeProperty = lockScopeVal.get(LOCK_SCOPE_PROPERTY);
+ HelixLockScope.LockScopeProperty property;
+ try {
+ property = HelixLockScope.LockScopeProperty.valueOf(lockScopeProperty);
+ } catch (IllegalArgumentException e) {
+ throw new Exception(String.format(
+ "User requested lock scope property %s, but this property does not exist in HelixLockScope.",
+ lockScopeProperty));
+ }
+
+ // Parse the path keys to construct the lock path
+ List<String> pathKeys = Lists.newArrayList();
+ switch (property) {
+ case CLUSTER: {
+ String clusterName = lockScopeVal.get(CLUSTER);
+ if (clusterName != null) {
+ pathKeys.add(clusterName);
+ }
+ }
+ break;
+ case RESOURCE: {
+ String clusterName = lockScopeVal.get(CLUSTER);
+ String resourceName = lockScopeVal.get(RESOURCE);
+ if (clusterName != null && resourceName != null) {
+ pathKeys.add(clusterName);
+ pathKeys.add(resourceName);
+ }
+ }
+ break;
+ case PARTICIPANT: {
+ String clusterName = lockScopeVal.get(CLUSTER);
+ String participantName = lockScopeVal.get(PARTICIPANT);
+ if (clusterName != null && participantName != null) {
+ pathKeys.add(clusterName);
+ pathKeys.add(participantName);
+ }
+ }
+ default:
Review comment:
Your previous try catch already excluded all other cases, so you don't need default here.
##########
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)
Review comment:
To be consistent with Java API, this one should be tryLock.
Actually I suggest to think over all APIs before finalizing a specific one. E.g. for unlocking a lock or getting lock information, etc. I feel unlock should be similar to this, as an action, instead of DELETE operation. For get lock information, what information will you provide to users?
----------------------------------------------------------------
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
[GitHub] [helix] narendly commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
narendly commented on a change in pull request #1509:
URL: https://github.com/apache/helix/pull/1509#discussion_r523830250
##########
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(
+ "Failed to lock the path %s using zk address %s, please check if this lock is held by someone else.",
+ lockLocator.getLockPath(), lockLocator.getZkAddress()));
+ }
+
+ private LockLocator parseLockLocator(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+
+ // Parse zk address from simple field
+ String zkAddr = znRecord.getSimpleField(ZK_ADDR);
+ if (zkAddr == null) {
+ throw new Exception("No ZK address is provided, could not create a lock.");
+ }
+
+ // Parse lock path from simple field
+ String lockPath = znRecord.getSimpleField(LOCK_PATH);
+
+ // If lock path is not provided, generate a lock path from lock scope
+ if (lockPath == null) {
+ Map<String, Map<String, String>> mapFields = znRecord.getMapFields();
+ LockScope lockScope = null;
+ for (LockScopeKey lockScopeKey : LockScopeKey.values()) {
+ // If a known lock scope is found, currently only HelixLockScope is available
+ if (mapFields.containsKey(lockScopeKey.name())) {
+ // Helix Lock Scope
+ if (lockScopeKey.name().equals(LockScopeKey.HelixLockScope.name())) {
+ lockScope = generateHelixLockScope(mapFields.get(lockScopeKey.name()));
+ }
+ }
+ }
+ if (lockScope == null) {
+ throw new Exception(
+ "Failed to parse a lock scope. Please check the lock scope property and path keys and make sure they are correct.");
+ }
+ return new LockLocator(zkAddr, lockScope.getPath());
+ }
+
+ return new LockLocator(zkAddr, lockPath);
+ }
+
+ private LockInfo parseLockInfo(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+ // Parse user iod
+ String userId = znRecord.getSimpleField(USER_ID);
+
+ // Parse lock timeout
+ long lockTimeout;
+ try {
+ lockTimeout = Long.parseLong(znRecord.getSimpleField(LOCK_TIMEOUT));
+ } catch (NumberFormatException e) {
+ throw new Exception("Please check the timeout value input, could not convert to long.");
+ }
+
+ // Parse lock message
+ String lockMsg = znRecord.getSimpleField(LOCK_MSG);
+ return new LockInfo(userId, lockMsg, lockTimeout);
+ }
+
+ private LockScope generateHelixLockScope(Map<String, String> lockScopeVal) throws Exception {
+ // Get the lock scope property: cluster, participant, resource, etc.
+ String CLUSTER = "cluster";
+ String RESOURCE = "resource";
+ String PARTICIPANT = "participant";
+ String lockScopeProperty = lockScopeVal.get(LOCK_SCOPE_PROPERTY);
+ HelixLockScope.LockScopeProperty property;
+ try {
+ property = HelixLockScope.LockScopeProperty.valueOf(lockScopeProperty);
+ } catch (IllegalArgumentException e) {
+ throw new Exception(String.format(
+ "User requested lock scope property %s, but this property does not exist in HelixLockScope.",
+ lockScopeProperty));
+ }
+
+ // Parse the path keys to construct the lock path
+ List<String> pathKeys = Lists.newArrayList();
+ switch (property) {
+ case CLUSTER: {
+ String clusterName = lockScopeVal.get(CLUSTER);
+ if (clusterName != null) {
+ pathKeys.add(clusterName);
+ }
+ }
+ break;
+ case RESOURCE: {
+ String clusterName = lockScopeVal.get(CLUSTER);
+ String resourceName = lockScopeVal.get(RESOURCE);
+ if (clusterName != null && resourceName != null) {
+ pathKeys.add(clusterName);
+ pathKeys.add(resourceName);
+ }
+ }
+ break;
+ case PARTICIPANT: {
+ String clusterName = lockScopeVal.get(CLUSTER);
+ String participantName = lockScopeVal.get(PARTICIPANT);
+ if (clusterName != null && participantName != null) {
+ pathKeys.add(clusterName);
+ pathKeys.add(participantName);
+ }
+ }
+ default:
+ // Not gonna happen
Review comment:
This doesn't look like a good example of a JavaDoc comment. Let's try to make it more descriptive or remove it altogether.
----------------------------------------------------------------
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
[GitHub] [helix] narendly commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
narendly commented on a change in pull request #1509:
URL: https://github.com/apache/helix/pull/1509#discussion_r523829571
##########
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(
+ "Failed to lock the path %s using zk address %s, please check if this lock is held by someone else.",
+ lockLocator.getLockPath(), lockLocator.getZkAddress()));
+ }
+
+ private LockLocator parseLockLocator(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+
+ // Parse zk address from simple field
+ String zkAddr = znRecord.getSimpleField(ZK_ADDR);
+ if (zkAddr == null) {
+ throw new Exception("No ZK address is provided, could not create a lock.");
+ }
+
+ // Parse lock path from simple field
+ String lockPath = znRecord.getSimpleField(LOCK_PATH);
+
+ // If lock path is not provided, generate a lock path from lock scope
+ if (lockPath == null) {
+ Map<String, Map<String, String>> mapFields = znRecord.getMapFields();
+ LockScope lockScope = null;
+ for (LockScopeKey lockScopeKey : LockScopeKey.values()) {
+ // If a known lock scope is found, currently only HelixLockScope is available
+ if (mapFields.containsKey(lockScopeKey.name())) {
+ // Helix Lock Scope
+ if (lockScopeKey.name().equals(LockScopeKey.HelixLockScope.name())) {
+ lockScope = generateHelixLockScope(mapFields.get(lockScopeKey.name()));
+ }
+ }
+ }
+ if (lockScope == null) {
+ throw new Exception(
+ "Failed to parse a lock scope. Please check the lock scope property and path keys and make sure they are correct.");
+ }
+ return new LockLocator(zkAddr, lockScope.getPath());
+ }
+
+ return new LockLocator(zkAddr, lockPath);
+ }
+
+ private LockInfo parseLockInfo(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
Review comment:
same - should we do a null check on znrecord in the beginning?
----------------------------------------------------------------
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
[GitHub] [helix] jiajunwang commented on pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
jiajunwang commented on pull request #1509:
URL: https://github.com/apache/helix/pull/1509#issuecomment-834665075
Close due to inactive.
--
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
[GitHub] [helix] narendly commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
narendly commented on a change in pull request #1509:
URL: https://github.com/apache/helix/pull/1509#discussion_r523829683
##########
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(
+ "Failed to lock the path %s using zk address %s, please check if this lock is held by someone else.",
+ lockLocator.getLockPath(), lockLocator.getZkAddress()));
+ }
+
+ private LockLocator parseLockLocator(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+
+ // Parse zk address from simple field
+ String zkAddr = znRecord.getSimpleField(ZK_ADDR);
+ if (zkAddr == null) {
+ throw new Exception("No ZK address is provided, could not create a lock.");
+ }
+
+ // Parse lock path from simple field
+ String lockPath = znRecord.getSimpleField(LOCK_PATH);
+
+ // If lock path is not provided, generate a lock path from lock scope
+ if (lockPath == null) {
+ Map<String, Map<String, String>> mapFields = znRecord.getMapFields();
+ LockScope lockScope = null;
+ for (LockScopeKey lockScopeKey : LockScopeKey.values()) {
+ // If a known lock scope is found, currently only HelixLockScope is available
+ if (mapFields.containsKey(lockScopeKey.name())) {
+ // Helix Lock Scope
+ if (lockScopeKey.name().equals(LockScopeKey.HelixLockScope.name())) {
+ lockScope = generateHelixLockScope(mapFields.get(lockScopeKey.name()));
+ }
+ }
+ }
+ if (lockScope == null) {
+ throw new Exception(
+ "Failed to parse a lock scope. Please check the lock scope property and path keys and make sure they are correct.");
+ }
+ return new LockLocator(zkAddr, lockScope.getPath());
+ }
+
+ return new LockLocator(zkAddr, lockPath);
+ }
+
+ private LockInfo parseLockInfo(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+ // Parse user iod
+ String userId = znRecord.getSimpleField(USER_ID);
+
+ // Parse lock timeout
+ long lockTimeout;
+ try {
+ lockTimeout = Long.parseLong(znRecord.getSimpleField(LOCK_TIMEOUT));
+ } catch (NumberFormatException e) {
+ throw new Exception("Please check the timeout value input, could not convert to long.");
Review comment:
let's let the caller know what the value is?
----------------------------------------------------------------
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
[GitHub] [helix] narendly commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
narendly commented on a change in pull request #1509:
URL: https://github.com/apache/helix/pull/1509#discussion_r523829029
##########
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();
Review comment:
Let's be careful here - what would the zk address be set as if the user was using it on multi-zk mode?
----------------------------------------------------------------
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
[GitHub] [helix] narendly commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
narendly commented on a change in pull request #1509:
URL: https://github.com/apache/helix/pull/1509#discussion_r523828869
##########
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;
Review comment:
Nit: probably better if we declare it as an interface?
----------------------------------------------------------------
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
[GitHub] [helix] narendly commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
narendly commented on a change in pull request #1509:
URL: https://github.com/apache/helix/pull/1509#discussion_r523828662
##########
File path: helix-rest/helix-rest-1.0.2-SNAPSHOT.ivy
##########
@@ -47,6 +47,7 @@ under the License.
<artifact name="snakeyaml" m:classifier="sources" ext="jar"/>
</dependency>
<dependency org="org.apache.helix" name="helix-core" rev="1.0.2-SNAPSHOT" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"/>
+ <dependency org="org.apache.helix" name="helix-lock" rev="1.0.2-SNAPSHOT" force="true" conf="compile->compile(*),master(*);runtime->runtime(*)"/>
Review comment:
Nit: fix indentation
----------------------------------------------------------------
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
[GitHub] [helix] narendly commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
narendly commented on a change in pull request #1509:
URL: https://github.com/apache/helix/pull/1509#discussion_r523829880
##########
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(
+ "Failed to lock the path %s using zk address %s, please check if this lock is held by someone else.",
+ lockLocator.getLockPath(), lockLocator.getZkAddress()));
+ }
+
+ private LockLocator parseLockLocator(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+
+ // Parse zk address from simple field
+ String zkAddr = znRecord.getSimpleField(ZK_ADDR);
+ if (zkAddr == null) {
+ throw new Exception("No ZK address is provided, could not create a lock.");
+ }
+
+ // Parse lock path from simple field
+ String lockPath = znRecord.getSimpleField(LOCK_PATH);
+
+ // If lock path is not provided, generate a lock path from lock scope
+ if (lockPath == null) {
+ Map<String, Map<String, String>> mapFields = znRecord.getMapFields();
+ LockScope lockScope = null;
+ for (LockScopeKey lockScopeKey : LockScopeKey.values()) {
+ // If a known lock scope is found, currently only HelixLockScope is available
+ if (mapFields.containsKey(lockScopeKey.name())) {
+ // Helix Lock Scope
+ if (lockScopeKey.name().equals(LockScopeKey.HelixLockScope.name())) {
+ lockScope = generateHelixLockScope(mapFields.get(lockScopeKey.name()));
+ }
+ }
+ }
+ if (lockScope == null) {
+ throw new Exception(
+ "Failed to parse a lock scope. Please check the lock scope property and path keys and make sure they are correct.");
+ }
+ return new LockLocator(zkAddr, lockScope.getPath());
+ }
+
+ return new LockLocator(zkAddr, lockPath);
+ }
+
+ private LockInfo parseLockInfo(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+ // Parse user iod
+ String userId = znRecord.getSimpleField(USER_ID);
+
+ // Parse lock timeout
+ long lockTimeout;
+ try {
+ lockTimeout = Long.parseLong(znRecord.getSimpleField(LOCK_TIMEOUT));
+ } catch (NumberFormatException e) {
+ throw new Exception("Please check the timeout value input, could not convert to long.");
+ }
+
+ // Parse lock message
+ String lockMsg = znRecord.getSimpleField(LOCK_MSG);
+ return new LockInfo(userId, lockMsg, lockTimeout);
+ }
+
+ private LockScope generateHelixLockScope(Map<String, String> lockScopeVal) throws Exception {
+ // Get the lock scope property: cluster, participant, resource, etc.
+ String CLUSTER = "cluster";
+ String RESOURCE = "resource";
+ String PARTICIPANT = "participant";
Review comment:
It looks like we already have the enum for it. why can't we just use the enum instead of defining new static constants?
Either add a method in the enum or just do a enum.getString().toLowerCase().
----------------------------------------------------------------
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
[GitHub] [helix] narendly commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@helix.apache.org
For additional commands, e-mail: reviews-help@helix.apache.org
[GitHub] [helix] narendly commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
narendly commented on a change in pull request #1509:
URL: https://github.com/apache/helix/pull/1509#discussion_r523830298
##########
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(
+ "Failed to lock the path %s using zk address %s, please check if this lock is held by someone else.",
+ lockLocator.getLockPath(), lockLocator.getZkAddress()));
+ }
+
+ private LockLocator parseLockLocator(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+
+ // Parse zk address from simple field
+ String zkAddr = znRecord.getSimpleField(ZK_ADDR);
+ if (zkAddr == null) {
+ throw new Exception("No ZK address is provided, could not create a lock.");
+ }
+
+ // Parse lock path from simple field
+ String lockPath = znRecord.getSimpleField(LOCK_PATH);
+
+ // If lock path is not provided, generate a lock path from lock scope
+ if (lockPath == null) {
+ Map<String, Map<String, String>> mapFields = znRecord.getMapFields();
+ LockScope lockScope = null;
+ for (LockScopeKey lockScopeKey : LockScopeKey.values()) {
+ // If a known lock scope is found, currently only HelixLockScope is available
+ if (mapFields.containsKey(lockScopeKey.name())) {
+ // Helix Lock Scope
+ if (lockScopeKey.name().equals(LockScopeKey.HelixLockScope.name())) {
+ lockScope = generateHelixLockScope(mapFields.get(lockScopeKey.name()));
+ }
+ }
+ }
+ if (lockScope == null) {
+ throw new Exception(
+ "Failed to parse a lock scope. Please check the lock scope property and path keys and make sure they are correct.");
+ }
+ return new LockLocator(zkAddr, lockScope.getPath());
+ }
+
+ return new LockLocator(zkAddr, lockPath);
+ }
+
+ private LockInfo parseLockInfo(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+ // Parse user iod
+ String userId = znRecord.getSimpleField(USER_ID);
+
+ // Parse lock timeout
+ long lockTimeout;
+ try {
+ lockTimeout = Long.parseLong(znRecord.getSimpleField(LOCK_TIMEOUT));
+ } catch (NumberFormatException e) {
+ throw new Exception("Please check the timeout value input, could not convert to long.");
+ }
+
+ // Parse lock message
+ String lockMsg = znRecord.getSimpleField(LOCK_MSG);
+ return new LockInfo(userId, lockMsg, lockTimeout);
+ }
+
+ private LockScope generateHelixLockScope(Map<String, String> lockScopeVal) throws Exception {
+ // Get the lock scope property: cluster, participant, resource, etc.
+ String CLUSTER = "cluster";
+ String RESOURCE = "resource";
+ String PARTICIPANT = "participant";
+ String lockScopeProperty = lockScopeVal.get(LOCK_SCOPE_PROPERTY);
+ HelixLockScope.LockScopeProperty property;
+ try {
+ property = HelixLockScope.LockScopeProperty.valueOf(lockScopeProperty);
+ } catch (IllegalArgumentException e) {
+ throw new Exception(String.format(
+ "User requested lock scope property %s, but this property does not exist in HelixLockScope.",
+ lockScopeProperty));
+ }
+
+ // Parse the path keys to construct the lock path
+ List<String> pathKeys = Lists.newArrayList();
+ switch (property) {
+ case CLUSTER: {
+ String clusterName = lockScopeVal.get(CLUSTER);
+ if (clusterName != null) {
+ pathKeys.add(clusterName);
+ }
+ }
+ break;
+ case RESOURCE: {
+ String clusterName = lockScopeVal.get(CLUSTER);
+ String resourceName = lockScopeVal.get(RESOURCE);
+ if (clusterName != null && resourceName != null) {
+ pathKeys.add(clusterName);
+ pathKeys.add(resourceName);
+ }
+ }
+ break;
+ case PARTICIPANT: {
+ String clusterName = lockScopeVal.get(CLUSTER);
+ String participantName = lockScopeVal.get(PARTICIPANT);
+ if (clusterName != null && participantName != null) {
+ pathKeys.add(clusterName);
+ pathKeys.add(participantName);
+ }
+ }
+ default:
+ // Not gonna happen
+ }
+ if (pathKeys.isEmpty()) {
+ throw new Exception(
+ "Not enough path keys are provided to construct the lock path. Please check the request.");
Review comment:
Let the user know what the request was. Also do we need to log it on the server side?
----------------------------------------------------------------
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
[GitHub] [helix] dasahcc commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
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
[GitHub] [helix] narendly commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
narendly commented on a change in pull request #1509:
URL: https://github.com/apache/helix/pull/1509#discussion_r523829452
##########
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(
+ "Failed to lock the path %s using zk address %s, please check if this lock is held by someone else.",
+ lockLocator.getLockPath(), lockLocator.getZkAddress()));
+ }
+
+ private LockLocator parseLockLocator(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
Review comment:
I'm not sure if this is a good pattern to use. Usually these functions expect a non-null values as parameters. Should you do a null check on the zn record before calling parseLockLocator()?
----------------------------------------------------------------
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
[GitHub] [helix] narendly commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
narendly commented on a change in pull request #1509:
URL: https://github.com/apache/helix/pull/1509#discussion_r523829880
##########
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(
+ "Failed to lock the path %s using zk address %s, please check if this lock is held by someone else.",
+ lockLocator.getLockPath(), lockLocator.getZkAddress()));
+ }
+
+ private LockLocator parseLockLocator(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+
+ // Parse zk address from simple field
+ String zkAddr = znRecord.getSimpleField(ZK_ADDR);
+ if (zkAddr == null) {
+ throw new Exception("No ZK address is provided, could not create a lock.");
+ }
+
+ // Parse lock path from simple field
+ String lockPath = znRecord.getSimpleField(LOCK_PATH);
+
+ // If lock path is not provided, generate a lock path from lock scope
+ if (lockPath == null) {
+ Map<String, Map<String, String>> mapFields = znRecord.getMapFields();
+ LockScope lockScope = null;
+ for (LockScopeKey lockScopeKey : LockScopeKey.values()) {
+ // If a known lock scope is found, currently only HelixLockScope is available
+ if (mapFields.containsKey(lockScopeKey.name())) {
+ // Helix Lock Scope
+ if (lockScopeKey.name().equals(LockScopeKey.HelixLockScope.name())) {
+ lockScope = generateHelixLockScope(mapFields.get(lockScopeKey.name()));
+ }
+ }
+ }
+ if (lockScope == null) {
+ throw new Exception(
+ "Failed to parse a lock scope. Please check the lock scope property and path keys and make sure they are correct.");
+ }
+ return new LockLocator(zkAddr, lockScope.getPath());
+ }
+
+ return new LockLocator(zkAddr, lockPath);
+ }
+
+ private LockInfo parseLockInfo(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+ // Parse user iod
+ String userId = znRecord.getSimpleField(USER_ID);
+
+ // Parse lock timeout
+ long lockTimeout;
+ try {
+ lockTimeout = Long.parseLong(znRecord.getSimpleField(LOCK_TIMEOUT));
+ } catch (NumberFormatException e) {
+ throw new Exception("Please check the timeout value input, could not convert to long.");
+ }
+
+ // Parse lock message
+ String lockMsg = znRecord.getSimpleField(LOCK_MSG);
+ return new LockInfo(userId, lockMsg, lockTimeout);
+ }
+
+ private LockScope generateHelixLockScope(Map<String, String> lockScopeVal) throws Exception {
+ // Get the lock scope property: cluster, participant, resource, etc.
+ String CLUSTER = "cluster";
+ String RESOURCE = "resource";
+ String PARTICIPANT = "participant";
Review comment:
It looks like we already have the enum for it. why can't we just use the enum instead of defining new static constants?
Either add a method in the enum that returns its lower-case equivalent or just do a enum.getString().toLowerCase().
----------------------------------------------------------------
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
[GitHub] [helix] narendly commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
narendly commented on a change in pull request #1509:
URL: https://github.com/apache/helix/pull/1509#discussion_r523829207
##########
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.");
Review comment:
1. We need to let the caller know what the request was. Consider embedding that information in the exception message.
2. Should we add a server-side exception log here 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:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@helix.apache.org
For additional commands, e-mail: reviews-help@helix.apache.org
[GitHub] [helix] jiajunwang closed pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
jiajunwang closed pull request #1509:
URL: https://github.com/apache/helix/pull/1509
--
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
[GitHub] [helix] narendly commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
narendly commented on a change in pull request #1509:
URL: https://github.com/apache/helix/pull/1509#discussion_r523829452
##########
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(
+ "Failed to lock the path %s using zk address %s, please check if this lock is held by someone else.",
+ lockLocator.getLockPath(), lockLocator.getZkAddress()));
+ }
+
+ private LockLocator parseLockLocator(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
Review comment:
I'm not sure if this is a good pattern to use. Usually these functions expect a non-null values as parameters. Should you do a null check on the zn record before calling parseLockLocator()? This is a debatable point, but maybe worth giving some consideration.
----------------------------------------------------------------
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
[GitHub] [helix] narendly commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
narendly commented on a change in pull request #1509:
URL: https://github.com/apache/helix/pull/1509#discussion_r523830139
##########
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(
+ "Failed to lock the path %s using zk address %s, please check if this lock is held by someone else.",
+ lockLocator.getLockPath(), lockLocator.getZkAddress()));
+ }
+
+ private LockLocator parseLockLocator(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+
+ // Parse zk address from simple field
+ String zkAddr = znRecord.getSimpleField(ZK_ADDR);
+ if (zkAddr == null) {
+ throw new Exception("No ZK address is provided, could not create a lock.");
+ }
+
+ // Parse lock path from simple field
+ String lockPath = znRecord.getSimpleField(LOCK_PATH);
+
+ // If lock path is not provided, generate a lock path from lock scope
+ if (lockPath == null) {
+ Map<String, Map<String, String>> mapFields = znRecord.getMapFields();
+ LockScope lockScope = null;
+ for (LockScopeKey lockScopeKey : LockScopeKey.values()) {
+ // If a known lock scope is found, currently only HelixLockScope is available
+ if (mapFields.containsKey(lockScopeKey.name())) {
+ // Helix Lock Scope
+ if (lockScopeKey.name().equals(LockScopeKey.HelixLockScope.name())) {
+ lockScope = generateHelixLockScope(mapFields.get(lockScopeKey.name()));
+ }
+ }
+ }
+ if (lockScope == null) {
+ throw new Exception(
+ "Failed to parse a lock scope. Please check the lock scope property and path keys and make sure they are correct.");
+ }
+ return new LockLocator(zkAddr, lockScope.getPath());
+ }
+
+ return new LockLocator(zkAddr, lockPath);
+ }
+
+ private LockInfo parseLockInfo(ZNRecord znRecord) throws Exception {
+ if (znRecord == null) {
+ return null;
+ }
+ // Parse user iod
+ String userId = znRecord.getSimpleField(USER_ID);
+
+ // Parse lock timeout
+ long lockTimeout;
+ try {
+ lockTimeout = Long.parseLong(znRecord.getSimpleField(LOCK_TIMEOUT));
+ } catch (NumberFormatException e) {
+ throw new Exception("Please check the timeout value input, could not convert to long.");
+ }
+
+ // Parse lock message
+ String lockMsg = znRecord.getSimpleField(LOCK_MSG);
+ return new LockInfo(userId, lockMsg, lockTimeout);
+ }
+
+ private LockScope generateHelixLockScope(Map<String, String> lockScopeVal) throws Exception {
+ // Get the lock scope property: cluster, participant, resource, etc.
+ String CLUSTER = "cluster";
+ String RESOURCE = "resource";
+ String PARTICIPANT = "participant";
+ String lockScopeProperty = lockScopeVal.get(LOCK_SCOPE_PROPERTY);
+ HelixLockScope.LockScopeProperty property;
+ try {
+ property = HelixLockScope.LockScopeProperty.valueOf(lockScopeProperty);
+ } catch (IllegalArgumentException e) {
+ throw new Exception(String.format(
+ "User requested lock scope property %s, but this property does not exist in HelixLockScope.",
+ lockScopeProperty));
+ }
Review comment:
I think there's a shorter way of doing this. Try googling? It looks like Enums.getIfPresent(Blah.class, "A")...
----------------------------------------------------------------
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
[GitHub] [helix] pkuwm commented on a change in pull request #1509: [WIP]Add tryLock REST API for distributed lock
Posted by GitBox <gi...@apache.org>.
pkuwm commented on a change in pull request #1509:
URL: https://github.com/apache/helix/pull/1509#discussion_r523699973
##########
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"?
Write
In the REST API design, we don't expect a verb to represent a resource in the endpoint: the method POST/PUT/DELETE already tells the purpose.
@mgao0 Is this request "idempotent"? If one calls this endpoint N times with the same payload, will there be N locks, or just 1 lock?
----------------------------------------------------------------
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