Check if Table Exists Before Creation

Attempt to see if the lease table exists before attempting to create it.

Related/Fixes
PR#67 - https://github.com/awslabs/amazon-kinesis-client/pull/67
This commit is contained in:
Pfifer, Justin 2016-07-21 13:40:56 -07:00
parent 2104f358c6
commit 8fc5dd5862

View file

@ -103,7 +103,16 @@ public class LeaseManager<T extends Lease> implements ILeaseManager<T> {
verifyNotNull(readCapacity, "readCapacity cannot be null");
verifyNotNull(writeCapacity, "writeCapacity cannot be null");
boolean tableDidNotExist = true;
try {
if (tableStatus() != null) {
return false;
}
} catch (DependencyException de) {
//
// Something went wrong with DynamoDB
//
LOG.error("Failed to get table status for " + table, de);
}
CreateTableRequest request = new CreateTableRequest();
request.setTableName(table);
request.setKeySchema(serializer.getKeySchema());
@ -117,14 +126,14 @@ public class LeaseManager<T extends Lease> implements ILeaseManager<T> {
try {
dynamoDBClient.createTable(request);
} catch (ResourceInUseException e) {
tableDidNotExist = false;
LOG.info("Table " + table + " already exists.");
return false;
} catch (LimitExceededException e) {
throw new ProvisionedThroughputException("Capacity exceeded when creating table " + table, e);
} catch (AmazonClientException e) {
throw new DependencyException(e);
}
return tableDidNotExist;
return true;
}
/**
@ -132,6 +141,10 @@ public class LeaseManager<T extends Lease> implements ILeaseManager<T> {
*/
@Override
public boolean leaseTableExists() throws DependencyException {
return TableStatus.ACTIVE == tableStatus();
}
private TableStatus tableStatus() throws DependencyException {
DescribeTableRequest request = new DescribeTableRequest();
request.setTableName(table);
@ -144,19 +157,17 @@ public class LeaseManager<T extends Lease> implements ILeaseManager<T> {
LOG.debug(String.format("Got ResourceNotFoundException for table %s in leaseTableExists, returning false.",
table));
}
return false;
return null;
} catch (AmazonClientException e) {
throw new DependencyException(e);
}
String tableStatus = result.getTable().getTableStatus();
TableStatus tableStatus = TableStatus.fromValue(result.getTable().getTableStatus());
if (LOG.isDebugEnabled()) {
LOG.debug("Lease table exists and is in status " + tableStatus);
}
return TableStatus.ACTIVE.name().equals(tableStatus);
return tableStatus;
}
@Override