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;
}
Aggregations