Search in sources :

Example 1 with PutScalingPolicyResult

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));
}
Also used : ScalableTarget(com.amazonaws.services.applicationautoscaling.model.ScalableTarget) AutoScalePolicyException(com.netflix.titus.api.appscale.service.AutoScalePolicyException) Completable(rx.Completable) CustomizedMetricSpecification(com.amazonaws.services.applicationautoscaling.model.CustomizedMetricSpecification) DeleteScalingPolicyRequest(com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyRequest) StepScalingPolicyConfiguration(com.amazonaws.services.applicationautoscaling.model.StepScalingPolicyConfiguration) LoggerFactory(org.slf4j.LoggerFactory) AutoScalableTarget(com.netflix.titus.api.appscale.model.AutoScalableTarget) AWSApplicationAutoScalingAsyncClientBuilder(com.amazonaws.services.applicationautoscaling.AWSApplicationAutoScalingAsyncClientBuilder) Singleton(javax.inject.Singleton) DescribeScalableTargetsRequest(com.amazonaws.services.applicationautoscaling.model.DescribeScalableTargetsRequest) TargetTrackingPolicy(com.netflix.titus.api.appscale.model.TargetTrackingPolicy) Observable(rx.Observable) Inject(javax.inject.Inject) DeleteScalingPolicyResult(com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyResult) AWSApplicationAutoScalingAsync(com.amazonaws.services.applicationautoscaling.AWSApplicationAutoScalingAsync) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) ValidationException(com.amazonaws.services.applicationautoscaling.model.ValidationException) ObjectNotFoundException(com.amazonaws.services.applicationautoscaling.model.ObjectNotFoundException) PutScalingPolicyResult(com.amazonaws.services.applicationautoscaling.model.PutScalingPolicyResult) RegisterScalableTargetResult(com.amazonaws.services.applicationautoscaling.model.RegisterScalableTargetResult) RetryWrapper(com.netflix.titus.ext.aws.RetryWrapper) Logger(org.slf4j.Logger) DeregisterScalableTargetResult(com.amazonaws.services.applicationautoscaling.model.DeregisterScalableTargetResult) StepAdjustment(com.amazonaws.services.applicationautoscaling.model.StepAdjustment) TargetTrackingScalingPolicyConfiguration(com.amazonaws.services.applicationautoscaling.model.TargetTrackingScalingPolicyConfiguration) AppAutoScalingClient(com.netflix.titus.api.connector.cloud.AppAutoScalingClient) Emitter(rx.Emitter) Collectors(java.util.stream.Collectors) PolicyType(com.netflix.titus.api.appscale.model.PolicyType) DescribeScalableTargetsResult(com.amazonaws.services.applicationautoscaling.model.DescribeScalableTargetsResult) List(java.util.List) AWSAppAutoScalingUtil.buildScalingPolicyName(com.netflix.titus.ext.aws.appscale.AWSAppAutoScalingUtil.buildScalingPolicyName) RegisterScalableTargetRequest(com.amazonaws.services.applicationautoscaling.model.RegisterScalableTargetRequest) AsyncHandler(com.amazonaws.handlers.AsyncHandler) Registry(com.netflix.spectator.api.Registry) DeregisterScalableTargetRequest(com.amazonaws.services.applicationautoscaling.model.DeregisterScalableTargetRequest) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MetricDimension(com.amazonaws.services.applicationautoscaling.model.MetricDimension) PutScalingPolicyRequest(com.amazonaws.services.applicationautoscaling.model.PutScalingPolicyRequest) Collections(java.util.Collections) PolicyConfiguration(com.netflix.titus.api.appscale.model.PolicyConfiguration) ValidationException(com.amazonaws.services.applicationautoscaling.model.ValidationException) CustomizedMetricSpecification(com.amazonaws.services.applicationautoscaling.model.CustomizedMetricSpecification) PutScalingPolicyResult(com.amazonaws.services.applicationautoscaling.model.PutScalingPolicyResult) StepAdjustment(com.amazonaws.services.applicationautoscaling.model.StepAdjustment) TargetTrackingScalingPolicyConfiguration(com.amazonaws.services.applicationautoscaling.model.TargetTrackingScalingPolicyConfiguration) MetricDimension(com.amazonaws.services.applicationautoscaling.model.MetricDimension) AutoScalePolicyException(com.netflix.titus.api.appscale.service.AutoScalePolicyException) ValidationException(com.amazonaws.services.applicationautoscaling.model.ValidationException) ObjectNotFoundException(com.amazonaws.services.applicationautoscaling.model.ObjectNotFoundException) TargetTrackingPolicy(com.netflix.titus.api.appscale.model.TargetTrackingPolicy) PutScalingPolicyRequest(com.amazonaws.services.applicationautoscaling.model.PutScalingPolicyRequest) StepScalingPolicyConfiguration(com.amazonaws.services.applicationautoscaling.model.StepScalingPolicyConfiguration)

Aggregations

AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)1 AsyncHandler (com.amazonaws.handlers.AsyncHandler)1 AWSApplicationAutoScalingAsync (com.amazonaws.services.applicationautoscaling.AWSApplicationAutoScalingAsync)1 AWSApplicationAutoScalingAsyncClientBuilder (com.amazonaws.services.applicationautoscaling.AWSApplicationAutoScalingAsyncClientBuilder)1 CustomizedMetricSpecification (com.amazonaws.services.applicationautoscaling.model.CustomizedMetricSpecification)1 DeleteScalingPolicyRequest (com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyRequest)1 DeleteScalingPolicyResult (com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyResult)1 DeregisterScalableTargetRequest (com.amazonaws.services.applicationautoscaling.model.DeregisterScalableTargetRequest)1 DeregisterScalableTargetResult (com.amazonaws.services.applicationautoscaling.model.DeregisterScalableTargetResult)1 DescribeScalableTargetsRequest (com.amazonaws.services.applicationautoscaling.model.DescribeScalableTargetsRequest)1 DescribeScalableTargetsResult (com.amazonaws.services.applicationautoscaling.model.DescribeScalableTargetsResult)1 MetricDimension (com.amazonaws.services.applicationautoscaling.model.MetricDimension)1 ObjectNotFoundException (com.amazonaws.services.applicationautoscaling.model.ObjectNotFoundException)1 PutScalingPolicyRequest (com.amazonaws.services.applicationautoscaling.model.PutScalingPolicyRequest)1 PutScalingPolicyResult (com.amazonaws.services.applicationautoscaling.model.PutScalingPolicyResult)1 RegisterScalableTargetRequest (com.amazonaws.services.applicationautoscaling.model.RegisterScalableTargetRequest)1 RegisterScalableTargetResult (com.amazonaws.services.applicationautoscaling.model.RegisterScalableTargetResult)1 ScalableTarget (com.amazonaws.services.applicationautoscaling.model.ScalableTarget)1 StepAdjustment (com.amazonaws.services.applicationautoscaling.model.StepAdjustment)1 StepScalingPolicyConfiguration (com.amazonaws.services.applicationautoscaling.model.StepScalingPolicyConfiguration)1