use of com.netflix.titus.api.appscale.model.PolicyConfiguration in project titus-control-plane by Netflix.
the class ScalingPolicyModelSanitizationTest method testInvalidStepScalingPolicyNoAlarmConfiguration.
@Test
public void testInvalidStepScalingPolicyNoAlarmConfiguration() {
PolicyConfiguration policyConfiguration = PolicyConfiguration.newBuilder().withStepScalingPolicyConfiguration(mock(StepScalingPolicyConfiguration.class)).build();
AutoScalingPolicy autoScalingPolicy = AutoScalingPolicy.newBuilder().withPolicyConfiguration(policyConfiguration).build();
Set<ValidationError> validationErrors = newScalingPolicySanitizer().validate(autoScalingPolicy);
assertThat(validationErrors).hasSize(1);
assertThat(CollectionsExt.first(validationErrors).getDescription()).isEqualTo("alarmConfiguration must be specified for a step scaling policy");
}
use of com.netflix.titus.api.appscale.model.PolicyConfiguration in project titus-control-plane by Netflix.
the class ScalingPolicyAssertions method validateScalingPolicy.
public Map<String, String> validateScalingPolicy(AutoScalingPolicy autoScalingPolicy) {
if (autoScalingPolicy == null || autoScalingPolicy.getPolicyConfiguration() == null) {
return Collections.emptyMap();
}
PolicyConfiguration policyConfiguration = autoScalingPolicy.getPolicyConfiguration();
Map<String, String> violations = new HashMap<>();
if ((policyConfiguration.getStepScalingPolicyConfiguration() != null) == (policyConfiguration.getTargetTrackingPolicy() != null)) {
violations.put("scalingPolicies", "exactly one scaling policy should be set");
}
if (policyConfiguration.getStepScalingPolicyConfiguration() != null && policyConfiguration.getAlarmConfiguration() == null) {
violations.put("alarmConfiguration", "alarmConfiguration must be specified for a step scaling policy");
}
return violations;
}
use of com.netflix.titus.api.appscale.model.PolicyConfiguration in project titus-control-plane by Netflix.
the class CassAppScalePolicyStoreTest method buildAutoScalingPolicy.
private AutoScalingPolicy buildAutoScalingPolicy(String jobId) {
AlarmConfiguration alarmConfiguration = AlarmConfiguration.newBuilder().withActionsEnabled(true).withComparisonOperator(ComparisonOperator.GreaterThanThreshold).withEvaluationPeriods(1).withPeriodSec(60).withMetricName("CPUUtilization").withMetricNamespace("AWS/EC2").withName(jobId).withStatistic(Statistic.Average).build();
StepAdjustment stepAdjustment = StepAdjustment.newBuilder().withMetricIntervalLowerBound(0).withScalingAdjustment(1).build();
StepScalingPolicyConfiguration stepScalingPolicyConfiguration = StepScalingPolicyConfiguration.newBuilder().withAdjustmentType(StepAdjustmentType.ChangeInCapacity).withCoolDownSec(60).withMetricAggregatorType(MetricAggregationType.Average).withMinAdjustmentMagnitude(1).withSteps(Arrays.asList(stepAdjustment)).build();
PolicyConfiguration policyConfiguration = PolicyConfiguration.newBuilder().withAlarmConfiguration(alarmConfiguration).withStepScalingPolicyConfiguration(stepScalingPolicyConfiguration).withPolicyType(PolicyType.StepScaling).withName(jobId).build();
AutoScalingPolicy autoScalingPolicy = AutoScalingPolicy.newBuilder().withPolicyConfiguration(policyConfiguration).withStatus(PolicyStatus.Pending).withStatusMessage("ICE-ed by AWS").withJobId(jobId).build();
return autoScalingPolicy;
}
use of com.netflix.titus.api.appscale.model.PolicyConfiguration in project titus-control-plane by Netflix.
the class InternalModelConverters method toPolicyConfiguration.
private static PolicyConfiguration toPolicyConfiguration(ScalingPolicy scalingPolicyGrpc) {
PolicyConfiguration.Builder policyConfigBuilder = PolicyConfiguration.newBuilder();
if (scalingPolicyGrpc.getScalingPolicyDescriptorCase() == STEPPOLICYDESCRIPTOR) {
policyConfigBuilder.withPolicyType(PolicyType.StepScaling);
StepScalingPolicyDescriptor stepScalingPolicyDesc = scalingPolicyGrpc.getStepPolicyDescriptor();
// Build alarm
if (stepScalingPolicyDesc.hasAlarmConfig()) {
policyConfigBuilder.withAlarmConfiguration(toAlarmConfiguration(stepScalingPolicyDesc.getAlarmConfig()));
}
// Build step policy
if (stepScalingPolicyDesc.hasScalingPolicy()) {
policyConfigBuilder.withStepScalingPolicyConfiguration(toStepScalingPolicyConfiguration(stepScalingPolicyDesc.getScalingPolicy()));
}
} else if (scalingPolicyGrpc.getScalingPolicyDescriptorCase() == TARGETPOLICYDESCRIPTOR) {
policyConfigBuilder.withPolicyType(PolicyType.TargetTrackingScaling);
TargetTrackingPolicy targetTrackingPolicy = toTargetTrackingPolicy(scalingPolicyGrpc.getTargetPolicyDescriptor());
policyConfigBuilder.withTargetTrackingPolicy(targetTrackingPolicy);
} else {
throw new IllegalArgumentException("Invalid ScalingPolicy Type provided " + scalingPolicyGrpc.getScalingPolicyDescriptorCase());
}
return policyConfigBuilder.build();
}
use of com.netflix.titus.api.appscale.model.PolicyConfiguration 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