Permalink
Browse files

Merge pull request #38 from meteor/retry-describe-instances

Retries DescribeInstance calls a few times if it fails. [JENKINS-15319]
  • Loading branch information...
2 parents 64a311d + c4a24a9 commit 76c95f9c4a02917d09e28e4e4efa705c845f30df @francisu francisu committed Mar 12, 2013
Showing with 30 additions and 9 deletions.
  1. +30 −9 src/main/java/hudson/plugins/ec2/EC2Computer.java
View
39 src/main/java/hudson/plugins/ec2/EC2Computer.java
@@ -31,6 +31,7 @@
import org.kohsuke.stapler.HttpRedirect;
import org.kohsuke.stapler.HttpResponse;
import com.amazonaws.AmazonClientException;
+import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.DescribeInstancesRequest;
import com.amazonaws.services.ec2.model.GetConsoleOutputRequest;
@@ -77,7 +78,7 @@ public String getConsoleOutput() throws AmazonClientException {
*
* The cache can be flushed using {@link #updateInstanceDescription()}
*/
- public Instance describeInstance() throws AmazonClientException {
+ public Instance describeInstance() throws AmazonClientException, InterruptedException {
if(ec2InstanceDescription==null)
ec2InstanceDescription = _describeInstance();
return ec2InstanceDescription;
@@ -86,7 +87,7 @@ public Instance describeInstance() throws AmazonClientException {
/**
* This will flush any cached description held by {@link #describeInstance()}.
*/
- public Instance updateInstanceDescription() throws AmazonClientException {
+ public Instance updateInstanceDescription() throws AmazonClientException, InterruptedException {
return ec2InstanceDescription = _describeInstance();
}
@@ -96,31 +97,51 @@ public Instance updateInstanceDescription() throws AmazonClientException {
* <p>
* Unlike {@link #describeInstance()}, this method always return the current status by calling EC2.
*/
- public InstanceState getState() throws AmazonClientException {
+ public InstanceState getState() throws AmazonClientException, InterruptedException {
ec2InstanceDescription=_describeInstance();
return InstanceState.find(ec2InstanceDescription.getState().getName());
}
/**
* Number of milli-secs since the instance was started.
*/
- public long getUptime() throws AmazonClientException {
+ public long getUptime() throws AmazonClientException, InterruptedException {
return System.currentTimeMillis()-describeInstance().getLaunchTime().getTime();
}
/**
* Returns uptime in the human readable form.
*/
- public String getUptimeString() throws AmazonClientException {
+ public String getUptimeString() throws AmazonClientException, InterruptedException {
return Util.getTimeSpanString(getUptime());
}
- private Instance _describeInstance() throws AmazonClientException {
- DescribeInstancesRequest request = new DescribeInstancesRequest();
- request.setInstanceIds(Collections.<String>singletonList(getNode().getInstanceId()));
- return EC2Cloud.get().connect().describeInstances(request).getReservations().get(0).getInstances().get(0);
+ private Instance _describeInstance() throws AmazonClientException, InterruptedException {
+ // Sometimes even after a successful RunInstances, DescribeInstances
+ // returns an error for a few seconds. We do a few retries instead of
+ // failing instantly. See [JENKINS-15319].
+ for (int i = 0; i < 5; i++) {
+ try {
+ return _describeInstanceOnce();
+ } catch (AmazonServiceException e) {
+ if (e.getErrorCode().equals("InvalidInstanceID.NotFound")) {
+ // retry in 5 seconds.
+ Thread.sleep(5000);
+ continue;
+ }
+ throw e;
+ }
+ }
+ // Last time, throw on any error.
+ return _describeInstanceOnce();
}
+ private Instance _describeInstanceOnce() throws AmazonClientException {
+ DescribeInstancesRequest request = new DescribeInstancesRequest();
+ request.setInstanceIds(Collections.<String>singletonList(getNode().getInstanceId()));
+ return EC2Cloud.get().connect().describeInstances(request).getReservations().get(0).getInstances().get(0);
+ }
+
/**
* When the slave is deleted, terminate the instance.
*/

0 comments on commit 76c95f9

Please sign in to comment.