/** * 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 + waitTime; while (System.currentTimeMillis() < endTime) { try {Thread.sleep(sleepDeleteTime);} catch (Exception e) {} try { DescribeTableRequest request = new DescribeTableRequest().withTableName(pTableName); TableDescription tableDescription = dynamoDBClient.describeTable(request).getTable(); String tableStatus = tableDescription.getTableStatus(); LOG.debug(pTableName + " - current state: " + tableStatus); } catch (AmazonServiceException ase) { if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == true) return; ase.printStackTrace(); } } LOG.debug(pTableName + " deleted."); }
/** * Waits up to 6 minutes to confirm if a table has been created or not * @param pTableName */ private void waitForTableToBecomeAvailable(String tableName) { LOG.debug("Waiting for " + tableName + " to become available"); long startTime = System.currentTimeMillis(); long endTime = startTime + waitTime; while (System.currentTimeMillis() < endTime) { try {Thread.sleep(sleepTime);} catch (Exception e) {} try { DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName); TableDescription tableDescription = dynamoDBClient.describeTable(request).getTable(); String tableStatus = tableDescription.getTableStatus(); LOG.debug(tableName + " - 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 became active"); }
/** * Verifies if the specified schemas exist * @throws IOException */ @Override public boolean schemaExists() { LOG.info("Verifying schemas."); TableDescription success = null; if (mapping.getTables().isEmpty()) throw new IllegalStateException("There are not tables defined."); if (preferredSchema == null){ LOG.debug("Verifying schemas"); if (mapping.getTables().isEmpty()) throw new IllegalStateException("There are not tables defined."); // read the mapping object for(String tableName : mapping.getTables().keySet()){ success = getTableSchema(tableName); if (success == null) return false; } } else{ LOG.info("Verifying schema " + preferredSchema); success = getTableSchema(preferredSchema); } LOG.info("Finished verifying schemas."); return (success != null)? true: false; }
/** * Retrieves the table description for the specific resource name * @param tableName * @return */ private TableDescription getTableSchema(String tableName){ TableDescription tableDescription = null; try{ DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName); tableDescription = dynamoDBClient.describeTable(describeTableRequest).getTable(); } catch(ResourceNotFoundException e){ LOG.error("Error while getting table schema: " + tableName); return tableDescription; } return tableDescription; }
/** * Checks if a resource exists or not * @param tableName Table name to be checked * @return */ public TableDescription checkResource(String tableName){ TableDescription tableDescription = null; try{ DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName); tableDescription = dynamoDBClient.describeTable(describeTableRequest).getTable(); } catch(ResourceNotFoundException e){ tableDescription = null; } return tableDescription; }
@Test public void testMissingPrimaryKey() throws IOException, InterruptedException { String tableName = "mortar_test_foo_table"; String awsAccessKeyId = "XXXXXXXXXXXXX"; String awsSecretKey = "YYYYYYYYYYYYYY"; ResourceSchema schema = new ResourceSchema(Utils.getSchemaFromString("my_field:int")); // mock dynamo client AmazonDynamoDBClient dynamo = mock(AmazonDynamoDBClient.class); DescribeTableResult describeResult = new DescribeTableResult() .withTable( new TableDescription() .withProvisionedThroughput( new ProvisionedThroughputDescription().withWriteCapacityUnits(50L)) .withKeySchema(new KeySchema() .withHashKeyElement(new KeySchemaElement() .withAttributeName("not_the_key_you_will_find") .withAttributeType(ScalarAttributeType.N)))); when(dynamo.describeTable(any(DescribeTableRequest.class))).thenReturn(describeResult); DynamoDBStorage storage = new DynamoDBStorage(tableName, awsAccessKeyId, awsSecretKey, dynamo, null); try { storage.checkSchema(schema); Assert.fail("Expected schema validation to fail"); } catch(IOException e) { Assert.assertTrue("Expected " + e.getMessage() + " to contain hash msg", e.getMessage().contains("hash primary key")); } }