From ed89f1243f2ff6c7bffcdbfc291da37281c5c345 Mon Sep 17 00:00:00 2001 From: Karl Lessard Date: Fri, 21 Sep 2018 15:46:04 -0400 Subject: [PATCH] GH-97: wait at least leaseDuration to acquire the lock Fixes https://github.com/spring-projects/spring-integration-aws/issues/97 This fixes an issue where `additionalTimeToWait` has a negative value that is subtracted later by the Amazon DynamoDB client to `leaseDuration`, making it impossible to acquire the lock if expired. Also allow to use the existing `refreshPeriod` properties in this case. * Use default refresh period in tryLock() The 'refreshPeriod' value might not satisfy both tryLock() and lock() requirements and should probably be used exclusively in the second case. We can make the value used for tryLock() configurable as well in another pull request. * Rollback previous commit and add author name --- .../integration/aws/lock/DynamoDbLockRegistry.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/integration/aws/lock/DynamoDbLockRegistry.java b/src/main/java/org/springframework/integration/aws/lock/DynamoDbLockRegistry.java index f2ec880..7584694 100644 --- a/src/main/java/org/springframework/integration/aws/lock/DynamoDbLockRegistry.java +++ b/src/main/java/org/springframework/integration/aws/lock/DynamoDbLockRegistry.java @@ -60,6 +60,7 @@ import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; * Can create table in DynamoDB if an external {@link AmazonDynamoDBLockClient} is not provided. * * @author Artem Bilan + * @author Karl Lessard * * @since 2.0 */ @@ -467,11 +468,11 @@ public class DynamoDbLockRegistry implements ExpirableLockRegistry, Initializing return false; } - long additionalTimeToWait = TimeUnit.MILLISECONDS.convert(time, unit) - System.currentTimeMillis() + start; + long additionalTimeToWait = Math.max(TimeUnit.MILLISECONDS.convert(time, unit) - System.currentTimeMillis() + start, 0L); this.acquireLockOptionsBuilder .withAdditionalTimeToWaitForLock(additionalTimeToWait) - .withRefreshPeriod(0L); + .withRefreshPeriod(DynamoDbLockRegistry.this.refreshPeriod); boolean acquired = false; try {