aboutsummaryrefslogtreecommitdiffstats
path: root/policy-core
diff options
context:
space:
mode:
Diffstat (limited to 'policy-core')
-rw-r--r--policy-core/src/main/java/org/onap/policy/drools/core/lock/PolicyResourceLockFeatureAPI.java3
-rw-r--r--policy-core/src/main/java/org/onap/policy/drools/core/lock/SimpleLockManager.java16
-rw-r--r--policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java4
3 files changed, 9 insertions, 14 deletions
diff --git a/policy-core/src/main/java/org/onap/policy/drools/core/lock/PolicyResourceLockFeatureAPI.java b/policy-core/src/main/java/org/onap/policy/drools/core/lock/PolicyResourceLockFeatureAPI.java
index ba6fe856..5aee490f 100644
--- a/policy-core/src/main/java/org/onap/policy/drools/core/lock/PolicyResourceLockFeatureAPI.java
+++ b/policy-core/src/main/java/org/onap/policy/drools/core/lock/PolicyResourceLockFeatureAPI.java
@@ -71,8 +71,7 @@ public interface PolicyResourceLockFeatureAPI extends OrderedService {
}
/**
- * This method is called before a lock is acquired on a resource. It may be
- * invoked repeatedly to extend the time that a lock is held.
+ * This method is called before a lock is acquired on a resource.
*
* @param resourceId
* @param owner
diff --git a/policy-core/src/main/java/org/onap/policy/drools/core/lock/SimpleLockManager.java b/policy-core/src/main/java/org/onap/policy/drools/core/lock/SimpleLockManager.java
index 081ad4c1..243ba3cb 100644
--- a/policy-core/src/main/java/org/onap/policy/drools/core/lock/SimpleLockManager.java
+++ b/policy-core/src/main/java/org/onap/policy/drools/core/lock/SimpleLockManager.java
@@ -74,7 +74,9 @@ public class SimpleLockManager {
}
/**
- * Attempts to lock a resource, extending the lock if the owner already holds it.
+ * Attempts to lock a resource, rejecting the lock if it is already owned, even if
+ * it's the same owner; the same owner can use {@link #refresh(String, String, int)
+ * refresh()}, instead, to extend a lock on a resource.
*
* @param resourceId
* @param owner
@@ -93,25 +95,19 @@ public class SimpleLockManager {
throw makeNullArgException(MSG_NULL_OWNER);
}
- Data existingLock;
+ boolean locked = false;
synchronized(locker) {
cleanUpLocks();
- if ((existingLock = resource2data.get(resourceId)) != null && existingLock.getOwner().equals(owner)) {
- // replace the existing lock with an extended lock
- locks.remove(existingLock);
- existingLock = null;
- }
-
- if (existingLock == null) {
+ if(!resource2data.containsKey(resourceId)) {
Data data = new Data(owner, resourceId, currentTime.getMillis() + TimeUnit.SECONDS.toMillis(holdSec));
resource2data.put(resourceId, data);
locks.add(data);
+ locked = true;
}
}
- boolean locked = (existingLock == null);
logger.info("lock {} for resource {} owner {}", locked, resourceId, owner);
return locked;
diff --git a/policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java b/policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java
index df6fb10a..feabdbc0 100644
--- a/policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java
+++ b/policy-core/src/test/java/org/onap/policy/drools/core/lock/SimpleLockManagerTest.java
@@ -116,7 +116,7 @@ public class SimpleLockManagerTest {
assertTrue(mgr.isLockedBy(RESOURCE_A, OWNER1));
// extend the lock
- mgr.lock(RESOURCE_A, OWNER1, MAX_AGE_SEC);
+ mgr.refresh(RESOURCE_A, OWNER1, MAX_AGE_SEC);
// verify still locked after sleeping the other half of the cycle
testTime.sleep(MAX_AGE_MS/2+1);
@@ -132,7 +132,7 @@ public class SimpleLockManagerTest {
mgr.lock(RESOURCE_A, OWNER1, MAX_AGE_SEC);
// same owner
- assertTrue(mgr.lock(RESOURCE_A, OWNER1, MAX_AGE_SEC));
+ assertFalse(mgr.lock(RESOURCE_A, OWNER1, MAX_AGE_SEC));
// different owner
assertFalse(mgr.lock(RESOURCE_A, OWNER2, MAX_AGE_SEC));