public class DynamoDBLeaseRefresher extends Object implements LeaseRefresher
LeaseRefresher that uses DynamoDB.| Modifier and Type | Field and Description |
|---|---|
protected boolean |
consistentReads |
protected software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient |
dynamoDBClient |
protected LeaseSerializer |
serializer |
protected String |
table |
| Constructor and Description |
|---|
DynamoDBLeaseRefresher(String table,
software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient dynamoDBClient,
LeaseSerializer serializer,
boolean consistentReads,
@NonNull TableCreatorCallback tableCreatorCallback,
Duration dynamoDbRequestTimeout,
DdbTableConfig ddbTableConfig,
boolean leaseTableDeletionProtectionEnabled,
boolean leaseTablePitrEnabled,
Collection<software.amazon.awssdk.services.dynamodb.model.Tag> tags)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
assignLease(Lease lease,
String newOwner)
Assigns given lease to newOwner owner by incrementing its leaseCounter and setting its owner field.
|
protected DependencyException |
convertAndRethrowExceptions(String operation,
String leaseKey,
Exception e) |
boolean |
createLeaseIfNotExists(@NonNull Lease lease)
Create a new lease.
|
String |
createLeaseOwnerToLeaseKeyIndexIfNotExists()
Creates the LeaseOwnerToLeaseKey index on the lease table if it doesn't exist and returns the status of index.
|
boolean |
createLeaseTableIfNotExists()
Creates the table that will store leases.
|
boolean |
createLeaseTableIfNotExists(@NonNull Long readCapacity,
@NonNull Long writeCapacity)
Creates the table that will store leases.
|
void |
deleteAll()
Delete all leases from DynamoDB.
|
void |
deleteLease(@NonNull Lease lease)
Delete the given lease from DynamoDB.
|
boolean |
evictLease(@NonNull Lease lease)
Evict the current owner of lease by setting owner to null.
|
ExtendedSequenceNumber |
getCheckpoint(String leaseKey)
Gets the current checkpoint of the shard.
|
Lease |
getLease(@NonNull String leaseKey) |
String |
getLeaseTableIdentifier() |
boolean |
initiateGracefulLeaseHandoff(Lease lease,
String newOwner)
Initiates a graceful handoff of the given lease to the specified new owner, allowing the current owner
to complete its processing before transferring ownership.
|
boolean |
isLeaseOwnerToLeaseKeyIndexActive()
Check if leaseOwner GSI is ACTIVE
|
boolean |
isLeaseTableEmpty()
Check (synchronously) if there are any leases in the lease table.
|
boolean |
leaseTableExists() |
List<String> |
listLeaseKeysForWorker(String workerIdentifier)
List all leases for a given workerIdentifier synchronously.
|
List<Lease> |
listLeases()
List all objects in table synchronously.
|
List<Lease> |
listLeasesForStream(StreamIdentifier streamIdentifier)
List all leases for a given stream synchronously.
|
Map.Entry<List<Lease>,List<String>> |
listLeasesParallely(ExecutorService parallelScanExecutorService,
int parallelScanTotalSegment)
List all leases from the storage parallely and deserialize into Lease objects.
|
boolean |
renewLease(@NonNull Lease lease)
Renew a lease by incrementing the lease counter.
|
boolean |
takeLease(@NonNull Lease lease,
@NonNull String owner)
Take a lease for the given owner by incrementing its leaseCounter and setting its owner field.
|
boolean |
updateLease(@NonNull Lease lease)
Update application-specific fields of the given lease in DynamoDB.
|
void |
updateLeaseWithMetaInfo(Lease lease,
UpdateField updateField)
Update application-specific fields of the given lease in DynamoDB.
|
boolean |
waitUntilLeaseOwnerToLeaseKeyIndexExists(long secondsBetweenPolls,
long timeoutSeconds)
Blocks until the index exists by polling storage till either the index is ACTIVE or else timeout has
happened.
|
boolean |
waitUntilLeaseTableExists(long secondsBetweenPolls,
long timeoutSeconds)
Blocks until the lease table exists by polling leaseTableExists.
|
protected final String table
protected final software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient dynamoDBClient
protected final LeaseSerializer serializer
protected final boolean consistentReads
public DynamoDBLeaseRefresher(String table, software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient dynamoDBClient, LeaseSerializer serializer, boolean consistentReads, @NonNull @NonNull TableCreatorCallback tableCreatorCallback, Duration dynamoDbRequestTimeout, DdbTableConfig ddbTableConfig, boolean leaseTableDeletionProtectionEnabled, boolean leaseTablePitrEnabled, Collection<software.amazon.awssdk.services.dynamodb.model.Tag> tags)
table - dynamoDBClient - serializer - consistentReads - tableCreatorCallback - dynamoDbRequestTimeout - ddbTableConfig - leaseTableDeletionProtectionEnabled - leaseTablePitrEnabled - tags - public boolean createLeaseTableIfNotExists(@NonNull
@NonNull Long readCapacity,
@NonNull
@NonNull Long writeCapacity)
throws ProvisionedThroughputException,
DependencyException
LeaseRefresher.createLeaseTableIfNotExists().
This method always creates table in PROVISIONED mode and with RCU and WCU provided as method argscreateLeaseTableIfNotExists in interface LeaseRefresherProvisionedThroughputException - if we cannot create the lease table due to per-AWS-account capacity
restrictions.DependencyException - if DynamoDB createTable fails in an unexpected waypublic boolean createLeaseTableIfNotExists()
throws ProvisionedThroughputException,
DependencyException
createLeaseTableIfNotExists in interface LeaseRefresherProvisionedThroughputException - if we cannot create the lease table due to per-AWS-account capacity
restrictions.DependencyException - if DynamoDB createTable fails in an unexpected waypublic boolean leaseTableExists()
throws DependencyException
leaseTableExists in interface LeaseRefresherDependencyException - if DynamoDB describeTable fails in an unexpected waypublic boolean waitUntilLeaseTableExists(long secondsBetweenPolls,
long timeoutSeconds)
throws DependencyException
LeaseRefresherwaitUntilLeaseTableExists in interface LeaseRefreshersecondsBetweenPolls - time to wait between polls in secondstimeoutSeconds - total time to wait in secondsDependencyException - if DynamoDB describeTable fails in an unexpected waypublic String createLeaseOwnerToLeaseKeyIndexIfNotExists() throws DependencyException
LeaseRefreshercreateLeaseOwnerToLeaseKeyIndexIfNotExists in interface LeaseRefresherDependencyException - if storage's describe API fails in an unexpected waypublic boolean waitUntilLeaseOwnerToLeaseKeyIndexExists(long secondsBetweenPolls,
long timeoutSeconds)
LeaseRefresherwaitUntilLeaseOwnerToLeaseKeyIndexExists in interface LeaseRefreshersecondsBetweenPolls - time to wait between polls in secondstimeoutSeconds - total time to wait in secondspublic boolean isLeaseOwnerToLeaseKeyIndexActive()
throws DependencyException
LeaseRefresherisLeaseOwnerToLeaseKeyIndexActive in interface LeaseRefresherDependencyException - if storage's describe API fails in an unexpected waypublic List<Lease> listLeasesForStream(StreamIdentifier streamIdentifier) throws DependencyException, InvalidStateException, ProvisionedThroughputException
listLeasesForStream in interface LeaseRefresherDependencyException - if DynamoDB scan fails in an unexpected wayInvalidStateException - if lease table does not existProvisionedThroughputException - if DynamoDB scan fails due to lack of capacitypublic List<String> listLeaseKeysForWorker(String workerIdentifier) throws DependencyException, InvalidStateException
LEASE_OWNER_TO_LEASE_KEY_INDEX_NAME does not exists.
If index creation is not done and want to listLeases for a worker,
use listLeases() and filter on that to list leases.listLeaseKeysForWorker in interface LeaseRefresherDependencyException - if DynamoDB scan fails in an unexpected wayInvalidStateException - if lease table does not existpublic List<Lease> listLeases() throws DependencyException, InvalidStateException, ProvisionedThroughputException
listLeases in interface LeaseRefresherDependencyException - if DynamoDB scan fails in an unexpected wayInvalidStateException - if lease table does not existProvisionedThroughputException - if DynamoDB scan fails due to lack of capacitypublic Map.Entry<List<Lease>,List<String>> listLeasesParallely(ExecutorService parallelScanExecutorService, int parallelScanTotalSegment) throws DependencyException, InvalidStateException, ProvisionedThroughputException
LeaseRefresherlistLeasesParallely in interface LeaseRefresherparallelScanExecutorService - thread pool to use for parallel scanparallelScanTotalSegment - no. of parallel scans.
If parallelismFactor is 0 then parallelismFactor will be calculated based on table sizeDependencyException - if DynamoDB scan fails in an unexpected wayInvalidStateException - if lease table does not existProvisionedThroughputException - if DynamoDB scan fails due to lack of capacitypublic boolean isLeaseTableEmpty()
throws DependencyException,
InvalidStateException,
ProvisionedThroughputException
isLeaseTableEmpty in interface LeaseRefresherDependencyException - if DynamoDB scan fails in an unexpected wayInvalidStateException - if lease table does not existProvisionedThroughputException - if DynamoDB scan fails due to lack of capacitypublic boolean createLeaseIfNotExists(@NonNull
@NonNull Lease lease)
throws DependencyException,
InvalidStateException,
ProvisionedThroughputException
createLeaseIfNotExists in interface LeaseRefresherlease - the lease to createDependencyException - if DynamoDB put fails in an unexpected wayInvalidStateException - if lease table does not existProvisionedThroughputException - if DynamoDB put fails due to lack of capacitypublic Lease getLease(@NonNull @NonNull String leaseKey) throws DependencyException, InvalidStateException, ProvisionedThroughputException
getLease in interface LeaseRefresherleaseKey - Get the lease for this leasekeyDependencyException - if DynamoDB get fails in an unexpected wayInvalidStateException - if lease table does not existProvisionedThroughputException - if DynamoDB get fails due to lack of capacitypublic boolean renewLease(@NonNull
@NonNull Lease lease)
throws DependencyException,
InvalidStateException,
ProvisionedThroughputException
renewLease in interface LeaseRefresherlease - the lease to renewDependencyException - if DynamoDB update fails in an unexpected wayInvalidStateException - if lease table does not existProvisionedThroughputException - if DynamoDB update fails due to lack of capacitypublic boolean takeLease(@NonNull
@NonNull Lease lease,
@NonNull
@NonNull String owner)
throws DependencyException,
InvalidStateException,
ProvisionedThroughputException
takeLease in interface LeaseRefresherlease - the lease to takeowner - the new ownerDependencyException - if DynamoDB update fails in an unexpected wayInvalidStateException - if lease table does not existProvisionedThroughputException - if DynamoDB update fails due to lack of capacitypublic boolean initiateGracefulLeaseHandoff(Lease lease, String newOwner) throws DependencyException, InvalidStateException, ProvisionedThroughputException
This method updates the lease with the new owner information but ensures that the current owner is given time to gracefully finish its work (e.g., processing records) before the lease is reassigned.
initiateGracefulLeaseHandoff in interface LeaseRefresherlease - the lease to be assignednewOwner - the new ownerDependencyException - if DynamoDB update fails in an unexpected wayInvalidStateException - if lease table does not existProvisionedThroughputException - if DynamoDB update fails due to lack of capacitypublic boolean assignLease(Lease lease, String newOwner) throws DependencyException, InvalidStateException, ProvisionedThroughputException
assignLease in interface LeaseRefresherlease - the lease to be assignednewOwner - the new ownerDependencyException - if DynamoDB update fails in an unexpected wayInvalidStateException - if lease table does not existProvisionedThroughputException - if DynamoDB update fails due to lack of capacitypublic boolean evictLease(@NonNull
@NonNull Lease lease)
throws DependencyException,
InvalidStateException,
ProvisionedThroughputException
evictLease in interface LeaseRefresherlease - the lease to voidDependencyException - if DynamoDB update fails in an unexpected wayInvalidStateException - if lease table does not existProvisionedThroughputException - if DynamoDB update fails due to lack of capacitypublic void deleteAll()
throws DependencyException,
InvalidStateException,
ProvisionedThroughputException
deleteAll in interface LeaseRefresherDependencyException - if DynamoDB scan or delete fail in an unexpected wayInvalidStateException - if lease table does not existProvisionedThroughputException - if DynamoDB scan or delete fail due to lack of capacitypublic void deleteLease(@NonNull
@NonNull Lease lease)
throws DependencyException,
InvalidStateException,
ProvisionedThroughputException
deleteLease in interface LeaseRefresherlease - the lease to deleteDependencyException - if DynamoDB delete fails in an unexpected wayInvalidStateException - if lease table does not existProvisionedThroughputException - if DynamoDB delete fails due to lack of capacitypublic boolean updateLease(@NonNull
@NonNull Lease lease)
throws DependencyException,
InvalidStateException,
ProvisionedThroughputException
updateLease in interface LeaseRefresherDependencyException - if DynamoDB update fails in an unexpected wayInvalidStateException - if lease table does not existProvisionedThroughputException - if DynamoDB update fails due to lack of capacitypublic void updateLeaseWithMetaInfo(Lease lease, UpdateField updateField) throws DependencyException, InvalidStateException, ProvisionedThroughputException
LeaseRefresherupdateLeaseWithMetaInfo in interface LeaseRefresherDependencyException - if DynamoDB update fails in an unexpected wayInvalidStateException - if lease table does not existProvisionedThroughputException - if DynamoDB update fails due to lack of capacitypublic ExtendedSequenceNumber getCheckpoint(String leaseKey) throws ProvisionedThroughputException, InvalidStateException, DependencyException
getCheckpoint in interface LeaseRefresherleaseKey - Checkpoint of this shard will be returnedProvisionedThroughputException - if DynamoDB update fails due to lack of capacityInvalidStateException - if lease table does not existDependencyException - if DynamoDB update fails in an unexpected waypublic String getLeaseTableIdentifier() throws DependencyException
getLeaseTableIdentifier in interface LeaseRefresherDependencyExceptionprotected DependencyException convertAndRethrowExceptions(String operation, String leaseKey, Exception e) throws ProvisionedThroughputException, InvalidStateException
Copyright © 2025. All rights reserved.