diff options
Diffstat (limited to 'feature-distributed-locking')
4 files changed, 68 insertions, 65 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 3d19c873..5994beb6 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 @@ -79,31 +79,31 @@ public class DistributedLockingFeature implements PolicyEngineFeatureAPI, Policy @Override public Future<Boolean> beforeLock(String resourceId, String owner, Callback callback) { - TargetLock tLock = new TargetLock(resourceId, this.uuid, owner, lockProps); + TargetLock tLock = new TargetLock(resourceId, uuid, owner, lockProps); return new LockRequestFuture(resourceId, owner, tLock.lock()); } @Override - public Boolean beforeUnlock(String resourceId, String owner) { - TargetLock tLock = new TargetLock(resourceId, this.uuid, owner, lockProps); + public OperResult beforeUnlock(String resourceId, String owner) { + TargetLock tLock = new TargetLock(resourceId, uuid, owner, lockProps); - return tLock.unlock(); + return(tLock.unlock() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED); } @Override - public Boolean beforeIsLockedBy(String resourceId, String owner) { - TargetLock tLock = new TargetLock(resourceId, this.uuid, owner, lockProps); - - return tLock.isActive(); + public OperResult beforeIsLockedBy(String resourceId, String owner) { + TargetLock tLock = new TargetLock(resourceId, uuid, owner, lockProps); + + return(tLock.isActive() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED); } @Override - public Boolean beforeIsLocked(String resourceId) { - TargetLock tLock = new TargetLock(resourceId, this.uuid, "dummyOwner", lockProps); - - return tLock.isLocked(); + public OperResult beforeIsLocked(String resourceId) { + TargetLock tLock = new TargetLock(resourceId, uuid, "dummyOwner", lockProps); + + return(tLock.isLocked() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED); } @Override @@ -119,7 +119,7 @@ public class DistributedLockingFeature implements PolicyEngineFeatureAPI, Policy long heartbeatInterval = this.lockProps.getHeartBeatIntervalProperty(); cleanLockTable(); - heartbeat = new Heartbeat(this.uuid, lockProps); + heartbeat = new Heartbeat(uuid, lockProps); this.scheduledExecutorService = Executors.newScheduledThreadPool(1); this.scheduledExecutorService.scheduleAtFixedRate(heartbeat, heartbeatInterval, heartbeatInterval, TimeUnit.MILLISECONDS); @@ -148,7 +148,7 @@ public class DistributedLockingFeature implements PolicyEngineFeatureAPI, Policy PreparedStatement statement = conn.prepareStatement("DELETE FROM pooling.locks WHERE host = ? OR expirationTime < ?"); ){ - statement.setString(1, this.uuid.toString()); + statement.setString(1, uuid.toString()); statement.setLong(2, System.currentTimeMillis()); statement.executeUpdate(); diff --git a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingProperties.java b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingProperties.java index 139bfb7b..97ba3b10 100644 --- a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingProperties.java +++ b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockingProperties.java @@ -23,14 +23,10 @@ import java.util.Properties; import org.onap.policy.common.utils.properties.PropertyConfiguration; import org.onap.policy.common.utils.properties.exception.PropertyException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class DistributedLockingProperties extends PropertyConfiguration{ - private static final Logger logger = LoggerFactory.getLogger(DistributedLockingProperties.class); - /** * Feature properties all begin with this prefix. */ 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 ceaa849f..4f09dc2a 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 @@ -88,19 +88,21 @@ public class TargetLock { */ private boolean grabLock() { + // try to insert a record into the table(thereby grabbing the lock) try (Connection conn = DriverManager.getConnection(lockProps.getDbUrl(), lockProps.getDbUser(), lockProps.getDbPwd()); - // try to insert a record into the table(thereby grabbing the lock) - PreparedStatement statement = conn - .prepareStatement("INSERT INTO pooling.locks (resourceId, host, owner, expirationTime) values (?, ?, ?, ?)");) { + PreparedStatement statement = conn.prepareStatement( + "INSERT INTO pooling.locks (resourceId, host, owner, expirationTime) values (?, ?, ?, ?)")) { + statement.setString(1, this.resourceId); statement.setString(2, this.uuid.toString()); statement.setString(3, this.owner); statement.setLong(4, System.currentTimeMillis() + lockProps.getAgingProperty()); - statement.executeUpdate(); - } catch (SQLException e) { + } + + catch (SQLException e) { logger.error("error in TargetLock.grabLock()", e); return secondGrab(); } @@ -117,9 +119,11 @@ public class TargetLock { try (Connection conn = DriverManager.getConnection(lockProps.getDbUrl(), lockProps.getDbUser(), lockProps.getDbPwd()); - PreparedStatement updateStatement = conn.prepareStatement("UPDATE pooling.locks SET host = ?, owner = ?, expirationTime = ? WHERE expirationTime <= ? AND resourceId = ?"); - - PreparedStatement insertStatement = conn.prepareStatement("INSERT INTO pooling.locks (resourceId, host, owner, expirationTime) values (?, ?, ?, ?)");) { + PreparedStatement updateStatement = conn.prepareStatement( + "UPDATE pooling.locks SET host = ?, owner = ?, expirationTime = ? WHERE expirationTime <= ? AND resourceId = ?"); + + PreparedStatement insertStatement = conn.prepareStatement( + "INSERT INTO pooling.locks (resourceId, host, owner, expirationTime) values (?, ?, ?, ?)");) { updateStatement.setString(1, this.uuid.toString()); updateStatement.setString(2, this.owner); @@ -132,6 +136,7 @@ public class TargetLock { if (updateStatement.executeUpdate() == 1) { return true; } + // If our update does not return 1 row, the lock either has not expired // or it was removed. Try one last grab else { @@ -140,7 +145,7 @@ public class TargetLock { insertStatement.setString(3, this.owner); insertStatement.setLong(4, System.currentTimeMillis() + lockProps.getAgingProperty()); - // If our insert returns 1 we successfully grabbed the lock + // If our insert returns 1 we successfully grabbed the lock return (insertStatement.executeUpdate() == 1); } @@ -159,13 +164,13 @@ public class TargetLock { try (Connection conn = DriverManager.getConnection(lockProps.getDbUrl(), lockProps.getDbUser(), lockProps.getDbPwd()); - PreparedStatement deleteStatement = conn - .prepareStatement("DELETE FROM pooling.locks WHERE resourceId = ? AND owner = ? AND host = ?");) { + PreparedStatement deleteStatement = conn.prepareStatement( + "DELETE FROM pooling.locks WHERE resourceId = ? AND owner = ? AND host = ?")) { deleteStatement.setString(1, this.resourceId); deleteStatement.setString(2, this.owner); deleteStatement.setString(3, this.uuid.toString()); - + return (deleteStatement.executeUpdate() == 1); } catch (SQLException e) { @@ -174,34 +179,35 @@ public class TargetLock { } } - + /** * Is the lock active */ public boolean isActive() { - try (Connection conn = DriverManager.getConnection(lockProps.getDbUrl(), lockProps.getDbUser(), lockProps.getDbPwd()); - PreparedStatement selectStatement = conn - .prepareStatement("SELECT * FROM pooling.locks WHERE resourceId = ? AND host = ? AND owner= ? AND expirationTime >= ?");) { - { - selectStatement.setString(1, this.resourceId); - selectStatement.setString(2, this.uuid.toString()); - selectStatement.setString(3, this.owner); - selectStatement.setLong(4, System.currentTimeMillis()); + PreparedStatement selectStatement = conn.prepareStatement( + "SELECT * FROM pooling.locks WHERE resourceId = ? AND host = ? AND owner= ? AND expirationTime >= ?")) { - ResultSet result = selectStatement.executeQuery(); + selectStatement.setString(1, this.resourceId); + selectStatement.setString(2, this.uuid.toString()); + selectStatement.setString(3, this.owner); + selectStatement.setLong(4, System.currentTimeMillis()); + try (ResultSet result = selectStatement.executeQuery()) { // This will return true if the // query returned at least one row return result.first(); - } - } catch (SQLException e) { + + } + + catch (SQLException e) { logger.error("error in TargetLock.isActive()", e); return false; } + } /** @@ -211,20 +217,20 @@ public class TargetLock { try (Connection conn = DriverManager.getConnection(lockProps.getDbUrl(), lockProps.getDbUser(), lockProps.getDbPwd()); - + PreparedStatement selectStatement = conn - .prepareStatement("SELECT * FROM pooling.locks WHERE resourceId = ? AND expirationTime >= ?");) { - { - selectStatement.setString(1, this.resourceId); - selectStatement.setLong(2, System.currentTimeMillis()); - ResultSet result = selectStatement.executeQuery(); + .prepareStatement("SELECT * FROM pooling.locks WHERE resourceId = ? AND expirationTime >= ?")) { + selectStatement.setString(1, this.resourceId); + selectStatement.setLong(2, System.currentTimeMillis()); + try (ResultSet result = selectStatement.executeQuery()) { // This will return true if the // query returned at least one row return result.first(); - } - } catch (SQLException e) { + } + + catch (SQLException e) { logger.error("error in TargetLock.isActive()", e); return false; } 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 2dc2ceb8..b01d9676 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 @@ -25,10 +25,11 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.distributed.locking.DistributedLockingFeature; +import org.onap.policy.drools.core.lock.PolicyResourceLockFeatureAPI.OperResult; import org.onap.policy.drools.persistence.SystemPersistence; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -115,7 +116,7 @@ public class TargetLockTest { } // Grab reference to heartbeat object - Heartbeat heartbeat = distLockFeat.getHeartbeat(); + Heartbeat heartbeat = DistributedLockingFeature.getHeartbeat(); // Pass heartbeat object countdown latch try { @@ -149,16 +150,16 @@ public class TargetLockTest { public void testUnlock() throws InterruptedException, ExecutionException { distLockFeat.beforeLock("resource1", "owner1", null); - assertTrue(distLockFeat.beforeUnlock("resource1", "owner1")); + assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeUnlock("resource1", "owner1")); assertTrue(distLockFeat.beforeLock("resource1", "owner1", null).get()); } @Test public void testIsActive() { - assertFalse(distLockFeat.beforeIsLockedBy("resource1", "owner1")); + assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLockedBy("resource1", "owner1")); distLockFeat.beforeLock("resource1", "owner1", null); - assertTrue(distLockFeat.beforeIsLockedBy("resource1", "owner1")); - assertFalse(distLockFeat.beforeIsLockedBy("resource1", "owner2")); + assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeIsLockedBy("resource1", "owner1")); + assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLockedBy("resource1", "owner2")); // isActive on expiredLock try (PreparedStatement updateStatement = conn @@ -172,12 +173,12 @@ public class TargetLockTest { throw new RuntimeException(e); } - assertFalse(distLockFeat.beforeIsLockedBy("resource1", "owner1")); + assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLockedBy("resource1", "owner1")); distLockFeat.beforeLock("resource1", "owner1", null); //Unlock record, next isActive attempt should fail distLockFeat.beforeUnlock("resource1", "owner1"); - assertFalse(distLockFeat.beforeIsLockedBy("resource1", "owner1")); + assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLockedBy("resource1", "owner1")); } @@ -198,7 +199,7 @@ public class TargetLockTest { } //Grab reference to heartbeat object - Heartbeat heartbeat = distLockFeat.getHeartbeat(); + Heartbeat heartbeat = DistributedLockingFeature.getHeartbeat(); //Pass heartbeat object countdown latch try { @@ -210,22 +211,22 @@ public class TargetLockTest { //At this point the heartbeat object should hve //refreshed the lock. assert that resource1 is //locked - assertTrue(distLockFeat.beforeIsLocked("resource1")); + assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeIsLocked("resource1")); } @Test public void unlockBeforeLock() { - assertFalse(distLockFeat.beforeUnlock("resource1", "owner1")); + assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeUnlock("resource1", "owner1")); distLockFeat.beforeLock("resource1", "owner1", null); - assertTrue(distLockFeat.beforeUnlock("resource1", "owner1")); - assertFalse(distLockFeat.beforeUnlock("resource1", "owner1")); + assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeUnlock("resource1", "owner1")); + assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeUnlock("resource1", "owner1")); } @Test public void testIsLocked() { - assertFalse(distLockFeat.beforeIsLocked("resource1")); + assertEquals(OperResult.OPER_DENIED, distLockFeat.beforeIsLocked("resource1")); distLockFeat.beforeLock("resource1", "owner1", null); - assertTrue(distLockFeat.beforeIsLocked("resource1")); + assertEquals(OperResult.OPER_ACCEPTED, distLockFeat.beforeIsLocked("resource1")); } |