summaryrefslogtreecommitdiffstats
path: root/feature-distributed-locking
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2018-07-12 17:47:09 -0400
committerJim Hahn <jrh3@att.com>2018-07-16 09:54:28 -0400
commit1480379b0e9907c0d41e8a782468c30f0ee83196 (patch)
treefbac13a23903776225b5ffe749e56007afd82263 /feature-distributed-locking
parentb65c92040c5acbf9013eccc5c9ce80dbd6587458 (diff)
Distinguish lock from refresh
This is the first step of separating the lock "refresh" operation from the original "lock" operation. This step entails adding the refresh() method to both the default and the feature-distriubted locking mechanisms. Change method call, in junit test, from lock to refresh. Change branch name in git review. Change-Id: I506de7a96cb3ee786839aca04ad67cdd7378832c Issue-ID: POLICY-872 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'feature-distributed-locking')
-rw-r--r--feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeature.java8
-rw-r--r--feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/TargetLock.java41
-rw-r--r--feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/TargetLockTest.java26
3 files changed, 75 insertions, 0 deletions
diff --git a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeature.java b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeature.java
index 88035ca7..f1c8b687 100644
--- a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeature.java
+++ b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingFeature.java
@@ -73,6 +73,14 @@ public class DistributedLockingFeature implements PolicyEngineFeatureAPI, Policy
return(tLock.lock(holdSec) ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED);
}
+
+ @Override
+ public OperResult beforeRefresh(String resourceId, String owner, int holdSec) {
+
+ TargetLock tLock = new TargetLock(resourceId, uuid, owner, dataSource);
+
+ return(tLock.refresh(holdSec) ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED);
+ }
@Override
public OperResult beforeUnlock(String resourceId, String owner) {
diff --git a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/TargetLock.java b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/TargetLock.java
index fe6f2fe0..1db34538 100644
--- a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/TargetLock.java
+++ b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/TargetLock.java
@@ -73,6 +73,17 @@ public class TargetLock {
return grabLock(holdSec);
}
+
+ /**
+ * refresh a lock
+ *
+ * @param holdSec the amount of time, in seconds, that the lock should be held
+ * @return {@code true} if the lock was refreshed, {@code false} if the resource is
+ * not currently locked by the given owner
+ */
+ public boolean refresh(int holdSec) {
+ return updateLock(holdSec);
+ }
/**
* Unlock a resource by deleting it's associated record in the db
@@ -158,6 +169,36 @@ public class TargetLock {
}
}
+
+ /**
+ * Updates the DB record associated with the lock.
+ *
+ * @param holdSec the amount of time, in seconds, that the lock should be held
+ * @return {@code true} if the record was updated, {@code false} otherwise
+ */
+ private boolean updateLock(int holdSec) {
+
+ try (Connection conn = dataSource.getConnection();
+
+ PreparedStatement updateStatement = conn.prepareStatement(
+ "UPDATE pooling.locks SET host = ?, owner = ?, expirationTime = timestampadd(second, ?, now()) WHERE resourceId = ? AND owner = ? AND expirationTime >= now()")) {
+
+ int i = 1;
+ updateStatement.setString(i++, this.uuid.toString());
+ updateStatement.setString(i++, this.owner);
+ updateStatement.setInt(i++, holdSec);
+ updateStatement.setString(i++, this.resourceId);
+ updateStatement.setString(i++, this.owner);
+
+ // refresh succeeded iff a record was updated
+ return (updateStatement.executeUpdate() == 1);
+
+ } catch (SQLException e) {
+ logger.error("error in TargetLock.refreshLock()", e);
+ return false;
+ }
+
+ }
/**
*To remove a lock we simply delete the record from the db
diff --git a/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/TargetLockTest.java b/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/TargetLockTest.java
index c1b46d67..6e33f224 100644
--- a/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/TargetLockTest.java
+++ b/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/TargetLockTest.java
@@ -119,6 +119,32 @@ public class TargetLockTest {
assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeLock("resource1", "owner2", MAX_AGE_SEC));
}
+
+ @Test
+ public void testUpdateLock() throws InterruptedException, ExecutionException {
+ // not locked yet - refresh should fail
+ assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeRefresh("resource1", "owner1", MAX_AGE_SEC));
+
+ // now lock it
+ assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeLock("resource1", "owner1", MAX_AGE_SEC));
+
+ // refresh should work now
+ assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeRefresh("resource1", "owner1", MAX_AGE_SEC));
+
+ // expire the lock
+ try (PreparedStatement updateStatement = conn.prepareStatement("UPDATE pooling.locks SET expirationTime = timestampadd(second, -1, now()) WHERE resourceId = ?");)
+ {
+ updateStatement.setString(1, "resource1");
+ updateStatement.executeUpdate();
+
+ } catch (SQLException e) {
+ logger.error("Error in TargetLockTest.testGrabLockSuccess()", e);
+ throw new RuntimeException(e);
+ }
+
+ // refresh should fail now
+ assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeRefresh("resource1", "owner1", MAX_AGE_SEC));
+ }
@Test