@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); } }
public TableDescription describeTable(String tableName) { final DescribeTableRequest describeTablesRequest = new DescribeTableRequest() .withTableName(tableName); try { RetryResult<DescribeTableResult> describeResult = getRetryDriver().runWithRetry( new Callable<DescribeTableResult>() { @Override public DescribeTableResult call() { DescribeTableResult result = dynamoDB.describeTable(describeTablesRequest); log.info("Describe table output: " + result); return result; } }, null, null); return describeResult.result.getTable(); } catch (Exception e) { throw new RuntimeException("Could not lookup table " + tableName + " in DynamoDB.", e); } }
@Override public void execute() { DescribeTableResult result = ddbClient.describeTable(new DescribeTableRequest() .withTableName(determineTableName())); Message msg = getMessageForResponse(exchange); msg.setHeader(DdbConstants.TABLE_NAME, result.getTable().getTableName()); msg.setHeader(DdbConstants.TABLE_STATUS, result.getTable().getTableStatus()); msg.setHeader(DdbConstants.CREATION_DATE, result.getTable().getCreationDateTime()); msg.setHeader(DdbConstants.ITEM_COUNT, result.getTable().getItemCount()); msg.setHeader(DdbConstants.KEY_SCHEMA, result.getTable().getKeySchema()); msg.setHeader(DdbConstants.READ_CAPACITY, result.getTable().getProvisionedThroughput().getReadCapacityUnits()); msg.setHeader(DdbConstants.WRITE_CAPACITY, result.getTable().getProvisionedThroughput().getWriteCapacityUnits()); msg.setHeader(DdbConstants.TABLE_SIZE, result.getTable().getTableSizeBytes()); }
@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 String enableStreamForTable(AmazonDynamoDBClient client, StreamViewType viewType, String tableName) { DescribeTableRequest describeTableRequest = new DescribeTableRequest() .withTableName(tableName); DescribeTableResult describeResult = client.describeTable(describeTableRequest); if (describeResult.getTable().getStreamSpecification().isStreamEnabled()) { //TODO: what if the viewtype doesn't match return describeResult.getTable().getLatestStreamId(); } StreamSpecification streamSpecification = new StreamSpecification(); streamSpecification.setStreamEnabled(true); streamSpecification.setStreamViewType(viewType); UpdateTableRequest updateTableRequest = new UpdateTableRequest() .withTableName(tableName) .withStreamSpecification(streamSpecification); UpdateTableResult result = client.updateTable(updateTableRequest); return result.getTableDescription().getLatestStreamId(); }
public static String enableStreamForTable(AmazonDynamoDBClient client, StreamViewType viewType, String tableName) { DescribeTableRequest describeTableRequest = new DescribeTableRequest() .withTableName(tableName); DescribeTableResult describeResult = client.describeTable(describeTableRequest); if (describeResult.getTable().getStreamSpecification().isStreamEnabled()) { //TODO: what if the viewtype doesn't match return describeResult.getTable().getLatestStreamId(); } StreamSpecification streamSpecification = new StreamSpecification(); streamSpecification.setStreamEnabled(true); streamSpecification.setStreamViewType(viewType); UpdateTableRequest updateTableRequest = new UpdateTableRequest() .withTableName(tableName) .withStreamSpecification(streamSpecification); UpdateTableResult result = client.updateTable(updateTableRequest); return result.getTableDescription().getLatestStreamId(); }
public static void awaitTableCreation(AmazonDynamoDBClient dynamoDBClient, String tableName) { Integer retries = 0; Boolean created = false; while(!created && retries < 100) { DescribeTableResult result = StreamAdapterDemoHelper.describeTable(dynamoDBClient, tableName); created = result.getTable().getTableStatus().equals("ACTIVE"); if (created) { System.out.println("Table is active."); return; } else { retries++; try { Thread.sleep(1000); } catch(InterruptedException e) { // do nothing } } } System.out.println("Timeout after table creation. Exiting..."); cleanupAndExit(dynamoDBClient, tableName, 1); }
@Test public void test_updateTable() throws Exception { createTable(); DescribeTableResult describeResult = dynamoDb.describeTable(TEST_TABLE_NAME); Long readUnits = describeResult.getTable().getProvisionedThroughput().getReadCapacityUnits(); assertThat(readUnits, equalTo(UNITS)); UpdateTableResult updateResult = dynamoDb.updateTable(TEST_TABLE_NAME, new ProvisionedThroughput() .withReadCapacityUnits(new Long(200)) .withWriteCapacityUnits(new Long(200))); String tableName = updateResult.getTableDescription().getTableName(); assertThat(tableName, equalTo(TEST_TABLE_NAME)); ListTablesResult listResult = listTables(); describeResult = dynamoDb.describeTable(TEST_TABLE_NAME); readUnits = describeResult.getTable().getProvisionedThroughput().getReadCapacityUnits(); assertThat(readUnits, equalTo(new Long(200))); }
/** * Private interface for describing tables which handles any instances of * Throttling of the API * * @param dynamoClient * @param dynamoTable * @return * @throws Exception */ public static DescribeTableResult safeDescribeTable( final AmazonDynamoDB dynamoClient, final String dynamoTable) throws Exception { DescribeTableResult res = null; final int tryMax = 10; int tries = 0; while (true) { try { res = dynamoClient.describeTable(dynamoTable); return res; } catch (ResourceNotFoundException e) { if (tries < tryMax) { // sleep for a short time as this is potentially an eventual // consistency issue with the table having been created ms // ago Thread.sleep(10); tries++; } else { throw e; } } } }
/** * 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 awaitTableCreation(String tableName) { Integer retries = 0; Boolean created = false; while(!created && retries < 100) { DescribeTableResult result = StreamsAdapterDemoHelper.describeTable(dynamoDBClient, tableName); created = result.getTable().getTableStatus().equals("ACTIVE"); if (created) { System.out.println("Table is active."); return; } else { retries++; try { Thread.sleep(1000); } catch(InterruptedException e) { // do nothing } } } System.out.println("Timeout after table creation. Exiting..."); cleanupAndExit(1); }
/** * Create table. * @throws InterruptedException If something fails */ public void create() throws InterruptedException { final AmazonDynamoDB aws = this.region.aws(); final String name = this.request.getTableName(); aws.createTable(this.request); Logger.info(this, "DynamoDB table '%s' creation requested...", name); final DescribeTableRequest req = new DescribeTableRequest() .withTableName(name); while (true) { final DescribeTableResult result = aws.describeTable(req); if ("ACTIVE".equals(result.getTable().getTableStatus())) { Logger.info( this, "DynamoDB table '%s' is %s", name, result.getTable().getTableStatus() ); break; } Logger.info( this, "waiting for DynamoDB table '%s': %s", name, result.getTable().getTableStatus() ); TimeUnit.SECONDS.sleep((long) Tv.TEN); } }
public static String getTestTableStatus() { try { AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager .ddb(); DescribeTableRequest request = new DescribeTableRequest() .withTableName(Constants.TEST_TABLE_NAME); DescribeTableResult result = ddb.describeTable(request); String status = result.getTable().getTableStatus(); return status == null ? "" : status; } catch (ResourceNotFoundException e) { } catch (AmazonServiceException ex) { UserPreferenceDemoActivity.clientManager .wipeCredentialsOnAuthError(ex); } return ""; }
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 Optional<TableDescription> describeTable() { try { DescribeTableResult result = client.describeTable(tableName); return Optional.of(result.getTable()); } catch (ResourceNotFoundException e) { return Optional.empty(); } }
@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); }
private DescribeTableResult describeTable(final DescribeTableRequest request) throws BackendException { controlPlaneRateLimiter.acquire(); final Timer.Context apiTimerContext = getTimerContext(DESCRIBE_TABLE, request.getTableName()); DescribeTableResult result; try { result = client.describeTable(request); } catch (final Exception e) { throw processDynamoDbApiException(e, DESCRIBE_TABLE, request.getTableName()); } finally { apiTimerContext.stop(); } return result; }
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 describeTable(String tableName) throws IllegalArgumentException { try { DescribeTableResult describeResult = dynamoDBClient.describeTable(tableName); tableDescription = describeResult.getTable(); } catch (ResourceNotFoundException rnfe) { throw new IllegalArgumentException("Error: given table " + tableName + " does not exist in given region."); } catch (AmazonServiceException ase) { if (ase.getErrorCode().equals("UnrecognizedClientException")) throw new IllegalArgumentException("Error: Security token in credential file invalid."); else throw new IllegalArgumentException(ase.getMessage()); } }
/** * Check if table has already existed Using describe table API. If table has * not been created, exception will be thrown. */ public String isTableExist(String tableName) { if (tableName == null) throw new IllegalArgumentException("tableName should not be null."); DescribeTableResult table = new DescribeTableResult(); try { table = client.describeTable(tableName); return table.getTable().getTableName(); } catch (ResourceNotFoundException rnfe) { return null; } }
@Test public void test_describeTable() throws Exception { createTable(); DescribeTableResult result = dynamoDb.describeTable(TEST_TABLE_NAME); String tableName = result.getTable().getTableName(); assertThat(tableName, equalTo(TEST_TABLE_NAME)); }
/** * Checks if the main table exists in the database. * @param appid name of the {@link com.erudika.para.core.App} * @return true if the table exists */ public static boolean existsTable(String appid) { if (StringUtils.isBlank(appid)) { return false; } try { DescribeTableResult res = getClient().describeTable(getTableNameForAppid(appid)); return res != null; } catch (Exception e) { return false; } }
/*** * Get Table Status * @param tableName * @return status */ public String getTableStatus(final String tableName) throws ResourceNotFoundException{ final DescribeTableResult description = describeTable(tableName); if(description.getTable()!=null){ return description.getTable().getTableStatus(); } return null; }
/** * Creates DynamoDB table. If the table already exists, it validates the key schema. If the key schemas match, a * warning is logged, otherwise an exception is raised. * * @param dynamoDB * {@link AmazonDynamoDB} used to create the table specified in the request. * @param request * Request for creating a table. * @return TableDescription of the existing table or newly created table */ public static TableDescription createTable(final AmazonDynamoDB dynamoDB, final CreateTableRequest request) { try { final DescribeTableResult result = dynamoDB.describeTable(request.getTableName()); if (!request.getKeySchema().equals(result.getTable().getKeySchema())) { throw new IllegalStateException("Table " + request.getTableName() + " already exists and has an invalid schema"); } LOGGER.warning("Table " + request.getTableName() + " already exists"); return result.getTable(); } catch (final ResourceNotFoundException e) { return dynamoDB.createTable(request).getTableDescription(); } }
@Test public void testCreateTableTableAlreadyExistsCorrectKeySchema() { final Collection<AttributeDefinition> ads = Arrays.asList(new AttributeDefinition("Hash", ScalarAttributeType.S)); final Collection<KeySchemaElement> kses = Arrays.asList(new KeySchemaElement("Hash", KeyType.HASH)); final TableDescription description = new TableDescription().withAttributeDefinitions(ads).withKeySchema(kses) .withTableName(tableName); final DescribeTableResult result = new DescribeTableResult().withTable(description); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result); final CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(ads).withKeySchema(kses) .withTableName(tableName); PowerMock.replayAll(); assertEquals(description, DynamoDBManager.createTable(dynamoDB, request)); PowerMock.verifyAll(); }
@Test(expected = IllegalStateException.class) public void testCreateTableTableAlreadyExistsIncorrectKeySchema() { final Collection<AttributeDefinition> ads = Arrays.asList(new AttributeDefinition("Hash", ScalarAttributeType.S)); final Collection<KeySchemaElement> kses = Arrays.asList(new KeySchemaElement("Hash", KeyType.HASH)); final TableDescription description = new TableDescription().withAttributeDefinitions(ads).withKeySchema(kses) .withTableName(tableName); final DescribeTableResult result = new DescribeTableResult().withTable(description); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result); final Collection<AttributeDefinition> ads2 = Arrays.asList(new AttributeDefinition("Hash2", ScalarAttributeType.S)); final Collection<KeySchemaElement> kses2 = Arrays.asList(new KeySchemaElement("Hash2", KeyType.HASH)); final CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(ads2).withKeySchema(kses2) .withTableName(tableName); PowerMock.replayAll(); DynamoDBManager.createTable(dynamoDB, request); }
@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 testTableExists() { final DescribeTableResult result = new DescribeTableResult(); dynamoDB.describeTable(tableName); PowerMock.expectLastCall().andReturn(result); PowerMock.replayAll(); assertTrue(DynamoDBManager.doesTableExist(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 void waitForTableToBeReady() { DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(config().getTableName()); DescribeTableResult describeTableResult = config().getDynamoDBClient().describeTable(describeTableRequest); while (!describeTableResult.getTable().getTableStatus().equalsIgnoreCase("ACTIVE")) { try { Thread.sleep(2000); //FIXME no endless retry } catch (InterruptedException e) { throw new RuntimeException(e); } describeTableResult = config().getDynamoDBClient().describeTable(describeTableRequest); } }
protected boolean doesTableExist(String tableName) { try { DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName); DescribeTableResult result = ddb.describeTable(request); return (result != null && "ACTIVE".equals(result.getTable().getTableStatus())); } catch (ResourceNotFoundException e) { return false; } }
private void waitForTableToBeReady() { GeoDataManagerConfiguration config = geoDataManager.getGeoDataManagerConfiguration(); DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(config.getTableName()); DescribeTableResult describeTableResult = config.getDynamoDBClient().describeTable(describeTableRequest); while (!describeTableResult.getTable().getTableStatus().equalsIgnoreCase("ACTIVE")) { try { Thread.sleep(2000); } catch (InterruptedException e) { throw new RuntimeException(e); } describeTableResult = config.getDynamoDBClient().describeTable(describeTableRequest); } }