From 8fc5dd5862b5daf777fad06c66d2a948e5caf9ad Mon Sep 17 00:00:00 2001 From: "Pfifer, Justin" Date: Thu, 21 Jul 2016 13:40:56 -0700 Subject: [PATCH] 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 --- .../kinesis/leases/impl/LeaseManager.java | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) 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..3a4d408c 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 @@ -103,7 +103,16 @@ public class LeaseManager implements ILeaseManager { 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 implements ILeaseManager { 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 implements ILeaseManager { */ @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 implements ILeaseManager { 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