From 2f2675dd6c0f591f94a7706d6658db4ceafc14ce Mon Sep 17 00:00:00 2001 From: abilan Date: Tue, 14 Feb 2023 14:12:49 -0500 Subject: [PATCH] Fix DynamoDbLockRegistry for additionalTimeToWait Fixes https://github.com/spring-cloud/spring-cloud-stream-binder-aws-kinesis/issues/186 The `DynamoDbLockClient` waits extra `leaseDuration` time in a loop breaking a `tryLock()` contract. * Fix `DynamoDbLockRegistry.tryLock()` to decrease an actual `additionalTimeToWait` by `leaseDuration`, so the target `DynamoDbLockClient` when it adds this `leaseDuration` will wait an actual timeout requested by the `tryLock()` contract. This way a `tryLock(0)` will definitely return immediately since we really are not interested in blocking **cherry-pick to 2.5.x** --- build.gradle | 7 +++++-- .../integration/aws/lock/DynamoDbLockRegistry.java | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index 7a06910..1c7743b 100644 --- a/build.gradle +++ b/build.gradle @@ -93,6 +93,7 @@ dependencyManagement { mavenBom "com.fasterxml.jackson:jackson-bom:$jacksonVersion" mavenBom "org.junit:junit-bom:$junitVersion" mavenBom "org.testcontainers:testcontainers-bom:$testcontainersVersion" + mavenBom "org.apache.logging.log4j:log4j-bom:$log4jVersion" } } @@ -136,8 +137,10 @@ dependencies { testImplementation 'org.testcontainers:junit-jupiter' testImplementation 'org.testcontainers:localstack' - testRuntimeOnly "org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion" - testRuntimeOnly "org.apache.logging.log4j:log4j-jcl:$log4jVersion" + testRuntimeOnly 'org.apache.logging.log4j:log4j-core' + testRuntimeOnly 'org.apache.logging.log4j:log4j-jcl' + testRuntimeOnly 'org.apache.logging.log4j:log4j-slf4j-impl' + testRuntimeOnly 'org.apache.logging.log4j:log4j-slf4j2-impl' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } 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 5b8fe5e..fbf8925 100644 --- a/src/main/java/org/springframework/integration/aws/lock/DynamoDbLockRegistry.java +++ b/src/main/java/org/springframework/integration/aws/lock/DynamoDbLockRegistry.java @@ -198,7 +198,7 @@ public class DynamoDbLockRegistry implements ExpirableLockRegistry, Initializing } /** - * Specify a period in milliseconds how often send locks renewal requests called heartbeat. + * Specify a period in seconds how often send locks renewal requests called heartbeat. * When the value is less than or equal to {@code 0}, the heartbeat is disabled. * @param heartbeatPeriod the heartbeat period for background thread to renew locks in DB */ @@ -501,8 +501,9 @@ public class DynamoDbLockRegistry implements ExpirableLockRegistry, Initializing return false; } - long additionalTimeToWait = Math - .max(TimeUnit.MILLISECONDS.convert(time, unit) - System.currentTimeMillis() + start, 0L); + long additionalTimeToWait = + TimeUnit.MILLISECONDS.convert(time, unit) + - System.currentTimeMillis() + start - DynamoDbLockRegistry.this.leaseDuration; this.acquireLockOptionsBuilder.withAdditionalTimeToWaitForLock(additionalTimeToWait);