下面列出了怎么用com.amazonaws.services.dynamodbv2.model.TableStatus的API类实例代码及写法,或者点击链接到github查看源代码。
@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 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) {
}
}
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 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) { }
}
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) { }
}
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 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");
}
public void waitForTableActive(String tableName, long waitTimeSeconds) throws InterruptedException {
if(waitTimeSeconds < 0) {
throw new IllegalArgumentException("Invalid waitTimeSeconds " + waitTimeSeconds);
}
long startTimeMs = System.currentTimeMillis();
long elapsedMs = 0;
do {
DescribeTableResult describe = client.describeTable(new DescribeTableRequest().withTableName(tableName));
String status = describe.getTable().getTableStatus();
if(TableStatus.ACTIVE.toString().equals(status)) {
return;
}
if(TableStatus.DELETING.toString().equals(status)) {
throw new ResourceInUseException("Table " + tableName + " is " + status + ", and waiting for it to become ACTIVE is not useful.");
}
Thread.sleep(10 * 1000);
elapsedMs = System.currentTimeMillis() - startTimeMs;
} while(elapsedMs / 1000.0 < waitTimeSeconds);
throw new ResourceInUseException("Table " + tableName + " did not become ACTIVE after " + waitTimeSeconds + " seconds.");
}
public void waitForTableActive(String tableName,
List<AttributeDefinition> definitions,
List<KeySchemaElement> keySchema,
List<LocalSecondaryIndex> localIndexes,
long waitTimeSeconds) throws InterruptedException {
if(waitTimeSeconds < 0) {
throw new IllegalArgumentException("Invalid waitTimeSeconds " + waitTimeSeconds);
}
long startTimeMs = System.currentTimeMillis();
long elapsedMs = 0;
do {
String status = verifyTableExists(tableName, definitions, keySchema, localIndexes);
if(TableStatus.ACTIVE.toString().equals(status)) {
return;
}
if(TableStatus.DELETING.toString().equals(status)) {
throw new ResourceInUseException("Table " + tableName + " is " + status + ", and waiting for it to become ACTIVE is not useful.");
}
Thread.sleep(10 * 1000);
elapsedMs = System.currentTimeMillis() - startTimeMs;
} while(elapsedMs / 1000.0 < waitTimeSeconds);
throw new ResourceInUseException("Table " + tableName + " did not become ACTIVE after " + waitTimeSeconds + " seconds.");
}
public void waitForTableDeleted(String tableName, long waitTimeSeconds) throws InterruptedException {
if(waitTimeSeconds < 0) {
throw new IllegalArgumentException("Invalid waitTimeSeconds " + waitTimeSeconds);
}
long startTimeMs = System.currentTimeMillis();
long elapsedMs = 0;
do {
try {
DescribeTableResult describe = client.describeTable(new DescribeTableRequest().withTableName(tableName));
String status = describe.getTable().getTableStatus();
if(! TableStatus.DELETING.toString().equals(status)) {
throw new ResourceInUseException("Table " + tableName + " is " + status + ", and waiting for it to not exist is only useful if it is DELETING.");
}
} catch (ResourceNotFoundException e) {
return;
}
Thread.sleep(10 * 1000);
elapsedMs = System.currentTimeMillis() - startTimeMs;
} while(elapsedMs / 1000.0 < waitTimeSeconds);
throw new ResourceInUseException("Table " + tableName + " was not deleted after " + waitTimeSeconds + " seconds.");
}
private TableDescription getTableDescription(String tableName, Collection<AttributeDefinition> attributeDefinitions, Collection<KeySchemaElement> keySchema) {
TableDescription tableDescription = (new TableDescription())
.withTableName(tableName)
.withAttributeDefinitions(attributeDefinitions)
.withKeySchema(keySchema)
.withTableStatus(TableStatus.ACTIVE)
.withCreationDateTime(new Date())
.withTableArn(tableName);
return tableDescription;
}
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);
}
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 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;
}
@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);
}
@Override
public boolean indexExists(final String indexName) throws IOException {
try {
return TableStatus.ACTIVE.name().equals(
client.describeTable(getQualifiedTableName(indexName)).getTable().getTableStatus());
} catch (final AmazonDynamoDBException e) {
LOGGER.info("Unable to check existence of table", e);
}
return false;
}
@Override
public boolean metadataExists(final MetadataType type) throws IOException {
try {
return TableStatus.ACTIVE.name().equals(
client.describeTable(getMetadataTableName(type)).getTable().getTableStatus());
} catch (final AmazonDynamoDBException e) {
LOGGER.info("Unable to check existence of table", e);
}
return false;
}
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");
}
/**
* Verifies that the table exists with the specified schema, and creates it if it does not exist.
*
* @param tableName
* @param definitions
* @param keySchema
* @param localIndexes
* @param provisionedThroughput
* @param waitTimeSeconds
* @throws InterruptedException
*/
public void verifyOrCreateTable(
String tableName,
List<AttributeDefinition> definitions,
List<KeySchemaElement> keySchema,
List<LocalSecondaryIndex> localIndexes,
ProvisionedThroughput provisionedThroughput,
Long waitTimeSeconds) throws InterruptedException {
if(waitTimeSeconds != null && waitTimeSeconds < 0) {
throw new IllegalArgumentException("Invalid waitTimeSeconds " + waitTimeSeconds);
}
String status = null;
try {
status = verifyTableExists(tableName, definitions, keySchema, localIndexes);
} catch(ResourceNotFoundException e) {
status = client.createTable(new CreateTableRequest()
.withTableName(tableName)
.withAttributeDefinitions(definitions)
.withKeySchema(keySchema)
.withLocalSecondaryIndexes(localIndexes)
.withProvisionedThroughput(provisionedThroughput)).getTableDescription().getTableStatus();
}
if(waitTimeSeconds != null && ! TableStatus.ACTIVE.toString().equals(status)) {
waitForTableActive(tableName, definitions, keySchema, localIndexes, waitTimeSeconds);
}
}
private TableDescription constructTableDescription(TableStatus status) {
return new TableDescription().withTableArn(TEST_ARN).withTableStatus(status);
}
private static boolean isTableAcceptingWrites(final String status) {
return isTableStatus(TableStatus.ACTIVE, status) || isTableStatus(TableStatus.UPDATING, status);
}