From 0cf282459fe7cf129a390d59d46b6de299ac86db Mon Sep 17 00:00:00 2001 From: Stan Bonev Date: Thu, 10 Jan 2019 12:52:58 -0500 Subject: RA: Fix locking when there is expired lock in DB Change-Id: I026bdea43c0386689c98902aac95be02e27f7704 Issue-ID: CCSDK-937 Signed-off-by: Stan Bonev --- .../sli/adaptors/lock/comp/LockHelperImpl.java | 15 +++++++--- .../sli/adaptors/lock/dao/ResourceLockDao.java | 2 +- .../sli/adaptors/lock/dao/ResourceLockDaoImpl.java | 32 +++++++++------------- .../sli/adaptors/ra/TestResourceLockNode.java | 1 + 4 files changed, 26 insertions(+), 24 deletions(-) (limited to 'resource-assignment/provider/src') diff --git a/resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/lock/comp/LockHelperImpl.java b/resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/lock/comp/LockHelperImpl.java index 0efd8977..735d240c 100644 --- a/resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/lock/comp/LockHelperImpl.java +++ b/resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/lock/comp/LockHelperImpl.java @@ -45,7 +45,8 @@ public class LockHelperImpl implements LockHelper { } @Override - public void lock(String resourceName, String lockRequester, int lockTimeout /* Seconds */, int lockWait /* Seconds */, int retryCount) { + public void lock(String resourceName, String lockRequester, int lockTimeout /* Seconds */, + int lockWait /* Seconds */, int retryCount) { lock(Collections.singleton(resourceName), lockRequester, lockTimeout, lockWait, retryCount); } @@ -60,7 +61,8 @@ public class LockHelperImpl implements LockHelper { } @Override - public void lock(Collection resourceNameList, String lockRequester, int lockTimeout /* Seconds */, int lockWait /* Seconds */, int retryCount) { + public void lock(Collection resourceNameList, String lockRequester, int lockTimeout /* Seconds */, + int lockWait /* Seconds */, int retryCount) { for (int i = 0; true; i++) { try { tryLock(resourceNameList, lockRequester, lockTimeout); @@ -129,6 +131,9 @@ public class LockHelperImpl implements LockHelper { } if (l != null) { + if (now.getTime() > l.expirationTime.getTime() || l.lockCount <= 0) { + l.lockCount = 0; + } dbLockList.add(l); } else { insertLockNameList.add(name); @@ -137,7 +142,7 @@ public class LockHelperImpl implements LockHelper { // Update the lock info in DB for (ResourceLock l : dbLockList) { - resourceLockDao.update(l.id, now, new Date(now.getTime() + lockTimeout * 1000), l.lockCount + 1); + resourceLockDao.update(l.id, lockRequester, now, new Date(now.getTime() + lockTimeout * 1000), l.lockCount + 1); } // Insert records for those that are not yet there @@ -159,7 +164,9 @@ public class LockHelperImpl implements LockHelper { resourceLockDao.commit(); - } finally { + }finally + + { resourceLockDao.rollback(); } } diff --git a/resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/lock/dao/ResourceLockDao.java b/resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/lock/dao/ResourceLockDao.java index f9d41135..dc86f70a 100644 --- a/resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/lock/dao/ResourceLockDao.java +++ b/resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/lock/dao/ResourceLockDao.java @@ -28,7 +28,7 @@ public interface ResourceLockDao { void add(ResourceLock l); - void update(long id, Date lockTime, Date expirationTime, int lockCount); + void update(long id, String lockHolder, Date lockTime, Date expirationTime, int lockCount); ResourceLock getByResourceName(String resourceName); diff --git a/resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/lock/dao/ResourceLockDaoImpl.java b/resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/lock/dao/ResourceLockDaoImpl.java index 48541922..b8b3a66d 100644 --- a/resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/lock/dao/ResourceLockDaoImpl.java +++ b/resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/lock/dao/ResourceLockDaoImpl.java @@ -21,8 +21,6 @@ package org.onap.ccsdk.sli.adaptors.lock.dao; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.Date; import java.util.List; import org.onap.ccsdk.sli.adaptors.lock.data.ResourceLock; @@ -44,32 +42,28 @@ public class ResourceLockDaoImpl implements ResourceLockDao { jdbcTemplate.update( "INSERT INTO RESOURCE_LOCK (resource_name, lock_holder, lock_count, lock_time, expiration_time)\n" + "VALUES (?, ?, ?, ?, ?)", - new Object[] { l.resourceName, l.lockHolder, l.lockCount, l.lockTime, l.expirationTime }); + new Object[] { l.resourceName, l.lockHolder, l.lockCount, l.lockTime, l.expirationTime }); } @Override - public void update(long id, Date lockTime, Date expirationTime, int lockCount) { + public void update(long id, String lockHolder, Date lockTime, Date expirationTime, int lockCount) { jdbcTemplate.update( - "UPDATE RESOURCE_LOCK SET lock_time = ?, expiration_time = ?, lock_count = ? WHERE resource_lock_id = ?", - new Object[] { lockTime, expirationTime, lockCount, id }); + "UPDATE RESOURCE_LOCK SET lock_holder = ?, lock_time = ?, expiration_time = ?, lock_count = ? WHERE resource_lock_id = ?", + new Object[] { lockHolder, lockTime, expirationTime, lockCount, id }); } @Override public ResourceLock getByResourceName(String resourceName) { List ll = jdbcTemplate.query("SELECT * FROM RESOURCE_LOCK WHERE resource_name = ?", - new Object[] { resourceName }, new RowMapper() { - - @Override - public ResourceLock mapRow(ResultSet rs, int rowNum) throws SQLException { - ResourceLock rl = new ResourceLock(); - rl.id = rs.getLong("resource_lock_id"); - rl.resourceName = rs.getString("resource_name"); - rl.lockHolder = rs.getString("lock_holder"); - rl.lockCount = rs.getInt("lock_count"); - rl.lockTime = rs.getTimestamp("lock_time"); - rl.expirationTime = rs.getTimestamp("expiration_time"); - return rl; - } + new Object[] { resourceName }, (RowMapper) (rs, rowNum) -> { + ResourceLock rl = new ResourceLock(); + rl.id = rs.getLong("resource_lock_id"); + rl.resourceName = rs.getString("resource_name"); + rl.lockHolder = rs.getString("lock_holder"); + rl.lockCount = rs.getInt("lock_count"); + rl.lockTime = rs.getTimestamp("lock_time"); + rl.expirationTime = rs.getTimestamp("expiration_time"); + return rl; }); return ll != null && !ll.isEmpty() ? ll.get(0) : null; } diff --git a/resource-assignment/provider/src/test/java/jtest/org/onap/ccsdk/sli/adaptors/ra/TestResourceLockNode.java b/resource-assignment/provider/src/test/java/jtest/org/onap/ccsdk/sli/adaptors/ra/TestResourceLockNode.java index 377257c4..bfb9ad7e 100644 --- a/resource-assignment/provider/src/test/java/jtest/org/onap/ccsdk/sli/adaptors/ra/TestResourceLockNode.java +++ b/resource-assignment/provider/src/test/java/jtest/org/onap/ccsdk/sli/adaptors/ra/TestResourceLockNode.java @@ -28,6 +28,7 @@ public class TestResourceLockNode { public void test1() throws Exception { Map paramMap = new HashMap<>(); paramMap.put("resource-name", "test-resource-1"); + paramMap.put("lock-requester", "SDNA"); resourceLockNode.lockResource(paramMap, null); resourceLockNode.unlockResource(paramMap, null); -- cgit 1.2.3-korg