You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by jx...@apache.org on 2020/04/14 02:30:31 UTC
[helix] 18/19: Remove dependency of LockInfo on HelixProperty
This is an automated email from the ASF dual-hosted git repository.
jxue pushed a commit to branch distributed-lock
in repository https://gitbox.apache.org/repos/asf/helix.git
commit 1dc7e5932d0b9347af397308cf5b1cf6c6f834fc
Author: Molly Gao <mg...@mgao-mn1.linkedin.biz>
AuthorDate: Tue Feb 18 14:21:15 2020 -0800
Remove dependency of LockInfo on HelixProperty
---
.../main/java/org/apache/helix/lock/LockInfo.java | 29 ++++++++++------------
.../helix/lock/helix/ZKHelixNonblockingLock.java | 7 +++---
2 files changed, 17 insertions(+), 19 deletions(-)
diff --git a/helix-lock/src/main/java/org/apache/helix/lock/LockInfo.java b/helix-lock/src/main/java/org/apache/helix/lock/LockInfo.java
index 250fb71..2c9a24f 100644
--- a/helix-lock/src/main/java/org/apache/helix/lock/LockInfo.java
+++ b/helix-lock/src/main/java/org/apache/helix/lock/LockInfo.java
@@ -19,14 +19,13 @@
package org.apache.helix.lock;
-import org.apache.helix.HelixProperty;
import org.apache.helix.ZNRecord;
/**
* Structure represents a lock node information, implemented using ZNRecord
*/
-public class LockInfo extends HelixProperty {
+public class LockInfo {
// Default values for each attribute if there are no current values set by user
public static final String DEFAULT_OWNER_TEXT = "";
@@ -37,6 +36,8 @@ public class LockInfo extends HelixProperty {
public static final LockInfo defaultLockInfo =
new LockInfo(DEFAULT_OWNER_TEXT, DEFAULT_MESSAGE_TEXT, DEFAULT_TIMEOUT_LONG);
+ private ZNRecord record;
+
/**
* The keys to lock information
*/
@@ -48,8 +49,8 @@ public class LockInfo extends HelixProperty {
* Initialize a default LockInfo instance
*/
private LockInfo() {
- super("LOCK");
- setLockInfoFields(null, null, DEFAULT_TIMEOUT_LONG);
+ record = new ZNRecord("LOCK");
+ setLockInfoFields(DEFAULT_OWNER_TEXT, DEFAULT_MESSAGE_TEXT, DEFAULT_TIMEOUT_LONG);
}
/**
@@ -84,11 +85,11 @@ public class LockInfo extends HelixProperty {
* @param timeout value of TIMEOUT attribute
*/
private void setLockInfoFields(String ownerId, String message, long timeout) {
- _record.setSimpleField(LockInfoAttribute.OWNER.name(),
+ record.setSimpleField(LockInfoAttribute.OWNER.name(),
ownerId == null ? DEFAULT_OWNER_TEXT : ownerId);
- _record.setSimpleField(LockInfoAttribute.MESSAGE.name(),
+ record.setSimpleField(LockInfoAttribute.MESSAGE.name(),
message == null ? DEFAULT_MESSAGE_TEXT : message);
- _record.setLongField(LockInfoAttribute.TIMEOUT.name(), timeout);
+ record.setLongField(LockInfoAttribute.TIMEOUT.name(), timeout);
}
/**
@@ -96,7 +97,7 @@ public class LockInfo extends HelixProperty {
* @return the owner id of the lock, empty string if there is no owner id set
*/
public String getOwner() {
- String owner = _record.getSimpleField(LockInfoAttribute.OWNER.name());
+ String owner = record.getSimpleField(LockInfoAttribute.OWNER.name());
return owner == null ? DEFAULT_OWNER_TEXT : owner;
}
@@ -105,7 +106,7 @@ public class LockInfo extends HelixProperty {
* @return the message of the lock, empty string if there is no message set
*/
public String getMessage() {
- String message = _record.getSimpleField(LockInfoAttribute.MESSAGE.name());
+ String message = record.getSimpleField(LockInfoAttribute.MESSAGE.name());
return message == null ? DEFAULT_MESSAGE_TEXT : message;
}
@@ -114,14 +115,10 @@ public class LockInfo extends HelixProperty {
* @return the expiring time of the lock, -1 if there is no timeout set
*/
public Long getTimeout() {
- return _record.getLongField(LockInfoAttribute.TIMEOUT.name(), DEFAULT_TIMEOUT_LONG);
+ return record.getLongField(LockInfoAttribute.TIMEOUT.name(), DEFAULT_TIMEOUT_LONG);
}
- /**
- * Check if a lock has timed out
- * @return return true if the lock has timed out, otherwise return false.
- */
- public boolean hasNotExpired() {
- return System.currentTimeMillis() < getTimeout();
+ public ZNRecord getRecord() {
+ return record;
}
}
diff --git a/helix-lock/src/main/java/org/apache/helix/lock/helix/ZKHelixNonblockingLock.java b/helix-lock/src/main/java/org/apache/helix/lock/helix/ZKHelixNonblockingLock.java
index 41de4a7..cbfe9da 100644
--- a/helix-lock/src/main/java/org/apache/helix/lock/helix/ZKHelixNonblockingLock.java
+++ b/helix-lock/src/main/java/org/apache/helix/lock/helix/ZKHelixNonblockingLock.java
@@ -110,7 +110,8 @@ public class ZKHelixNonblockingLock implements HelixLock {
@Override
public boolean isCurrentOwner() {
LockInfo lockInfo = getCurrentLockInfo();
- return lockInfo.getOwner().equals(_userId) && lockInfo.hasNotExpired();
+ return lockInfo.getOwner().equals(_userId) && (System.currentTimeMillis() < lockInfo
+ .getTimeout());
}
/**
@@ -132,12 +133,12 @@ public class ZKHelixNonblockingLock implements HelixLock {
// If no one owns the lock, allow the update
// If the user is the current lock owner, allow the update
LockInfo curLockInfo = new LockInfo(current);
- if (!curLockInfo.hasNotExpired() || isCurrentOwner()) {
+ if (!(System.currentTimeMillis() < curLockInfo.getTimeout()) || isCurrentOwner()) {
return _record;
}
// For users who are not the lock owner and try to do an update on a lock that is held by someone else, exception thrown is to be caught by data accessor, and return false for the update
LOG.error(
- "User " + _userId + "tried to update the lock at " + new Date(System.currentTimeMillis())
+ "User " + _userId + " tried to update the lock at " + new Date(System.currentTimeMillis())
+ ". Lock path: " + _lockPath);
throw new HelixException("User is not authorized to perform this operation.");
}