Skip to content

Commit

Permalink
Check if Table Exists Before Creation
Browse files Browse the repository at this point in the history
Attempt to see if the lease table exists before attempting to create it.

Related/Fixes
PR#67 - #67
  • Loading branch information
pfifer committed Jul 21, 2016
1 parent 2104f35 commit 8fc5dd5
Showing 1 changed file with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,16 @@ public boolean createLeaseTableIfNotExists(Long readCapacity, Long writeCapacity
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());
Expand All @@ -117,21 +126,25 @@ public boolean createLeaseTableIfNotExists(Long readCapacity, Long writeCapacity
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;
}

/**
* {@inheritDoc}
*/
@Override
public boolean leaseTableExists() throws DependencyException {
return TableStatus.ACTIVE == tableStatus();
}

private TableStatus tableStatus() throws DependencyException {
DescribeTableRequest request = new DescribeTableRequest();

request.setTableName(table);
Expand All @@ -144,19 +157,17 @@ public boolean leaseTableExists() throws DependencyException {
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
Expand Down

0 comments on commit 8fc5dd5

Please sign in to comment.