Search in sources :

Example 1 with ResourceNotFoundException

use of com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException in project camel by apache.

the class DdbEndpoint method waitForTableToBecomeAvailable.

private void waitForTableToBecomeAvailable(String tableName) {
    LOG.trace("Waiting for [{}] to become ACTIVE...", tableName);
    long waitTime = 5 * 60 * 1000;
    while (waitTime > 0) {
        try {
            Thread.sleep(1000 * 5);
            waitTime -= 5000;
        } catch (Exception e) {
        }
        try {
            DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
            TableDescription tableDescription = getDdbClient().describeTable(request).getTable();
            if (isTableActive(tableDescription)) {
                LOG.trace("Table [{}] became active", tableName);
                return;
            }
            LOG.trace("Table [{}] not active yet", tableName);
        } catch (AmazonServiceException ase) {
            if (!ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException")) {
                throw ase;
            }
        }
    }
    throw new RuntimeException("Table " + tableName + " never went active");
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription) AmazonServiceException(com.amazonaws.AmazonServiceException) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)

Example 2 with ResourceNotFoundException

use of com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException in project camel by apache.

the class DdbEndpoint method doStart.

@Override
public void doStart() throws Exception {
    super.doStart();
    ddbClient = configuration.getAmazonDDBClient() != null ? configuration.getAmazonDDBClient() : createDdbClient();
    if (ObjectHelper.isNotEmpty(configuration.getAmazonDdbEndpoint())) {
        ddbClient.setEndpoint(configuration.getAmazonDdbEndpoint());
    }
    String tableName = getConfiguration().getTableName();
    LOG.trace("Querying whether table [{}] already exists...", tableName);
    try {
        DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
        TableDescription tableDescription = ddbClient.describeTable(request).getTable();
        if (!isTableActive(tableDescription)) {
            waitForTableToBecomeAvailable(tableName);
        }
        LOG.trace("Table [{}] already exists", tableName);
        return;
    } catch (ResourceNotFoundException e) {
        LOG.trace("Table [{}] doesn't exist yet", tableName);
        LOG.trace("Creating table [{}]...", tableName);
        TableDescription tableDescription = createTable(tableName);
        if (!isTableActive(tableDescription)) {
            waitForTableToBecomeAvailable(tableName);
        }
        LOG.trace("Table [{}] created", tableName);
    }
}
Also used : DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription)

Example 3 with ResourceNotFoundException

use of com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException in project camel by apache.

the class AmazonDDBClientMock method describeTable.

@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");
}
Also used : DescribeTableResult(com.amazonaws.services.dynamodbv2.model.DescribeTableResult) ProvisionedThroughputDescription(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughputDescription) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription) Date(java.util.Date) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Example 4 with ResourceNotFoundException

use of com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException in project openhab1-addons by openhab.

the class DynamoDBPersistenceService method createTable.

/**
     * Create table (if not present) and wait for table to become active.
     *
     * Synchronized in order to ensure that at most single thread is creating the table at a time
     *
     * @param mapper
     * @param dtoClass
     * @return whether table creation succeeded.
     */
private synchronized boolean createTable(DynamoDBMapper mapper, Class<?> dtoClass) {
    if (db == null) {
        return false;
    }
    String tableName;
    try {
        ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(dbConfig.getReadCapacityUnits(), dbConfig.getWriteCapacityUnits());
        CreateTableRequest request = mapper.generateCreateTableRequest(dtoClass);
        request.setProvisionedThroughput(provisionedThroughput);
        if (request.getGlobalSecondaryIndexes() != null) {
            for (GlobalSecondaryIndex index : request.getGlobalSecondaryIndexes()) {
                index.setProvisionedThroughput(provisionedThroughput);
            }
        }
        tableName = request.getTableName();
        try {
            db.getDynamoClient().describeTable(tableName);
        } catch (ResourceNotFoundException e) {
            // No table present, continue with creation
            db.getDynamoClient().createTable(request);
        } catch (AmazonClientException e) {
            logger.error("Table creation failed due to error in describeTable operation", e);
            return false;
        }
        // table found or just created, wait
        return waitForTableToBecomeActive(tableName);
    } catch (AmazonClientException e) {
        logger.error("Exception when creating table", e);
        return false;
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) GlobalSecondaryIndex(com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex)

Example 5 with ResourceNotFoundException

use of com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException in project gora by apache.

the class DynamoDBStore method waitForTableToBeDeleted.

/**
   * Waits up to 6 minutes to confirm if a table has been deleted or not
   * 
   * @param pTableName
   */
private void waitForTableToBeDeleted(String pTableName) {
    LOG.debug("Waiting for " + pTableName + " to be deleted.");
    long startTime = System.currentTimeMillis();
    long endTime = startTime + WAIT_TIME;
    while (System.currentTimeMillis() < endTime) {
        try {
            Thread.sleep(SLEEP_DELETE_TIME);
        } catch (Exception e) {
        }
        try {
            DescribeTableRequest request = new DescribeTableRequest().withTableName(pTableName);
            TableDescription tableDescription = getDynamoDBClient().describeTable(request).getTable();
            String tableStatus = tableDescription.getTableStatus();
            LOG.debug(pTableName + " - current state: " + tableStatus);
        } catch (AmazonServiceException ase) {
            if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == true)
                return;
            LOG.error(ase.getMessage());
        }
    }
    LOG.debug(pTableName + " deleted.");
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription) GoraException(org.apache.gora.util.GoraException) AmazonServiceException(com.amazonaws.AmazonServiceException) IOException(java.io.IOException) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)

Aggregations

ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)10 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)7 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)6 AmazonServiceException (com.amazonaws.AmazonServiceException)5 HashMap (java.util.HashMap)3 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)2 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AttributeValueUpdate (com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate)1 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)1 DescribeTableResult (com.amazonaws.services.dynamodbv2.model.DescribeTableResult)1 GlobalSecondaryIndex (com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex)1 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)1 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)1 ProvisionedThroughputDescription (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughputDescription)1 ResourceInUseException (com.amazonaws.services.dynamodbv2.model.ResourceInUseException)1 FileNotFoundException (java.io.FileNotFoundException)1 Collection (java.util.Collection)1