Search in sources :

Example 1 with DeleteScalingPolicyRequest

use of com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyRequest in project titus-control-plane by Netflix.

the class AWSAppAutoScalingClientTest method deleteScalingPolicyIsIdempotent.

@Test
public void deleteScalingPolicyIsIdempotent() {
    String jobId = UUID.randomUUID().toString();
    String policyId = UUID.randomUUID().toString();
    AWSApplicationAutoScalingAsync clientAsync = mock(AWSApplicationAutoScalingAsync.class);
    AWSAppScalingConfig config = mock(AWSAppScalingConfig.class);
    AWSAppAutoScalingClient autoScalingClient = new AWSAppAutoScalingClient(clientAsync, config, new NoopRegistry());
    // delete happens successfully on the first attempt
    AtomicBoolean isDeleted = new AtomicBoolean(false);
    when(clientAsync.deleteScalingPolicyAsync(any(), any())).thenAnswer(invocation -> {
        DeleteScalingPolicyRequest request = invocation.getArgument(0);
        AsyncHandler<DeleteScalingPolicyRequest, DeleteScalingPolicyResult> handler = invocation.getArgument(1);
        if (isDeleted.get()) {
            ObjectNotFoundException notFoundException = new ObjectNotFoundException(policyId + " does not exist");
            handler.onError(notFoundException);
            return Future.failed(notFoundException);
        }
        DeleteScalingPolicyResult resultSuccess = new DeleteScalingPolicyResult();
        HttpResponse successResponse = new HttpResponse(null, null);
        successResponse.setStatusCode(200);
        resultSuccess.setSdkHttpMetadata(SdkHttpMetadata.from(successResponse));
        isDeleted.set(true);
        handler.onSuccess(request, resultSuccess);
        return Future.successful(resultSuccess);
    });
    AssertableSubscriber<Void> firstCall = autoScalingClient.deleteScalingPolicy(policyId, jobId).test();
    firstCall.awaitTerminalEvent(2, TimeUnit.SECONDS);
    firstCall.assertNoErrors();
    firstCall.assertCompleted();
    verify(clientAsync, times(1)).deleteScalingPolicyAsync(any(), any());
    // second should complete fast when NotFound and not retry with exponential backoff
    AssertableSubscriber<Void> secondCall = autoScalingClient.deleteScalingPolicy(policyId, jobId).test();
    secondCall.awaitTerminalEvent(2, TimeUnit.SECONDS);
    secondCall.assertNoErrors();
    secondCall.assertCompleted();
    verify(clientAsync, times(2)).deleteScalingPolicyAsync(any(), any());
}
Also used : DeleteScalingPolicyResult(com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyResult) HttpResponse(com.amazonaws.http.HttpResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AWSApplicationAutoScalingAsync(com.amazonaws.services.applicationautoscaling.AWSApplicationAutoScalingAsync) NoopRegistry(com.netflix.spectator.api.NoopRegistry) ObjectNotFoundException(com.amazonaws.services.applicationautoscaling.model.ObjectNotFoundException) DeleteScalingPolicyRequest(com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyRequest) Test(org.junit.Test)

Example 2 with DeleteScalingPolicyRequest

use of com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyRequest in project aws-doc-sdk-examples by awsdocs.

the class DisableDynamoDBAutoscaling method main.

public static void main(String[] args) {
    ServiceNamespace ns = ServiceNamespace.Dynamodb;
    ScalableDimension tableWCUs = ScalableDimension.DynamodbTableWriteCapacityUnits;
    String resourceID = "table/TestTable";
    // Delete the scaling policy
    DeleteScalingPolicyRequest delSPRequest = new DeleteScalingPolicyRequest().withServiceNamespace(ns).withScalableDimension(tableWCUs).withResourceId(resourceID).withPolicyName("MyScalingPolicy");
    try {
        aaClient.deleteScalingPolicy(delSPRequest);
    } catch (Exception e) {
        System.err.println("Unable to delete scaling policy: ");
        System.err.println(e.getMessage());
    }
    // Verify that the scaling policy was deleted
    DescribeScalingPoliciesRequest descSPRequest = new DescribeScalingPoliciesRequest().withServiceNamespace(ns).withScalableDimension(tableWCUs).withResourceId(resourceID);
    try {
        DescribeScalingPoliciesResult dspResult = aaClient.describeScalingPolicies(descSPRequest);
        System.out.println("DescribeScalingPolicies result: ");
        System.out.println(dspResult);
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Unable to describe scaling policy: ");
        System.err.println(e.getMessage());
    }
    System.out.println();
    // Remove the scalable target
    DeregisterScalableTargetRequest delSTRequest = new DeregisterScalableTargetRequest().withServiceNamespace(ns).withScalableDimension(tableWCUs).withResourceId(resourceID);
    try {
        aaClient.deregisterScalableTarget(delSTRequest);
    } catch (Exception e) {
        System.err.println("Unable to deregister scalable target: ");
        System.err.println(e.getMessage());
    }
    // Verify that the scalable target was removed
    DescribeScalableTargetsRequest dscRequest = new DescribeScalableTargetsRequest().withServiceNamespace(ns).withScalableDimension(tableWCUs).withResourceIds(resourceID);
    try {
        DescribeScalableTargetsResult dsaResult = aaClient.describeScalableTargets(dscRequest);
        System.out.println("DescribeScalableTargets result: ");
        System.out.println(dsaResult);
        System.out.println();
    } catch (Exception e) {
        System.err.println("Unable to describe scalable target: ");
        System.err.println(e.getMessage());
    }
}
Also used : DeregisterScalableTargetRequest(com.amazonaws.services.applicationautoscaling.model.DeregisterScalableTargetRequest) ServiceNamespace(com.amazonaws.services.applicationautoscaling.model.ServiceNamespace) ScalableDimension(com.amazonaws.services.applicationautoscaling.model.ScalableDimension) DeleteScalingPolicyRequest(com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyRequest) DescribeScalingPoliciesRequest(com.amazonaws.services.applicationautoscaling.model.DescribeScalingPoliciesRequest) DescribeScalingPoliciesResult(com.amazonaws.services.applicationautoscaling.model.DescribeScalingPoliciesResult) DescribeScalableTargetsRequest(com.amazonaws.services.applicationautoscaling.model.DescribeScalableTargetsRequest) DescribeScalableTargetsResult(com.amazonaws.services.applicationautoscaling.model.DescribeScalableTargetsResult)

Example 3 with DeleteScalingPolicyRequest

use of com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyRequest in project titus-control-plane by Netflix.

the class AWSAppAutoScalingClient method deleteScalingPolicy.

@Override
public Completable deleteScalingPolicy(String policyRefId, String jobId) {
    DeleteScalingPolicyRequest deleteScalingPolicyRequest = new DeleteScalingPolicyRequest();
    deleteScalingPolicyRequest.setResourceId(AWSAppAutoScalingUtil.buildGatewayResourceId(jobId, awsAppScalingConfig.getAWSGatewayEndpointPrefix(), awsAppScalingConfig.getRegion(), awsAppScalingConfig.getAWSGatewayEndpointTargetStage()));
    deleteScalingPolicyRequest.setServiceNamespace(SERVICE_NAMESPACE);
    deleteScalingPolicyRequest.setScalableDimension(SCALABLE_DIMENSION);
    deleteScalingPolicyRequest.setPolicyName(buildScalingPolicyName(policyRefId, jobId));
    return RetryWrapper.wrapWithExponentialRetry(String.format("deleteScalingPolicy %s for job %s", policyRefId, jobId), Observable.create(emitter -> awsAppAutoScalingClientAsync.deleteScalingPolicyAsync(deleteScalingPolicyRequest, new AsyncHandler<DeleteScalingPolicyRequest, DeleteScalingPolicyResult>() {

        @Override
        public void onError(Exception exception) {
            if (exception instanceof ObjectNotFoundException) {
                logger.info("Scaling policy does not exist anymore for job/policyRefId {}/{}", jobId, policyRefId);
                emitter.onCompleted();
            } else {
                logger.error("Delete scaling policy exception {} - {}", jobId, exception.getMessage());
                awsAppAutoScalingMetrics.registerAwsDeletePolicyError(exception);
                emitter.onError(AutoScalePolicyException.errorDeletingPolicy(policyRefId, exception.getMessage()));
            }
        }

        @Override
        public void onSuccess(DeleteScalingPolicyRequest request, DeleteScalingPolicyResult deleteScalingPolicyResult) {
            int httpStatusCode = deleteScalingPolicyResult.getSdkHttpMetadata().getHttpStatusCode();
            logger.info("Deleted scaling policy for job/policyRefId {}/{}, status - {}", jobId, policyRefId, httpStatusCode);
            awsAppAutoScalingMetrics.registerAwsDeletePolicySuccess();
            emitter.onCompleted();
        }
    }), Emitter.BackpressureMode.NONE)).toCompletable();
}
Also used : AsyncHandler(com.amazonaws.handlers.AsyncHandler) DeleteScalingPolicyResult(com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyResult) ObjectNotFoundException(com.amazonaws.services.applicationautoscaling.model.ObjectNotFoundException) DeleteScalingPolicyRequest(com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyRequest) AutoScalePolicyException(com.netflix.titus.api.appscale.service.AutoScalePolicyException) ValidationException(com.amazonaws.services.applicationautoscaling.model.ValidationException) ObjectNotFoundException(com.amazonaws.services.applicationautoscaling.model.ObjectNotFoundException)

Aggregations

DeleteScalingPolicyRequest (com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyRequest)3 DeleteScalingPolicyResult (com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyResult)2 ObjectNotFoundException (com.amazonaws.services.applicationautoscaling.model.ObjectNotFoundException)2 AsyncHandler (com.amazonaws.handlers.AsyncHandler)1 HttpResponse (com.amazonaws.http.HttpResponse)1 AWSApplicationAutoScalingAsync (com.amazonaws.services.applicationautoscaling.AWSApplicationAutoScalingAsync)1 DeregisterScalableTargetRequest (com.amazonaws.services.applicationautoscaling.model.DeregisterScalableTargetRequest)1 DescribeScalableTargetsRequest (com.amazonaws.services.applicationautoscaling.model.DescribeScalableTargetsRequest)1 DescribeScalableTargetsResult (com.amazonaws.services.applicationautoscaling.model.DescribeScalableTargetsResult)1 DescribeScalingPoliciesRequest (com.amazonaws.services.applicationautoscaling.model.DescribeScalingPoliciesRequest)1 DescribeScalingPoliciesResult (com.amazonaws.services.applicationautoscaling.model.DescribeScalingPoliciesResult)1 ScalableDimension (com.amazonaws.services.applicationautoscaling.model.ScalableDimension)1 ServiceNamespace (com.amazonaws.services.applicationautoscaling.model.ServiceNamespace)1 ValidationException (com.amazonaws.services.applicationautoscaling.model.ValidationException)1 NoopRegistry (com.netflix.spectator.api.NoopRegistry)1 AutoScalePolicyException (com.netflix.titus.api.appscale.service.AutoScalePolicyException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1