summaryrefslogtreecommitdiffstats
path: root/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src
diff options
context:
space:
mode:
Diffstat (limited to 'appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src')
-rw-r--r--appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/inmemory/LockManagerInMemoryImpl.java9
-rw-r--r--appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/JdbcLockManager.java3
-rw-r--r--appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/Messages.java2
-rw-r--r--appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/MySqlConnectionFactory.java4
-rw-r--r--appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/optimistic/LockRecord.java70
-rw-r--r--appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/optimistic/MySqlLockManager.java10
-rw-r--r--appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/optimistic/SqlLockManager.java444
-rw-r--r--appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/pessimistic/LockRecord.java56
-rw-r--r--appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/pessimistic/MySqlLockManager.java94
-rw-r--r--appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/pessimistic/SqlLockManager.java451
-rw-r--r--appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/api/LockManagerBaseTests.java264
-rw-r--r--appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/MySqlLockManagerBaseTests.java98
-rw-r--r--appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/optimistic/MySqlLockManagerMock.java178
-rw-r--r--appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/optimistic/TestMySqlLockManager.java384
-rw-r--r--appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/pessimistic/MySqlLockManagerMock.java200
-rw-r--r--appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/pessimistic/TestMySqlLockManager.java18
16 files changed, 1178 insertions, 1107 deletions
diff --git a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/inmemory/LockManagerInMemoryImpl.java b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/inmemory/LockManagerInMemoryImpl.java
index b141d9607..140d9ebbc 100644
--- a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/inmemory/LockManagerInMemoryImpl.java
+++ b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/inmemory/LockManagerInMemoryImpl.java
@@ -38,7 +38,7 @@ public class LockManagerInMemoryImpl implements LockManager {
private static LockManagerInMemoryImpl instance = null;
private Map<String, LockValue> lockedVNFs;
- private static final EELFLogger debugLogger = EELFManager.getInstance().getDebugLogger();
+ private final EELFLogger debugLogger = EELFManager.getInstance().getDebugLogger();
private LockManagerInMemoryImpl() {
lockedVNFs = new ConcurrentHashMap<>();
@@ -110,7 +110,12 @@ public class LockManagerInMemoryImpl implements LockManager {
@Override
public boolean isLocked(String resource) {
- return lockedVNFs.get(resource)!=null?true:false;
+ return lockedVNFs.get(resource) != null;
+ }
+
+ @Override
+ public String getLockOwner(String resource) {
+ return lockedVNFs.get(resource).getOwner();
}
private boolean lockIsMine(LockValue lockValue, String owner, long now) {
diff --git a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/JdbcLockManager.java b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/JdbcLockManager.java
index 9b2083101..b8cf0f741 100644
--- a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/JdbcLockManager.java
+++ b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/JdbcLockManager.java
@@ -26,7 +26,8 @@ package org.onap.appc.lockmanager.impl.sql;
import java.sql.Connection;
-import org.onap.appc.dao.util.JdbcConnectionFactory;
+
+import org.onap.appc.dao.util.api.JdbcConnectionFactory;
import org.onap.appc.lockmanager.api.LockManager;
public abstract class JdbcLockManager implements LockManager {
diff --git a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/Messages.java b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/Messages.java
index 769d94257..4f2831157 100644
--- a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/Messages.java
+++ b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/Messages.java
@@ -27,7 +27,7 @@ package org.onap.appc.lockmanager.impl.sql;
public enum Messages {
ERR_NULL_LOCK_OWNER("Cannot acquire lock for resource [%s]: lock owner must be specified"),
- ERR_LOCK_LOCKED_BY_OTHER("Cannot lock resource [%s] for [%s]: already locked by [%s]"),
+ ERR_LOCK_LOCKED_BY_OTHER("VNF : [%s] is locked by request id : [%s]"),
ERR_UNLOCK_NOT_LOCKED("Error unlocking resource [%s]: resource is not locked"),
ERR_UNLOCK_LOCKED_BY_OTHER("Error unlocking resource [%s] by [%s]: resource is locked by [%s]"),
EXP_LOCK("Error locking resource [%s]."),
diff --git a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/MySqlConnectionFactory.java b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/MySqlConnectionFactory.java
index 4df89541d..7f91d21cd 100644
--- a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/MySqlConnectionFactory.java
+++ b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/MySqlConnectionFactory.java
@@ -32,6 +32,8 @@ import org.onap.appc.dao.util.DefaultJdbcConnectionFactory;
public class MySqlConnectionFactory extends DefaultJdbcConnectionFactory {
protected void registedDriver() throws SQLException {
- DriverManager.registerDriver(new org.mariadb.jdbc.Driver());
+ DriverManager.registerDriver(new org.mariadb.jdbc.Driver());
+
}
+
}
diff --git a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/optimistic/LockRecord.java b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/optimistic/LockRecord.java
index ff0e63b8d..94e656d2d 100644
--- a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/optimistic/LockRecord.java
+++ b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/optimistic/LockRecord.java
@@ -26,49 +26,49 @@ package org.onap.appc.lockmanager.impl.sql.optimistic;
class LockRecord {
- private String resource;
- private String owner;
- private long updated;
- private long timeout;
- private long ver;
+ private String resource;
+ private String owner;
+ private long updated;
+ private long timeout;
+ private long ver;
- LockRecord(String resource) {
- this.resource = resource;
- }
+ LockRecord(String resource) {
+ this.resource = resource;
+ }
- public String getResource() {
- return resource;
- }
+ public String getResource() {
+ return resource;
+ }
- public String getOwner() {
- return owner;
- }
+ public String getOwner() {
+ return owner;
+ }
- public void setOwner(String owner) {
- this.owner = owner;
- }
+ public void setOwner(String owner) {
+ this.owner = owner;
+ }
- public long getUpdated() {
- return updated;
- }
+ public long getUpdated() {
+ return updated;
+ }
- public void setUpdated(long updated) {
- this.updated = updated;
- }
+ public void setUpdated(long updated) {
+ this.updated = updated;
+ }
- public long getTimeout() {
- return timeout;
- }
+ public long getTimeout() {
+ return timeout;
+ }
- public void setTimeout(long timeout) {
- this.timeout = timeout;
- }
+ public void setTimeout(long timeout) {
+ this.timeout = timeout;
+ }
- public long getVer() {
- return ver;
- }
+ public long getVer() {
+ return ver;
+ }
- public void setVer(long ver) {
- this.ver = ver;
- }
+ public void setVer(long ver) {
+ this.ver = ver;
+ }
}
diff --git a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/optimistic/MySqlLockManager.java b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/optimistic/MySqlLockManager.java
index d1dd2c5a3..1cbc1fb04 100644
--- a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/optimistic/MySqlLockManager.java
+++ b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/optimistic/MySqlLockManager.java
@@ -28,8 +28,10 @@ import java.sql.SQLException;
public class MySqlLockManager extends SqlLockManager {
- @Override
- protected boolean isDuplicatePkError(SQLException e) {
- return (e.getErrorCode() == 1062);
- }
+ @Override
+ protected boolean isDuplicatePkError(SQLException e) {
+ return (e.getErrorCode() == 1062);
+ }
+
+
}
diff --git a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/optimistic/SqlLockManager.java b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/optimistic/SqlLockManager.java
index 26ec6a274..481350199 100644
--- a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/optimistic/SqlLockManager.java
+++ b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/optimistic/SqlLockManager.java
@@ -36,231 +36,271 @@ import org.onap.appc.lockmanager.impl.sql.Messages;
abstract class SqlLockManager extends JdbcLockManager {
- private static final String SQL_LOAD_LOCK_RECORD = "SELECT * FROM %s WHERE RESOURCE_ID=?";
- private static final String SQL_INSERT_LOCK_RECORD = "INSERT INTO %s (RESOURCE_ID, OWNER_ID, UPDATED, TIMEOUT, VER) VALUES (?, ?, ?, ?, ?)";
- private static final String SQL_UPDATE_LOCK_RECORD = "UPDATE %s SET OWNER_ID=?, UPDATED=?, TIMEOUT=?, VER=? WHERE RESOURCE_ID=? AND VER=?";
-// private static final String SQL_DELETE_LOCK_RECORD = "DELETE FROM %s WHERE RESOURCE_ID=? AND VER=?";
- private static final String SQL_CURRENT_TIMESTAMP = "SELECT CURRENT_TIMESTAMP()";
+ private static final String SQL_LOAD_LOCK_RECORD = "SELECT * FROM %s WHERE RESOURCE_ID=?";
+ private static final String SQL_INSERT_LOCK_RECORD = "INSERT INTO %s (RESOURCE_ID, OWNER_ID, UPDATED, TIMEOUT, VER) VALUES (?, ?, ?, ?, ?)";
+ private static final String SQL_UPDATE_LOCK_RECORD = "UPDATE %s SET OWNER_ID=?, UPDATED=?, TIMEOUT=?, VER=? WHERE RESOURCE_ID=? AND VER=?";
+// private static final String SQL_DELETE_LOCK_RECORD = "DELETE FROM %s WHERE RESOURCE_ID=? AND VER=?";
+ private static final String SQL_CURRENT_TIMESTAMP = "SELECT CURRENT_TIMESTAMP()";
+ private static final String SQL_LOAD_LOCK_RECORD_WITH_OWNER = "SELECT * FROM LOCK_MANAGEMENT WHERE RESOURCE_ID = ? AND OWNER = ? ";
- private String sqlLoadLockRecord;
- private String sqlInsertLockRecord;
- private String sqlUpdateLockRecord;
-// private String sqlDeleteLockRecord;
+ private String sqlLoadLockRecord;
+ private String sqlInsertLockRecord;
+ private String sqlUpdateLockRecord;
+// private String sqlDeleteLockRecord;
- @Override
- public boolean acquireLock(String resource, String owner) throws LockException {
- return acquireLock(resource, owner, 0);
- }
+ @Override
+ public boolean acquireLock(String resource, String owner) throws LockException {
+ return acquireLock(resource, owner, 0);
+ }
- @Override
- public boolean acquireLock(String resource, String owner, long timeout) throws LockException {
- if(owner == null) {
- throw new LockRuntimeException(Messages.ERR_NULL_LOCK_OWNER.format(resource));
- }
- boolean res = false;
- Connection connection = openDbConnection();
- try {
- res = lockResource(connection, resource, owner, timeout);
- } finally {
- closeDbConnection(connection);
- }
- return res;
- }
+ @Override
+ public boolean acquireLock(String resource, String owner, long timeout) throws LockException {
+ if(owner == null) {
+ throw new LockRuntimeException(Messages.ERR_NULL_LOCK_OWNER.format(resource));
+ }
+ boolean res = false;
+ Connection connection = openDbConnection();
+ try {
+ res = lockResource(connection, resource, owner, timeout);
+ } finally {
+ closeDbConnection(connection);
+ }
+ return res;
+ }
- @Override
- public void releaseLock(String resource, String owner) throws LockException {
- Connection connection = openDbConnection();
- try {
- unlockResource(connection, resource, owner);
- } finally {
- closeDbConnection(connection);
- }
- }
+ @Override
+ public void releaseLock(String resource, String owner) throws LockException {
+ Connection connection = openDbConnection();
+ try {
+ unlockResource(connection, resource, owner);
+ } finally {
+ closeDbConnection(connection);
+ }
+ }
- @Override
- public boolean isLocked(String resource) {
+ @Override
+ public boolean isLocked(String resource) {
Connection connection=openDbConnection();
- try {
- LockRecord lockRecord=loadLockRecord(connection,resource);
- if(lockRecord==null){
- return false;
- }else{
- if(lockRecord.getOwner()==null){
- return false;
- }else if(isLockExpired(lockRecord, connection)){
- return false;
- }else{
+ try {
+ LockRecord lockRecord=loadLockRecord(connection,resource);
+ if(lockRecord==null){
+ return false;
+ }else{
+ if(lockRecord.getOwner()==null){
+ return false;
+ }else if(isLockExpired(lockRecord, connection)){
+ return false;
+ }else{
return true;
}
- }
- } catch (SQLException e) {
- throw new LockRuntimeException(Messages.EXP_CHECK_LOCK.format(resource));
- }finally {
+ }
+ } catch (SQLException e) {
+ throw new LockRuntimeException(Messages.EXP_CHECK_LOCK.format(resource));
+ }finally {
closeDbConnection(connection);
}
}
- private boolean lockResource(Connection connection, String resource, String owner, long timeout) throws LockException {
- try {
- boolean res = false;
- LockRecord lockRecord = loadLockRecord(connection, resource);
- if(lockRecord != null) {
- // lock record already exists
- String currentOwner = lockRecord.getOwner();
- if(currentOwner != null) {
- if(isLockExpired(lockRecord, connection)) {
- currentOwner = null;
- } else if(!owner.equals(currentOwner)) {
- throw new LockException(Messages.ERR_LOCK_LOCKED_BY_OTHER.format(resource, owner, currentOwner));
- }
- }
- // set new owner on the resource lock record
- if(!updateLockRecord(connection, resource, owner, timeout, lockRecord.getVer())) {
- // try again - maybe same owner updated the record
- lockResource(connection, resource, owner, timeout);
- }
- if(currentOwner == null) {
- // no one locked the resource before
- res = true;
- }
- } else {
- // resource record does not exist in lock table => create new record
- try {
- addLockRecord(connection, resource, owner, timeout);
- res = true;
- } catch(SQLException e) {
- if(isDuplicatePkError(e)) {
- // try again - maybe same owner inserted the record
- lockResource(connection, resource, owner, timeout);
- } else {
- throw e;
- }
- }
- }
- return res;
- } catch(SQLException e) {
- throw new LockRuntimeException(Messages.EXP_LOCK.format(resource), e);
- }
- }
+ @Override
+ public String getLockOwner(String resource) {
+ Connection connection=openDbConnection();
+ try {
+ LockRecord lockRecord=loadLockRecord(connection,resource);
+ if(lockRecord==null || lockRecord.getOwner() ==null ){
+ return null;
+ }else{
+ if(isLockExpired(lockRecord, connection)){
+ return null;
+ }else{
+ return lockRecord.getOwner();
+ }
+ }
+ } catch (SQLException e) {
+ throw new LockRuntimeException(Messages.EXP_CHECK_LOCK.format(resource));
+ }finally {
+ closeDbConnection(connection);
+ }
+ }
- protected boolean isDuplicatePkError(SQLException e) {
- return e.getSQLState().startsWith("23");
- }
+ private boolean lockResource(Connection connection, String resource, String owner, long timeout) throws LockException {
+ try {
+ boolean res = false;
+ LockRecord lockRecord = loadLockRecord(connection, resource);
+ if(lockRecord != null) {
+ // lock record already exists
+ String currentOwner = lockRecord.getOwner();
+ if(currentOwner != null) {
+ if(isLockExpired(lockRecord, connection)) {
+ currentOwner = null;
+ } else if(!owner.equals(currentOwner)) {
+ throw new LockException(Messages.ERR_LOCK_LOCKED_BY_OTHER.format(resource, currentOwner));
+ }
+ }
+ // set new owner on the resource lock record
+ if(!updateLockRecord(connection, resource, owner, timeout, lockRecord.getVer())) {
+ // try again - maybe same owner updated the record
+ lockResource(connection, resource, owner, timeout);
+ }
+ if(currentOwner == null) {
+ // no one locked the resource before
+ res = true;
+ }
+ } else {
+ // resource record does not exist in lock table => create new record
+ try {
+ addLockRecord(connection, resource, owner, timeout);
+ res = true;
+ } catch(SQLException e) {
+ if(isDuplicatePkError(e)) {
+ // try again - maybe same owner inserted the record
+ lockResource(connection, resource, owner, timeout);
+ } else {
+ throw e;
+ }
+ }
+ }
+ return res;
+ } catch(SQLException e) {
+ throw new LockRuntimeException(Messages.EXP_LOCK.format(resource), e);
+ }
+ }
+
+ protected boolean isDuplicatePkError(SQLException e) {
+ return e.getSQLState().startsWith("23");
+ }
- private void unlockResource(Connection connection, String resource, String owner) throws LockException {
- try {
- LockRecord lockRecord = loadLockRecord(connection, resource);
- if(lockRecord != null) {
- // check if expired
- if(isLockExpired(lockRecord, connection)) {
- // lock is expired => no lock
- lockRecord = null;
- }
- }
- if((lockRecord == null) || (lockRecord.getOwner() == null)) {
- // resource is not locked
- throw new LockException(Messages.ERR_UNLOCK_NOT_LOCKED.format(resource));
- }
- String currentOwner = lockRecord.getOwner();
- if(!owner.equals(currentOwner)) {
- throw new LockException(Messages.ERR_UNLOCK_LOCKED_BY_OTHER.format(resource, owner, currentOwner));
+ private void unlockResource(Connection connection, String resource, String owner) throws LockException {
+ try {
+ LockRecord lockRecord = loadLockRecord(connection, resource);
+ if(lockRecord != null) {
+ // check if expired
+ if(isLockExpired(lockRecord, connection)) {
+ // lock is expired => no lock
+ lockRecord = null;
+ }
+ }
+ if((lockRecord == null) || (lockRecord.getOwner() == null)) {
+ // resource is not locked
+ throw new LockException(Messages.ERR_UNLOCK_NOT_LOCKED.format(resource));
+ }
+ String currentOwner = lockRecord.getOwner();
+ if(!owner.equals(currentOwner)) {
+ throw new LockException(Messages.ERR_UNLOCK_LOCKED_BY_OTHER.format(resource, owner, currentOwner));
}
if (!updateLockRecord(connection, resource, null, 0, lockRecord.getVer())) {
- unlockResource(connection, resource, owner);
- }
- // TODO delete record from table on lock release?
-// deleteLockRecord(connection, resource, lockRecord.getVer());
- } catch(SQLException e) {
- throw new LockRuntimeException(Messages.EXP_UNLOCK.format(resource), e);
- }
- }
+ unlockResource(connection, resource, owner);
+ }
+ // TODO delete record from table on lock release?
+// deleteLockRecord(connection, resource, lockRecord.getVer());
+ } catch(SQLException e) {
+ throw new LockRuntimeException(Messages.EXP_UNLOCK.format(resource), e);
+ }
+ }
- protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
- LockRecord res = null;
- if(sqlLoadLockRecord == null) {
- sqlLoadLockRecord = String.format(SQL_LOAD_LOCK_RECORD, tableName);
- }
- try(PreparedStatement statement = connection.prepareStatement(sqlLoadLockRecord)) {
- statement.setString(1, resource);
- try(ResultSet resultSet = statement.executeQuery()) {
- if(resultSet.next()) {
- res = new LockRecord(resource);
- res.setOwner(resultSet.getString(2));
- res.setUpdated(resultSet.getLong(3));
- res.setTimeout(resultSet.getLong(4));
- res.setVer(resultSet.getLong(5));
- }
- }
- }
- return res;
- }
+ protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
+ LockRecord res = null;
+ if(sqlLoadLockRecord == null) {
+ sqlLoadLockRecord = String.format(SQL_LOAD_LOCK_RECORD, tableName);
+ }
+ try(PreparedStatement statement = connection.prepareStatement(sqlLoadLockRecord)) {
+ statement.setString(1, resource);
+ try(ResultSet resultSet = statement.executeQuery()) {
+ if(resultSet.next()) {
+ res = new LockRecord(resource);
+ res.setOwner(resultSet.getString(2));
+ res.setUpdated(resultSet.getLong(3));
+ res.setTimeout(resultSet.getLong(4));
+ res.setVer(resultSet.getLong(5));
+ }
+ }
+ }
+ return res;
+ }
+
+ protected LockRecord loadLockRecord(Connection connection, String resource,String owner) throws SQLException {
+ LockRecord res = null;
+ try(PreparedStatement statement = connection.prepareStatement(SQL_LOAD_LOCK_RECORD_WITH_OWNER)) {
+ statement.setString(1, resource);
+ statement.setString(2, owner);
+ try(ResultSet resultSet = statement.executeQuery()) {
+ if(resultSet.next()) {
+ res = new LockRecord(resource);
+ res.setOwner(resultSet.getString(2));
+ res.setUpdated(resultSet.getLong(3));
+ res.setTimeout(resultSet.getLong(4));
+ res.setVer(resultSet.getLong(5));
+ }
+ }
+ }
+ return res;
+ }
- protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
- if(sqlInsertLockRecord == null) {
- sqlInsertLockRecord = String.format(SQL_INSERT_LOCK_RECORD, tableName);
- }
- try(PreparedStatement statement = connection.prepareStatement(sqlInsertLockRecord)) {
- statement.setString(1, resource);
- statement.setString(2, owner);
+ protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
+ if(sqlInsertLockRecord == null) {
+ sqlInsertLockRecord = String.format(SQL_INSERT_LOCK_RECORD, tableName);
+ }
+ try(PreparedStatement statement = connection.prepareStatement(sqlInsertLockRecord)) {
+ statement.setString(1, resource);
+ statement.setString(2, owner);
statement.setLong(3, getCurrentTime(connection));
- statement.setLong(4, timeout);
- statement.setLong(5, 1);
- statement.executeUpdate();
- }
- }
+ statement.setLong(4, timeout);
+ statement.setLong(5, 1);
+ statement.executeUpdate();
+ }
+ }
- protected boolean updateLockRecord(Connection connection, String resource, String owner, long timeout, long ver) throws SQLException {
- if(sqlUpdateLockRecord == null) {
- sqlUpdateLockRecord = String.format(SQL_UPDATE_LOCK_RECORD, tableName);
- }
- try(PreparedStatement statement = connection.prepareStatement(sqlUpdateLockRecord)) {
- long newVer = (ver >= Long.MAX_VALUE) ? 1 : (ver + 1);
- statement.setString(1, owner);
+ protected boolean updateLockRecord(Connection connection, String resource, String owner, long timeout, long ver) throws SQLException {
+ if(sqlUpdateLockRecord == null) {
+ sqlUpdateLockRecord = String.format(SQL_UPDATE_LOCK_RECORD, tableName);
+ }
+ try(PreparedStatement statement = connection.prepareStatement(sqlUpdateLockRecord)) {
+ long newVer = (ver >= Long.MAX_VALUE) ? 1 : (ver + 1);
+ statement.setString(1, owner);
statement.setLong(2, getCurrentTime(connection));
- statement.setLong(3, timeout);
- statement.setLong(4, newVer);
- statement.setString(5, resource);
- statement.setLong(6, ver);
- return (statement.executeUpdate() != 0);
- }
- }
+ statement.setLong(3, timeout);
+ statement.setLong(4, newVer);
+ statement.setString(5, resource);
+ statement.setLong(6, ver);
+ return (statement.executeUpdate() != 0);
+ }
+ }
-// protected void deleteLockRecord(Connection connection, String resource, long ver) throws SQLException {
-// if(sqlDeleteLockRecord == null) {
-// sqlDeleteLockRecord = String.format(SQL_DELETE_LOCK_RECORD, tableName);
-// }
-// try(PreparedStatement statement = connection.prepareStatement(sqlDeleteLockRecord)) {
-// statement.setString(1, resource);
-// statement.setLong(2, ver);
-// statement.executeUpdate();
-// }
-// }
+// protected void deleteLockRecord(Connection connection, String resource, long ver) throws SQLException {
+// if(sqlDeleteLockRecord == null) {
+// sqlDeleteLockRecord = String.format(SQL_DELETE_LOCK_RECORD, tableName);
+// }
+// try(PreparedStatement statement = connection.prepareStatement(sqlDeleteLockRecord)) {
+// statement.setString(1, resource);
+// statement.setLong(2, ver);
+// statement.executeUpdate();
+// }
+// }
- private boolean isLockExpired(LockRecord lockRecord, Connection connection) throws SQLException {
- long timeout = lockRecord.getTimeout();
- if(timeout == 0) {
- return false;
- }
- long updated = lockRecord.getUpdated();
+ private boolean isLockExpired(LockRecord lockRecord, Connection connection) throws SQLException {
+ long timeout = lockRecord.getTimeout();
+ if(timeout == 0) {
+ return false;
+ }
+ long updated = lockRecord.getUpdated();
long now = getCurrentTime(connection);
- long expiration = updated + timeout;
- return (now > expiration);
- }
+ long expiration = updated + timeout;
+ return (now > expiration);
+ }
- private long getCurrentTime(Connection connection) throws SQLException {
- long res = -1;
- if(connection != null) {
- try(PreparedStatement statement = connection.prepareStatement(SQL_CURRENT_TIMESTAMP)) {
- try(ResultSet resultSet = statement.executeQuery()) {
- if(resultSet.next()) {
- res = resultSet.getTimestamp(1).getTime();
- }
- }
- }
- }
- if(res == -1) {
- res = System.currentTimeMillis();
- }
- return res;
- }
+ private long getCurrentTime(Connection connection) throws SQLException {
+ long res = -1;
+ if(connection != null) {
+ try(PreparedStatement statement = connection.prepareStatement(SQL_CURRENT_TIMESTAMP)) {
+ try(ResultSet resultSet = statement.executeQuery()) {
+ if(resultSet.next()) {
+ res = resultSet.getTimestamp(1).getTime();
+ }
+ }
+ }
+ }
+ if(res == -1) {
+ res = System.currentTimeMillis();
+ }
+ return res;
+ }
}
diff --git a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/pessimistic/LockRecord.java b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/pessimistic/LockRecord.java
index 1960ba9a3..3a3799697 100644
--- a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/pessimistic/LockRecord.java
+++ b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/pessimistic/LockRecord.java
@@ -26,40 +26,40 @@ package org.onap.appc.lockmanager.impl.sql.pessimistic;
class LockRecord {
- private String resource;
- private String owner;
- private long updated;
- private long timeout;
+ private String resource;
+ private String owner;
+ private long updated;
+ private long timeout;
- LockRecord(String resource) {
- this.resource = resource;
- }
+ LockRecord(String resource) {
+ this.resource = resource;
+ }
- public String getResource() {
- return resource;
- }
+ public String getResource() {
+ return resource;
+ }
- public String getOwner() {
- return owner;
- }
+ public String getOwner() {
+ return owner;
+ }
- public void setOwner(String owner) {
- this.owner = owner;
- }
+ public void setOwner(String owner) {
+ this.owner = owner;
+ }
- public long getUpdated() {
- return updated;
- }
+ public long getUpdated() {
+ return updated;
+ }
- public void setUpdated(long updated) {
- this.updated = updated;
- }
+ public void setUpdated(long updated) {
+ this.updated = updated;
+ }
- public long getTimeout() {
- return timeout;
- }
+ public long getTimeout() {
+ return timeout;
+ }
- public void setTimeout(long timeout) {
- this.timeout = timeout;
- }
+ public void setTimeout(long timeout) {
+ this.timeout = timeout;
+ }
}
diff --git a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/pessimistic/MySqlLockManager.java b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/pessimistic/MySqlLockManager.java
index b76f15c8f..5268acf20 100644
--- a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/pessimistic/MySqlLockManager.java
+++ b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/pessimistic/MySqlLockManager.java
@@ -33,56 +33,56 @@ import org.onap.appc.lockmanager.api.LockRuntimeException;
public class MySqlLockManager extends SqlLockManager {
- private static final int DEF_CRITICAL_SECTION_WAIT_TIMEOUT = 3;
+ private static final int DEF_CRITICAL_SECTION_WAIT_TIMEOUT = 3;
- protected int criticalSectionWaitTimeoutSecs = DEF_CRITICAL_SECTION_WAIT_TIMEOUT;
+ protected int criticalSectionWaitTimeoutSecs = DEF_CRITICAL_SECTION_WAIT_TIMEOUT;
- public void setCriticalSectionWaitTimeoutSecs(int criticalSectionWaitTimeoutSecs) {
- this.criticalSectionWaitTimeoutSecs = criticalSectionWaitTimeoutSecs;
- }
+ public void setCriticalSectionWaitTimeoutSecs(int criticalSectionWaitTimeoutSecs) {
+ this.criticalSectionWaitTimeoutSecs = criticalSectionWaitTimeoutSecs;
+ }
- @Override
- protected void enterCriticalSection(Connection connection, String resource) {
- try {
- CallableStatement statement = connection.prepareCall("SELECT COALESCE(GET_LOCK(?,?),0)");
- try {
- statement.setString(1, resource);
- statement.setInt(2, criticalSectionWaitTimeoutSecs);
- boolean execRes = statement.execute();
- int result = 0;
- if(execRes) {
- ResultSet resultSet = statement.getResultSet();
- try {
- if(resultSet.next()) {
- result = resultSet.getInt(1);
- }
- } finally {
- resultSet.close();
- }
- }
- if(result != 1) { // lock is not obtained
- throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].");
- }
- } finally {
- statement.close();
- }
- } catch(SQLException e) {
+ @Override
+ protected void enterCriticalSection(Connection connection, String resource) {
+ try {
+ CallableStatement statement = connection.prepareCall("SELECT COALESCE(GET_LOCK(?,?),0)");
+ try {
+ statement.setString(1, resource);
+ statement.setInt(2, criticalSectionWaitTimeoutSecs);
+ boolean execRes = statement.execute();
+ int result = 0;
+ if(execRes) {
+ ResultSet resultSet = statement.getResultSet();
+ try {
+ if(resultSet.next()) {
+ result = resultSet.getInt(1);
+ }
+ } finally {
+ resultSet.close();
+ }
+ }
+ if(result != 1) { // lock is not obtained
+ throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].");
+ }
+ } finally {
+ statement.close();
+ }
+ } catch(SQLException e) {
throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].", e);
- }
- }
+ }
+ }
- @Override
- protected void leaveCriticalSection(Connection connection, String resource) {
- try {
- CallableStatement statement = connection.prepareCall("SELECT RELEASE_LOCK(?)");
- try {
- statement.setString(1, resource);
- statement.execute();
- } finally {
- statement.close();
- }
- } catch(SQLException e) {
- throw new LockRuntimeException("Error releasing critical section lock.", e);
- }
- }
+ @Override
+ protected void leaveCriticalSection(Connection connection, String resource) {
+ try {
+ CallableStatement statement = connection.prepareCall("SELECT RELEASE_LOCK(?)");
+ try {
+ statement.setString(1, resource);
+ statement.execute();
+ } finally {
+ statement.close();
+ }
+ } catch(SQLException e) {
+ throw new LockRuntimeException("Error releasing critical section lock.", e);
+ }
+ }
}
diff --git a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/pessimistic/SqlLockManager.java b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/pessimistic/SqlLockManager.java
index a1536d969..98c753616 100644
--- a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/pessimistic/SqlLockManager.java
+++ b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/main/java/org/onap/appc/lockmanager/impl/sql/pessimistic/SqlLockManager.java
@@ -36,219 +36,240 @@ import org.onap.appc.lockmanager.impl.sql.Messages;
abstract class SqlLockManager extends JdbcLockManager {
- private static final String SQL_LOAD_LOCK_RECORD = "SELECT * FROM %s WHERE RESOURCE_ID=?";
- private static final String SQL_INSERT_LOCK_RECORD = "INSERT INTO %s (RESOURCE_ID, OWNER_ID, UPDATED, TIMEOUT) VALUES (?, ?, ?, ?)";
- private static final String SQL_UPDATE_LOCK_RECORD = "UPDATE %s SET OWNER_ID=?, UPDATED=?, TIMEOUT=? WHERE RESOURCE_ID=?";
- private static final String SQL_CURRENT_TIMESTAMP = "SELECT CURRENT_TIMESTAMP()";
-
- private String sqlLoadLockRecord;
- private String sqlInsertLockRecord;
- private String sqlUpdateLockRecord;
-
- @Override
- public boolean acquireLock(String resource, String owner) throws LockException {
- return acquireLock(resource, owner, 0);
- }
-
- @Override
- public boolean acquireLock(String resource, String owner, long timeout) throws LockException {
- if(owner == null) {
- throw new LockRuntimeException(Messages.ERR_NULL_LOCK_OWNER.format(resource));
- }
- boolean res = false;
- Connection connection = openDbConnection();
- try {
- enterCriticalSection(connection, resource);
- try {
- res = lockResource(connection, resource, owner, timeout);
- } finally {
- leaveCriticalSection(connection, resource);
- }
- } finally {
- closeDbConnection(connection);
- }
- return res;
- }
-
- @Override
- public void releaseLock(String resource, String owner) throws LockException {
- Connection connection = openDbConnection();
- try {
- enterCriticalSection(connection, resource);
- try {
- unlockResource(connection, resource, owner);
- } finally {
- leaveCriticalSection(connection, resource);
- }
- } finally {
- closeDbConnection(connection);
- }
- }
-
- @Override
- public boolean isLocked(String resource) {
- Connection connection=openDbConnection();
- try {
- LockRecord lockRecord=loadLockRecord(connection,resource);
- if(lockRecord==null){
- return false;
- }else{
- if(lockRecord.getOwner()==null){
- return false;
- }else if(isLockExpired(lockRecord, connection)){
- return false;
- }else{
- return true;
- }
- }
- } catch (SQLException e) {
- throw new LockRuntimeException(Messages.EXP_CHECK_LOCK.format(resource));
- }finally {
- closeDbConnection(connection);
- }
- }
-
- private boolean lockResource(Connection connection, String resource, String owner, long timeout) throws LockException {
- try {
- boolean res = false;
- LockRecord lockRecord = loadLockRecord(connection, resource);
- if(lockRecord != null) {
- // lock record already exists
- String currentOwner = lockRecord.getOwner();
- if(currentOwner != null) {
- if(isLockExpired(lockRecord, connection)) {
- currentOwner = null;
- } else if(!owner.equals(currentOwner)) {
- throw new LockException(Messages.ERR_LOCK_LOCKED_BY_OTHER.format(resource, owner, currentOwner));
- }
- }
- // set new owner on the resource lock record
- updateLockRecord(connection, resource, owner, timeout);
- if(currentOwner == null) {
- // no one locked the resource before
- res = true;
- }
- } else {
- // resource record does not exist in lock table => create new record
- addLockRecord(connection, resource, owner, timeout);
- res = true;
- }
- return res;
- } catch(SQLException e) {
- throw new LockRuntimeException(Messages.EXP_LOCK.format(resource), e);
- }
- }
-
- private void unlockResource(Connection connection, String resource, String owner) throws LockException {
- try {
- LockRecord lockRecord = loadLockRecord(connection, resource);
- if(lockRecord != null) {
- // check if expired
- if(isLockExpired(lockRecord, connection)) {
- // lock is expired => no lock
- lockRecord = null;
- }
- }
- if((lockRecord == null) || (lockRecord.getOwner() == null)) {
- // resource is not locked
- throw new LockException(Messages.ERR_UNLOCK_NOT_LOCKED.format(resource));
- }
- String currentOwner = lockRecord.getOwner();
- if(!owner.equals(currentOwner)) {
- throw new LockException(Messages.ERR_UNLOCK_LOCKED_BY_OTHER.format(resource, owner, currentOwner));
- }
- updateLockRecord(connection, resource, null, 0);
- // TODO delete record from table on lock release?
-// deleteLockRecord(connection, resource);
- } catch(SQLException e) {
- throw new LockRuntimeException(Messages.EXP_UNLOCK.format(resource), e);
- }
- }
-
- protected abstract void enterCriticalSection(Connection connection, String resource);
-
- protected abstract void leaveCriticalSection(Connection connection, String resource);
-
- protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
- LockRecord res = null;
- if(sqlLoadLockRecord == null) {
- sqlLoadLockRecord = String.format(SQL_LOAD_LOCK_RECORD, tableName);
- }
- try(PreparedStatement statement = connection.prepareStatement(sqlLoadLockRecord)) {
- statement.setString(1, resource);
- try(ResultSet resultSet = statement.executeQuery()) {
- if(resultSet.next()) {
- res = new LockRecord(resource);
- res.setOwner(resultSet.getString(2));
- res.setUpdated(resultSet.getLong(3));
- res.setTimeout(resultSet.getLong(4));
- }
- }
- }
- return res;
- }
-
- protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
- if(sqlInsertLockRecord == null) {
- sqlInsertLockRecord = String.format(SQL_INSERT_LOCK_RECORD, tableName);
- }
- try(PreparedStatement statement = connection.prepareStatement(sqlInsertLockRecord)) {
- statement.setString(1, resource);
- statement.setString(2, owner);
- statement.setLong(3, getCurrentTime(connection));
- statement.setLong(4, timeout);
- statement.executeUpdate();
- }
- }
-
- protected void updateLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
- if(sqlUpdateLockRecord == null) {
- sqlUpdateLockRecord = String.format(SQL_UPDATE_LOCK_RECORD, tableName);
- }
- try(PreparedStatement statement = connection.prepareStatement(sqlUpdateLockRecord)) {
- statement.setString(1, owner);
- statement.setLong(2, getCurrentTime(connection));
- statement.setLong(3, timeout);
- statement.setString(4, resource);
- statement.executeUpdate();
- }
- }
-
-// protected void deleteLockRecord(Connection connection, String resource) throws SQLException {
-// if(sqlDeleteLockRecord == null) {
-// sqlDeleteLockRecord = String.format(SQL_DELETE_LOCK_RECORD, tableName);
-// }
-// try(PreparedStatement statement = connection.prepareStatement(sqlDeleteLockRecord)) {
-// statement.setString(1, resource);
-// statement.executeUpdate();
-// }
-// }
-
- private boolean isLockExpired(LockRecord lockRecord, Connection connection) throws SQLException {
- long timeout = lockRecord.getTimeout();
- if(timeout == 0) {
- return false;
- }
- long updated = lockRecord.getUpdated();
- long now = getCurrentTime(connection);
- long expiration = updated + timeout;
- return (now > expiration);
- }
-
- private long getCurrentTime(Connection connection) throws SQLException {
- long res = -1;
- if(connection != null) {
- try(PreparedStatement statement = connection.prepareStatement(SQL_CURRENT_TIMESTAMP)) {
- try(ResultSet resultSet = statement.executeQuery()) {
- if(resultSet.next()) {
- res = resultSet.getTimestamp(1).getTime();
- }
- }
- }
- }
- if(res == -1) {
- res = System.currentTimeMillis();
- }
- return res;
- }
+ private static final String SQL_LOAD_LOCK_RECORD = "SELECT * FROM %s WHERE RESOURCE_ID=?";
+ private static final String SQL_INSERT_LOCK_RECORD = "INSERT INTO %s (RESOURCE_ID, OWNER_ID, UPDATED, TIMEOUT) VALUES (?, ?, ?, ?)";
+ private static final String SQL_UPDATE_LOCK_RECORD = "UPDATE %s SET OWNER_ID=?, UPDATED=?, TIMEOUT=? WHERE RESOURCE_ID=?";
+ private static final String SQL_CURRENT_TIMESTAMP = "SELECT CURRENT_TIMESTAMP()";
+
+ private String sqlLoadLockRecord;
+ private String sqlInsertLockRecord;
+ private String sqlUpdateLockRecord;
+
+ @Override
+ public boolean acquireLock(String resource, String owner) throws LockException {
+ return acquireLock(resource, owner, 0);
+ }
+
+ @Override
+ public boolean acquireLock(String resource, String owner, long timeout) throws LockException {
+ if(owner == null) {
+ throw new LockRuntimeException(Messages.ERR_NULL_LOCK_OWNER.format(resource));
+ }
+ boolean res = false;
+ Connection connection = openDbConnection();
+ try {
+ enterCriticalSection(connection, resource);
+ try {
+ res = lockResource(connection, resource, owner, timeout);
+ } finally {
+ leaveCriticalSection(connection, resource);
+ }
+ } finally {
+ closeDbConnection(connection);
+ }
+ return res;
+ }
+
+ @Override
+ public void releaseLock(String resource, String owner) throws LockException {
+ Connection connection = openDbConnection();
+ try {
+ enterCriticalSection(connection, resource);
+ try {
+ unlockResource(connection, resource, owner);
+ } finally {
+ leaveCriticalSection(connection, resource);
+ }
+ } finally {
+ closeDbConnection(connection);
+ }
+ }
+
+ @Override
+ public boolean isLocked(String resource) {
+ Connection connection=openDbConnection();
+ try {
+ LockRecord lockRecord=loadLockRecord(connection,resource);
+ if(lockRecord==null){
+ return false;
+ }else{
+ if(lockRecord.getOwner()==null){
+ return false;
+ }else if(isLockExpired(lockRecord, connection)){
+ return false;
+ }else{
+ return true;
+ }
+ }
+ } catch (SQLException e) {
+ throw new LockRuntimeException(Messages.EXP_CHECK_LOCK.format(resource));
+ }finally {
+ closeDbConnection(connection);
+ }
+ }
+
+ @Override
+ public String getLockOwner(String resource) {
+ Connection connection=openDbConnection();
+ try {
+ org.onap.appc.lockmanager.impl.sql.pessimistic.LockRecord lockRecord=loadLockRecord(connection,resource);
+ if(lockRecord==null || lockRecord.getOwner() ==null ){
+ return null;
+ }else{
+ if(isLockExpired(lockRecord, connection)){
+ return null;
+ }else{
+ return lockRecord.getOwner();
+ }
+ }
+ } catch (SQLException e) {
+ throw new LockRuntimeException(Messages.EXP_CHECK_LOCK.format(resource));
+ }finally {
+ closeDbConnection(connection);
+ }
+ }
+
+ private boolean lockResource(Connection connection, String resource, String owner, long timeout) throws LockException {
+ try {
+ boolean res = false;
+ LockRecord lockRecord = loadLockRecord(connection, resource);
+ if(lockRecord != null) {
+ // lock record already exists
+ String currentOwner = lockRecord.getOwner();
+ if(currentOwner != null) {
+ if(isLockExpired(lockRecord, connection)) {
+ currentOwner = null;
+ } else if(!owner.equals(currentOwner)) {
+ throw new LockException(Messages.ERR_LOCK_LOCKED_BY_OTHER.format(resource, currentOwner));
+ }
+ }
+ // set new owner on the resource lock record
+ updateLockRecord(connection, resource, owner, timeout);
+ if(currentOwner == null) {
+ // no one locked the resource before
+ res = true;
+ }
+ } else {
+ // resource record does not exist in lock table => create new record
+ addLockRecord(connection, resource, owner, timeout);
+ res = true;
+ }
+ return res;
+ } catch(SQLException e) {
+ throw new LockRuntimeException(Messages.EXP_LOCK.format(resource), e);
+ }
+ }
+
+ private void unlockResource(Connection connection, String resource, String owner) throws LockException {
+ try {
+ LockRecord lockRecord = loadLockRecord(connection, resource);
+ if(lockRecord != null) {
+ // check if expired
+ if(isLockExpired(lockRecord, connection)) {
+ // lock is expired => no lock
+ lockRecord = null;
+ }
+ }
+ if((lockRecord == null) || (lockRecord.getOwner() == null)) {
+ // resource is not locked
+ throw new LockException(Messages.ERR_UNLOCK_NOT_LOCKED.format(resource));
+ }
+ String currentOwner = lockRecord.getOwner();
+ if(!owner.equals(currentOwner)) {
+ throw new LockException(Messages.ERR_UNLOCK_LOCKED_BY_OTHER.format(resource, owner, currentOwner));
+ }
+ updateLockRecord(connection, resource, null, 0);
+ // TODO delete record from table on lock release?
+// deleteLockRecord(connection, resource);
+ } catch(SQLException e) {
+ throw new LockRuntimeException(Messages.EXP_UNLOCK.format(resource), e);
+ }
+ }
+
+ protected abstract void enterCriticalSection(Connection connection, String resource);
+
+ protected abstract void leaveCriticalSection(Connection connection, String resource);
+
+ protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
+ LockRecord res = null;
+ if(sqlLoadLockRecord == null) {
+ sqlLoadLockRecord = String.format(SQL_LOAD_LOCK_RECORD, tableName);
+ }
+ try(PreparedStatement statement = connection.prepareStatement(sqlLoadLockRecord)) {
+ statement.setString(1, resource);
+ try(ResultSet resultSet = statement.executeQuery()) {
+ if(resultSet.next()) {
+ res = new LockRecord(resource);
+ res.setOwner(resultSet.getString(2));
+ res.setUpdated(resultSet.getLong(3));
+ res.setTimeout(resultSet.getLong(4));
+ }
+ }
+ }
+ return res;
+ }
+
+ protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
+ if(sqlInsertLockRecord == null) {
+ sqlInsertLockRecord = String.format(SQL_INSERT_LOCK_RECORD, tableName);
+ }
+ try(PreparedStatement statement = connection.prepareStatement(sqlInsertLockRecord)) {
+ statement.setString(1, resource);
+ statement.setString(2, owner);
+ statement.setLong(3, getCurrentTime(connection));
+ statement.setLong(4, timeout);
+ statement.executeUpdate();
+ }
+ }
+
+ protected void updateLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
+ if(sqlUpdateLockRecord == null) {
+ sqlUpdateLockRecord = String.format(SQL_UPDATE_LOCK_RECORD, tableName);
+ }
+ try(PreparedStatement statement = connection.prepareStatement(sqlUpdateLockRecord)) {
+ statement.setString(1, owner);
+ statement.setLong(2, getCurrentTime(connection));
+ statement.setLong(3, timeout);
+ statement.setString(4, resource);
+ statement.executeUpdate();
+ }
+ }
+
+// protected void deleteLockRecord(Connection connection, String resource) throws SQLException {
+// if(sqlDeleteLockRecord == null) {
+// sqlDeleteLockRecord = String.format(SQL_DELETE_LOCK_RECORD, tableName);
+// }
+// try(PreparedStatement statement = connection.prepareStatement(sqlDeleteLockRecord)) {
+// statement.setString(1, resource);
+// statement.executeUpdate();
+// }
+// }
+
+ private boolean isLockExpired(LockRecord lockRecord, Connection connection) throws SQLException {
+ long timeout = lockRecord.getTimeout();
+ if(timeout == 0) {
+ return false;
+ }
+ long updated = lockRecord.getUpdated();
+ long now = getCurrentTime(connection);
+ long expiration = updated + timeout;
+ return (now > expiration);
+ }
+
+ private long getCurrentTime(Connection connection) throws SQLException {
+ long res = -1;
+ if(connection != null) {
+ try(PreparedStatement statement = connection.prepareStatement(SQL_CURRENT_TIMESTAMP)) {
+ try(ResultSet resultSet = statement.executeQuery()) {
+ if(resultSet.next()) {
+ res = resultSet.getTimestamp(1).getTime();
+ }
+ }
+ }
+ }
+ if(res == -1) {
+ res = System.currentTimeMillis();
+ }
+ return res;
+ }
}
diff --git a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/api/LockManagerBaseTests.java b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/api/LockManagerBaseTests.java
index 9f7e84c43..8a53372ce 100644
--- a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/api/LockManagerBaseTests.java
+++ b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/api/LockManagerBaseTests.java
@@ -32,139 +32,139 @@ import org.onap.appc.lockmanager.api.LockManager;
public abstract class LockManagerBaseTests {
- protected enum Resource {Resource1, Resource2};
- protected enum Owner {A, B};
-
- protected LockManager lockManager;
-
- @Before
- public void beforeTest() {
- lockManager = createLockManager();
- }
-
- protected abstract LockManager createLockManager();
-
- @Test
- public void testAcquireLock() throws LockException {
- boolean lockRes = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
- try {
- Assert.assertTrue(lockRes);
- } finally {
- lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
- }
- }
-
- @Test
- public void testAcquireLock_AlreadyLockedBySameOwner() throws LockException {
- boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
- try {
- Assert.assertTrue(lockRes1);
- boolean lockRes2 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
- Assert.assertFalse(lockRes2);
- } finally {
- lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
- }
- }
-
- @Test(expected = LockException.class)
- public void testAcquireLock_AlreadyLockedByOtherOwner() throws LockException {
- String owner2 = "B";
- boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
- try {
- Assert.assertTrue(lockRes1);
- boolean lockRes2 = lockManager.acquireLock(Resource.Resource1.name(), owner2);
- Assert.assertFalse(lockRes2);
- } finally {
- lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
- }
- }
-
- @Test
- public void testAcquireLock_LockDifferentResources() throws LockException {
- boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
- try {
- Assert.assertTrue(lockRes1);
- boolean lockRes2 = lockManager.acquireLock(Resource.Resource2.name(), Owner.B.name());
- try {
- Assert.assertTrue(lockRes2);
- } finally {
- lockManager.releaseLock(Resource.Resource2.name(), Owner.B.name());
- }
- } finally {
- lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
- }
- }
-
- @Test(expected = LockException.class)
- public void testReleaseLock_NotLockedResource() throws LockException {
- lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
- }
-
- @Test(expected = LockException.class)
- public void testReleaseLock_LockedByOtherOwnerResource() throws LockException {
- boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
- try {
- Assert.assertTrue(lockRes1);
- lockManager.releaseLock(Resource.Resource1.name(), Owner.B.name());
- } finally {
- lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
- }
- }
-
- @Test(expected = LockException.class)
- public void testAcquireLock_LockExpired() throws LockException, InterruptedException {
- boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name(), 50);
- Assert.assertTrue(lockRes1);
- Thread.sleep(1000);
- lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
- }
-
- @Test
- public void testAcquireLock_OtherLockExpired() throws LockException, InterruptedException {
- boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name(), 50);
- Assert.assertTrue(lockRes1);
- Thread.sleep(1000);
- boolean lockRes2 = lockManager.acquireLock(Resource.Resource1.name(), Owner.B.name());
- try {
- Assert.assertTrue(lockRes2);
- }finally {
- lockManager.releaseLock(Resource.Resource1.name(), Owner.B.name());
- }
- }
-
- @Test
- public void testIsLocked_WhenLocked() throws LockException, InterruptedException {
- boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name(), 50);
- try {
- Assert.assertTrue(lockManager.isLocked(Resource.Resource1.name()));
- }finally {
- lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
- }
- }
+ protected enum Resource {Resource1, Resource2};
+ protected enum Owner {A, B};
+
+ protected LockManager lockManager;
+
+ @Before
+ public void beforeTest() {
+ lockManager = createLockManager();
+ }
+
+ protected abstract LockManager createLockManager();
+
+ @Test
+ public void testAcquireLock() throws LockException {
+ boolean lockRes = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
+ try {
+ Assert.assertTrue(lockRes);
+ } finally {
+ lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
+ }
+ }
+
+ @Test
+ public void testAcquireLock_AlreadyLockedBySameOwner() throws LockException {
+ boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
+ try {
+ Assert.assertTrue(lockRes1);
+ boolean lockRes2 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
+ Assert.assertFalse(lockRes2);
+ } finally {
+ lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
+ }
+ }
+
+ @Test(expected = LockException.class)
+ public void testAcquireLock_AlreadyLockedByOtherOwner() throws LockException {
+ String owner2 = "B";
+ boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
+ try {
+ Assert.assertTrue(lockRes1);
+ boolean lockRes2 = lockManager.acquireLock(Resource.Resource1.name(), owner2);
+ Assert.assertFalse(lockRes2);
+ } finally {
+ lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
+ }
+ }
+
+ @Test
+ public void testAcquireLock_LockDifferentResources() throws LockException {
+ boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
+ try {
+ Assert.assertTrue(lockRes1);
+ boolean lockRes2 = lockManager.acquireLock(Resource.Resource2.name(), Owner.B.name());
+ try {
+ Assert.assertTrue(lockRes2);
+ } finally {
+ lockManager.releaseLock(Resource.Resource2.name(), Owner.B.name());
+ }
+ } finally {
+ lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
+ }
+ }
+
+ @Test(expected = LockException.class)
+ public void testReleaseLock_NotLockedResource() throws LockException {
+ lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
+ }
+
+ @Test(expected = LockException.class)
+ public void testReleaseLock_LockedByOtherOwnerResource() throws LockException {
+ boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
+ try {
+ Assert.assertTrue(lockRes1);
+ lockManager.releaseLock(Resource.Resource1.name(), Owner.B.name());
+ } finally {
+ lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
+ }
+ }
+
+ @Test(expected = LockException.class)
+ public void testAcquireLock_LockExpired() throws LockException, InterruptedException {
+ boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name(), 50);
+ Assert.assertTrue(lockRes1);
+ Thread.sleep(1000);
+ lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
+ }
+
+ @Test
+ public void testAcquireLock_OtherLockExpired() throws LockException, InterruptedException {
+ boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name(), 50);
+ Assert.assertTrue(lockRes1);
+ Thread.sleep(1000);
+ boolean lockRes2 = lockManager.acquireLock(Resource.Resource1.name(), Owner.B.name());
+ try {
+ Assert.assertTrue(lockRes2);
+ }finally {
+ lockManager.releaseLock(Resource.Resource1.name(), Owner.B.name());
+ }
+ }
+
+ @Test
+ public void testIsLocked_WhenLocked() throws LockException, InterruptedException {
+ boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name(), 50);
+ try {
+ Assert.assertTrue(lockManager.isLocked(Resource.Resource1.name()));
+ }finally {
+ lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
+ }
+ }
@Test(expected = LockException.class)
- public void testIsLocked_LockExpired() throws LockException, InterruptedException {
- boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name(), 50);
- Assert.assertTrue(lockRes1);
- Assert.assertTrue(lockManager.isLocked(Resource.Resource1.name()));
- Thread.sleep(1000);
- try {
- Assert.assertFalse(lockManager.isLocked(Resource.Resource1.name()));
- }finally {
- lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
- }
- }
-
- @Test
- public void testIsLocked_LockReleased() throws LockException, InterruptedException {
- boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name(), 50);
- lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
- Assert.assertFalse(lockManager.isLocked(Resource.Resource1.name()));
- }
-
- @Test
- public void testIsLocked_NoLock() throws LockException, InterruptedException {
- Assert.assertFalse(lockManager.isLocked(Resource.Resource1.name()));
- }
+ public void testIsLocked_LockExpired() throws LockException, InterruptedException {
+ boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name(), 50);
+ Assert.assertTrue(lockRes1);
+ Assert.assertTrue(lockManager.isLocked(Resource.Resource1.name()));
+ Thread.sleep(1000);
+ try {
+ Assert.assertFalse(lockManager.isLocked(Resource.Resource1.name()));
+ }finally {
+ lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
+ }
+ }
+
+ @Test
+ public void testIsLocked_LockReleased() throws LockException, InterruptedException {
+ boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name(), 50);
+ lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
+ Assert.assertFalse(lockManager.isLocked(Resource.Resource1.name()));
+ }
+
+ @Test
+ public void testIsLocked_NoLock() throws LockException, InterruptedException {
+ Assert.assertFalse(lockManager.isLocked(Resource.Resource1.name()));
+ }
}
diff --git a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/MySqlLockManagerBaseTests.java b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/MySqlLockManagerBaseTests.java
index b8cd95b94..12b587bfa 100644
--- a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/MySqlLockManagerBaseTests.java
+++ b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/MySqlLockManagerBaseTests.java
@@ -38,59 +38,59 @@ import java.sql.SQLException;
public abstract class MySqlLockManagerBaseTests extends LockManagerBaseTests {
- private static final boolean USE_REAL_DB = Boolean.getBoolean("lockmanager.tests.useRealDb");
- private static final String TABLE_LOCK_MANAGEMENT = "TEST_LOCK_MANAGEMENT";
- private static final String JDBC_URL = System.getProperty("lockmanager.tests.jdbcUrl", "jdbc:mysql://192.168.1.2/test");
- private static final String JDBC_USERNAME = System.getProperty("lockmanager.tests.jdbcUsername", "test");
- private static final String JDBC_PASSWORD = System.getProperty("lockmanager.tests.jdbcPassword", "123456");
+ private static final boolean USE_REAL_DB = Boolean.getBoolean("lockmanager.tests.useRealDb");
+ private static final String TABLE_LOCK_MANAGEMENT = "TEST_LOCK_MANAGEMENT";
+ private static final String JDBC_URL = System.getProperty("lockmanager.tests.jdbcUrl", "jdbc:mysql://192.168.1.2/test");
+ private static final String JDBC_USERNAME = System.getProperty("lockmanager.tests.jdbcUsername", "test");
+ private static final String JDBC_PASSWORD = System.getProperty("lockmanager.tests.jdbcPassword", "123456");
- protected static final int CONCURRENT_TEST_WAIT_TIME = 10; // secs
+ protected static final int CONCURRENT_TEST_WAIT_TIME = 10; // secs
- @Rule
- public TestName testName = new TestName();
+ @Rule
+ public TestName testName = new TestName();
- @Override
- protected LockManager createLockManager() {
- JdbcLockManager jdbcLockManager = createJdbcLockManager(USE_REAL_DB);
- DefaultJdbcConnectionFactory connectionFactory = new MySqlConnectionFactory();
- connectionFactory.setJdbcURL(JDBC_URL);
- connectionFactory.setJdbcUserName(JDBC_USERNAME);
- connectionFactory.setJdbcPassword(JDBC_PASSWORD);
- jdbcLockManager.setConnectionFactory(connectionFactory);
- jdbcLockManager.setTableName(TABLE_LOCK_MANAGEMENT);
- System.out.println("=> Running LockManager test [" + jdbcLockManager.getClass().getName() + "." + testName.getMethodName() + "]" + (USE_REAL_DB ? ". JDBC URL is [" + JDBC_URL + "]" : ""));
- clearTestLocks(jdbcLockManager);
- return jdbcLockManager;
- }
+ @Override
+ protected LockManager createLockManager() {
+ JdbcLockManager jdbcLockManager = createJdbcLockManager(USE_REAL_DB);
+ DefaultJdbcConnectionFactory connectionFactory = new MySqlConnectionFactory();
+ connectionFactory.setJdbcURL(JDBC_URL);
+ connectionFactory.setJdbcUserName(JDBC_USERNAME);
+ connectionFactory.setJdbcPassword(JDBC_PASSWORD);
+ jdbcLockManager.setConnectionFactory(connectionFactory);
+ jdbcLockManager.setTableName(TABLE_LOCK_MANAGEMENT);
+ System.out.println("=> Running LockManager test [" + jdbcLockManager.getClass().getName() + "." + testName.getMethodName() + "]" + (USE_REAL_DB ? ". JDBC URL is [" + JDBC_URL + "]" : ""));
+ clearTestLocks(jdbcLockManager);
+ return jdbcLockManager;
+ }
- protected abstract JdbcLockManager createJdbcLockManager(boolean useRealDb);
+ protected abstract JdbcLockManager createJdbcLockManager(boolean useRealDb);
- protected boolean setSynchronizer(Synchronizer synchronizer) {
- if(!(lockManager instanceof SynchronizerReceiver)) {
- System.err.println("Skipping concurrency test [" + testName.getMethodName() + "] for LockManager of type " + lockManager.getClass());
- return false;
- }
- ((SynchronizerReceiver)lockManager).setSynchronizer(synchronizer);
- return true;
- }
+ protected boolean setSynchronizer(Synchronizer synchronizer) {
+ if(!(lockManager instanceof SynchronizerReceiver)) {
+ System.err.println("Skipping concurrency test [" + testName.getMethodName() + "] for LockManager of type " + lockManager.getClass());
+ return false;
+ }
+ ((SynchronizerReceiver)lockManager).setSynchronizer(synchronizer);
+ return true;
+ }
- private static final String SQL_DELETE_LOCK_RECORD = String.format("DELETE FROM %s WHERE RESOURCE_ID=?", TABLE_LOCK_MANAGEMENT);
- private void clearTestLocks(JdbcLockManager jdbcLockManager) {
- Connection connection = jdbcLockManager.openDbConnection();
- if(connection == null) {
- return;
- }
- try {
- for(Resource resource: Resource.values()) {
- try(PreparedStatement statement = connection.prepareStatement(SQL_DELETE_LOCK_RECORD)) {
- statement.setString(1, resource.name());
- statement.executeUpdate();
- }
- }
- } catch(SQLException e) {
- throw new RuntimeException("Cannot clear test resources in table", e);
- } finally {
- jdbcLockManager.closeDbConnection(connection);
- }
- }
+ private static final String SQL_DELETE_LOCK_RECORD = String.format("DELETE FROM %s WHERE RESOURCE_ID=?", TABLE_LOCK_MANAGEMENT);
+ private void clearTestLocks(JdbcLockManager jdbcLockManager) {
+ Connection connection = jdbcLockManager.openDbConnection();
+ if(connection == null) {
+ return;
+ }
+ try {
+ for(Resource resource: Resource.values()) {
+ try(PreparedStatement statement = connection.prepareStatement(SQL_DELETE_LOCK_RECORD)) {
+ statement.setString(1, resource.name());
+ statement.executeUpdate();
+ }
+ }
+ } catch(SQLException e) {
+ throw new RuntimeException("Cannot clear test resources in table", e);
+ } finally {
+ jdbcLockManager.closeDbConnection(connection);
+ }
+ }
}
diff --git a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/optimistic/MySqlLockManagerMock.java b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/optimistic/MySqlLockManagerMock.java
index f9c072e6c..076e23343 100644
--- a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/optimistic/MySqlLockManagerMock.java
+++ b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/optimistic/MySqlLockManagerMock.java
@@ -36,100 +36,100 @@ import org.onap.appc.lockmanager.impl.sql.optimistic.MySqlLockManager;
class MySqlLockManagerMock extends MySqlLockManager implements SynchronizerReceiver {
- private final ConcurrentMap<String, LockRecord> locks = new ConcurrentHashMap<>();
- private boolean useReal;
- private Synchronizer synchronizer;
+ private final ConcurrentMap<String, LockRecord> locks = new ConcurrentHashMap<>();
+ private boolean useReal;
+ private Synchronizer synchronizer;
- MySqlLockManagerMock(boolean useReal) {
- this.useReal = useReal;
- }
+ MySqlLockManagerMock(boolean useReal) {
+ this.useReal = useReal;
+ }
- @Override
- public void setSynchronizer(Synchronizer synchronizer) {
- this.synchronizer = synchronizer;
- }
+ @Override
+ public void setSynchronizer(Synchronizer synchronizer) {
+ this.synchronizer = synchronizer;
+ }
- @Override
- protected Connection openDbConnection() {
- if(useReal) {
- return super.openDbConnection();
- }
- return null;
- }
+ @Override
+ protected Connection openDbConnection() {
+ if(useReal) {
+ return super.openDbConnection();
+ }
+ return null;
+ }
- @Override
- protected void closeDbConnection(Connection connection) {
- if(useReal) {
- super.closeDbConnection(connection);
- }
- }
+ @Override
+ protected void closeDbConnection(Connection connection) {
+ if(useReal) {
+ super.closeDbConnection(connection);
+ }
+ }
- @Override
- protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
- LockRecord res;
- if(useReal) {
- res = super.loadLockRecord(connection, resource);
- } else {
- res = locks.get(resource);
- }
- if(synchronizer != null) {
- synchronizer.postLoadLockRecord(resource, (res == null) ? null : res.getOwner());
- }
- return res;
- }
+ @Override
+ protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
+ LockRecord res;
+ if(useReal) {
+ res = super.loadLockRecord(connection, resource);
+ } else {
+ res = locks.get(resource);
+ }
+ if(synchronizer != null) {
+ synchronizer.postLoadLockRecord(resource, (res == null) ? null : res.getOwner());
+ }
+ return res;
+ }
- @Override
- protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
- if(synchronizer != null) {
- synchronizer.preAddLockRecord(resource, owner);
- }
- try {
- if(useReal) {
- super.addLockRecord(connection, resource, owner, timeout);
- return;
- }
- LockRecord lockRecord = new LockRecord(resource);
- lockRecord.setOwner(owner);
- lockRecord.setUpdated(System.currentTimeMillis());
- lockRecord.setTimeout(timeout);
- lockRecord.setVer(1);
- LockRecord prevLockRecord = locks.putIfAbsent(resource, lockRecord);
- if(prevLockRecord != null) {
- // simulate unique constraint violation
- throw new SQLException("Duplicate PK exception", "23000", 1062);
- }
- } finally {
- if(synchronizer != null) {
- synchronizer.postAddLockRecord(resource, owner);
- }
- }
- }
+ @Override
+ protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
+ if(synchronizer != null) {
+ synchronizer.preAddLockRecord(resource, owner);
+ }
+ try {
+ if(useReal) {
+ super.addLockRecord(connection, resource, owner, timeout);
+ return;
+ }
+ LockRecord lockRecord = new LockRecord(resource);
+ lockRecord.setOwner(owner);
+ lockRecord.setUpdated(System.currentTimeMillis());
+ lockRecord.setTimeout(timeout);
+ lockRecord.setVer(1);
+ LockRecord prevLockRecord = locks.putIfAbsent(resource, lockRecord);
+ if(prevLockRecord != null) {
+ // simulate unique constraint violation
+ throw new SQLException("Duplicate PK exception", "23000", 1062);
+ }
+ } finally {
+ if(synchronizer != null) {
+ synchronizer.postAddLockRecord(resource, owner);
+ }
+ }
+ }
- @Override
- protected boolean updateLockRecord(Connection connection, String resource, String owner, long timeout, long ver) throws SQLException {
- if(synchronizer != null) {
- synchronizer.preUpdateLockRecord(resource, owner);
- }
- try {
- if(useReal) {
- return super.updateLockRecord(connection, resource, owner, timeout, ver);
- }
- LockRecord lockRecord = loadLockRecord(connection, resource);
- synchronized(lockRecord) {
- // should be atomic operation
- if(ver != lockRecord.getVer()) {
- return false;
- }
- lockRecord.setOwner(owner);
- lockRecord.setUpdated(System.currentTimeMillis());
- lockRecord.setTimeout(timeout);
- lockRecord.setVer(ver + 1);
- }
- return true;
- } finally {
- if(synchronizer != null) {
- synchronizer.postUpdateLockRecord(resource, owner);
- }
- }
- }
+ @Override
+ protected boolean updateLockRecord(Connection connection, String resource, String owner, long timeout, long ver) throws SQLException {
+ if(synchronizer != null) {
+ synchronizer.preUpdateLockRecord(resource, owner);
+ }
+ try {
+ if(useReal) {
+ return super.updateLockRecord(connection, resource, owner, timeout, ver);
+ }
+ LockRecord lockRecord = loadLockRecord(connection, resource);
+ synchronized(lockRecord) {
+ // should be atomic operation
+ if(ver != lockRecord.getVer()) {
+ return false;
+ }
+ lockRecord.setOwner(owner);
+ lockRecord.setUpdated(System.currentTimeMillis());
+ lockRecord.setTimeout(timeout);
+ lockRecord.setVer(ver + 1);
+ }
+ return true;
+ } finally {
+ if(synchronizer != null) {
+ synchronizer.postUpdateLockRecord(resource, owner);
+ }
+ }
+ }
}
diff --git a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/optimistic/TestMySqlLockManager.java b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/optimistic/TestMySqlLockManager.java
index d652c7523..1ab1e4ef9 100644
--- a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/optimistic/TestMySqlLockManager.java
+++ b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/optimistic/TestMySqlLockManager.java
@@ -35,196 +35,196 @@ import java.util.concurrent.*;
public class TestMySqlLockManager extends MySqlLockManagerBaseTests {
- @Override
- protected JdbcLockManager createJdbcLockManager(boolean useReal) {
- return new MySqlLockManagerMock(useReal);
- }
-
- @Test
- public void testConcurrentLockDifferentOwners() throws LockException, InterruptedException, ExecutionException, TimeoutException {
-
- final int participantsNo = 2;
- Synchronizer synchronizer = new Synchronizer(participantsNo) {
-
- private boolean wait = true;
-
- @Override
- public void preAddLockRecord(String resource, String owner) {
- if(Owner.A.name().equals(owner)) {
- synchronized(this) {
- if(wait) {
- waitOn(this);
- }
- }
- }
- }
-
- @Override
- public void postAddLockRecord(String resource, String owner) {
- if(!Owner.A.name().equals(owner)) {
- synchronized(this) {
- notifyAll();
- wait = false;
- }
- }
- }
-
- @Override
- public void preUpdateLockRecord(String resource, String owner) {
- preAddLockRecord(resource, owner);
- }
-
- @Override
- public void postUpdateLockRecord(String resource, String owner) {
- postAddLockRecord(resource, owner);
- }
- };
- if(!setSynchronizer(synchronizer)) {
- return;
- }
- ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
- // acquireLock by owner A should fail as it will wait for acquireLock by owner B
- Future<Boolean> future1 = executor.submit(new Callable<Boolean>() {
- @Override
- public Boolean call() throws Exception {
- try {
- lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
- return false;
- } catch(LockException e) {
- // this call should fail as Synchronizer delays its lock to make sure the second call locks the resource first
- Assert.assertEquals("Cannot lock resource [" + Resource.Resource1.name() + "] for [" + Owner.A.name() + "]: already locked by [" + Owner.B.name() + "]", e.getMessage());
- return true;
- }
- }
- });
- try {
- // acquireLock by owner B should success
- Future<Boolean> future2 = executor.submit(new Callable<Boolean>() {
- @Override
- public Boolean call() throws Exception {
- // this call should success as Synchronizer delays the above lock to make sure this call success to lock the resource
- return lockManager.acquireLock(Resource.Resource1.name(), Owner.B.name());
- }
- });
- try {
- Assert.assertTrue(future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS));
- Assert.assertTrue(future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS));
- } finally {
- future2.cancel(true);
- }
- } finally {
- future1.cancel(true);
- }
- }
-
- @Test
- public void testConcurrentLockSameOwner() throws LockException, InterruptedException, ExecutionException, TimeoutException {
- final int participantsNo = 2;
- Synchronizer synchronizer = new Synchronizer(participantsNo) {
-
- private boolean wait = true;
-
- @Override
- public void preAddLockRecord(String resource, String owner) {
- synchronized(this) {
- if(wait) {
- wait = false;
- waitOn(this);
- }
- }
- }
-
- @Override
- public void postAddLockRecord(String resource, String owner) {
- synchronized(this) {
- notifyAll();
- }
- }
- };
- if(!setSynchronizer(synchronizer)) {
- return;
- }
- ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
- // one acquireLock should return true and the other should return false
- Callable<Boolean> callable = new Callable<Boolean>() {
- @Override
- public Boolean call() throws Exception {
- return lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
- }
- };
- Future<Boolean> future1 = executor.submit(callable);
- try {
- Future<Boolean> future2 = executor.submit(callable);
- try {
- boolean future1Res = future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
- boolean future2Res = future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
- // one of the lock requests should return true, the other one false as lock is requested simultaneously from 2 threads by same owner
- Assert.assertNotEquals(future1Res, future2Res);
- } finally {
- future2.cancel(true);
- }
- } finally {
- future1.cancel(true);
- }
- }
-
- @Test
- public void testConcurrentUnlockSameOwner() throws LockException, InterruptedException, ExecutionException, TimeoutException {
- lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
- final int participantsNo = 2;
- Synchronizer synchronizer = new Synchronizer(participantsNo) {
-
- private boolean wait = true;
-
- @Override
- public void preUpdateLockRecord(String resource, String owner) {
- synchronized(this) {
- // make sure second call updates the LockRecord first
- if(wait) {
- wait = false;
- waitOn(this);
- }
- }
- }
-
- @Override
- public void postUpdateLockRecord(String resource, String owner) {
- synchronized(this) {
- notifyAll();
- }
- }
- };
- if(!setSynchronizer(synchronizer)) {
- return;
- }
- ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
- Callable<Boolean> callable = new Callable<Boolean>() {
- @Override
- public Boolean call() throws Exception {
- try {
- lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
- // one of the unlock calls should success
- return true;
- } catch(LockException e) {
- // one of the unlock calls should throw the LockException as the resource should already be unlocked by other call
- Assert.assertEquals("Error unlocking resource [" + Resource.Resource1.name() + "]: resource is not locked", e.getMessage());
- return false;
- }
- }
- };
- Future<Boolean> future1 = executor.submit(callable);
- try {
- Future<Boolean> future2 = executor.submit(callable);
- try {
- boolean future1Res = future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
- boolean future2Res = future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
- // one of the unlock calls should return true, the other one false as unlock is requested simultaneously from 2 threads by same owner
- Assert.assertNotEquals(future1Res, future2Res);
- } finally {
- future2.cancel(true);
- }
- } finally {
- future1.cancel(true);
- }
- }
+ @Override
+ protected JdbcLockManager createJdbcLockManager(boolean useReal) {
+ return new MySqlLockManagerMock(useReal);
+ }
+
+ @Test
+ public void testConcurrentLockDifferentOwners() throws LockException, InterruptedException, ExecutionException, TimeoutException {
+
+ final int participantsNo = 2;
+ Synchronizer synchronizer = new Synchronizer(participantsNo) {
+
+ private boolean wait = true;
+
+ @Override
+ public void preAddLockRecord(String resource, String owner) {
+ if(Owner.A.name().equals(owner)) {
+ synchronized(this) {
+ if(wait) {
+ waitOn(this);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void postAddLockRecord(String resource, String owner) {
+ if(!Owner.A.name().equals(owner)) {
+ synchronized(this) {
+ notifyAll();
+ wait = false;
+ }
+ }
+ }
+
+ @Override
+ public void preUpdateLockRecord(String resource, String owner) {
+ preAddLockRecord(resource, owner);
+ }
+
+ @Override
+ public void postUpdateLockRecord(String resource, String owner) {
+ postAddLockRecord(resource, owner);
+ }
+ };
+ if(!setSynchronizer(synchronizer)) {
+ return;
+ }
+ ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
+ // acquireLock by owner A should fail as it will wait for acquireLock by owner B
+ Future<Boolean> future1 = executor.submit(new Callable<Boolean>() {
+ @Override
+ public Boolean call() throws Exception {
+ try {
+ lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
+ return false;
+ } catch(LockException e) {
+ // this call should fail as Synchronizer delays its lock to make sure the second call locks the resource first
+ Assert.assertEquals("VNF : [" + Resource.Resource1.name() + "] is locked by request id : [" + Owner.B.name() + "]", e.getMessage());
+ return true;
+ }
+ }
+ });
+ try {
+ // acquireLock by owner B should success
+ Future<Boolean> future2 = executor.submit(new Callable<Boolean>() {
+ @Override
+ public Boolean call() throws Exception {
+ // this call should success as Synchronizer delays the above lock to make sure this call success to lock the resource
+ return lockManager.acquireLock(Resource.Resource1.name(), Owner.B.name());
+ }
+ });
+ try {
+ Assert.assertTrue(future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS));
+ Assert.assertTrue(future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS));
+ } finally {
+ future2.cancel(true);
+ }
+ } finally {
+ future1.cancel(true);
+ }
+ }
+
+ @Test
+ public void testConcurrentLockSameOwner() throws LockException, InterruptedException, ExecutionException, TimeoutException {
+ final int participantsNo = 2;
+ Synchronizer synchronizer = new Synchronizer(participantsNo) {
+
+ private boolean wait = true;
+
+ @Override
+ public void preAddLockRecord(String resource, String owner) {
+ synchronized(this) {
+ if(wait) {
+ wait = false;
+ waitOn(this);
+ }
+ }
+ }
+
+ @Override
+ public void postAddLockRecord(String resource, String owner) {
+ synchronized(this) {
+ notifyAll();
+ }
+ }
+ };
+ if(!setSynchronizer(synchronizer)) {
+ return;
+ }
+ ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
+ // one acquireLock should return true and the other should return false
+ Callable<Boolean> callable = new Callable<Boolean>() {
+ @Override
+ public Boolean call() throws Exception {
+ return lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
+ }
+ };
+ Future<Boolean> future1 = executor.submit(callable);
+ try {
+ Future<Boolean> future2 = executor.submit(callable);
+ try {
+ boolean future1Res = future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
+ boolean future2Res = future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
+ // one of the lock requests should return true, the other one false as lock is requested simultaneously from 2 threads by same owner
+ Assert.assertNotEquals(future1Res, future2Res);
+ } finally {
+ future2.cancel(true);
+ }
+ } finally {
+ future1.cancel(true);
+ }
+ }
+
+ @Test
+ public void testConcurrentUnlockSameOwner() throws LockException, InterruptedException, ExecutionException, TimeoutException {
+ lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
+ final int participantsNo = 2;
+ Synchronizer synchronizer = new Synchronizer(participantsNo) {
+
+ private boolean wait = true;
+
+ @Override
+ public void preUpdateLockRecord(String resource, String owner) {
+ synchronized(this) {
+ // make sure second call updates the LockRecord first
+ if(wait) {
+ wait = false;
+ waitOn(this);
+ }
+ }
+ }
+
+ @Override
+ public void postUpdateLockRecord(String resource, String owner) {
+ synchronized(this) {
+ notifyAll();
+ }
+ }
+ };
+ if(!setSynchronizer(synchronizer)) {
+ return;
+ }
+ ExecutorService executor = Executors.newFixedThreadPool(participantsNo);
+ Callable<Boolean> callable = new Callable<Boolean>() {
+ @Override
+ public Boolean call() throws Exception {
+ try {
+ lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
+ // one of the unlock calls should success
+ return true;
+ } catch(LockException e) {
+ // one of the unlock calls should throw the LockException as the resource should already be unlocked by other call
+ Assert.assertEquals("Error unlocking resource [" + Resource.Resource1.name() + "]: resource is not locked", e.getMessage());
+ return false;
+ }
+ }
+ };
+ Future<Boolean> future1 = executor.submit(callable);
+ try {
+ Future<Boolean> future2 = executor.submit(callable);
+ try {
+ boolean future1Res = future1.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
+ boolean future2Res = future2.get(CONCURRENT_TEST_WAIT_TIME, TimeUnit.SECONDS);
+ // one of the unlock calls should return true, the other one false as unlock is requested simultaneously from 2 threads by same owner
+ Assert.assertNotEquals(future1Res, future2Res);
+ } finally {
+ future2.cancel(true);
+ }
+ } finally {
+ future1.cancel(true);
+ }
+ }
}
diff --git a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/pessimistic/MySqlLockManagerMock.java b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/pessimistic/MySqlLockManagerMock.java
index a4bbd3406..51b1c3111 100644
--- a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/pessimistic/MySqlLockManagerMock.java
+++ b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/pessimistic/MySqlLockManagerMock.java
@@ -40,114 +40,114 @@ import org.onap.appc.lockmanager.impl.sql.pessimistic.MySqlLockManager;
class MySqlLockManagerMock extends MySqlLockManager implements SynchronizerReceiver {
- private final Map<String, LockRecord> locks = new HashMap<>();
- private final Lock lock = new ReentrantLock();
- private boolean useReal;
- private Synchronizer synchronizer;
+ private final Map<String, LockRecord> locks = new HashMap<>();
+ private final Lock lock = new ReentrantLock();
+ private boolean useReal;
+ private Synchronizer synchronizer;
- MySqlLockManagerMock(boolean useReal) {
- this.useReal = useReal;
- }
+ MySqlLockManagerMock(boolean useReal) {
+ this.useReal = useReal;
+ }
- @Override
- public void setSynchronizer(Synchronizer synchronizer) {
- this.synchronizer = synchronizer;
- }
+ @Override
+ public void setSynchronizer(Synchronizer synchronizer) {
+ this.synchronizer = synchronizer;
+ }
- @Override
- protected Connection openDbConnection() {
- if(useReal) {
- return super.openDbConnection();
- }
- return null;
- }
+ @Override
+ protected Connection openDbConnection() {
+ if(useReal) {
+ return super.openDbConnection();
+ }
+ return null;
+ }
- @Override
- protected void closeDbConnection(Connection connection) {
- if(useReal) {
- super.closeDbConnection(connection);
- }
- }
+ @Override
+ protected void closeDbConnection(Connection connection) {
+ if(useReal) {
+ super.closeDbConnection(connection);
+ }
+ }
- @Override
- protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
- LockRecord res;
- if(useReal) {
- res = super.loadLockRecord(connection, resource);
- } else {
- res = locks.get(resource);
- }
- if(synchronizer != null) {
- synchronizer.postLoadLockRecord(resource, (res == null) ? null : res.getOwner());
- }
- return res;
- }
+ @Override
+ protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
+ LockRecord res;
+ if(useReal) {
+ res = super.loadLockRecord(connection, resource);
+ } else {
+ res = locks.get(resource);
+ }
+ if(synchronizer != null) {
+ synchronizer.postLoadLockRecord(resource, (res == null) ? null : res.getOwner());
+ }
+ return res;
+ }
- @Override
- protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
- if(synchronizer != null) {
- synchronizer.preAddLockRecord(resource, owner);
- }
- try {
- if(useReal) {
- super.addLockRecord(connection, resource, owner, timeout);
- return;
- }
- LockRecord lockRecord = new LockRecord(resource);
- lockRecord.setOwner(owner);
- lockRecord.setUpdated(System.currentTimeMillis());
- lockRecord.setTimeout(timeout);
- locks.put(resource, lockRecord);
- } finally {
- if(synchronizer != null) {
- synchronizer.postAddLockRecord(resource, owner);
- }
- }
- }
+ @Override
+ protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
+ if(synchronizer != null) {
+ synchronizer.preAddLockRecord(resource, owner);
+ }
+ try {
+ if(useReal) {
+ super.addLockRecord(connection, resource, owner, timeout);
+ return;
+ }
+ LockRecord lockRecord = new LockRecord(resource);
+ lockRecord.setOwner(owner);
+ lockRecord.setUpdated(System.currentTimeMillis());
+ lockRecord.setTimeout(timeout);
+ locks.put(resource, lockRecord);
+ } finally {
+ if(synchronizer != null) {
+ synchronizer.postAddLockRecord(resource, owner);
+ }
+ }
+ }
- @Override
- protected void updateLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
- if(synchronizer != null) {
- synchronizer.preUpdateLockRecord(resource, owner);
- }
- try {
- if(useReal) {
- super.updateLockRecord(connection, resource, owner, timeout);
- return;
- }
- LockRecord lockRecord = loadLockRecord(connection, resource);
- lockRecord.setOwner(owner);
- lockRecord.setUpdated(System.currentTimeMillis());
- lockRecord.setTimeout(timeout);
- locks.put(resource, lockRecord);
- } finally {
- if(synchronizer != null) {
- synchronizer.postUpdateLockRecord(resource, owner);
- }
- }
- }
+ @Override
+ protected void updateLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
+ if(synchronizer != null) {
+ synchronizer.preUpdateLockRecord(resource, owner);
+ }
+ try {
+ if(useReal) {
+ super.updateLockRecord(connection, resource, owner, timeout);
+ return;
+ }
+ LockRecord lockRecord = loadLockRecord(connection, resource);
+ lockRecord.setOwner(owner);
+ lockRecord.setUpdated(System.currentTimeMillis());
+ lockRecord.setTimeout(timeout);
+ locks.put(resource, lockRecord);
+ } finally {
+ if(synchronizer != null) {
+ synchronizer.postUpdateLockRecord(resource, owner);
+ }
+ }
+ }
- @Override
- protected void enterCriticalSection(Connection connection, String resource) {
- if(useReal) {
- super.enterCriticalSection(connection, resource);
- return;
- }
- try {
- if(!lock.tryLock(criticalSectionWaitTimeoutSecs, TimeUnit.SECONDS)) {
+ @Override
+ protected void enterCriticalSection(Connection connection, String resource) {
+ if(useReal) {
+ super.enterCriticalSection(connection, resource);
+ return;
+ }
+ try {
+ if(!lock.tryLock(criticalSectionWaitTimeoutSecs, TimeUnit.SECONDS)) {
throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].");
- }
- } catch(InterruptedException e) {
- throw new LockRuntimeException("Cannot obtain critical section lock.", e);
- }
- }
+ }
+ } catch(InterruptedException e) {
+ throw new LockRuntimeException("Cannot obtain critical section lock.", e);
+ }
+ }
- @Override
- protected void leaveCriticalSection(Connection connection, String resource) {
- if(useReal) {
- super.leaveCriticalSection(connection, resource);
- return;
- }
- lock.unlock();
- }
+ @Override
+ protected void leaveCriticalSection(Connection connection, String resource) {
+ if(useReal) {
+ super.leaveCriticalSection(connection, resource);
+ return;
+ }
+ lock.unlock();
+ }
}
diff --git a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/pessimistic/TestMySqlLockManager.java b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/pessimistic/TestMySqlLockManager.java
index c54cfc81a..b13207d6d 100644
--- a/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/pessimistic/TestMySqlLockManager.java
+++ b/appc-dispatcher/appc-dispatcher-common/lock-manager-lib/lock-manager-impl/src/test/java/org/onap/appc/lockmanager/impl/sql/pessimistic/TestMySqlLockManager.java
@@ -39,13 +39,13 @@ public class TestMySqlLockManager extends MySqlLockManagerBaseTests {
private static int CRITICAL_SECTION_WAIT_TIMEOUT = 1; // in secs
- @Override
- protected JdbcLockManager createJdbcLockManager(boolean useReal) {
- return new MySqlLockManagerMock(useReal);
- }
+ @Override
+ protected JdbcLockManager createJdbcLockManager(boolean useReal) {
+ return new MySqlLockManagerMock(useReal);
+ }
- @Test
- public void testConcurrentLock() throws LockException, InterruptedException, ExecutionException, TimeoutException {
+ @Test
+ public void testConcurrentLock() throws LockException, InterruptedException, ExecutionException, TimeoutException {
try {
callConcurrentTest(new Callable<Boolean>() {
@Override
@@ -61,10 +61,10 @@ public class TestMySqlLockManager extends MySqlLockManagerBaseTests {
});
} finally {
lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
- }
- }
+ }
+ }
- @Test
+ @Test
public void testConcurrentUnlock() throws LockException, InterruptedException, ExecutionException, TimeoutException {
lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
callConcurrentTest(new Callable<Boolean>() {