From e7ea3bb537aaa81f379d2bd0c9db9c57bde30bba Mon Sep 17 00:00:00 2001 From: Avram Lyon Date: Wed, 23 Sep 2015 10:36:49 -0700 Subject: [PATCH] Check for lease table using DescribeTable prior to trying to create it --- .../kinesis/leases/impl/LeaseManager.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/amazonaws/services/kinesis/leases/impl/LeaseManager.java b/src/main/java/com/amazonaws/services/kinesis/leases/impl/LeaseManager.java index e2970def..74aed8b6 100644 --- a/src/main/java/com/amazonaws/services/kinesis/leases/impl/LeaseManager.java +++ b/src/main/java/com/amazonaws/services/kinesis/leases/impl/LeaseManager.java @@ -99,11 +99,25 @@ public class LeaseManager implements ILeaseManager { */ @Override public boolean createLeaseTableIfNotExists(Long readCapacity, Long writeCapacity) - throws ProvisionedThroughputException, DependencyException { + throws ProvisionedThroughputException, DependencyException { verifyNotNull(readCapacity, "readCapacity cannot be null"); verifyNotNull(writeCapacity, "writeCapacity cannot be null"); boolean tableDidNotExist = true; + + // First, attempt to describe the table and short-circuit if it already existed + try { + dynamoDBClient.describeTable(table); + return false; + } catch (ResourceNotFoundException e) { + tableDidNotExist = true; + } catch (AmazonClientException e) { + // In order to insulate ourselves from failures specific to DescribeTable, we swallow this exception; + // we can still rely on CreateTable request failing if present. + LOG.error("Failed to retrieve table description for table " + table, e); + } + + // We believe the table does not exist, so we proceed to creating it CreateTableRequest request = new CreateTableRequest(); request.setTableName(table); request.setKeySchema(serializer.getKeySchema());