software.amazon.awssdk.services.dynamodb.model.TableStatus Java Examples

The following examples show how to use software.amazon.awssdk.services.dynamodb.model.TableStatus. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DynamoDBUtils.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static CompletableFuture<Boolean> retryAsync(Supplier<CompletableFuture<DescribeTableResponse>> action,
        final long endTime) {

    return action.get()
            .thenComposeAsync(result -> {
                if (result.table().tableStatus() == TableStatus.ACTIVE) {
                    return CompletableFuture.completedFuture(true);
                } else {
                    try {
                        Thread.sleep(DEFAULT_WAIT_INTERVAL);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                    LOG.info("Async table - Retry table created status");
                    if (System.currentTimeMillis() < endTime) {
                        return retryAsync(action, endTime);
                    } else {
                        return CompletableFuture.completedFuture(false);
                    }
                }
            });
}
 
Example #2
Source File: DynamoDBIOTestHelper.java    From beam with Apache License 2.0 6 votes vote down vote up
static void createTestTable(String tableName) {
  CreateTableResponse res = createDynamoTable(tableName);

  TableDescription tableDesc = res.tableDescription();

  Assert.assertEquals(tableName, tableDesc.tableName());
  Assert.assertTrue(tableDesc.keySchema().toString().contains(ATTR_NAME_1));
  Assert.assertTrue(tableDesc.keySchema().toString().contains(ATTR_NAME_2));

  Assert.assertEquals(tableDesc.provisionedThroughput().readCapacityUnits(), Long.valueOf(1000));
  Assert.assertEquals(tableDesc.provisionedThroughput().writeCapacityUnits(), Long.valueOf(1000));
  Assert.assertEquals(TableStatus.ACTIVE, tableDesc.tableStatus());
  Assert.assertEquals(
      "arn:aws:dynamodb:ddblocal:000000000000:table/" + tableName, tableDesc.tableArn());

  ListTablesResponse tables = dynamoDBClient.listTables();
  Assert.assertEquals(1, tables.tableNames().size());
}
 
Example #3
Source File: AWSDynamoUtils.java    From para with Apache License 2.0 6 votes vote down vote up
private static void waitForActive(String table) throws InterruptedException {
	int attempts = 0;
	boolean active = false;
	int	tries = 30;
	int sleep = 2000;
	while (attempts < tries) {
		DescribeTableResponse result = getClient().describeTable(b -> b.tableName(table));
		if (result.table().tableStatus().equals(TableStatus.ACTIVE)) {
			active = true;
			break;
		}
		Thread.sleep(sleep);
		attempts++;
	}
	if (!active) {
		logger.warn("DynamoDB table {} did not become active within {}s!", table, ((tries * sleep) / 1000));
	}
}
 
Example #4
Source File: DynamoDBTableResource.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public ResourceStatus getResourceStatus() {
    CreateTableRequest createRequest = getCreateTableRequest();
    TableDescription table;
    try {
        table = getClient().describeTable(DescribeTableRequest.builder().tableName(
                createRequest.tableName()).build()).table();
    } catch (AwsServiceException exception) {
        if (exception.awsErrorDetails().errorCode().equalsIgnoreCase("ResourceNotFoundException")) {
            return ResourceStatus.NOT_EXIST;
        }
        throw exception;
    }

    if (table.tableStatus() == TableStatus.ACTIVE) {
        // returns AVAILABLE only if table KeySchema + LSIs + GSIs all match.
        if (UnorderedCollectionComparator.equalUnorderedCollections(createRequest.keySchema(), table.keySchema())
            && equalUnorderedGsiLists(createRequest.globalSecondaryIndexes(), table.globalSecondaryIndexes())
            && equalUnorderedLsiLists(createRequest.localSecondaryIndexes(), table.localSecondaryIndexes())) {
            return ResourceStatus.AVAILABLE;
        } else {
            return ResourceStatus.EXIST_INCOMPATIBLE_RESOURCE;
        }
    } else if (table.tableStatus() == TableStatus.CREATING
               || table.tableStatus() == TableStatus.UPDATING
               || table.tableStatus() == TableStatus.DELETING) {
        return ResourceStatus.TRANSIENT;
    } else {
        return ResourceStatus.NOT_EXIST;
    }
}
 
Example #5
Source File: TableUtils.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Wait for the table to reach the desired status and returns the table
 * description
 *
 * @param dynamo
 *            Dynamo client to use
 * @param tableName
 *            Table name to poll status of
 * @param desiredStatus
 *            Desired {@link TableStatus} to wait for. If null this method
 *            simply waits until DescribeTable returns something non-null
 *            (i.e. any status)
 * @param timeout
 *            Timeout in milliseconds to continue to poll for desired status
 * @param interval
 *            Time to wait in milliseconds between poll attempts
 * @return Null if DescribeTables never returns a result, otherwise the
 *         result of the last poll attempt (which may or may not have the
 *         desired state)
 * @throws {@link
 *             IllegalArgumentException} If timeout or interval is invalid
 */
private static TableDescription waitForTableDescription(final DynamoDbClient dynamo, final String tableName,
                                                        TableStatus desiredStatus, final int timeout, final int interval)
        throws InterruptedException, IllegalArgumentException {
    if (timeout < 0) {
        throw new IllegalArgumentException("Timeout must be >= 0");
    }
    if (interval <= 0 || interval >= timeout) {
        throw new IllegalArgumentException("Interval must be > 0 and < timeout");
    }
    long startTime = System.currentTimeMillis();
    long endTime = startTime + timeout;

    TableDescription table = null;
    while (System.currentTimeMillis() < endTime) {
        try {
            table = dynamo.describeTable(DescribeTableRequest.builder().tableName(tableName).build()).table();
            if (desiredStatus == null || table.tableStatus().equals(desiredStatus)) {
                return table;

            }
        } catch (ResourceNotFoundException rnfe) {
            // ResourceNotFound means the table doesn't exist yet,
            // so ignore this error and just keep polling.
        }

        Thread.sleep(interval);
    }
    return table;
}
 
Example #6
Source File: DynamoDBLockProviderIntegrationTest.java    From ShedLock with Apache License 2.0 5 votes vote down vote up
@BeforeAll
static void createLockProvider() {
    dynamodb = createClient();
    String lockTable = DynamoDBUtils.createLockTable(
        dynamodb,
        TABLE_NAME,
        ProvisionedThroughput.builder()
            .readCapacityUnits(1L)
            .writeCapacityUnits(1L)
            .build()
    );
    while (getTableStatus(lockTable) != TableStatus.ACTIVE) ;
}
 
Example #7
Source File: DynamoDBLeaseRefresherTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testWaitUntilLeaseTableExistsUpdatingStatus() throws Exception {
    when(dynamoDbClient.describeTable(any(DescribeTableRequest.class))).thenReturn(mockDescribeTableFuture);
    when(mockDescribeTableFuture.get(anyLong(), any()))
            .thenReturn(DescribeTableResponse.builder()
                    .table(TableDescription.builder().tableStatus(TableStatus.UPDATING).build())
                    .build());
    assertTrue(leaseRefresher.waitUntilLeaseTableExists(0, 0));
}
 
Example #8
Source File: DynamoDBLeaseRefresherTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testWaitUntilLeaseTableExistsActiveStatus() throws Exception {
    when(dynamoDbClient.describeTable(any(DescribeTableRequest.class))).thenReturn(mockDescribeTableFuture);
    when(mockDescribeTableFuture.get(anyLong(), any()))
            .thenReturn(DescribeTableResponse.builder()
                    .table(TableDescription.builder().tableStatus(TableStatus.ACTIVE).build())
                    .build());
    assertTrue(leaseRefresher.waitUntilLeaseTableExists(0, 0));
}
 
Example #9
Source File: DynamoDBLeaseRefresherTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testWaitUntilLeaseTableExistsCreatingStatus() throws Exception {
    when(dynamoDbClient.describeTable(any(DescribeTableRequest.class))).thenReturn(mockDescribeTableFuture);
    when(mockDescribeTableFuture.get(anyLong(), any()))
            .thenReturn(DescribeTableResponse.builder()
                    .table(TableDescription.builder().tableStatus(TableStatus.CREATING).build())
                    .build());
    assertFalse(leaseRefresher.waitUntilLeaseTableExists(0, 0));
}
 
Example #10
Source File: DynamoDBLeaseRefresherTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testWaitUntilLeaseTableExistsDeletingStatus() throws Exception {
    when(dynamoDbClient.describeTable(any(DescribeTableRequest.class))).thenReturn(mockDescribeTableFuture);
    when(mockDescribeTableFuture.get(anyLong(), any()))
            .thenReturn(DescribeTableResponse.builder()
                    .table(TableDescription.builder().tableStatus(TableStatus.DELETING).build())
                    .build());
    assertFalse(leaseRefresher.waitUntilLeaseTableExists(0, 0));
}
 
Example #11
Source File: TableUtils.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
public TableNeverTransitionedToStateException(String tableName, TableStatus desiredStatus) {
    super(SdkClientException.builder()
                            .message("Table " + tableName + " never transitioned to desired state of " +
                                     desiredStatus.toString()));
}
 
Example #12
Source File: DynamoDBLockProviderIntegrationTest.java    From ShedLock with Apache License 2.0 4 votes vote down vote up
private static TableStatus getTableStatus(String lockTable) {
    return dynamodb.describeTable(DescribeTableRequest.builder().tableName(lockTable).build()).table().tableStatus();
}
 
Example #13
Source File: TableUtils.java    From aws-sdk-java-v2 with Apache License 2.0 3 votes vote down vote up
/**
 * Waits up to a specified amount of time for a specified DynamoDB table to
 * move into the <code>ACTIVE</code> state. If the table does not exist or
 * does not transition to the <code>ACTIVE</code> state after this time,
 * then a SdkClientException is thrown.
 *
 * @param dynamo
 *            The DynamoDB client to use to make requests.
 * @param tableName
 *            The name of the table whose status is being checked.
 * @param timeout
 *            The maximum number of milliseconds to wait.
 * @param interval
 *            The poll interval in milliseconds.
 *
 * @throws TableNeverTransitionedToStateException
 *             If the specified table does not exist or does not transition
 *             into the <code>ACTIVE</code> state before this method times
 *             out and stops polling.
 * @throws InterruptedException
 *             If the thread is interrupted while waiting for the table to
 *             transition into the <code>ACTIVE</code> state.
 */
public static void waitUntilActive(final DynamoDbClient dynamo, final String tableName, final int timeout,
                                   final int interval) throws InterruptedException, TableNeverTransitionedToStateException {
    TableDescription table = waitForTableDescription(dynamo, tableName, TableStatus.ACTIVE, timeout, interval);

    if (table == null || !table.tableStatus().equals(TableStatus.ACTIVE)) {
        throw new TableNeverTransitionedToStateException(tableName, TableStatus.ACTIVE);
    }
}