Search in sources :

Example 1 with Instance

use of com.netflix.titus.api.connector.cloud.Instance in project titus-control-plane by Netflix.

the class AwsInstanceCloudConnector method getInstance.

@Override
public Mono<Instance> getInstance(String instanceId) {
    return Mono.defer(() -> {
        DescribeInstancesRequest req = new DescribeInstancesRequest().withInstanceIds(instanceId).withSdkRequestTimeout((int) configuration.getAwsRequestTimeoutMs());
        Mono<DescribeInstancesResult> instancesResult = AwsReactorExt.toMono(req, ec2Client::describeInstancesAsync);
        return instancesResult.filter(isr -> isr.getReservations() != null && !isr.getReservations().isEmpty() && !isr.getReservations().get(0).getInstances().isEmpty()).map(isr -> {
            Reservation reservation = isr.getReservations().get(0);
            return toInstance(reservation.getInstances().get(0));
        }).onErrorResume(e -> {
            if (e instanceof AmazonEC2Exception) {
                AmazonEC2Exception ec2Exception = (AmazonEC2Exception) e;
                String errorCode = ec2Exception.getErrorCode();
                int statusCode = ec2Exception.getStatusCode();
                // Instance id provided is unknown / invalid
                if (errorCode != null && errorCode.contains(ERROR_INSTANCE_NOT_FOUND) && statusCode == 400) {
                    logger.info("AWS instance {} NOT FOUND", instanceId);
                    return Mono.empty();
                }
            }
            // Any other exception should bubble up
            return Mono.error(e);
        });
    });
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) InstanceLaunchConfiguration(com.netflix.titus.api.connector.cloud.InstanceLaunchConfiguration) BiFunction(java.util.function.BiFunction) CollectionsExt(com.netflix.titus.common.util.CollectionsExt) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) LoggerFactory(org.slf4j.LoggerFactory) DescribeLaunchConfigurationsResult(com.amazonaws.services.autoscaling.model.DescribeLaunchConfigurationsResult) Collections.singletonList(java.util.Collections.singletonList) PreDestroy(javax.annotation.PreDestroy) Future(java.util.concurrent.Future) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) CloudConnectorException(com.netflix.titus.api.connector.cloud.CloudConnectorException) Map(java.util.Map) Schedulers(rx.schedulers.Schedulers) Either(com.netflix.titus.common.util.tuple.Either) ResourceDimension(com.netflix.titus.api.model.ResourceDimension) Reservation(com.amazonaws.services.ec2.model.Reservation) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Scheduler(rx.Scheduler) Collectors(java.util.stream.Collectors) List(java.util.List) AutoScalingInstanceDetails(com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails) UpdateAutoScalingGroupResult(com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupResult) Tag(com.amazonaws.services.ec2.model.Tag) ProxyConfiguration(com.netflix.titus.common.util.guice.annotation.ProxyConfiguration) Optional(java.util.Optional) ObservableExt(com.netflix.titus.common.util.rx.ObservableExt) Completable(rx.Completable) AwsInstanceType(com.netflix.titus.common.aws.AwsInstanceType) ProxyType(com.netflix.titus.common.util.guice.ProxyType) Singleton(javax.inject.Singleton) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Observable(rx.Observable) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) UpdateAutoScalingGroupRequest(com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest) Pair(com.netflix.titus.common.util.tuple.Pair) InstanceCloudConnector(com.netflix.titus.api.connector.cloud.InstanceCloudConnector) AwsInstanceDescriptor(com.netflix.titus.common.aws.AwsInstanceDescriptor) DescribeAutoScalingInstancesResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingInstancesResult) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) Filter(com.amazonaws.services.ec2.model.Filter) Named(javax.inject.Named) TagDescription(com.amazonaws.services.autoscaling.model.TagDescription) Instance(com.netflix.titus.api.connector.cloud.Instance) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) Logger(org.slf4j.Logger) DescribeLaunchConfigurationsRequest(com.amazonaws.services.autoscaling.model.DescribeLaunchConfigurationsRequest) DetachInstancesRequest(com.amazonaws.services.autoscaling.model.DetachInstancesRequest) InstanceState(com.netflix.titus.api.connector.cloud.Instance.InstanceState) SuspendedProcess(com.amazonaws.services.autoscaling.model.SuspendedProcess) CreateTagsResult(com.amazonaws.services.ec2.model.CreateTagsResult) AmazonEC2Async(com.amazonaws.services.ec2.AmazonEC2Async) Mono(reactor.core.publisher.Mono) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) Emitter(rx.Emitter) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) Activator(com.netflix.titus.common.util.guice.annotation.Activator) TimeUnit(java.util.concurrent.TimeUnit) DescribeAutoScalingInstancesRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingInstancesRequest) AsyncHandler(com.amazonaws.handlers.AsyncHandler) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) InstanceGroup(com.netflix.titus.api.connector.cloud.InstanceGroup) AmazonAutoScalingAsync(com.amazonaws.services.autoscaling.AmazonAutoScalingAsync) Collections(java.util.Collections) DetachInstancesResult(com.amazonaws.services.autoscaling.model.DetachInstancesResult) LaunchConfiguration(com.amazonaws.services.autoscaling.model.LaunchConfiguration) Reservation(com.amazonaws.services.ec2.model.Reservation) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Example 2 with Instance

use of com.netflix.titus.api.connector.cloud.Instance in project titus-control-plane by Netflix.

the class AwsInstanceCloudConnector method scaleUp.

@Override
public Completable scaleUp(String instanceGroupId, int scaleUpCount) {
    return getInstanceGroups(singletonList(instanceGroupId)).map(list -> list.get(0)).flatMap(instanceGroup -> {
        int newDesired = instanceGroup.getDesired() + scaleUpCount;
        if (newDesired > instanceGroup.getMax()) {
            return Observable.error(CloudConnectorException.invalidArgument("Instance group requested desired size %s > max size %s", newDesired, instanceGroup.getMax()));
        }
        logger.info("Scaling up instance group {}, by {} instances (desired changed from {} to {})", instanceGroup, scaleUpCount, instanceGroup.getDesired(), newDesired);
        UpdateAutoScalingGroupRequest request = new UpdateAutoScalingGroupRequest().withAutoScalingGroupName(instanceGroupId).withDesiredCapacity(newDesired);
        Observable<UpdateAutoScalingGroupResult> observable = toObservable(request, autoScalingClient::updateAutoScalingGroupAsync);
        return observable;
    }).toCompletable().timeout(configuration.getAwsRequestTimeoutMs(), TimeUnit.MILLISECONDS);
}
Also used : InstanceLaunchConfiguration(com.netflix.titus.api.connector.cloud.InstanceLaunchConfiguration) BiFunction(java.util.function.BiFunction) CollectionsExt(com.netflix.titus.common.util.CollectionsExt) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) LoggerFactory(org.slf4j.LoggerFactory) DescribeLaunchConfigurationsResult(com.amazonaws.services.autoscaling.model.DescribeLaunchConfigurationsResult) Collections.singletonList(java.util.Collections.singletonList) PreDestroy(javax.annotation.PreDestroy) Future(java.util.concurrent.Future) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) CloudConnectorException(com.netflix.titus.api.connector.cloud.CloudConnectorException) Map(java.util.Map) Schedulers(rx.schedulers.Schedulers) Either(com.netflix.titus.common.util.tuple.Either) ResourceDimension(com.netflix.titus.api.model.ResourceDimension) Reservation(com.amazonaws.services.ec2.model.Reservation) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Scheduler(rx.Scheduler) Collectors(java.util.stream.Collectors) List(java.util.List) AutoScalingInstanceDetails(com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails) UpdateAutoScalingGroupResult(com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupResult) Tag(com.amazonaws.services.ec2.model.Tag) ProxyConfiguration(com.netflix.titus.common.util.guice.annotation.ProxyConfiguration) Optional(java.util.Optional) ObservableExt(com.netflix.titus.common.util.rx.ObservableExt) Completable(rx.Completable) AwsInstanceType(com.netflix.titus.common.aws.AwsInstanceType) ProxyType(com.netflix.titus.common.util.guice.ProxyType) Singleton(javax.inject.Singleton) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Observable(rx.Observable) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) UpdateAutoScalingGroupRequest(com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest) Pair(com.netflix.titus.common.util.tuple.Pair) InstanceCloudConnector(com.netflix.titus.api.connector.cloud.InstanceCloudConnector) AwsInstanceDescriptor(com.netflix.titus.common.aws.AwsInstanceDescriptor) DescribeAutoScalingInstancesResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingInstancesResult) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) Filter(com.amazonaws.services.ec2.model.Filter) Named(javax.inject.Named) TagDescription(com.amazonaws.services.autoscaling.model.TagDescription) Instance(com.netflix.titus.api.connector.cloud.Instance) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) Logger(org.slf4j.Logger) DescribeLaunchConfigurationsRequest(com.amazonaws.services.autoscaling.model.DescribeLaunchConfigurationsRequest) DetachInstancesRequest(com.amazonaws.services.autoscaling.model.DetachInstancesRequest) InstanceState(com.netflix.titus.api.connector.cloud.Instance.InstanceState) SuspendedProcess(com.amazonaws.services.autoscaling.model.SuspendedProcess) CreateTagsResult(com.amazonaws.services.ec2.model.CreateTagsResult) AmazonEC2Async(com.amazonaws.services.ec2.AmazonEC2Async) Mono(reactor.core.publisher.Mono) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) Emitter(rx.Emitter) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) Activator(com.netflix.titus.common.util.guice.annotation.Activator) TimeUnit(java.util.concurrent.TimeUnit) DescribeAutoScalingInstancesRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingInstancesRequest) AsyncHandler(com.amazonaws.handlers.AsyncHandler) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) InstanceGroup(com.netflix.titus.api.connector.cloud.InstanceGroup) AmazonAutoScalingAsync(com.amazonaws.services.autoscaling.AmazonAutoScalingAsync) Collections(java.util.Collections) DetachInstancesResult(com.amazonaws.services.autoscaling.model.DetachInstancesResult) LaunchConfiguration(com.amazonaws.services.autoscaling.model.LaunchConfiguration) UpdateAutoScalingGroupResult(com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupResult) UpdateAutoScalingGroupRequest(com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest)

Aggregations

AmazonWebServiceRequest (com.amazonaws.AmazonWebServiceRequest)2 AsyncHandler (com.amazonaws.handlers.AsyncHandler)2 AmazonAutoScalingAsync (com.amazonaws.services.autoscaling.AmazonAutoScalingAsync)2 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)2 AutoScalingInstanceDetails (com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails)2 DescribeAutoScalingGroupsRequest (com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest)2 DescribeAutoScalingGroupsResult (com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult)2 DescribeAutoScalingInstancesRequest (com.amazonaws.services.autoscaling.model.DescribeAutoScalingInstancesRequest)2 DescribeAutoScalingInstancesResult (com.amazonaws.services.autoscaling.model.DescribeAutoScalingInstancesResult)2 DescribeLaunchConfigurationsRequest (com.amazonaws.services.autoscaling.model.DescribeLaunchConfigurationsRequest)2 DescribeLaunchConfigurationsResult (com.amazonaws.services.autoscaling.model.DescribeLaunchConfigurationsResult)2 DetachInstancesRequest (com.amazonaws.services.autoscaling.model.DetachInstancesRequest)2 DetachInstancesResult (com.amazonaws.services.autoscaling.model.DetachInstancesResult)2 LaunchConfiguration (com.amazonaws.services.autoscaling.model.LaunchConfiguration)2 SuspendedProcess (com.amazonaws.services.autoscaling.model.SuspendedProcess)2 TagDescription (com.amazonaws.services.autoscaling.model.TagDescription)2 UpdateAutoScalingGroupRequest (com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest)2 UpdateAutoScalingGroupResult (com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupResult)2 AmazonEC2Async (com.amazonaws.services.ec2.AmazonEC2Async)2 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)2