com.amazonaws.services.dynamodbv2.model.ResourceInUseException Java Examples

The following examples show how to use com.amazonaws.services.dynamodbv2.model.ResourceInUseException. 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: ProductInfoRepositoryIntegrationTest.java    From tutorials with MIT License 7 votes vote down vote up
@Before
public void setup() {
    try {
        repository = new ProductInfoRepository();
        repository.setMapper(dynamoDBMapper);

        CreateTableRequest tableRequest = dynamoDBMapper.generateCreateTableRequest(ProductInfo.class);

        tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L));

        amazonDynamoDB.createTable(tableRequest);
    } catch (ResourceInUseException e) {
        // Do nothing, table already created
    }

    // TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfo on table
    dynamoDBMapper.batchDelete((List<ProductInfo>) repository.findAll());
}
 
Example #2
Source File: DynamoDBDynamicFaultInjection.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void createTable() {
    List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("name").withAttributeType("S"));

    List<KeySchemaElement> ks = new ArrayList<KeySchemaElement>();
    ks.add(new KeySchemaElement().withAttributeName("name").withKeyType(KeyType.HASH)); // Partition
                                                                                        // key

    ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(10L)
        .withWriteCapacityUnits(10L);

    CreateTableRequest request = new CreateTableRequest().withTableName(TABLENAME)
        .withAttributeDefinitions(attributeDefinitions).withKeySchema(ks)
        .withProvisionedThroughput(provisionedThroughput);

    try {
        CreateTableResult createdTableDescription = dynamoDBClient.createTable(request);
        logger.info("Created Table: " + createdTableDescription);
        // Wait for it to become active
        waitForTableToBecomeAvailable(TABLENAME);
    }
    catch (ResourceInUseException e) {
        logger.warn("Table already existed", e);
    }
}
 
Example #3
Source File: ProductInfoRepositoryIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Before
public void setup() throws Exception {

    try {
        dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);

        CreateTableRequest tableRequest = dynamoDBMapper.generateCreateTableRequest(ProductInfo.class);

        tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L));

        amazonDynamoDB.createTable(tableRequest);
    } catch (ResourceInUseException e) {
        // Do nothing, table already created
    }

    // TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfo on table
    dynamoDBMapper.batchDelete((List<ProductInfo>) repository.findAll());
}
 
Example #4
Source File: TableHelper.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
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.");
}
 
Example #5
Source File: TableHelper.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
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.");
}
 
Example #6
Source File: TableHelper.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
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.");
}
 
Example #7
Source File: DynamoDBInstalledAppContextStore.java    From smartapp-sdk-java with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() {
    try {
        dynamoDB.createTable(tableName, Arrays.asList(new KeySchemaElement("installedAppId", KeyType.HASH)),
                Arrays.asList(new AttributeDefinition("installedAppId", "S")), new ProvisionedThroughput(10L, 10L));
    } catch (ResourceInUseException e) {
        log.info("InstalledAppContext table already exists");
    }
}
 
Example #8
Source File: DynamoDBInstallAppContextStoreTest.java    From smartapp-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void initIsFineWithTableAlreadyExisting() {
    when(dynamoDB.createTable(eq("installedAppContext"),
            eq(Arrays.asList(new KeySchemaElement("installedAppId", KeyType.HASH))),
            eq(Arrays.asList(new AttributeDefinition("installedAppId", "S"))), any()))
                    .thenThrow(new ResourceInUseException("table already exists"));

    tester.init();

    verify(dynamoDB).createTable(eq("installedAppContext"),
            eq(Arrays.asList(new KeySchemaElement("installedAppId", KeyType.HASH))),
            eq(Arrays.asList(new AttributeDefinition("installedAppId", "S"))), any());
}
 
Example #9
Source File: GenericDynamoDB.java    From strongbox with Apache License 2.0 5 votes vote down vote up
@Override
public String create() {
    readWriteLock.writeLock().lock();

    try {
        CreateTableResult result = client.createTable(constructCreateTableRequest());
        waitForTableToBecomeActive();
        return result.getTableDescription().getTableArn();
    } catch (ResourceInUseException e) {
        throw new AlreadyExistsException(String.format("There is already a DynamoDB table called '%s'", tableName), e);
    } finally {
        readWriteLock.writeLock().unlock();
    }
}
 
Example #10
Source File: StreamsAdapterDemoHelper.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
/**
 * @return StreamArn
 */
public static String createTable(AmazonDynamoDB client, String tableName) {
    java.util.List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));

    java.util.List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
    keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH)); // Partition
                                                                                             // key

    ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(2L)
        .withWriteCapacityUnits(2L);

    StreamSpecification streamSpecification = new StreamSpecification();
    streamSpecification.setStreamEnabled(true);
    streamSpecification.setStreamViewType(StreamViewType.NEW_IMAGE);
    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
        .withAttributeDefinitions(attributeDefinitions).withKeySchema(keySchema)
        .withProvisionedThroughput(provisionedThroughput).withStreamSpecification(streamSpecification);

    try {
        System.out.println("Creating table " + tableName);
        CreateTableResult result = client.createTable(createTableRequest);
        return result.getTableDescription().getLatestStreamArn();
    }
    catch (ResourceInUseException e) {
        System.out.println("Table already exists.");
        return describeTable(client, tableName).getTable().getLatestStreamArn();
    }
}
 
Example #11
Source File: TitanGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 5 votes vote down vote up
private void open(boolean batchLoading)
{
    if(type == GraphDatabaseType.TITAN_DYNAMODB && config.getDynamodbPrecreateTables()) {
        List<CreateTableRequest> requests = new LinkedList<>();
        long wcu = config.getDynamodbTps();
        long rcu = Math.max(1, config.dynamodbConsistentRead() ? wcu : (wcu / 2));
        for(String store : Constants.REQUIRED_BACKEND_STORES) {
            final String tableName = config.getDynamodbTablePrefix() + "_" + store;
            if(BackendDataModel.MULTI == config.getDynamodbDataModel()) {
                requests.add(DynamoDBStore.createTableRequest(tableName,
                    rcu, wcu));
            } else if(BackendDataModel.SINGLE == config.getDynamodbDataModel()) {
                requests.add(DynamoDBSingleRowStore.createTableRequest(tableName, rcu, wcu));
            }
        }
        //TODO is this autocloseable?
        final AmazonDynamoDB client =
            new AmazonDynamoDBClient(Client.createAWSCredentialsProvider(config.getDynamodbCredentialsFqClassName(),
                config.getDynamodbCredentialsCtorArguments() == null ? null : config.getDynamodbCredentialsCtorArguments().split(",")));
        client.setEndpoint(config.getDynamodbEndpoint());
        for(CreateTableRequest request : requests) {
            try {
                client.createTable(request);
            } catch(ResourceInUseException ignore) {
                //already created, good
            }
        }
        client.shutdown();
    }
    titanGraph = buildTitanGraph(type, dbStorageDirectory, config, batchLoading);
}
 
Example #12
Source File: StreamsAdapterDemoHelper.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
/**
 * @return StreamArn
 */
public static String createTable(AmazonDynamoDBClient client, String tableName) {
    java.util.List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));

    java.util.List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
    keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH));

    ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()
        .withReadCapacityUnits(2L).withWriteCapacityUnits(2L);

    StreamSpecification streamSpecification = new StreamSpecification();
    streamSpecification.setStreamEnabled(true);
    streamSpecification.setStreamViewType(StreamViewType.NEW_IMAGE);
    CreateTableRequest createTableRequest = new CreateTableRequest()
        .withTableName(tableName)
        .withAttributeDefinitions(attributeDefinitions)
        .withKeySchema(keySchema)
        .withProvisionedThroughput(provisionedThroughput)
        .withStreamSpecification(streamSpecification);

    try {
        System.out.println("Creating table " + tableName);
        CreateTableResult result = client.createTable(createTableRequest);
        return result.getTableDescription().getLatestStreamArn();
    } catch(ResourceInUseException e) {
        System.out.println("Table already exists.");
        return describeTable(client, tableName).getTable().getLatestStreamArn();
    }
}
 
Example #13
Source File: DynamoDBDynamicFaultInjection.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
private static void createTable()
{
    List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("name").withAttributeType("S"));

    List<KeySchemaElement> ks = new ArrayList<KeySchemaElement>();
    ks.add(new KeySchemaElement().withAttributeName("name").withKeyType(KeyType.HASH));

    ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(10L)
        .withWriteCapacityUnits(10L);

    CreateTableRequest request = new CreateTableRequest().withTableName(TABLENAME)
        .withAttributeDefinitions(attributeDefinitions).withKeySchema(ks)
        .withProvisionedThroughput(provisionedThroughput);

    try
    {
        CreateTableResult createdTableDescription = dynamoDBClient.createTable(request);
        logger.info("Created Table: " + createdTableDescription);
        // Wait for it to become active
        waitForTableToBecomeAvailable(TABLENAME);
    }
    catch (ResourceInUseException e)
    {
        logger.warn("Table already existed", e);
    }
}
 
Example #14
Source File: TransactionManager.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
/**
 * Ensures that the transaction table exists and has the correct schema.
 * 
 * @param client
 * @param transactionTableName
 * @param transactionImagesTableName
 * @throws ResourceInUseException if the table exists but has the wrong schema
 * @throws ResourceNotFoundException if the table does not exist
 */
public static void verifyTransactionTablesExist(AmazonDynamoDB client, String transactionTableName, String transactionImagesTableName) {
    String state = new TableHelper(client).verifyTableExists(transactionTableName, TRANSACTIONS_TABLE_ATTRIBUTES, TRANSACTIONS_TABLE_KEY_SCHEMA, null/*localIndexes*/);
    if(! "ACTIVE".equals(state)) {
        throw new ResourceInUseException("Table " + transactionTableName + " is not ACTIVE");
    }
    
    state = new TableHelper(client).verifyTableExists(transactionImagesTableName, TRANSACTION_IMAGES_TABLE_ATTRIBUTES, TRANSACTION_IMAGES_TABLE_KEY_SCHEMA, null/*localIndexes*/);
    if(! "ACTIVE".equals(state)) {
        throw new ResourceInUseException("Table " + transactionImagesTableName + " is not ACTIVE");
    }
}
 
Example #15
Source File: TableHelper.java    From dynamodb-transactions with Apache License 2.0 4 votes vote down vote up
public String verifyTableExists( 
    String tableName, 
    List<AttributeDefinition> definitions, 
    List<KeySchemaElement> keySchema,
    List<LocalSecondaryIndex> localIndexes) {
    
    DescribeTableResult describe = client.describeTable(new DescribeTableRequest().withTableName(tableName));
    if(! new HashSet<AttributeDefinition>(definitions).equals(new HashSet<AttributeDefinition>(describe.getTable().getAttributeDefinitions()))) {
        throw new ResourceInUseException("Table " + tableName + " had the wrong AttributesToGet." 
            + " Expected: " + definitions + " "
            + " Was: " + describe.getTable().getAttributeDefinitions());
    }
    
    if(! keySchema.equals(describe.getTable().getKeySchema())) {
        throw new ResourceInUseException("Table " + tableName + " had the wrong KeySchema." 
            + " Expected: " + keySchema + " "
            + " Was: " + describe.getTable().getKeySchema());
    }
    
    List<LocalSecondaryIndex> theirLSIs = null;
    if(describe.getTable().getLocalSecondaryIndexes() != null) {
        theirLSIs = new ArrayList<LocalSecondaryIndex>();
        for(LocalSecondaryIndexDescription description : describe.getTable().getLocalSecondaryIndexes()) {
            LocalSecondaryIndex lsi = new LocalSecondaryIndex()
                .withIndexName(description.getIndexName())
                .withKeySchema(description.getKeySchema())
                .withProjection(description.getProjection());
            theirLSIs.add(lsi);
        }
    }
    
    if(localIndexes != null) {
        if(! new HashSet<LocalSecondaryIndex>(localIndexes).equals(new HashSet<LocalSecondaryIndex>(theirLSIs))) {
            throw new ResourceInUseException("Table " + tableName + " did not have the expected LocalSecondaryIndexes."
                + " Expected: " + localIndexes
                + " Was: " + theirLSIs);
        }
    } else {
        if(theirLSIs != null) {
            throw new ResourceInUseException("Table " + tableName + " had local secondary indexes, but expected none."
                + " Indexes: " + theirLSIs);
        }
    }

    return describe.getTable().getTableStatus();
}