use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.
the class BatchHandler method handle.
/**
* Execute the specified list of {@link Mutation}s in batch. All the {@link Mutation}s in the list
* must be for the same partition.
*
* @param mutations a list of {@code Mutation}s to execute
* @throws NoMutationException if at least one of conditional {@code Mutation}s failed because it
* didn't meet the condition
*/
public void handle(List<? extends Mutation> mutations) throws ExecutionException {
if (mutations.size() > 25) {
throw new IllegalArgumentException("DynamoDB cannot batch more than 25 mutations at once.");
}
TableMetadata tableMetadata = metadataManager.getTableMetadata(mutations.get(0));
TransactWriteItemsRequest.Builder builder = TransactWriteItemsRequest.builder();
List<TransactWriteItem> transactItems = new ArrayList<>();
mutations.forEach(m -> transactItems.add(makeWriteItem(m, tableMetadata)));
builder.transactItems(transactItems);
try {
client.transactWriteItems(builder.build());
} catch (TransactionCanceledException e) {
boolean allReasonsAreTransactionConflicts = true;
for (CancellationReason reason : e.cancellationReasons()) {
if (reason.code().equals("ConditionalCheckFailed")) {
throw new NoMutationException("no mutation was applied.", e);
}
if (!reason.code().equals("TransactionConflict") && !reason.code().equals("None")) {
allReasonsAreTransactionConflicts = false;
}
}
if (allReasonsAreTransactionConflicts) {
// RetriableExecutionException
throw new RetriableExecutionException(e.getMessage(), e);
}
throw new ExecutionException(e.getMessage(), e);
} catch (DynamoDbException e) {
throw new ExecutionException(e.getMessage(), e);
}
}
use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.
the class DynamoAdmin method createIndex.
@Override
public void createIndex(String namespace, String table, String columnName, Map<String, String> options) throws ExecutionException {
long ru = Long.parseLong(options.getOrDefault(REQUEST_UNIT, DEFAULT_REQUEST_UNIT));
TableMetadata metadata = getTableMetadata(namespace, table);
try {
client.updateTable(UpdateTableRequest.builder().tableName(getFullTableName(namespace, table)).attributeDefinitions(AttributeDefinition.builder().attributeName(columnName).attributeType(SECONDARY_INDEX_DATATYPE_MAP.get(metadata.getColumnDataType(columnName))).build()).globalSecondaryIndexUpdates(GlobalSecondaryIndexUpdate.builder().create(CreateGlobalSecondaryIndexAction.builder().indexName(getGlobalIndexName(namespace, table, columnName)).keySchema(KeySchemaElement.builder().attributeName(columnName).keyType(KeyType.HASH).build()).projection(Projection.builder().projectionType(ProjectionType.ALL).build()).provisionedThroughput(ProvisionedThroughput.builder().readCapacityUnits(ru).writeCapacityUnits(ru).build()).build()).build()).build());
} catch (Exception e) {
throw new ExecutionException("creating the secondary index failed", e);
}
waitForIndexCreation(namespace, table, columnName);
// enable auto scaling
boolean noScaling = Boolean.parseBoolean(options.getOrDefault(NO_SCALING, DEFAULT_NO_SCALING));
if (!noScaling) {
List<RegisterScalableTargetRequest> registerScalableTargetRequestList = new ArrayList<>();
List<PutScalingPolicyRequest> putScalingPolicyRequestList = new ArrayList<>();
// write, read scaling of global indexes (secondary indexes)
for (String scalingType : SECONDARY_INDEX_SCALING_TYPE_SET) {
registerScalableTargetRequestList.add(buildRegisterScalableTargetRequest(getGlobalIndexResourceID(namespace, table, columnName), scalingType, (int) ru));
putScalingPolicyRequestList.add(buildPutScalingPolicyRequest(getGlobalIndexResourceID(namespace, table, columnName), scalingType));
}
registerScalableTarget(registerScalableTargetRequestList);
putScalingPolicy(putScalingPolicyRequestList);
}
// update metadata
TableMetadata tableMetadata = getTableMetadata(namespace, table);
putTableMetadata(namespace, table, TableMetadata.newBuilder(tableMetadata).addSecondaryIndex(columnName).build());
}
use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.
the class DynamoAdmin method truncateTable.
@Override
public void truncateTable(String namespace, String table) throws ExecutionException {
String fullTableName = getFullTableName(namespace, table);
Map<String, AttributeValue> lastKeyEvaluated = null;
do {
ScanResponse scanResponse;
try {
scanResponse = client.scan(ScanRequest.builder().tableName(fullTableName).limit(DELETE_BATCH_SIZE).exclusiveStartKey(lastKeyEvaluated).build());
} catch (Exception e) {
throw new ExecutionException("scanning items from table " + fullTableName + " failed.", e);
}
for (Map<String, AttributeValue> item : scanResponse.items()) {
Map<String, AttributeValue> keyToDelete = new HashMap<>();
keyToDelete.put(PARTITION_KEY, item.get(PARTITION_KEY));
if (item.containsKey(CLUSTERING_KEY)) {
keyToDelete.put(CLUSTERING_KEY, item.get(CLUSTERING_KEY));
}
try {
client.deleteItem(DeleteItemRequest.builder().tableName(fullTableName).key(keyToDelete).build());
} catch (Exception e) {
throw new ExecutionException("deleting item from table " + fullTableName + " failed.", e);
}
}
lastKeyEvaluated = scanResponse.lastEvaluatedKey();
} while (!lastKeyEvaluated.isEmpty());
}
use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.
the class DynamoAdmin method createMetadataTableIfNotExists.
private void createMetadataTableIfNotExists(boolean noBackup) throws ExecutionException {
if (metadataTableExists()) {
return;
}
List<AttributeDefinition> columnsToAttributeDefinitions = new ArrayList<>();
columnsToAttributeDefinitions.add(AttributeDefinition.builder().attributeName(METADATA_ATTR_TABLE).attributeType(ScalarAttributeType.S).build());
try {
client.createTable(CreateTableRequest.builder().attributeDefinitions(columnsToAttributeDefinitions).keySchema(KeySchemaElement.builder().attributeName(METADATA_ATTR_TABLE).keyType(KeyType.HASH).build()).provisionedThroughput(ProvisionedThroughput.builder().readCapacityUnits(METADATA_TABLE_REQUEST_UNIT).writeCapacityUnits(METADATA_TABLE_REQUEST_UNIT).build()).tableName(getFullTableName(metadataNamespace, METADATA_TABLE)).build());
} catch (Exception e) {
throw new ExecutionException("creating the metadata table failed", e);
}
waitForTableCreation(metadataNamespace, METADATA_TABLE);
if (!noBackup) {
enableContinuousBackup(metadataNamespace, METADATA_TABLE);
}
}
use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.
the class DynamoAdmin method namespaceExists.
@Override
public boolean namespaceExists(String namespace) throws ExecutionException {
try {
boolean namespaceExists = false;
String lastEvaluatedTableName = null;
do {
ListTablesRequest listTablesRequest = ListTablesRequest.builder().exclusiveStartTableName(lastEvaluatedTableName).build();
ListTablesResponse listTablesResponse = client.listTables(listTablesRequest);
lastEvaluatedTableName = listTablesResponse.lastEvaluatedTableName();
List<String> tableNames = listTablesResponse.tableNames();
for (String tableName : tableNames) {
if (tableName.startsWith(namespace)) {
namespaceExists = true;
break;
}
}
} while (lastEvaluatedTableName != null);
return namespaceExists;
} catch (DynamoDbException e) {
throw new ExecutionException("checking the namespace existence failed", e);
}
}
Aggregations