From 4463b520517731525bc4db32bc747547f390badb Mon Sep 17 00:00:00 2001 From: Andrew Donald Kennedy Date: Mon, 16 Jul 2012 16:58:37 +0100 Subject: [PATCH] Added optional timeout to EC2 listNodes strategy and made awaitCompletion update latch in finally block --- .../ec2/compute/strategy/EC2ListNodesStrategy.java | 9 +++++++-- .../java/org/jclouds/concurrent/FutureIterables.java | 8 +++++--- .../ec2/compute/strategy/AWSEC2ListNodesStrategy.java | 4 ++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/strategy/EC2ListNodesStrategy.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/strategy/EC2ListNodesStrategy.java index 2c0d1d01dc..a805e47e3c 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/strategy/EC2ListNodesStrategy.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/strategy/EC2ListNodesStrategy.java @@ -31,7 +31,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import javax.annotation.Resource; -import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; @@ -51,6 +50,7 @@ import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.base.Supplier; import com.google.common.collect.ImmutableSet; +import com.google.inject.Inject; /** * @@ -58,10 +58,15 @@ import com.google.common.collect.ImmutableSet; */ @Singleton public class EC2ListNodesStrategy implements ListNodesStrategy { + @Resource @Named(ComputeServiceConstants.COMPUTE_LOGGER) protected Logger logger = Logger.NULL; + @Inject(optional = true) + @Named(Constants.PROPERTY_REQUEST_TIMEOUT) + protected static Long maxTime; + protected final EC2AsyncClient client; protected final Supplier> regions; protected final Function runningInstanceToNodeMetadata; @@ -100,7 +105,7 @@ public class EC2ListNodesStrategy implements ListNodesStrategy { return castToSpecificTypedFuture(client.getInstanceServices().describeInstancesInRegion(from)); } - }, executor, null, logger, "reservations"); + }, executor, maxTime, logger, "reservations"); return concat(concat(reservations)); } diff --git a/core/src/main/java/org/jclouds/concurrent/FutureIterables.java b/core/src/main/java/org/jclouds/concurrent/FutureIterables.java index 2655df9dff..119f5807a5 100644 --- a/core/src/main/java/org/jclouds/concurrent/FutureIterables.java +++ b/core/src/main/java/org/jclouds/concurrent/FutureIterables.java @@ -137,8 +137,9 @@ public class FutureIterables { errors.incrementAndGet(); logException(logger, logPrefix, total, complete.get(), errors.get(), start, e); errorMap.put(future.getKey(), e); + } finally { + doneSignal.countDown(); } - doneSignal.countDown(); } @Override @@ -148,10 +149,11 @@ public class FutureIterables { }, exec); } try { - if (maxTime != null) + if (maxTime != null) { doneSignal.await(maxTime, TimeUnit.MILLISECONDS); - else + } else { doneSignal.await(); + } if (errors.get() > 0) { String message = message(logPrefix, total, complete.get(), errors.get(), start); RuntimeException exception = new RuntimeException(message); diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/strategy/AWSEC2ListNodesStrategy.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/strategy/AWSEC2ListNodesStrategy.java index 240e616079..7316c750be 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/strategy/AWSEC2ListNodesStrategy.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/compute/strategy/AWSEC2ListNodesStrategy.java @@ -29,7 +29,6 @@ import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; -import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; @@ -45,6 +44,7 @@ import org.jclouds.location.Region; import com.google.common.base.Function; import com.google.common.base.Supplier; +import com.google.inject.Inject; /** * @@ -76,7 +76,7 @@ public class AWSEC2ListNodesStrategy extends EC2ListNodesStrategy { .describeSpotInstanceRequestsInRegion(from); } - }, executor, null, logger, "reservations")), spotConverter), notNull()); + }, executor, maxTime, logger, "reservations")), spotConverter), notNull()); return concat(super.pollRunningInstances(), spots); }