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:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]