Search in sources :

Example 1 with ScalableDimension

use of software.amazon.awssdk.services.applicationautoscaling.model.ScalableDimension in project para by Erudika.

the class AWSDynamoUtils method createTableInternal.

private static boolean createTableInternal(String appid, long maxReadCapacity, long maxWriteCapacity, String region) {
    boolean replicate = !getReplicaRegions().isEmpty() && !App.isRoot(appid);
    try {
        String table = getTableNameForAppid(appid);
        CreateTableRequest.Builder ctr = CreateTableRequest.builder().tableName(table).sseSpecification(b2 -> b2.enabled(ENCRYPTION_AT_REST_ENABLED)).keySchema(KeySchemaElement.builder().attributeName(Config._KEY).keyType(KeyType.HASH).build()).attributeDefinitions(AttributeDefinition.builder().attributeName(Config._KEY).attributeType(ScalarAttributeType.S).build());
        if (replicate) {
            ctr.streamSpecification(s -> s.streamEnabled(replicate).streamViewType(StreamViewType.NEW_AND_OLD_IMAGES));
        }
        if (PROVISIONED_MODE) {
            ctr.billingMode(BillingMode.PROVISIONED);
            ctr.provisionedThroughput(b4 -> b4.readCapacityUnits(1L).writeCapacityUnits(1L));
        } else {
            ctr.billingMode(BillingMode.PAY_PER_REQUEST);
        }
        CreateTableResponse tbl = getClient(region).createTable(ctr.build());
        waitForActive(table, region);
        logger.info("Created DynamoDB table '{}', status {}.", table, tbl.tableDescription().tableStatus());
        if (replicate && PROVISIONED_MODE) {
            logger.info("Enabling autoscaling for DynamoDB table '{}'...", table);
            ApplicationAutoScalingClient aasClient = getAutoScalingClient(region);
            aasClient.registerScalableTarget(t -> t.serviceNamespace(ServiceNamespace.DYNAMODB).resourceId("table/" + table).scalableDimension(ScalableDimension.DYNAMODB_TABLE_READ_CAPACITY_UNITS).minCapacity(1).maxCapacity((int) maxReadCapacity));
            aasClient.registerScalableTarget(t -> t.serviceNamespace(ServiceNamespace.DYNAMODB).resourceId("table/" + table).scalableDimension(ScalableDimension.DYNAMODB_TABLE_WRITE_CAPACITY_UNITS).minCapacity(1).maxCapacity((int) maxWriteCapacity));
            aasClient.putScalingPolicy(s -> s.policyName(table + "-autoscale-reads").resourceId("table/" + table).serviceNamespace(ServiceNamespace.DYNAMODB).scalableDimension(ScalableDimension.DYNAMODB_TABLE_READ_CAPACITY_UNITS).policyType(PolicyType.TARGET_TRACKING_SCALING).targetTrackingScalingPolicyConfiguration(t -> t.predefinedMetricSpecification(p -> p.predefinedMetricType(MetricType.DYNAMO_DB_READ_CAPACITY_UTILIZATION)).targetValue(70.0).scaleInCooldown(60).scaleOutCooldown(60)));
            aasClient.putScalingPolicy(s -> s.policyName(table + "-autoscale-writes").resourceId("table/" + table).serviceNamespace(ServiceNamespace.DYNAMODB).scalableDimension(ScalableDimension.DYNAMODB_TABLE_WRITE_CAPACITY_UNITS).policyType(PolicyType.TARGET_TRACKING_SCALING).targetTrackingScalingPolicyConfiguration(t -> t.predefinedMetricSpecification(p -> p.predefinedMetricType(MetricType.DYNAMO_DB_WRITE_CAPACITY_UTILIZATION)).targetValue(70.0).scaleInCooldown(60).scaleOutCooldown(60)));
            waitForActive(table, region);
        }
    } catch (Exception e) {
        logger.error(null, e);
        return false;
    }
    return true;
}
Also used : TableDescription(software.amazon.awssdk.services.dynamodb.model.TableDescription) KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes) LoggerFactory(org.slf4j.LoggerFactory) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) StringUtils(org.apache.commons.lang3.StringUtils) StreamViewType(software.amazon.awssdk.services.dynamodb.model.StreamViewType) Map(java.util.Map) GlobalSecondaryIndex(software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex) URI(java.net.URI) WaiterResponse(software.amazon.awssdk.core.waiters.WaiterResponse) Config(com.erudika.para.core.utils.Config) ScalarAttributeType(software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) ParaObject(com.erudika.para.core.ParaObject) ProjectionType(software.amazon.awssdk.services.dynamodb.model.ProjectionType) ScanResponse(software.amazon.awssdk.services.dynamodb.model.ScanResponse) KeyType(software.amazon.awssdk.services.dynamodb.model.KeyType) QueryResponse(software.amazon.awssdk.services.dynamodb.model.QueryResponse) ReturnConsumedCapacity(software.amazon.awssdk.services.dynamodb.model.ReturnConsumedCapacity) Collectors(java.util.stream.Collectors) List(java.util.List) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) Annotation(java.lang.annotation.Annotation) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) Optional(java.util.Optional) Para(com.erudika.para.core.utils.Para) DefaultAwsRegionProviderChain(software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain) ListTablesResponse(software.amazon.awssdk.services.dynamodb.model.ListTablesResponse) App(com.erudika.para.core.App) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) Replica(software.amazon.awssdk.services.dynamodb.model.Replica) DescribeTableResponse(software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse) ParaObjectUtils(com.erudika.para.core.utils.ParaObjectUtils) HashMap(java.util.HashMap) Pager(com.erudika.para.core.utils.Pager) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) ServiceNamespace(software.amazon.awssdk.services.applicationautoscaling.model.ServiceNamespace) ProvisionedThroughputExceededException(software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughputExceededException) MetricType(software.amazon.awssdk.services.applicationautoscaling.model.MetricType) ScalableDimension(software.amazon.awssdk.services.applicationautoscaling.model.ScalableDimension) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest) ScanRequest(software.amazon.awssdk.services.dynamodb.model.ScanRequest) ApplicationAutoScalingClient(software.amazon.awssdk.services.applicationautoscaling.ApplicationAutoScalingClient) WriteRequest(software.amazon.awssdk.services.dynamodb.model.WriteRequest) LinkedList(java.util.LinkedList) Region(software.amazon.awssdk.regions.Region) GlobalSecondaryIndexDescription(software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndexDescription) Logger(org.slf4j.Logger) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) PolicyType(software.amazon.awssdk.services.applicationautoscaling.model.PolicyType) BatchWriteItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchWriteItemResponse) ReplicaUpdate(software.amazon.awssdk.services.dynamodb.model.ReplicaUpdate) KeySchemaElement(software.amazon.awssdk.services.dynamodb.model.KeySchemaElement) BillingMode(software.amazon.awssdk.services.dynamodb.model.BillingMode) Projection(software.amazon.awssdk.services.dynamodb.model.Projection) CreateTableResponse(software.amazon.awssdk.services.dynamodb.model.CreateTableResponse) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Collections(java.util.Collections) CreateTableResponse(software.amazon.awssdk.services.dynamodb.model.CreateTableResponse) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest) ProvisionedThroughputExceededException(software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughputExceededException) ApplicationAutoScalingClient(software.amazon.awssdk.services.applicationautoscaling.ApplicationAutoScalingClient)

Aggregations

App (com.erudika.para.core.App)1 ParaObject (com.erudika.para.core.ParaObject)1 Config (com.erudika.para.core.utils.Config)1 Pager (com.erudika.para.core.utils.Pager)1 Para (com.erudika.para.core.utils.Para)1 ParaObjectUtils (com.erudika.para.core.utils.ParaObjectUtils)1 Annotation (java.lang.annotation.Annotation)1 URI (java.net.URI)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 AwsBasicCredentials (software.amazon.awssdk.auth.credentials.AwsBasicCredentials)1 StaticCredentialsProvider (software.amazon.awssdk.auth.credentials.StaticCredentialsProvider)1