Example usage for com.amazonaws.services.ec2.model VolumeAttachmentState Attached

List of usage examples for com.amazonaws.services.ec2.model VolumeAttachmentState Attached

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model VolumeAttachmentState Attached.

Prototype

VolumeAttachmentState Attached

To view the source code for com.amazonaws.services.ec2.model VolumeAttachmentState Attached.

Click Source Link

Usage

From source file:com.cloudera.director.aws.ec2.ebs.EBSAllocator.java

License:Apache License

/**
 * Wait for the specified list of volumes to be attached within a timeout.
 *
 * @param volumeIds collection of volume ids to wait for
 * @return collection of volumes that were successfully attached
 * @throws InterruptedException if the operation is interrupted
 *///from www .j av a 2s  .  c o  m
private Collection<String> waitUntilVolumesAttached(Collection<String> volumeIds) throws InterruptedException {
    checkNotNull(volumeIds);
    if (volumeIds.isEmpty()) {
        LOG.info("No volumes are being attached, skipping wait");
        return Collections.emptySet();
    }

    Set<String> unattachedVolumes = Sets.newHashSet(volumeIds);
    Set<String> attachedVolumes = Sets.newHashSet();

    LOG.info("Waiting for a maximum of {} seconds for volumes to be attached", attachTimeoutSeconds);

    Stopwatch watch = Stopwatch.createStarted();
    while (watch.elapsed(TimeUnit.SECONDS) < attachTimeoutSeconds) {
        DescribeVolumesRequest volumeRequest = new DescribeVolumesRequest().withVolumeIds(unattachedVolumes);
        List<Volume> volumes = client.describeVolumes(volumeRequest).getVolumes();

        for (Volume volume : volumes) {
            VolumeAttachment attachment = Iterables.getOnlyElement(volume.getAttachments());
            VolumeAttachmentState state = VolumeAttachmentState.fromValue(attachment.getState());

            if (state == VolumeAttachmentState.Attached) {
                unattachedVolumes.remove(volume.getVolumeId());
                attachedVolumes.add(volume.getVolumeId());
            }
        }

        if (unattachedVolumes.isEmpty()) {
            return attachedVolumes;
        }

        LOG.info("Waiting on {} out of {} volumes to be attached, next check in {} seconds",
                unattachedVolumes.size(), volumeIds.size(), WAIT_UNTIL_ATTACHED_INTERVAL_SECONDS);
        TimeUnit.SECONDS.sleep(WAIT_UNTIL_ATTACHED_INTERVAL_SECONDS);
    }

    LOG.error("Timed out while waiting for all volumes to be attached, {} out of {} volumes were attached",
            attachedVolumes.size(), volumeIds.size());
    return attachedVolumes;
}