public class LeaseManager<T extends Lease> extends Object implements ILeaseManager<T>
Modifier and Type | Field and Description |
---|---|
protected boolean |
consistentReads |
protected com.amazonaws.services.dynamodbv2.AmazonDynamoDB |
dynamoDBClient |
protected ILeaseSerializer<T> |
serializer |
protected String |
table |
Constructor and Description |
---|
LeaseManager(String table,
com.amazonaws.services.dynamodbv2.AmazonDynamoDB dynamoDBClient,
ILeaseSerializer<T> serializer)
Constructor.
|
LeaseManager(String table,
com.amazonaws.services.dynamodbv2.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(String operation,
String leaseKey,
com.amazonaws.AmazonClientException e) |
boolean |
createLeaseIfNotExists(T lease)
Create a new lease.
|
boolean |
createLeaseTableIfNotExists(Long readCapacity,
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(String leaseKey) |
boolean |
leaseTableExists() |
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,
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 String table
protected com.amazonaws.services.dynamodbv2.AmazonDynamoDB dynamoDBClient
protected ILeaseSerializer<T extends Lease> serializer
protected boolean consistentReads
public LeaseManager(String table, com.amazonaws.services.dynamodbv2.AmazonDynamoDB dynamoDBClient, ILeaseSerializer<T> serializer)
table
- leases tabledynamoDBClient
- DynamoDB client to useserializer
- LeaseSerializer to use to convert to/from DynamoDB objects.public LeaseManager(String table, com.amazonaws.services.dynamodbv2.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(Long readCapacity, 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
ILeaseManager
waitUntilLeaseTableExists
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 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(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, 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(String operation, String leaseKey, com.amazonaws.AmazonClientException e) throws ProvisionedThroughputException, InvalidStateException
Copyright © 2016. All rights reserved.