Some tweaks for DynamoDB locks heartbeat

Related to https://github.com/spring-cloud/spring-cloud-stream-binder-aws-kinesis/issues/180

* Treat non-positive `DynamoDbLockRegistry.heartbeatPeriod` as no heartbeat.
This way locks renewal is a responsibility of the target `DynamoDbLockRegistry` consumer.
For example, the `KinesisMessageDrivenChannelAdapter` does call `tryLock()` in a loop
for locks on shards it is consuming at the moment
* Increase locks loop sleep timeout in the `KinesisMessageDrivenChannelAdapter` to one second
to avoid many requests to DynamoDB

**Cherry-pick to `2.5.x`**

# Conflicts:
#	src/main/java/org/springframework/integration/aws/inbound/kinesis/KinesisMessageDrivenChannelAdapter.java
This commit is contained in:
abilan
2023-01-10 10:50:55 -05:00
parent 4f516a37de
commit e4a72cee17
2 changed files with 10 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 the original author or authors.
* Copyright 2017-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -1561,7 +1561,7 @@ public class KinesisMessageDrivenChannelAdapter extends MessageProducerSupport
}
}
sleep(250,
sleep(1000,
new IllegalStateException("ShardConsumerManager Thread [" + this + "] has been interrupted"),
true);
}

View File

@@ -26,7 +26,6 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
@@ -138,12 +137,6 @@ public class DynamoDbLockRegistry implements ExpirableLockRegistry, Initializing
private long heartbeatPeriod = 5L;
/**
* Flag to denote whether the {@link ExecutorService} was provided via the setter and
* thus should not be shutdown when {@link #destroy()} is called.
*/
private boolean executorExplicitlySet;
private volatile boolean initialized;
public DynamoDbLockRegistry(AmazonDynamoDB dynamoDB) {
@@ -205,6 +198,11 @@ public class DynamoDbLockRegistry implements ExpirableLockRegistry, Initializing
this.leaseDuration = leaseDuration;
}
/**
* Specify a period in milliseconds 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
*/
public void setHeartbeatPeriod(long heartbeatPeriod) {
this.heartbeatPeriod = heartbeatPeriod;
}
@@ -228,7 +226,9 @@ public class DynamoDbLockRegistry implements ExpirableLockRegistry, Initializing
if (!this.dynamoDBLockClientExplicitlySet) {
AmazonDynamoDBLockClientOptions dynamoDBLockClientOptions = AmazonDynamoDBLockClientOptions
.builder(this.dynamoDB, this.tableName).withPartitionKeyName(this.partitionKey)
.withSortKeyName(this.sortKeyName).withHeartbeatPeriod(this.heartbeatPeriod)
.withSortKeyName(this.sortKeyName)
.withCreateHeartbeatBackgroundThread(this.heartbeatPeriod > 0)
.withHeartbeatPeriod(this.heartbeatPeriod)
.withLeaseDuration(this.leaseDuration).build();
this.dynamoDBLockClient = new AmazonDynamoDBLockClient(dynamoDBLockClientOptions);