use of com.amazonaws.services.applicationautoscaling.model.PutScalingPolicyResult in project titus-control-plane by Netflix.
the class AWSAppAutoScalingClient method createOrUpdateScalingPolicy.
@Override
public Observable<String> createOrUpdateScalingPolicy(String policyRefId, String jobId, PolicyConfiguration policyConfiguration) {
PutScalingPolicyRequest putScalingPolicyRequest = new PutScalingPolicyRequest();
putScalingPolicyRequest.setPolicyName(buildScalingPolicyName(policyRefId, jobId));
putScalingPolicyRequest.setPolicyType(policyConfiguration.getPolicyType().name());
putScalingPolicyRequest.setResourceId(AWSAppAutoScalingUtil.buildGatewayResourceId(jobId, awsAppScalingConfig.getAWSGatewayEndpointPrefix(), awsAppScalingConfig.getRegion(), awsAppScalingConfig.getAWSGatewayEndpointTargetStage()));
putScalingPolicyRequest.setServiceNamespace(SERVICE_NAMESPACE);
putScalingPolicyRequest.setScalableDimension(SCALABLE_DIMENSION);
if (policyConfiguration.getPolicyType() == PolicyType.StepScaling) {
StepScalingPolicyConfiguration stepScalingPolicyConfiguration = new StepScalingPolicyConfiguration();
if (policyConfiguration.getStepScalingPolicyConfiguration().getMetricAggregationType().isPresent()) {
stepScalingPolicyConfiguration.setMetricAggregationType(policyConfiguration.getStepScalingPolicyConfiguration().getMetricAggregationType().get().name());
}
if (policyConfiguration.getStepScalingPolicyConfiguration().getCoolDownSec().isPresent()) {
stepScalingPolicyConfiguration.setCooldown(policyConfiguration.getStepScalingPolicyConfiguration().getCoolDownSec().get());
}
List<com.netflix.titus.api.appscale.model.StepAdjustment> steps = policyConfiguration.getStepScalingPolicyConfiguration().getSteps();
List<StepAdjustment> stepAdjustments = steps.stream().map(step -> {
StepAdjustment stepAdjustment = new StepAdjustment();
if (step.getMetricIntervalUpperBound() != null && step.getMetricIntervalUpperBound().isPresent()) {
stepAdjustment.setMetricIntervalUpperBound(step.getMetricIntervalUpperBound().get());
}
if (step.getMetricIntervalLowerBound() != null && step.getMetricIntervalLowerBound().isPresent()) {
stepAdjustment.setMetricIntervalLowerBound(step.getMetricIntervalLowerBound().get());
}
stepAdjustment.setScalingAdjustment(step.getScalingAdjustment());
return stepAdjustment;
}).collect(Collectors.toList());
stepScalingPolicyConfiguration.setStepAdjustments(stepAdjustments);
if (policyConfiguration.getStepScalingPolicyConfiguration().getAdjustmentType().isPresent()) {
stepScalingPolicyConfiguration.setAdjustmentType(policyConfiguration.getStepScalingPolicyConfiguration().getAdjustmentType().get().name());
}
putScalingPolicyRequest.setStepScalingPolicyConfiguration(stepScalingPolicyConfiguration);
} else if (policyConfiguration.getPolicyType() == PolicyType.TargetTrackingScaling) {
TargetTrackingScalingPolicyConfiguration targetTrackingConfigAws = new TargetTrackingScalingPolicyConfiguration();
TargetTrackingPolicy targetTrackingPolicyInt = policyConfiguration.getTargetTrackingPolicy();
targetTrackingConfigAws.setTargetValue(targetTrackingPolicyInt.getTargetValue());
if (targetTrackingPolicyInt.getDisableScaleIn().isPresent()) {
targetTrackingConfigAws.setDisableScaleIn(targetTrackingPolicyInt.getDisableScaleIn().get());
}
if (targetTrackingPolicyInt.getScaleInCooldownSec().isPresent()) {
targetTrackingConfigAws.setScaleInCooldown(targetTrackingPolicyInt.getScaleInCooldownSec().get());
}
if (targetTrackingPolicyInt.getScaleOutCooldownSec().isPresent()) {
targetTrackingConfigAws.setScaleOutCooldown(targetTrackingPolicyInt.getScaleOutCooldownSec().get());
}
if (targetTrackingPolicyInt.getCustomizedMetricSpecification().isPresent()) {
com.netflix.titus.api.appscale.model.CustomizedMetricSpecification customizedMetricSpecInt = targetTrackingPolicyInt.getCustomizedMetricSpecification().get();
CustomizedMetricSpecification customizedMetricSpecAws = new CustomizedMetricSpecification();
customizedMetricSpecAws.setDimensions(customizedMetricSpecInt.getMetricDimensionList().stream().map(metricDimensionInt -> {
MetricDimension metricDimensionAws = new MetricDimension().withName(metricDimensionInt.getName()).withValue(metricDimensionInt.getValue());
return metricDimensionAws;
}).collect(Collectors.toList()));
customizedMetricSpecAws.setMetricName(customizedMetricSpecInt.getMetricName());
customizedMetricSpecAws.setNamespace(customizedMetricSpecInt.getNamespace());
customizedMetricSpecAws.setStatistic(customizedMetricSpecInt.getStatistic().name());
if (customizedMetricSpecInt.getUnit().isPresent()) {
customizedMetricSpecAws.setUnit(customizedMetricSpecInt.getUnit().get());
}
targetTrackingConfigAws.setCustomizedMetricSpecification(customizedMetricSpecAws);
}
putScalingPolicyRequest.setTargetTrackingScalingPolicyConfiguration(targetTrackingConfigAws);
} else {
return Observable.error(new UnsupportedOperationException(String.format("Scaling policy type not supported: jobId=%s, policyRefId=%s, type=%s", jobId, policyRefId, policyConfiguration.getPolicyType().name())));
}
return RetryWrapper.wrapWithExponentialRetry(String.format("createOrUpdateScalingPolicy %s for job %s", policyRefId, jobId), Observable.create(emitter -> awsAppAutoScalingClientAsync.putScalingPolicyAsync(putScalingPolicyRequest, new AsyncHandler<PutScalingPolicyRequest, PutScalingPolicyResult>() {
@Override
public void onError(Exception exception) {
logger.error("Exception creating scaling policy: jobId={}, policyRefId={}", jobId, policyRefId, exception);
awsAppAutoScalingMetrics.registerAwsCreatePolicyError(exception);
if (exception instanceof ValidationException) {
emitter.onError(AutoScalePolicyException.invalidScalingPolicy(policyRefId, exception.getMessage()));
} else {
emitter.onError(AutoScalePolicyException.errorCreatingPolicy(policyRefId, exception.getMessage()));
}
}
@Override
public void onSuccess(PutScalingPolicyRequest request, PutScalingPolicyResult putScalingPolicyResult) {
String policyARN = putScalingPolicyResult.getPolicyARN();
logger.info("New Scaling policy {} created {} for Job {}", request, policyARN, jobId);
awsAppAutoScalingMetrics.registerAwsCreatePolicySuccess();
emitter.onNext(policyARN);
emitter.onCompleted();
}
}), Emitter.BackpressureMode.NONE));
}
Aggregations