summaryrefslogtreecommitdiffstats
path: root/feature-distributed-locking
diff options
context:
space:
mode:
Diffstat (limited to 'feature-distributed-locking')
-rw-r--r--feature-distributed-locking/src/main/feature/db/pooling/sql/1804-distributedlocking.upgrade.sql17
-rw-r--r--feature-distributed-locking/src/main/feature/db/pooling/sql/1811-distributedlocking.upgrade.sql6
-rw-r--r--feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockManager.java32
-rw-r--r--feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java27
4 files changed, 46 insertions, 36 deletions
diff --git a/feature-distributed-locking/src/main/feature/db/pooling/sql/1804-distributedlocking.upgrade.sql b/feature-distributed-locking/src/main/feature/db/pooling/sql/1804-distributedlocking.upgrade.sql
index be56d35e..07b30738 100644
--- a/feature-distributed-locking/src/main/feature/db/pooling/sql/1804-distributedlocking.upgrade.sql
+++ b/feature-distributed-locking/src/main/feature/db/pooling/sql/1804-distributedlocking.upgrade.sql
@@ -1,7 +1,7 @@
# ============LICENSE_START=======================================================
# feature-distributed-locking
# ================================================================================
-# Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+# Copyright (C) 2018, 2022 AT&T Intellectual Property. All rights reserved.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -16,8 +16,15 @@
# limitations under the License.
# ============LICENSE_END=========================================================
- set foreign_key_checks=0;
-
- CREATE TABLE if not exists pooling.locks (resourceId VARCHAR(128), host VARCHAR(128), owner VARCHAR(128), expirationTime BIGINT, PRIMARY KEY (resourceId), INDEX idx_expirationTime(expirationTime), INDEX idx_host(host));
+SET foreign_key_checks=0;
- set foreign_key_checks=1; \ No newline at end of file
+CREATE TABLE if NOT EXISTS pooling.locks(
+ resourceId VARCHAR(128),
+ host VARCHAR(128),
+ owner VARCHAR(128),
+ expirationTime BIGINT,
+ PRIMARY KEY (resourceId),
+ INDEX idx_expirationTime(expirationTime),
+ INDEX idx_host(host));
+
+SET foreign_key_checks=1; \ No newline at end of file
diff --git a/feature-distributed-locking/src/main/feature/db/pooling/sql/1811-distributedlocking.upgrade.sql b/feature-distributed-locking/src/main/feature/db/pooling/sql/1811-distributedlocking.upgrade.sql
index 847c44ac..0cdad5b9 100644
--- a/feature-distributed-locking/src/main/feature/db/pooling/sql/1811-distributedlocking.upgrade.sql
+++ b/feature-distributed-locking/src/main/feature/db/pooling/sql/1811-distributedlocking.upgrade.sql
@@ -16,8 +16,8 @@
# limitations under the License.
# ============LICENSE_END=========================================================
- set foreign_key_checks=0;
+SET foreign_key_checks=0;
- ALTER TABLE pooling.locks modify expirationTime timestamp default '1971-01-01 00:00:00.000000';
+ALTER TABLE pooling.locks MODIFY expirationTime TIMESTAMP DEFAULT '1971-01-01 00:00:00.000000';
- set foreign_key_checks=1;
+SET foreign_key_checks=1;
diff --git a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockManager.java b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockManager.java
index 04138eed..198b3303 100644
--- a/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockManager.java
+++ b/feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockManager.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -87,17 +87,16 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
@Setter(AccessLevel.PROTECTED)
private static DistributedLockManager latestInstance = null;
-
/**
* Name of the host on which this JVM is running.
*/
- @Getter
- private final String pdpName;
+ @Getter(AccessLevel.PUBLIC)
+ private String pdpName;
/**
* UUID of this object.
*/
- @Getter
+ @Getter(AccessLevel.PUBLIC)
private final String uuidString = UUID.randomUUID().toString();
/**
@@ -133,7 +132,6 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
* Constructs the object.
*/
public DistributedLockManager() {
- this.pdpName = PolicyEngineConstants.PDP_NAME;
this.resource2lock = getResource2lock();
}
@@ -146,6 +144,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
public PolicyResourceLockManager beforeCreateLockManager(PolicyEngine engine, Properties properties) {
try {
+ this.pdpName = PolicyEngineConstants.getManager().getPdpName();
this.featProps = new DistributedLockProperties(getProperties(CONFIGURATION_PROPERTIES_NAME));
this.dataSource = makeDataSource();
@@ -256,8 +255,9 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
*/
private void checkExpired() {
try {
- logger.info("checking for expired locks");
Set<String> expiredIds = new HashSet<>(resource2lock.keySet());
+ logger.info("checking for expired locks: {}", this);
+
identifyDbLocks(expiredIds);
expireLocks(expiredIds);
@@ -337,7 +337,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
DistributedLock lock = lockref.get();
if (lock != null) {
- logger.debug("removed lock from map {}", lock);
+ logger.info("lost lock: removed lock from map {}", lock);
lock.deny(FeatureLockImpl.LOCK_LOST_MSG);
}
}
@@ -704,7 +704,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
* @throws SQLException if a DB error occurs
*/
protected boolean doDbInsert(Connection conn) throws SQLException {
- logger.debug("insert lock record {}", this);
+ logger.info("insert lock record {}", this);
try (var stmt = conn.prepareStatement("INSERT INTO pooling.locks (resourceId, host, owner, expirationTime) "
+ "values (?, ?, ?, timestampadd(second, ?, now()))")) {
@@ -731,7 +731,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
* @throws SQLException if a DB error occurs
*/
protected boolean doDbUpdate(Connection conn) throws SQLException {
- logger.debug("update lock record {}", this);
+ logger.info("update lock record {}", this);
try (var stmt = conn.prepareStatement("UPDATE pooling.locks SET resourceId=?, host=?, owner=?,"
+ " expirationTime=timestampadd(second, ?, now()) WHERE resourceId=?"
+ " AND ((host=? AND owner=?) OR expirationTime < now())")) {
@@ -763,7 +763,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
* @throws SQLException if a DB error occurs
*/
protected void doDbDelete(Connection conn) throws SQLException {
- logger.debug("delete lock record {}", this);
+ logger.info("delete lock record {}", this);
try (var stmt = conn
.prepareStatement("DELETE FROM pooling.locks WHERE resourceId=? AND host=? AND owner=?")) {
@@ -780,7 +780,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
* thread.
*/
private void removeFromMap() {
- logger.debug("remove lock from map {}", this);
+ logger.info("remove lock from map {}", this);
feature.resource2lock.remove(getResourceId(), this);
synchronized (this) {
@@ -799,10 +799,16 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
}
@FunctionalInterface
- private static interface RunnableWithEx {
+ private interface RunnableWithEx {
void run() throws SQLException;
}
+ @Override
+ public String toString() {
+ return "DistributedLockManager [" + "pdpName=" + pdpName + ", uuidString=" + uuidString
+ + ", resource2lock=" + resource2lock + "]";
+ }
+
// these may be overridden by junit tests
protected Properties getProperties(String fileName) {
diff --git a/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java b/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java
index 2fbdb26b..082a6199 100644
--- a/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java
+++ b/feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -156,6 +156,7 @@ public class DistributedLockManagerTest {
@BeforeClass
public static void setUpBeforeClass() throws SQLException {
SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
+ PolicyEngineConstants.getManager().configure(new Properties());
conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
@@ -311,10 +312,9 @@ public class DistributedLockManagerTest {
/**
* Tests deleteExpiredDbLocks(), when getConnection() throws an exception.
*
- * @throws SQLException if an error occurs
*/
@Test
- public void testDeleteExpiredDbLocksEx() throws SQLException {
+ public void testDeleteExpiredDbLocksEx() {
feature = new InvalidDbLockingFeature(TRANSIENT);
// get the clean-up function and execute it
@@ -342,16 +342,15 @@ public class DistributedLockManagerTest {
/**
* Tests afterStop(), when the data source throws an exception when close() is called.
*
- * @throws SQLException if an error occurs
*/
@Test
- public void testAfterStopEx() throws SQLException {
+ public void testAfterStopEx() {
shutdownFeature();
// use a data source that throws an exception when closed
feature = new InvalidDbLockingFeature(TRANSIENT);
- assertThatCode(() -> shutdownFeature()).doesNotThrowAnyException();
+ assertThatCode(this::shutdownFeature).doesNotThrowAnyException();
}
@Test
@@ -877,7 +876,7 @@ public class DistributedLockManagerTest {
}
@Test
- public void testDistributedLockRescheduleRequest() throws SQLException {
+ public void testDistributedLockRescheduleRequest() {
// use a data source that throws an exception when getConnection() is called
InvalidDbLockingFeature invfeat = new InvalidDbLockingFeature(TRANSIENT);
feature = invfeat;
@@ -937,7 +936,7 @@ public class DistributedLockManagerTest {
}
@Test
- public void testDistributedLockDoRequest() throws SQLException {
+ public void testDistributedLockDoRequest() {
lock = getLock(RESOURCE, OWNER_KEY, HOLD_SEC, callback, false);
assertTrue(lock.isWaiting());
@@ -1019,7 +1018,7 @@ public class DistributedLockManagerTest {
// use a data source that throws an exception when getConnection() is called
feature = new MyLockingFeature(true) {
@Override
- protected BasicDataSource makeDataSource() throws Exception {
+ protected BasicDataSource makeDataSource() {
return datasrc;
}
};
@@ -1052,7 +1051,7 @@ public class DistributedLockManagerTest {
// use a data source that throws an exception when getConnection() is called
feature = new MyLockingFeature(true) {
@Override
- protected BasicDataSource makeDataSource() throws Exception {
+ protected BasicDataSource makeDataSource() {
return datasrc;
}
};
@@ -1232,10 +1231,9 @@ public class DistributedLockManagerTest {
/**
* Tests doUnlock() when a DB exception is thrown.
*
- * @throws SQLException if an error occurs
*/
@Test
- public void testDistributedLockDoUnlockEx() throws SQLException {
+ public void testDistributedLockDoUnlockEx() {
feature = new InvalidDbLockingFeature(PERMANENT);
lock = getLock(RESOURCE, OWNER_KEY, HOLD_SEC, callback, false);
@@ -1338,10 +1336,9 @@ public class DistributedLockManagerTest {
/**
* Tests doExtend() when both update and insert fail.
*
- * @throws SQLException if an error occurs
*/
@Test
- public void testDistributedLockDoExtendNeitherSucceeds() throws SQLException {
+ public void testDistributedLockDoExtendNeitherSucceeds() {
/*
* this feature will create a lock that returns false when doDbUpdate() is
* invoked, or when doDbInsert() is invoked a second time
@@ -1441,7 +1438,7 @@ public class DistributedLockManagerTest {
feature.beforeCreateLockManager(engine, new Properties());
feature.afterStart(engine);
- assertThatCode(() -> shutdownFeature()).doesNotThrowAnyException();
+ assertThatCode(this::shutdownFeature).doesNotThrowAnyException();
}
/**