List of usage examples for com.amazonaws.services.ec2.model VolumeState fromValue
public static VolumeState fromValue(String value)
From source file:com.cloudera.director.aws.ec2.ebs.EBSAllocator.java
License:Apache License
/** * Waits for the volumes in a list of {@code InstanceEbsVolumes} to reach an available state. * Returns an updated list of {@code InstanceEbsVolumes} with the volumes that became * available marked as AVAILABLE and volumes that failed or timed out marked as FAILED. * * @param createdInstanceVolumes list of instances with their created ebs volumes * @return updated list of instances EBS volumes *//*from w w w .j a v a2 s . c o m*/ public List<InstanceEbsVolumes> waitUntilVolumesAvailable(List<InstanceEbsVolumes> createdInstanceVolumes) throws InterruptedException { Set<String> volumesToCheck = getAllVolumeIdsWithStatus(createdInstanceVolumes, InstanceEbsVolumes.Status.CREATED); int numRequestedVolumes = volumesToCheck.size(); Set<String> volumesAvailable = Sets.newHashSetWithExpectedSize(numRequestedVolumes); if (numRequestedVolumes > 0) { LOG.info("Waiting for a maximum of {} seconds for volumes to become available", availableTimeoutSeconds); Stopwatch watch = Stopwatch.createStarted(); while (watch.elapsed(TimeUnit.SECONDS) < availableTimeoutSeconds) { DescribeVolumesRequest volumeRequest = new DescribeVolumesRequest().withVolumeIds(volumesToCheck); try { List<Volume> volumes = client.describeVolumes(volumeRequest).getVolumes(); for (Volume volume : volumes) { String id = volume.getVolumeId(); VolumeState state = VolumeState.fromValue(volume.getState()); switch (state) { case Creating: break; case Available: volumesToCheck.remove(id); volumesAvailable.add(id); break; case Error: // TODO log why the volume failed which may need a separate api call volumesToCheck.remove(id); break; default: String err = String .format("A requested volume went into an unexpected state %s while waiting " + "for volume to become available", state); throw new IllegalStateException(String.format(err, state)); } } if (volumesToCheck.isEmpty()) { break; } } catch (AmazonServiceException ex) { // ignore exception when volume isn't found, newly created volumes may not be found right away if (ex.getErrorCode().equals("InvalidVolume.NotFound")) { LOG.info("Requested volume(s) not yet found"); } else { throw AWSExceptions.propagate(ex); } } LOG.info("Waiting on {} out of {} volumes to reach a final state, next check in {} seconds", volumesToCheck.size(), numRequestedVolumes, WAIT_UNTIL_AVAILABLE_INTERVAL_SECONDS); TimeUnit.SECONDS.sleep(WAIT_UNTIL_AVAILABLE_INTERVAL_SECONDS); } if (volumesToCheck.size() > 0) { LOG.error( "Timed out while waiting for volumes to be created, {} out of {} volumes became available", volumesAvailable.size(), numRequestedVolumes); } } else { LOG.info("Skipping wait for availability because no EBS volumes were created"); } // Update the status of each volume to AVAILABLE or FAILED based on the result List<InstanceEbsVolumes> updated = Lists.newArrayList(); for (InstanceEbsVolumes instanceEbsVolumes : createdInstanceVolumes) { Map<String, InstanceEbsVolumes.Status> updatedVolumes = Maps.newHashMap(); for (String volumeId : instanceEbsVolumes.getVolumeStatuses().keySet()) { InstanceEbsVolumes.Status updatedStatus = volumesAvailable.contains(volumeId) ? InstanceEbsVolumes.Status.AVAILABLE : InstanceEbsVolumes.Status.FAILED; updatedVolumes.put(volumeId, updatedStatus); } updated.add(new InstanceEbsVolumes(instanceEbsVolumes.getVirtualInstanceId(), instanceEbsVolumes.getEc2InstanceId(), updatedVolumes)); } return updated; }
From source file:jp.primecloud.auto.process.aws.AwsCommonProcess.java
License:Open Source License
public Volume waitVolume(AwsProcessClient awsProcessClient, String volumeId) { // ???/*from www . ja v a 2s . c o m*/ Volume volume; while (true) { try { Thread.sleep(1000L * awsProcessClient.getDescribeInterval()); } catch (InterruptedException ignore) { } volume = describeVolume(awsProcessClient, volumeId); VolumeState state; try { state = VolumeState.fromValue(volume.getState()); } catch (IllegalArgumentException e) { // ??? AutoException exception = new AutoException("EPROCESS-000112", volume, volume.getState()); exception.addDetailInfo("result=" + ReflectionToStringBuilder.toString(volume)); throw exception; } // ????? if (state == VolumeState.Available || state == VolumeState.InUse || state == VolumeState.Deleted || state == VolumeState.Error) { break; } } return volume; }