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**

# Conflicts:
#	build.gradle
This commit is contained in:
abilan
2023-02-14 14:12:49 -05:00
parent 624740b8d7
commit 390417d898
2 changed files with 9 additions and 5 deletions

View File

@@ -95,6 +95,7 @@ dependencyManagement {
mavenBom "org.springframework.integration:spring-integration-bom:$springIntegrationVersion"
mavenBom "com.fasterxml.jackson:jackson-bom:$jacksonVersion"
mavenBom "org.junit:junit-bom:$junitVersion"
mavenBom "org.apache.logging.log4j:log4j-bom:$log4jVersion"
}
}
@@ -137,8 +138,10 @@ dependencies {
}
testImplementation 'org.junit.jupiter:junit-jupiter-api'
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'
}

View File

@@ -199,7 +199,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
*/
@@ -502,8 +502,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);