Search in sources :

Example 1 with StateReason

use of com.amazonaws.services.ec2.model.StateReason in project photon-model by vmware.

the class AWSTaskStatusChecker method buildHandler.

private AsyncHandler buildHandler(T type) {
    return new AsyncHandler<AmazonWebServiceRequest, AmazonWebServiceResult>() {

        @Override
        public void onError(Exception exception) {
            // particular instanceId.
            if (exception instanceof AmazonServiceException && ((AmazonServiceException) exception).getErrorCode().equalsIgnoreCase(AWS_INVALID_INSTANCE_ID_ERROR_CODE)) {
                AWSTaskStatusChecker.this.service.logWarning("Could not retrieve status for instance %s. Retrying... Exception on AWS is %s", AWSTaskStatusChecker.this.instanceId, exception);
                AWSTaskStatusChecker.create(AWSTaskStatusChecker.this.amazonEC2Client, AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, AWSTaskStatusChecker.this.failureStates, AWSTaskStatusChecker.this.consumer, AWSTaskStatusChecker.this.taskManager, AWSTaskStatusChecker.this.service, AWSTaskStatusChecker.this.expirationTimeMicros).start(type);
                return;
            } else if (exception instanceof AmazonEC2Exception && ((AmazonEC2Exception) exception).getErrorCode().equalsIgnoreCase(AWS_INVALID_VOLUME_ID_ERROR_CODE)) {
                AWSTaskStatusChecker.this.consumer.accept(null);
                return;
            }
            AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(exception);
            return;
        }

        @Override
        public void onSuccess(AmazonWebServiceRequest request, AmazonWebServiceResult result) {
            String status;
            Object instance;
            String failureMessage = null;
            String stateReason = null;
            if (result instanceof DescribeInstancesResult) {
                instance = ((DescribeInstancesResult) result).getReservations().get(0).getInstances().get(0);
                Instance vm = (Instance) instance;
                status = vm.getState().getName();
                stateReason = vm.getStateReason() != null ? vm.getStateReason().getMessage() : null;
            } else if (result instanceof DescribeNatGatewaysResult) {
                instance = ((DescribeNatGatewaysResult) result).getNatGateways().get(0);
                status = ((NatGateway) instance).getState();
                // if NAT gateway creation fails, the status is still "pending";
                // rather than keep checking for status and eventually time out, get the
                // failure message and fail the task
                failureMessage = ((NatGateway) instance).getFailureMessage();
            } else if (result instanceof DescribeVolumesResult) {
                instance = ((DescribeVolumesResult) result).getVolumes().get(0);
                status = ((Volume) instance).getState().toLowerCase();
            } else {
                AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalArgumentException("Invalid type " + result));
                return;
            }
            if (failureMessage != null) {
                // operation failed; no need to keep checking for desired status
                AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalStateException(failureMessage));
                return;
            } else if (AWSTaskStatusChecker.this.failureStates.contains(status)) {
                // operation failed; no need to keep checking for desired status
                AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalStateException("Resource is state:[" + status + "]," + "reason:" + stateReason));
                return;
            } else if (!status.equals(AWSTaskStatusChecker.this.desiredState)) {
                AWSTaskStatusChecker.this.service.logInfo("Instance %s not yet in desired state %s. Current state %s, failure states %s, waiting 5s", AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, status, AWSTaskStatusChecker.this.failureStates);
                // if the instance is not in the desired state, schedule thread
                // to run again in 5 seconds
                AWSTaskStatusChecker.this.service.getHost().schedule(() -> {
                    AWSTaskStatusChecker.create(AWSTaskStatusChecker.this.amazonEC2Client, AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, AWSTaskStatusChecker.this.failureStates, AWSTaskStatusChecker.this.consumer, AWSTaskStatusChecker.this.taskManager, AWSTaskStatusChecker.this.service, AWSTaskStatusChecker.this.expirationTimeMicros).start(type);
                }, 5, TimeUnit.SECONDS);
                return;
            }
            AWSTaskStatusChecker.this.consumer.accept(instance);
            return;
        }
    };
}
Also used : AsyncHandler(com.amazonaws.handlers.AsyncHandler) Instance(com.amazonaws.services.ec2.model.Instance) DescribeNatGatewaysResult(com.amazonaws.services.ec2.model.DescribeNatGatewaysResult) NatGateway(com.amazonaws.services.ec2.model.NatGateway) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AmazonWebServiceResult(com.amazonaws.AmazonWebServiceResult) Volume(com.amazonaws.services.ec2.model.Volume) AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Example 2 with StateReason

use of com.amazonaws.services.ec2.model.StateReason in project aws-athena-query-federation by awslabs.

the class Ec2TableProviderTest method makeInstance.

private Instance makeInstance(String id) {
    Instance instance = new Instance();
    instance.withInstanceId(id).withImageId("image_id").withInstanceType("instance_type").withPlatform("platform").withPrivateDnsName("private_dns_name").withPrivateIpAddress("private_ip_address").withPublicDnsName("public_dns_name").withPublicIpAddress("public_ip_address").withSubnetId("subnet_id").withVpcId("vpc_id").withArchitecture("architecture").withInstanceLifecycle("instance_lifecycle").withRootDeviceName("root_device_name").withRootDeviceType("root_device_type").withSpotInstanceRequestId("spot_instance_request_id").withVirtualizationType("virtualization_type").withKeyName("key_name").withKernelId("kernel_id").withCapacityReservationId("capacity_reservation_id").withLaunchTime(new Date(100_000)).withState(new InstanceState().withCode(100).withName("name")).withStateReason(new StateReason().withCode("code").withMessage("message")).withEbsOptimized(true);
    List<InstanceNetworkInterface> interfaces = new ArrayList<>();
    interfaces.add(new InstanceNetworkInterface().withStatus("status").withSubnetId("subnet").withVpcId("vpc").withMacAddress("mac_address").withPrivateDnsName("private_dns").withPrivateIpAddress("private_ip").withNetworkInterfaceId("interface_id").withGroups(new GroupIdentifier().withGroupId("group_id").withGroupName("group_name")));
    interfaces.add(new InstanceNetworkInterface().withStatus("status").withSubnetId("subnet").withVpcId("vpc").withMacAddress("mac").withPrivateDnsName("private_dns").withPrivateIpAddress("private_ip").withNetworkInterfaceId("interface_id").withGroups(new GroupIdentifier().withGroupId("group_id").withGroupName("group_name")));
    instance.withNetworkInterfaces(interfaces).withSecurityGroups(new GroupIdentifier().withGroupId("group_id").withGroupName("group_name")).withBlockDeviceMappings(new InstanceBlockDeviceMapping().withDeviceName("device_name").withEbs(new EbsInstanceBlockDevice().withVolumeId("volume_id")));
    return instance;
}
Also used : InstanceState(com.amazonaws.services.ec2.model.InstanceState) Instance(com.amazonaws.services.ec2.model.Instance) StateReason(com.amazonaws.services.ec2.model.StateReason) ArrayList(java.util.ArrayList) InstanceNetworkInterface(com.amazonaws.services.ec2.model.InstanceNetworkInterface) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) Date(java.util.Date) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier) EbsInstanceBlockDevice(com.amazonaws.services.ec2.model.EbsInstanceBlockDevice)

Example 3 with StateReason

use of com.amazonaws.services.ec2.model.StateReason in project cloud-pipeline by epam.

the class PodMonitor method checkAndUpdateEc2State.

private void checkAndUpdateEc2State(final PipelineRun run, final boolean allowRestart) {
    final RunInstance instance = run.getInstance();
    if (instance == null || run.getExecutionPreferences().getEnvironment() != ExecutionEnvironment.CLOUD_PLATFORM || StringUtils.isBlank(instance.getNodeId()) || run.getStatus() == TaskStatus.STOPPED) {
        return;
    }
    final Optional<StateReason> stateReason = ec2Helper.getInstanceStateReason(instance.getNodeId(), instance.getAwsRegionId());
    stateReason.ifPresent(reason -> {
        pipelineRunManager.updateStateReasonMessage(run, reason.getMessage());
        if (allowRestart && shouldRerunBatchRun(run, reason.getCode())) {
            LOGGER.debug("Restarting run {}", run.getId());
            pipelineRunManager.restartRun(run);
        }
    });
}
Also used : StateReason(com.amazonaws.services.ec2.model.StateReason) RunInstance(com.epam.pipeline.entity.pipeline.RunInstance)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)2 StateReason (com.amazonaws.services.ec2.model.StateReason)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonWebServiceRequest (com.amazonaws.AmazonWebServiceRequest)1 AmazonWebServiceResult (com.amazonaws.AmazonWebServiceResult)1 AsyncHandler (com.amazonaws.handlers.AsyncHandler)1 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)1 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)1 DescribeNatGatewaysResult (com.amazonaws.services.ec2.model.DescribeNatGatewaysResult)1 DescribeVolumesResult (com.amazonaws.services.ec2.model.DescribeVolumesResult)1 EbsInstanceBlockDevice (com.amazonaws.services.ec2.model.EbsInstanceBlockDevice)1 GroupIdentifier (com.amazonaws.services.ec2.model.GroupIdentifier)1 InstanceBlockDeviceMapping (com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping)1 InstanceNetworkInterface (com.amazonaws.services.ec2.model.InstanceNetworkInterface)1 InstanceState (com.amazonaws.services.ec2.model.InstanceState)1 NatGateway (com.amazonaws.services.ec2.model.NatGateway)1 Volume (com.amazonaws.services.ec2.model.Volume)1 RunInstance (com.epam.pipeline.entity.pipeline.RunInstance)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1