jclouds/jclouds — pull request #326 by karthikarun (2012-01-18 20:11:11+00:00)
Title: RetryablePredicate for checking if a EBS volume is detached

Here is the email thread that prompted the addition of VolumeDetached predicate.

Karthik
-------------
The specific task I am trying to get a handle of is to delete a EBS volume attached to an EC2 instance. Before trying to delete the volume, first the volume needs to be detached from the instance. Given there is no guaranteed time frame within which the detach process would succeed, what would be the best way to handle the delete call? i.e. After the detach command (detachVolumeInRegion), I can't immediately issue a delete command (deleteVolumeInRegion), since there is a high probability that the detach is still in progress and the delete would fail. Also surprisingly (or not), the detach command seems to return immediately i.e it doesn't seem to be a blocking call. So if I have the delete command immediately after the detach command, it is bound to fail, right? At least that is what I have observed during my tests.

So what is the best way for me to handle the task of deleting a volume (attached to an instance) in AWS? Do I have to implement my own timeout mechanism in the code? Looking at the DetachVolumeOptions, I don't think I see any timeout options available there or did I miss that? Any other suggestions/ideas?

Adrian
----------
This looks like a case for RetryablePredicate. Basically you do in fact need to wait until a particular state is achieved. In this case, it is a state of the volume. Check out Predicates in jclouds source tree like ServerRunning and make something like this. Stick that in a RetryablePredicate and you should be good to go.

Karthik
-----------
Thanks for the tip. That helps. Just one quick clarification though. In the list of predicates for EC2 at I see "VolumeAttached". My understanding is that this will block until the volume is attached (or it times out). How about if I want to block until the volume is detached? I don't see a VolumeDetached predicate. or The question is how do I negate the predicate?

Adrian
---------
n general, you can use Predicates.not(thingToNegate)

However, this one is a bit tricky as not attached doesn't mean
detached :) ex. it can be in an intermediate state. I suggest making
a new predicate and maybe a pull request for it :)

Karthik
-----------
In summary, it would be useful to have the "VolumeDetached" predicate for Amazon EC2.