Search in sources :

Example 36 with TableMetadata

use of com.scalar.db.api.TableMetadata 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);
    }
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) CancellationReason(software.amazon.awssdk.services.dynamodb.model.CancellationReason) TransactWriteItemsRequest(software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest) ArrayList(java.util.ArrayList) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) TransactionCanceledException(software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) ExecutionException(com.scalar.db.exception.storage.ExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) NoMutationException(com.scalar.db.exception.storage.NoMutationException)

Example 37 with TableMetadata

use of com.scalar.db.api.TableMetadata 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());
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) RegisterScalableTargetRequest(software.amazon.awssdk.services.applicationautoscaling.model.RegisterScalableTargetRequest) PutScalingPolicyRequest(software.amazon.awssdk.services.applicationautoscaling.model.PutScalingPolicyRequest) ArrayList(java.util.ArrayList) ExecutionException(com.scalar.db.exception.storage.ExecutionException) ExecutionException(com.scalar.db.exception.storage.ExecutionException) ObjectNotFoundException(software.amazon.awssdk.services.applicationautoscaling.model.ObjectNotFoundException) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException)

Example 38 with TableMetadata

use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.

the class DynamoAdmin method disableAutoScaling.

private void disableAutoScaling(String namespace, String table) throws ExecutionException {
    TableMetadata tableMetadata = getTableMetadata(namespace, table);
    if (tableMetadata == null) {
        return;
    }
    List<DeleteScalingPolicyRequest> deleteScalingPolicyRequestList = new ArrayList<>();
    List<DeregisterScalableTargetRequest> deregisterScalableTargetRequestList = new ArrayList<>();
    // write, read scaling of table
    for (String scalingType : TABLE_SCALING_TYPE_SET) {
        deleteScalingPolicyRequestList.add(buildDeleteScalingPolicyRequest(getTableResourceID(namespace, table), scalingType));
        deregisterScalableTargetRequestList.add(buildDeregisterScalableTargetRequest(getTableResourceID(namespace, table), scalingType));
    }
    // write, read scaling of global indexes (secondary indexes)
    Set<String> secondaryIndexes = tableMetadata.getSecondaryIndexNames();
    for (String secondaryIndex : secondaryIndexes) {
        for (String scalingType : SECONDARY_INDEX_SCALING_TYPE_SET) {
            deleteScalingPolicyRequestList.add(buildDeleteScalingPolicyRequest(getGlobalIndexResourceID(namespace, table, secondaryIndex), scalingType));
            deregisterScalableTargetRequestList.add(buildDeregisterScalableTargetRequest(getGlobalIndexResourceID(namespace, table, secondaryIndex), scalingType));
        }
    }
    deleteScalingPolicy(deleteScalingPolicyRequestList);
    deregisterScalableTarget(deregisterScalableTargetRequestList);
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) DeregisterScalableTargetRequest(software.amazon.awssdk.services.applicationautoscaling.model.DeregisterScalableTargetRequest) ArrayList(java.util.ArrayList) DeleteScalingPolicyRequest(software.amazon.awssdk.services.applicationautoscaling.model.DeleteScalingPolicyRequest)

Example 39 with TableMetadata

use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.

the class Cosmos method get.

@Override
@Nonnull
public Optional<Result> get(Get get) throws ExecutionException {
    get = copyAndSetTargetToIfNot(get);
    operationChecker.check(get);
    List<Record> records = selectStatementHandler.handle(get);
    if (records.size() > 1) {
        throw new IllegalArgumentException("please use scan() for non-exact match selection");
    }
    if (records.isEmpty() || records.get(0) == null) {
        return Optional.empty();
    }
    TableMetadata metadata = metadataManager.getTableMetadata(get);
    return Optional.of(new ResultInterpreter(get.getProjections(), metadata).interpret(records.get(0)));
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) Nonnull(javax.annotation.Nonnull)

Example 40 with TableMetadata

use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.

the class Cosmos method scan.

@Override
public Scanner scan(Scan scan) throws ExecutionException {
    scan = copyAndSetTargetToIfNot(scan);
    if (scan instanceof ScanAll) {
        operationChecker.check((ScanAll) scan);
    } else {
        operationChecker.check(scan);
    }
    List<Record> records = selectStatementHandler.handle(scan);
    TableMetadata metadata = metadataManager.getTableMetadata(scan);
    return new ScannerImpl(records, new ResultInterpreter(scan.getProjections(), metadata));
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) ScanAll(com.scalar.db.api.ScanAll)

Aggregations

TableMetadata (com.scalar.db.api.TableMetadata)101 Test (org.junit.jupiter.api.Test)39 Get (com.scalar.db.api.Get)11 PreparedStatement (java.sql.PreparedStatement)10 ExecutionException (com.scalar.db.exception.storage.ExecutionException)9 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Key (com.scalar.db.io.Key)6 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)6 Mutation (com.scalar.db.api.Mutation)5 Result (com.scalar.db.api.Result)5 GetTableMetadataResponse (com.scalar.db.rpc.GetTableMetadataResponse)5 ResultSet (java.sql.ResultSet)5 Nonnull (javax.annotation.Nonnull)5 HashMap (java.util.HashMap)4 Options (com.datastax.driver.core.schemabuilder.Create.Options)3 KeyspaceOptions (com.datastax.driver.core.schemabuilder.KeyspaceOptions)3 TableOptions (com.datastax.driver.core.schemabuilder.TableOptions)3 ScanAll (com.scalar.db.api.ScanAll)3 NoMutationException (com.scalar.db.exception.storage.NoMutationException)3