public class LeaseManager<T extends Lease> extends java.lang.Object implements ILeaseManager<T>
| Modifier and Type | Field and Description |
|---|---|
protected boolean |
consistentReads |
protected AmazonDynamoDB |
dynamoDBClient |
protected ILeaseSerializer<T> |
serializer |
protected java.lang.String |
table |
| Constructor and Description |
|---|
LeaseManager(java.lang.String table,
AmazonDynamoDB dynamoDBClient,
ILeaseSerializer<T> serializer)
Constructor.
|
LeaseManager(java.lang.String table,
AmazonDynamoDB dynamoDBClient,
ILeaseSerializer<T> serializer,
boolean consistentReads)
Constructor for test cases - allows control of consistent reads.
|
| Modifier and Type | Method and Description |
|---|---|
protected DependencyException |
convertAndRethrowExceptions(java.lang.String operation,
java.lang.String leaseKey,
AmazonClientException e) |
boolean |
createLeaseIfNotExists(T lease)
Create a new lease.
|
boolean |
createLeaseTableIfNotExists(java.lang.Long readCapacity,
java.lang.Long writeCapacity)
Creates the table that will store leases.
|
void |
deleteAll()
Delete all leases from DynamoDB.
|
void |
deleteLease(T lease)
Delete the given lease from DynamoDB.
|
boolean |
evictLease(T lease)
Evict the current owner of lease by setting owner to null.
|
T |
getLease(java.lang.String leaseKey) |
boolean |
leaseTableExists() |
java.util.List<T> |
listLeases()
List all objects in table synchronously.
|
boolean |
renewLease(T lease)
Renew a lease by incrementing the lease counter.
|
boolean |
takeLease(T lease,
java.lang.String owner)
Take a lease for the given owner by incrementing its leaseCounter and setting its owner field.
|
boolean |
updateLease(T lease)
Update application-specific fields of the given lease in DynamoDB.
|
boolean |
waitUntilLeaseTableExists(long secondsBetweenPolls,
long timeoutSeconds)
Blocks until the lease table exists by polling leaseTableExists.
|
protected java.lang.String table
protected AmazonDynamoDB dynamoDBClient
protected ILeaseSerializer<T extends Lease> serializer
protected boolean consistentReads
public LeaseManager(java.lang.String table,
AmazonDynamoDB dynamoDBClient,
ILeaseSerializer<T> serializer)
table - leases tabledynamoDBClient - DynamoDB client to useserializer - LeaseSerializer to use to convert to/from DynamoDB objects.public LeaseManager(java.lang.String table,
AmazonDynamoDB dynamoDBClient,
ILeaseSerializer<T> serializer,
boolean consistentReads)
table - leases tabledynamoDBClient - DynamoDB client to useserializer - lease serializer to useconsistentReads - true if we want consistent reads for testing purposes.public boolean createLeaseTableIfNotExists(java.lang.Long readCapacity,
java.lang.Long writeCapacity)
throws ProvisionedThroughputException,
DependencyException
createLeaseTableIfNotExists in interface ILeaseManager<T extends Lease>ProvisionedThroughputException - 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 ILeaseManager<T extends Lease>DependencyException - if DynamoDB describeTable fails in an unexpected waypublic boolean waitUntilLeaseTableExists(long secondsBetweenPolls,
long timeoutSeconds)
throws DependencyException
ILeaseManagerwaitUntilLeaseTableExists in interface ILeaseManager<T extends Lease>secondsBetweenPolls - time to wait between polls in secondstimeoutSeconds - total time to wait in secondsDependencyException - if DynamoDB describeTable fails in an unexpected waypublic java.util.List<T> listLeases() throws DependencyException, InvalidStateException, ProvisionedThroughputException
listLeases in interface ILeaseManager<T extends Lease>DependencyException - 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(T lease) throws DependencyException, InvalidStateException, ProvisionedThroughputException
createLeaseIfNotExists in interface ILeaseManager<T extends Lease>lease - 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 T getLease(java.lang.String leaseKey) throws DependencyException, InvalidStateException, ProvisionedThroughputException
getLease in interface ILeaseManager<T extends Lease>leaseKey - Get the lease for this shardIdDependencyException - 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(T lease) throws DependencyException, InvalidStateException, ProvisionedThroughputException
renewLease in interface ILeaseManager<T extends Lease>lease - 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(T lease, java.lang.String owner) throws DependencyException, InvalidStateException, ProvisionedThroughputException
takeLease in interface ILeaseManager<T extends Lease>lease - 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 evictLease(T lease) throws DependencyException, InvalidStateException, ProvisionedThroughputException
evictLease in interface ILeaseManager<T extends Lease>lease - 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 ILeaseManager<T extends Lease>DependencyException - 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(T lease) throws DependencyException, InvalidStateException, ProvisionedThroughputException
deleteLease in interface ILeaseManager<T extends Lease>lease - 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(T lease) throws DependencyException, InvalidStateException, ProvisionedThroughputException
updateLease in interface ILeaseManager<T extends Lease>DependencyException - if DynamoDB update fails in an unexpected wayInvalidStateException - if lease table does not existProvisionedThroughputException - if DynamoDB update fails due to lack of capacityprotected DependencyException convertAndRethrowExceptions(java.lang.String operation, java.lang.String leaseKey, AmazonClientException e) throws ProvisionedThroughputException, InvalidStateException