Search in sources :

Example 1 with PutScalingPolicyRequest

use of software.amazon.awssdk.services.applicationautoscaling.model.PutScalingPolicyRequest 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 2 with PutScalingPolicyRequest

use of software.amazon.awssdk.services.applicationautoscaling.model.PutScalingPolicyRequest in project scalardb by scalar-labs.

the class DynamoAdmin method enableAutoScaling.

private void enableAutoScaling(String namespace, String table, Set<String> secondaryIndexes, long ru) throws ExecutionException {
    List<RegisterScalableTargetRequest> registerScalableTargetRequestList = new ArrayList<>();
    List<PutScalingPolicyRequest> putScalingPolicyRequestList = new ArrayList<>();
    // write, read scaling of table
    for (String scalingType : TABLE_SCALING_TYPE_SET) {
        registerScalableTargetRequestList.add(buildRegisterScalableTargetRequest(getTableResourceID(namespace, table), scalingType, (int) ru));
        putScalingPolicyRequestList.add(buildPutScalingPolicyRequest(getTableResourceID(namespace, table), scalingType));
    }
    // write, read scaling of global indexes (secondary indexes)
    for (String secondaryIndex : secondaryIndexes) {
        for (String scalingType : SECONDARY_INDEX_SCALING_TYPE_SET) {
            registerScalableTargetRequestList.add(buildRegisterScalableTargetRequest(getGlobalIndexResourceID(namespace, table, secondaryIndex), scalingType, (int) ru));
            putScalingPolicyRequestList.add(buildPutScalingPolicyRequest(getGlobalIndexResourceID(namespace, table, secondaryIndex), scalingType));
        }
    }
    // request
    for (RegisterScalableTargetRequest registerScalableTargetRequest : registerScalableTargetRequestList) {
        try {
            applicationAutoScalingClient.registerScalableTarget(registerScalableTargetRequest);
        } catch (Exception e) {
            throw new ExecutionException("Unable to register scalable target for " + registerScalableTargetRequest.resourceId(), e);
        }
    }
    for (PutScalingPolicyRequest putScalingPolicyRequest : putScalingPolicyRequestList) {
        try {
            applicationAutoScalingClient.putScalingPolicy(putScalingPolicyRequest);
        } catch (Exception e) {
            throw new ExecutionException("Unable to put scaling policy request for " + putScalingPolicyRequest.resourceId(), e);
        }
    }
}
Also used : 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)

Aggregations

ExecutionException (com.scalar.db.exception.storage.ExecutionException)2 ArrayList (java.util.ArrayList)2 ObjectNotFoundException (software.amazon.awssdk.services.applicationautoscaling.model.ObjectNotFoundException)2 PutScalingPolicyRequest (software.amazon.awssdk.services.applicationautoscaling.model.PutScalingPolicyRequest)2 RegisterScalableTargetRequest (software.amazon.awssdk.services.applicationautoscaling.model.RegisterScalableTargetRequest)2 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)2 ResourceNotFoundException (software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)2 TableMetadata (com.scalar.db.api.TableMetadata)1