@Test public void testCreateTableWithWait() throws Exception { // Create fake responses from AWS. First response is still creating the table, second response the table // has become active. TableDescription creatingDescription = constructTableDescription(TableStatus.CREATING); TableDescription createdDescription = constructTableDescription(TableStatus.ACTIVE); CreateTableResult mockCreateResult = new CreateTableResult().withTableDescription(creatingDescription); DescribeTableResult mockDescribeResultCreating = new DescribeTableResult().withTable(creatingDescription); DescribeTableResult mockDescribeResultCreated = new DescribeTableResult().withTable(createdDescription); // Create the table. CreateTableRequest expectedRequest = dynamoDB.constructCreateTableRequest(); when(mockDynamoDBClient.createTable(expectedRequest)).thenReturn(mockCreateResult); when(mockDynamoDBClient.describeTable(tableName)).thenReturn(mockDescribeResultCreating, mockDescribeResultCreated); assertEquals(dynamoDB.create(), TEST_ARN); verify(mockDynamoDBClient, times(1)).createTable(expectedRequest); verify(mockDynamoDBClient, times(2)).describeTable(tableName); }
private static Task<TableDescription> waitForActiveTableStatus(final DynamoDBConnection dynamoDBConnection, final String tableName) { try { for (int i = 0; i < WAITING_FOR_ACTIVE_TABLE_STATUS_MAX_ATTEMPTS; i++) { final DescribeTableResult describe = dynamoDBConnection.getDynamoClient().describeTable(tableName); if (describe.getTable().getTableStatus().equals(TableStatus.ACTIVE.name())) { return Task.fromValue(describe.getTable()); } Thread.sleep(WAITING_FOR_ACTIVE_TABLE_STATUS_RETRY_DELAY_MILLIS); } } catch (InterruptedException e) { throw new UncheckedException(e); } throw new UncheckedException("Hit max retry attempts while waiting for table to become active: " + tableName); }
private void ensureTableIsActive(final String tableName) { try { while (true) { final DescribeTableResult describe = dynamoDBConnection.getDynamoClient().describeTable(tableName); if (describe.getTable().getTableStatus().equals(TableStatus.ACTIVE.name())) { return; } Thread.sleep(500); } } catch (InterruptedException e) { throw new UncheckedException(e); } }
@Override public DescribeTableResult describeTable(DescribeTableRequest describeTableRequest) { this.describeTableRequest = describeTableRequest; String tableName = describeTableRequest.getTableName(); if ("activeTable".equals(tableName)) { return tableWithStatus(TableStatus.ACTIVE); } else if ("creatibleTable".equals(tableName) && createTableRequest != null) { return tableWithStatus(TableStatus.ACTIVE); } else if ("FULL_DESCRIBE_TABLE".equals(tableName)) { return new DescribeTableResult().withTable(new TableDescription() .withTableName(tableName) .withTableStatus(TableStatus.ACTIVE) .withCreationDateTime(new Date(NOW)) .withItemCount(100L) .withKeySchema(new KeySchemaElement().withAttributeName("name")) .withProvisionedThroughput(new ProvisionedThroughputDescription() .withReadCapacityUnits(20L) .withWriteCapacityUnits(10L)) .withTableSizeBytes(1000L)); } throw new ResourceNotFoundException(tableName + " is missing"); }
private void waitForStatus(String tableName, TableStatus status) { logger.info("Waiting for " + tableName + " to become " + status.toString() + "..."); long startTime = System.currentTimeMillis(); long endTime = startTime + (10 * 60 * 1000); while (System.currentTimeMillis() < endTime) { try { Thread.sleep(1000 * 2); } catch (Exception e) { } try { DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName); TableDescription tableDescription = dynamoDb.describeTable(request).getTable(); String tableStatus = tableDescription.getTableStatus(); logger.debug(" - current state: " + tableStatus); if (tableStatus.equals(status.toString())) return; } catch (AmazonServiceException ase) { if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == false) throw ase; } } throw new RuntimeException("Table " + tableName + " never went " + status.toString()); }
private void waitForTableToBecomeAvailable(String tableName) { System.out.println("Waiting for " + tableName + " to become ACTIVE..."); long startTime = System.currentTimeMillis(); long endTime = startTime + (10 * 60 * 1000); while (System.currentTimeMillis() < endTime) { DescribeTableRequest request = new DescribeTableRequest() .withTableName(tableName); TableDescription tableDescription = client.describeTable( request).getTable(); String tableStatus = tableDescription.getTableStatus(); System.out.println(" - current state: " + tableStatus); if (tableStatus.equals(TableStatus.ACTIVE.toString())) return; try { Thread.sleep(1000 * 20); } catch (Exception e) { } } throw new RuntimeException("Table " + tableName + " never went active"); }
private void waitForTableAvailable(String tableName) { LOG.info("Waiting for table " + tableName + " to become ACTIVE..."); long startTime = System.currentTimeMillis(); long endTime = startTime + (10 * 60 * 1000); while (System.currentTimeMillis() < endTime) { DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName); TableDescription tableDescription = dynamoDBClient.describeTable(describeTableRequest).getTable(); // Display current status of table String tableStatus = tableDescription.getTableStatus(); LOG.info("Current state for table " + tableName + ": " + tableStatus); if (tableStatus.equals(TableStatus.ACTIVE.toString())) { return; } try { Thread.sleep(1000 * 20); } catch (Exception ex) { LOG.warn(ex.getMessage()); } } throw new RuntimeException("Table " + tableName + " never went active"); }
/** * Interface which will block until a dynamo table reaches a specified * state. Also returns immediately if the object doesn't exist * * @param dynamoClient * Dynamo DB Client to use for connection to Dynamo DB. * @param dynamoTable * The table name to check. * @param status * The status to wait for * @throws Exception */ private static void waitForTableState(final AmazonDynamoDB dynamoClient, final String dynamoTable, TableStatus status) throws Exception { DescribeTableResult tableRequest = null; while (true) { try { tableRequest = dynamoClient.describeTable(dynamoTable); if (tableRequest.getTable().getTableStatus() .equals(status.name())) break; Thread.sleep(1000); } catch (InterruptedException e) { return; } } }
private static void waitForTableToBecomeAvailable(String tableName) { System.out.println("Waiting for " + tableName + " to become ACTIVE..."); long startTime = System.currentTimeMillis(); long endTime = startTime + (10 * 60 * 1000); while (System.currentTimeMillis() < endTime) { DescribeTableRequest request = new DescribeTableRequest() .withTableName(tableName); TableDescription tableDescription = client.describeTable( request).getTable(); String tableStatus = tableDescription.getTableStatus(); System.out.println(" - current state: " + tableStatus); if (tableStatus.equals(TableStatus.ACTIVE.toString())) return; try { Thread.sleep(1000 * 20); } catch (Exception e) { } } throw new RuntimeException("Table " + tableName + " never went active"); }
private static void waitForTableToBecomeAvailable(String tableName) { System.out.println("Waiting for " + tableName + " to become ACTIVE..."); long startTime = System.currentTimeMillis(); long endTime = startTime + (10 * 60 * 1000); while (System.currentTimeMillis() < endTime) { DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName); TableDescription tableDescription = client.describeTable(request).getTable(); String tableStatus = tableDescription.getTableStatus(); System.out.println(" - current state: " + tableStatus); if (tableStatus.equals(TableStatus.ACTIVE.toString())) return; try { Thread.sleep(1000 * 20); } catch (Exception e) { e.printStackTrace(); } } throw new RuntimeException("Table " + tableName + " never went active"); }
private void waitForTable(String name) { log.info(String.format("Waiting for creation of table '%s' to complete.", name)); long startTime = System.currentTimeMillis(); long endTime = startTime + (10 * 60 * 1000); while (System.currentTimeMillis() < endTime) { sleep(1000 * 20); try { DescribeTableRequest request = new DescribeTableRequest().withTableName(name); TableDescription tableDescription = client.describeTable(request).getTable(); String tableStatus = tableDescription.getTableStatus(); log.info(String.format("Table '%s' is in state: '%s'.", name, tableStatus)); if (tableStatus.equals(TableStatus.ACTIVE.toString())) { return; } } catch (ResourceNotFoundException e) { // nop - maybe the table isn't showing up yet. } } throw new RuntimeException(String.format("Table '%s' never went active.", name)); }
private void waitForTableToBecomeActive() { int retries = 0; String tableStatus = "Unknown"; try { while (retries < MAX_RETRIES) { log.info("Waiting for table to become active..."); Thread.sleep(SLEEP_TIME); DescribeTableResult result = client.describeTable(tableName); tableStatus = result.getTable().getTableStatus(); if (tableStatus.equals(TableStatus.ACTIVE.toString()) || tableStatus.equals(TableStatus.UPDATING.toString())) { return; } if (tableStatus.equals(TableStatus.DELETING.toString())) { throw new UnexpectedStateException( tableName, tableStatus, TableStatus.ACTIVE.toString(), "Table state changed to 'DELETING' before creation was confirmed"); } retries++; } } catch (InterruptedException e) { throw new UnexpectedStateException(tableName, tableStatus, TableStatus.ACTIVE.toString(), "Error occurred while waiting for DynamoDB table", e); } throw new UnexpectedStateException(tableName, tableStatus, TableStatus.ACTIVE.toString(), "DynamoDB table did not become active before timeout"); }
private static void cleanUpDynamoDBTables(Regions testRegion, String testResourcePrefix, Date createdBeforeThreshold, AWSCredentialsProvider awsCredentials) { LOG.info("Cleaning DynamoDB..."); AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder.standard() .withCredentials(awsCredentials) .withRegion(testRegion) .build(); List<String> tableNames = dynamoDBClient.listTables().getTableNames(); for (String tableName: tableNames) { if (!tableName.startsWith(testResourcePrefix)) { continue; } LOG.info(String.format("Checking if table %s needs cleaning...", tableName)); try { TableDescription desc = dynamoDBClient.describeTable(tableName).getTable(); if (!desc.getTableName().equals(TableStatus.DELETING.toString()) && desc.getCreationDateTime() != null && desc.getCreationDateTime().before(createdBeforeThreshold)) { LOG.info("Cleaning up table: " + tableName); dynamoDBClient.deleteTable(tableName); } } catch (ResourceNotFoundException e) { LOG.info("Looks like table was already cleaned up: " + tableName); } } }
@Test public void testDeleteTableWithWait() throws Exception { // Create fake responses from AWS. TableDescription deletingDescription = constructTableDescription(TableStatus.DELETING); DescribeTableResult mockDescribeResult = new DescribeTableResult().withTable(deletingDescription); // Delete the table. First response the table is still deleting, the second response the table has deleted // and the ResourceNotFoundException is thrown. when(mockDynamoDBClient.describeTable(tableName)).thenReturn(mockDescribeResult).thenThrow( new ResourceNotFoundException("Table not found")); dynamoDB.delete(); verify(mockDynamoDBClient, times(1)).deleteTable(tableName); verify(mockDynamoDBClient, times(2)).describeTable(tableName); }
public boolean tableExists(final String tableName) { try { return TableStatus.ACTIVE.toString().equals(db.getTable(tableName).describe().getTableStatus().toUpperCase()); } catch (final ResourceNotFoundException rnfe) { return false; } }
@Test public void testExecute() { command.execute(); assertEquals("DOMAIN1", ddbClient.deleteTableRequest.getTableName()); assertEquals(new ProvisionedThroughputDescription(), exchange.getIn().getHeader( DdbConstants.PROVISIONED_THROUGHPUT)); assertEquals(new Date(AmazonDDBClientMock.NOW), exchange.getIn().getHeader(DdbConstants.CREATION_DATE, Date.class)); assertEquals(Long.valueOf(10L), exchange.getIn().getHeader(DdbConstants.ITEM_COUNT, Long.class)); assertEquals(new ArrayList<KeySchemaElement>(), exchange.getIn().getHeader(DdbConstants.KEY_SCHEMA, ArrayList.class)); assertEquals(Long.valueOf(20L), exchange.getIn().getHeader(DdbConstants.TABLE_SIZE, Long.class)); assertEquals(TableStatus.ACTIVE, exchange.getIn().getHeader(DdbConstants.TABLE_STATUS, TableStatus.class)); }
@Override public DeleteTableResult deleteTable(DeleteTableRequest deleteTableRequest) { this.deleteTableRequest = deleteTableRequest; return new DeleteTableResult().withTableDescription(new TableDescription() .withProvisionedThroughput(new ProvisionedThroughputDescription()) .withTableName(deleteTableRequest.getTableName()) .withCreationDateTime(new Date(NOW)) .withItemCount(10L) .withKeySchema(new ArrayList<KeySchemaElement>()) .withTableSizeBytes(20L) .withTableStatus(TableStatus.ACTIVE)); }
/** * * @{inheritDoc */ @Override public void createTable(String tableName) { try { if (!hasTable(tableName)) { logger.info("Creating table: " + tableName); HierarchicalConfiguration resultsProviderConfig = config.getVmManagerConfig() .getResultsProviderConfig(); long readCapacity = getCapacity(resultsProviderConfig, "read-capacity", 10L); long writeCapacity = getCapacity(resultsProviderConfig, "write-capacity", 50L); ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName( DatabaseKeys.JOB_ID_KEY.getShortKey()).withAttributeType(ScalarAttributeType.S)); attributeDefinitions.add(new AttributeDefinition().withAttributeName( DatabaseKeys.REQUEST_NAME_KEY.getShortKey()).withAttributeType(ScalarAttributeType.S)); ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits( readCapacity).withWriteCapacityUnits(writeCapacity); KeySchemaElement hashKeyElement = new KeySchemaElement().withAttributeName( DatabaseKeys.JOB_ID_KEY.getShortKey()).withKeyType(KeyType.HASH); KeySchemaElement rangeKeyElement = new KeySchemaElement().withAttributeName( DatabaseKeys.REQUEST_NAME_KEY.getShortKey()).withKeyType(KeyType.RANGE); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withKeySchema(hashKeyElement, rangeKeyElement) .withAttributeDefinitions(attributeDefinitions) .withProvisionedThroughput(provisionedThroughput); CreateTableResult result = dynamoDb.createTable(request); waitForStatus(tableName, TableStatus.ACTIVE); logger.info("Created table: " + result.getTableDescription().getTableName()); } } catch (Exception t) { logger.error(t, t); throw new RuntimeException(t); } }
private boolean leaseTableExists() { DescribeTableRequest request = new DescribeTableRequest(); request.setTableName(conf.applicationName); DescribeTableResult result; try { result = dynamoDBClient.describeTable(request); } catch (ResourceNotFoundException e) { LOG.debug("Lease table '{}' does not exist", conf.applicationName); return false; } TableStatus tableStatus = TableStatus.fromValue(result.getTable().getTableStatus()); LOG.debug("Lease table exists and is in '{}' state", tableStatus); return tableStatus == TableStatus.ACTIVE; }
private void waitForTableDeleted(String tableName) { LOG.info("Waiting for table " + tableName + " while status DELETING..."); long startTime = System.currentTimeMillis(); long endTime = startTime + (10 * 60 * 1000); while (System.currentTimeMillis() < endTime) { try { DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName); TableDescription tableDescription = dynamoDBClient.describeTable(describeTableRequest) .getTable(); String tableStatus = tableDescription.getTableStatus(); LOG.info("Current state for table " + tableName + ": " + tableStatus); if (tableStatus.equals(TableStatus.ACTIVE.toString())) { return; } } catch (ResourceNotFoundException rne) { LOG.warn("Table " + tableName + " is not found. It was deleted."); return; } try { Thread.sleep(1000 * 20); } catch (Exception ex) { LOG.warn(ex.getMessage()); } } throw new RuntimeException("Table " + tableName + " was never deleted"); }
public static void dropTable(final AmazonDynamoDB dynamoClient, final String dynamoTable) throws Exception { if (dynamoTable != null) { LOG.info(String.format("Dropping Dynamo Table %s", dynamoTable)); try { dynamoClient.deleteTable(dynamoTable); waitForTableState(dynamoClient, dynamoTable, TableStatus.DELETING); } catch (ResourceNotFoundException e) { LOG.info("OK - Table Not Found"); } } }
/** * Waits up to 6 minutes to confirm if a table has been created or not * * @param awsClient * @param tableName */ public static void waitForTableToBecomeAvailable(AmazonDynamoDB awsClient, String tableName) { LOG.debug("Waiting for {} to become available", tableName); long startTime = System.currentTimeMillis(); long endTime = startTime + WAIT_TIME; while (System.currentTimeMillis() < endTime) { try { Thread.sleep(SLEEP_TIME); } catch (Exception e) { } try { DescribeTableRequest request = new DescribeTableRequest() .withTableName(tableName); TableDescription tableDescription = awsClient.describeTable(request) .getTable(); String tableStatus = tableDescription.getTableStatus(); LOG.debug("{} - current state: {}", tableName, tableStatus); if (tableStatus.equals(TableStatus.ACTIVE.toString())) return; } catch (AmazonServiceException ase) { if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == false) throw ase; } } throw new RuntimeException("Table " + tableName + " never became active"); }
@Test public void testGetTableStatus() { final TableDescription description = new TableDescription(); final DescribeTableResult result = new DescribeTableResult().withTable(description); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result).anyTimes(); for (final TableStatus status : TableStatus.values()) { description.setTableStatus(status); PowerMock.replayAll(); assertEquals(status, DynamoDBManager.getTableStatus(dynamoDB, tableName)); PowerMock.verifyAll(); } }
@Test public void testWaitForTableToBecomeActiveAlreadyActive() { final TableDescription table = new TableDescription(); final DescribeTableResult result = new DescribeTableResult().withTable(table); table.setTableStatus(TableStatus.ACTIVE); dynamoDB.describeTable(tableName); PowerMock.expectLastCall().andReturn(result); PowerMock.expectLastCall().andReturn(result); PowerMock.replayAll(); DynamoDBManager.waitForTableToBecomeActive(dynamoDB, tableName); }
@Test public void testWaitForTableToBecomeActiveCreatingThenActive() { // Creating table final TableDescription table1 = new TableDescription(); table1.setTableStatus(TableStatus.CREATING); final DescribeTableResult result1 = new DescribeTableResult().withTable(table1); // Active table final TableDescription table2 = new TableDescription(); table2.setTableStatus(TableStatus.ACTIVE); final DescribeTableResult result2 = new DescribeTableResult().withTable(table2); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result1); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result2); PowerMock.replayAll(); DynamoDBManager.waitForTableToBecomeActive(dynamoDB, tableName); }
@Test(expected = IllegalStateException.class) public void testWaitForTableToBecomeActiveDeleting() { final TableDescription table = new TableDescription().withTableStatus(TableStatus.DELETING); final DescribeTableResult result = new DescribeTableResult().withTable(table); PowerMock.expectLastCall().andReturn(result); PowerMock.replayAll(); DynamoDBManager.waitForTableToBecomeActive(dynamoDB, tableName); }
@Test(expected = IllegalStateException.class) public void testWaitForTableToBecomeActiveNeverGoingActive() { final TableDescription table = new TableDescription(); final DescribeTableResult result = new DescribeTableResult().withTable(table); table.setTableStatus(TableStatus.CREATING); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result).anyTimes(); PowerMock.replayAll(); DynamoDBManager.waitForTableToBecomeActive(dynamoDB, tableName); }
@Test public void testWaitForTableToBecomeActiveUpdatingThenActive() { // Updating table final TableDescription table1 = new TableDescription(); table1.setTableStatus(TableStatus.UPDATING); final DescribeTableResult result1 = new DescribeTableResult().withTable(table1); // Active table final TableDescription table2 = new TableDescription(); table2.setTableStatus(TableStatus.ACTIVE); final DescribeTableResult result2 = new DescribeTableResult().withTable(table2); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result1); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result2); PowerMock.replayAll(); DynamoDBManager.waitForTableToBecomeActive(dynamoDB, tableName); }
private static void waitForTableToBecomeAvailable(String tableName) { logger.info("Waiting for " + tableName + " to become ACTIVE..."); long startTime = System.currentTimeMillis(); long endTime = startTime + (10 * 60 * 1000); while (System.currentTimeMillis() < endTime) { try { Thread.sleep(1000 * 20); } catch (Exception e) { } try { DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName); TableDescription tableDescription = dynamoDBClient.describeTable(request).getTable(); String tableStatus = tableDescription.getTableStatus(); logger.info(" - current state: " + tableStatus); if (tableStatus.equals(TableStatus.ACTIVE.toString())) return; } catch (AmazonServiceException ase) { if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == false) throw ase; } } throw new RuntimeException("Table " + tableName + " never went active"); }
private static void waitForTableToBecomeAvailable(String tableName) { LOG.info("Waiting for " + tableName + " to become ACTIVE..."); long startTime = System.currentTimeMillis(); long endTime = startTime + (10 * 60 * 1000); while (System.currentTimeMillis() < endTime) { try { Thread.sleep(1000 * 20); } catch (Exception e) { } try { DescribeTableRequest request = new DescribeTableRequest() .withTableName(tableName); TableDescription tableDescription = dynamoDB.describeTable( request).getTable(); String tableStatus = tableDescription.getTableStatus(); System.out.println(" - current state: " + tableStatus); if (tableStatus.equals(TableStatus.ACTIVE.toString())) return; } catch (AmazonServiceException ase) { if (ase.getErrorCode().equalsIgnoreCase( "ResourceNotFoundException") == false) throw ase; } } throw new RuntimeException("Table " + tableName + " never went active"); }
private TableDescription constructTableDescription(TableStatus status) { return new TableDescription().withTableArn(TEST_ARN).withTableStatus(status); }
public static Task<Table> getTable(final DynamoDBConnection dynamoDBConnection, final String tableName) { final String tableCacheId = generateTableCacheId(dynamoDBConnection, tableName); final Table table = tableCache.get(tableCacheId); if(table != null) { return Task.fromValue(table); } else { return Task.fromFuture(dynamoDBConnection.getDynamoClient().describeTableAsync(tableName)) .thenApply(DescribeTableResult::getTable) .thenCompose(descriptor -> { if (descriptor.getTableStatus().equals(TableStatus.CREATING.name())) { return waitForActiveTableStatus(dynamoDBConnection, tableName); } else { return Task.fromValue(descriptor); } }) .thenApply(descriptor -> { final Table retrievedTable = dynamoDBConnection.getDynamoDB().getTable(descriptor.getTableName()); tableCache.putIfAbsent(tableCacheId, retrievedTable); return retrievedTable; }) .exceptionally(e -> { if (e instanceof ResourceInUseException) { return getTable(dynamoDBConnection, tableName).join(); } if (ExceptionUtils.isCauseInChain(ResourceNotFoundException.class, e)) { try { createTable(dynamoDBConnection, tableName); return getTable(dynamoDBConnection, tableName).join(); } catch (ResourceInUseException resourceInUseException) { return getTable(dynamoDBConnection, tableName).join(); } catch (InterruptedException interruptedException) { throw new UncheckedException(interruptedException); } } else { throw new UncheckedException(e); } }); } }
private boolean isTableActive(TableDescription tableDescription) { return tableDescription.getTableStatus().equals(TableStatus.ACTIVE.toString()); }
private DescribeTableResult tableWithStatus(TableStatus active) { return new DescribeTableResult().withTable(new TableDescription().withTableStatus(active)); }
@Override public CreateTableResult createTable(CreateTableRequest createTableRequest) { this.createTableRequest = createTableRequest; return new CreateTableResult().withTableDescription( new TableDescription().withTableStatus(TableStatus.CREATING)); }
private static boolean isTableAcceptingWrites(final String status) { return isTableStatus(TableStatus.ACTIVE, status) || isTableStatus(TableStatus.UPDATING, status); }
private static boolean isTableStatus(final TableStatus constant, final String status) { return constant.toString().equals(status); }
@Override public Table execute() throws MetaModelException { final MutableTable table = getTable(); final String tableName = table.getName(); final Collection<AttributeDefinition> attributes = new ArrayList<>(); final Collection<KeySchemaElement> keySchema = new ArrayList<>(); final Collection<GlobalSecondaryIndex> globalSecondaryIndices = new ArrayList<>(); final long readCapacity = Long.parseLong(System.getProperty( DynamoDbDataContext.SYSTEM_PROPERTY_THROUGHPUT_READ_CAPACITY, "5")); final long writeCapacity = Long.parseLong(System.getProperty( DynamoDbDataContext.SYSTEM_PROPERTY_THROUGHPUT_WRITE_CAPACITY, "5")); final ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(readCapacity, writeCapacity); for (Column column : table.getColumns()) { if (column.isPrimaryKey()) { final KeyType keyType = getKeyType(column.getRemarks()); keySchema.add(new KeySchemaElement(column.getName(), keyType)); attributes.add(new AttributeDefinition(column.getName(), DynamoDbUtils.toAttributeType(column .getType()))); } } final CreateTableRequest createTableRequest = new CreateTableRequest(); createTableRequest.setTableName(tableName); createTableRequest.setAttributeDefinitions(attributes); createTableRequest.setGlobalSecondaryIndexes(globalSecondaryIndices); createTableRequest.setKeySchema(keySchema); createTableRequest.setProvisionedThroughput(provisionedThroughput); final AmazonDynamoDB client = getUpdateCallback().getDataContext().getDynamoDb(); final CreateTableResult createTableResult = client.createTable(createTableRequest); // await the table creation to be "done". { String tableStatus = createTableResult.getTableDescription().getTableStatus(); while (TableStatus.CREATING.name().equals(tableStatus)) { logger.debug("Waiting for table status to be ACTIVE. Currently: {}", tableStatus); try { Thread.sleep(300); } catch (InterruptedException e) { getUpdateCallback().setInterrupted(true); } tableStatus = client.describeTable(tableName).getTable().getTableStatus(); } } return table; }