Search in sources :

Example 1 with ObjectNotFoundException

use of com.amazonaws.services.applicationautoscaling.model.ObjectNotFoundException 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 ObjectNotFoundException

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

the class AWSAppAutoScalingClient method deleteScalableTarget.

@Override
public Completable deleteScalableTarget(String jobId) {
    DeregisterScalableTargetRequest deRegisterRequest = new DeregisterScalableTargetRequest();
    deRegisterRequest.setResourceId(AWSAppAutoScalingUtil.buildGatewayResourceId(jobId, awsAppScalingConfig.getAWSGatewayEndpointPrefix(), awsAppScalingConfig.getRegion(), awsAppScalingConfig.getAWSGatewayEndpointTargetStage()));
    deRegisterRequest.setServiceNamespace(SERVICE_NAMESPACE);
    deRegisterRequest.setScalableDimension(SCALABLE_DIMENSION);
    return RetryWrapper.wrapWithExponentialRetry(String.format("deleteScalableTarget for job %s", jobId), Observable.create(emitter -> awsAppAutoScalingClientAsync.deregisterScalableTargetAsync(deRegisterRequest, new AsyncHandler<DeregisterScalableTargetRequest, DeregisterScalableTargetResult>() {

        @Override
        public void onError(Exception exception) {
            if (exception instanceof ObjectNotFoundException) {
                logger.info("Scalable target does not exist anymore for job {}", jobId);
                emitter.onCompleted();
            } else {
                logger.error("Deregister scalable target exception {} - {}", jobId, exception.getMessage());
                awsAppAutoScalingMetrics.registerAwsDeleteTargetError(exception);
                emitter.onError(exception);
            }
        }

        @Override
        public void onSuccess(DeregisterScalableTargetRequest request, DeregisterScalableTargetResult deregisterScalableTargetResult) {
            int httpStatusCode = deregisterScalableTargetResult.getSdkHttpMetadata().getHttpStatusCode();
            logger.info("De-registered scalable target for {}, status {}", jobId, httpStatusCode);
            awsAppAutoScalingMetrics.registerAwsDeleteTargetSuccess();
            emitter.onCompleted();
        }
    }), Emitter.BackpressureMode.NONE)).toCompletable();
}
Also used : DeregisterScalableTargetRequest(com.amazonaws.services.applicationautoscaling.model.DeregisterScalableTargetRequest) AsyncHandler(com.amazonaws.handlers.AsyncHandler) ObjectNotFoundException(com.amazonaws.services.applicationautoscaling.model.ObjectNotFoundException) DeregisterScalableTargetResult(com.amazonaws.services.applicationautoscaling.model.DeregisterScalableTargetResult) AutoScalePolicyException(com.netflix.titus.api.appscale.service.AutoScalePolicyException) ValidationException(com.amazonaws.services.applicationautoscaling.model.ValidationException) ObjectNotFoundException(com.amazonaws.services.applicationautoscaling.model.ObjectNotFoundException)

Example 3 with ObjectNotFoundException

use of com.amazonaws.services.applicationautoscaling.model.ObjectNotFoundException 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)

Example 4 with ObjectNotFoundException

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

the class AWSAppAutoScalingClientTest method deleteScalableTargetIsIdempotent.

@Test
public void deleteScalableTargetIsIdempotent() {
    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());
    AtomicBoolean isDeleted = new AtomicBoolean(false);
    when(clientAsync.deregisterScalableTargetAsync(any(), any())).thenAnswer(invocation -> {
        DeregisterScalableTargetRequest request = invocation.getArgument(0);
        AsyncHandler<DeregisterScalableTargetRequest, DeregisterScalableTargetResult> handler = invocation.getArgument(1);
        if (isDeleted.get()) {
            ObjectNotFoundException notFoundException = new ObjectNotFoundException(policyId + " does not exist");
            handler.onError(notFoundException);
            return Future.failed(notFoundException);
        }
        DeregisterScalableTargetResult resultSuccess = new DeregisterScalableTargetResult();
        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.deleteScalableTarget(jobId).test();
    firstCall.awaitTerminalEvent(2, TimeUnit.SECONDS);
    firstCall.assertNoErrors();
    firstCall.assertCompleted();
    verify(clientAsync, times(1)).deregisterScalableTargetAsync(any(), any());
    // second should complete fast when NotFound and not retry with exponential backoff
    AssertableSubscriber<Void> secondCall = autoScalingClient.deleteScalableTarget(jobId).test();
    secondCall.awaitTerminalEvent(2, TimeUnit.SECONDS);
    secondCall.assertNoErrors();
    secondCall.assertCompleted();
    verify(clientAsync, times(2)).deregisterScalableTargetAsync(any(), any());
}
Also used : HttpResponse(com.amazonaws.http.HttpResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DeregisterScalableTargetRequest(com.amazonaws.services.applicationautoscaling.model.DeregisterScalableTargetRequest) AWSApplicationAutoScalingAsync(com.amazonaws.services.applicationautoscaling.AWSApplicationAutoScalingAsync) NoopRegistry(com.netflix.spectator.api.NoopRegistry) ObjectNotFoundException(com.amazonaws.services.applicationautoscaling.model.ObjectNotFoundException) DeregisterScalableTargetResult(com.amazonaws.services.applicationautoscaling.model.DeregisterScalableTargetResult) Test(org.junit.Test)

Aggregations

ObjectNotFoundException (com.amazonaws.services.applicationautoscaling.model.ObjectNotFoundException)4 AsyncHandler (com.amazonaws.handlers.AsyncHandler)2 HttpResponse (com.amazonaws.http.HttpResponse)2 AWSApplicationAutoScalingAsync (com.amazonaws.services.applicationautoscaling.AWSApplicationAutoScalingAsync)2 DeleteScalingPolicyRequest (com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyRequest)2 DeleteScalingPolicyResult (com.amazonaws.services.applicationautoscaling.model.DeleteScalingPolicyResult)2 DeregisterScalableTargetRequest (com.amazonaws.services.applicationautoscaling.model.DeregisterScalableTargetRequest)2 DeregisterScalableTargetResult (com.amazonaws.services.applicationautoscaling.model.DeregisterScalableTargetResult)2 ValidationException (com.amazonaws.services.applicationautoscaling.model.ValidationException)2 NoopRegistry (com.netflix.spectator.api.NoopRegistry)2 AutoScalePolicyException (com.netflix.titus.api.appscale.service.AutoScalePolicyException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Test (org.junit.Test)2