RA: Add timeout parameters to the lock API 65/71465/1
authorStan Bonev <sb5356@att.com>
Tue, 30 Oct 2018 14:02:42 +0000 (10:02 -0400)
committerStan Bonev <sb5356@att.com>
Tue, 30 Oct 2018 14:02:42 +0000 (10:02 -0400)
Change-Id: I5f8cd856d245829036ed563969fe75887e443467
Issue-ID: CCSDK-642
Signed-off-by: Stan Bonev <sb5356@att.com>
resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/lock/comp/LockHelper.java
resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/lock/comp/LockHelperImpl.java
resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/ra/ResourceLockNode.java

index d33703d..040d192 100644 (file)
@@ -8,9 +8,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -27,9 +27,13 @@ public interface LockHelper {
 
     void lock(String resourceName, String lockRequester, int lockTimeout /* Seconds */);
 
+    void lock(String resourceName, String lockRequester, int lockTimeout /* Seconds */, int lockWait /* Seconds */, int retryCount);
+
     void unlock(String resourceName, boolean force);
 
     void lock(Collection<String> resourceNameList, String lockRequester, int lockTimeout /* Seconds */);
 
+    void lock(Collection<String> resourceNameList, String lockRequester, int lockTimeout /* Seconds */, int lockWait /* Seconds */, int retryCount);
+
     void unlock(Collection<String> resourceNameList, boolean force);
 }
index 3a8c730..1611b53 100644 (file)
@@ -41,7 +41,12 @@ public class LockHelperImpl implements LockHelper {
 
     @Override
     public void lock(String resourceName, String lockRequester, int lockTimeout /* Seconds */) {
-        lock(Collections.singleton(resourceName), lockRequester, lockTimeout);
+        lock(resourceName, lockRequester, lockTimeout, lockWait, retryCount);
+    }
+
+    @Override
+    public void lock(String resourceName, String lockRequester, int lockTimeout /* Seconds */, int lockWait /* Seconds */, int retryCount) {
+        lock(Collections.singleton(resourceName), lockRequester, lockTimeout, lockWait, retryCount);
     }
 
     @Override
@@ -51,13 +56,18 @@ public class LockHelperImpl implements LockHelper {
 
     @Override
     public void lock(Collection<String> resourceNameList, String lockRequester, int lockTimeout /* Seconds */) {
+        lock(resourceNameList, lockRequester, lockTimeout, lockWait, retryCount);
+    }
+
+    @Override
+    public void lock(Collection<String> resourceNameList, String lockRequester, int lockTimeout /* Seconds */, int lockWait /* Seconds */, int retryCount) {
         for (int i = 0; true; i++) {
             try {
                 tryLock(resourceNameList, lockRequester, lockTimeout);
                 log.info("Resources locked: " + resourceNameList);
                 return;
             } catch (ResourceLockedException e) {
-                if (i > retryCount) {
+                if (i >= retryCount) {
                     throw e;
                 }
                 try {
index d1ba1eb..a7d4b0e 100644 (file)
@@ -45,8 +45,12 @@ public class ResourceLockNode implements SvcLogicJavaPlugin {
         String lockRequester = getParam(paramMap, "lock-requester", false, generateLockRequester());
         String lockTimeoutStr = getParam(paramMap, "lock-timeout", false, "600"); // Default lock timeout: 10 min
         int lockTimeout = Integer.parseInt(lockTimeoutStr);
+        String lockWaitStr = getParam(paramMap, "lock-wait", false, "5"); // Time waiting before next retry. Default: 5 sec
+        int lockWait = Integer.parseInt(lockWaitStr);
+        String lockRetryCountStr = getParam(paramMap, "lock-retry-count", false, "10"); // Default: 10 retries
+        int lockRetryCount = Integer.parseInt(lockRetryCountStr);
 
-        lockHelper.lock(resourceName, lockRequester, lockTimeout);
+        lockHelper.lock(resourceName, lockRequester, lockTimeout, lockWait, lockRetryCount);
     }
 
     public void unlockResource(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
@@ -56,6 +60,10 @@ public class ResourceLockNode implements SvcLogicJavaPlugin {
     }
 
     public void lockResource(String resourceName, String lockRequester, int lockTimeout /* sec */) {
+        lockResource(resourceName, lockRequester, lockTimeout, 5, 10);
+    }
+
+    public void lockResource(String resourceName, String lockRequester, int lockTimeout /* sec */, int lockWait /* Seconds */, int retryCount) {
         if (lockRequester == null) {
             lockRequester = generateLockRequester();
         }
@@ -63,7 +71,7 @@ public class ResourceLockNode implements SvcLogicJavaPlugin {
             lockTimeout = 600;
         }
 
-        lockHelper.lock(resourceName, lockRequester, lockTimeout);
+        lockHelper.lock(resourceName, lockRequester, lockTimeout, lockWait, retryCount);
     }
 
     public void unlockResource(String resourceName) {