JCLOUDS-781 cluster regions is a subset of regions.

This commit is contained in:
Adrian Cole 2014-11-19 14:26:09 -08:00 committed by Adrian Cole
parent 239d45034b
commit d71ea714fe
1 changed files with 24 additions and 27 deletions

View File

@ -16,8 +16,12 @@
*/ */
package org.jclouds.aws.ec2.compute.suppliers; package org.jclouds.aws.ec2.compute.suppliers;
import static com.google.common.base.Predicates.in;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterables.concat; import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform; import static com.google.common.collect.Iterables.transform;
import static org.jclouds.Constants.PROPERTY_USER_THREADS;
import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_AMI_QUERY; import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_AMI_QUERY;
import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_CC_AMI_QUERY; import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_CC_AMI_QUERY;
import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_CC_REGIONS; import static org.jclouds.aws.ec2.reference.AWSEC2Constants.PROPERTY_EC2_CC_REGIONS;
@ -28,9 +32,7 @@ import java.util.Set;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.aws.ec2.compute.config.ClusterCompute; import org.jclouds.aws.ec2.compute.config.ClusterCompute;
import org.jclouds.aws.ec2.compute.config.ImageQuery; import org.jclouds.aws.ec2.compute.config.ImageQuery;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
@ -43,7 +45,6 @@ import org.jclouds.logging.Logger;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
import com.google.common.collect.ForwardingSet; import com.google.common.collect.ForwardingSet;
import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultimap;
@ -54,18 +55,11 @@ import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.ListeningExecutorService;
/** public final class AWSEC2ImageSupplier implements Supplier<Set<? extends Image>> {
*
* @author Adrian Cole
*/
@Singleton
public class AWSEC2ImageSupplier implements Supplier<Set<? extends Image>> {
// TODO could/should this sub-class EC2ImageSupplier? Or does that confuse guice?
@Resource @Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER) @Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL; private Logger logger = Logger.NULL;
private final Set<String> clusterComputeIds; private final Set<String> clusterComputeIds;
private final CallForImages.Factory factory; private final CallForImages.Factory factory;
@ -76,12 +70,11 @@ public class AWSEC2ImageSupplier implements Supplier<Set<? extends Image>> {
private final Iterable<String> clusterRegions; private final Iterable<String> clusterRegions;
private final Supplier<LoadingCache<RegionAndName, ? extends Image>> cache; private final Supplier<LoadingCache<RegionAndName, ? extends Image>> cache;
@Inject @Inject AWSEC2ImageSupplier(@Region Supplier<Set<String>> regions, @ImageQuery Map<String, String> queries,
protected AWSEC2ImageSupplier(@Region Supplier<Set<String>> regions, @Named(PROPERTY_EC2_CC_REGIONS) String clusterRegions,
@ImageQuery Map<String, String> queries, @Named(PROPERTY_EC2_CC_REGIONS) String clusterRegions, Supplier<LoadingCache<RegionAndName, ? extends Image>> cache, CallForImages.Factory factory,
Supplier<LoadingCache<RegionAndName, ? extends Image>> cache, @ClusterCompute Set<String> clusterComputeIds,
CallForImages.Factory factory, @ClusterCompute Set<String> clusterComputeIds, @Named(PROPERTY_USER_THREADS) ListeningExecutorService userExecutor) {
@Named(Constants.PROPERTY_USER_THREADS) ListeningExecutorService userExecutor) {
this.factory = factory; this.factory = factory;
this.regions = regions; this.regions = regions;
this.queries = queries; this.queries = queries;
@ -97,13 +90,16 @@ public class AWSEC2ImageSupplier implements Supplier<Set<? extends Image>> {
String amiQuery = queries.get(PROPERTY_EC2_AMI_QUERY); String amiQuery = queries.get(PROPERTY_EC2_AMI_QUERY);
String ccAmiQuery = queries.get(PROPERTY_EC2_CC_AMI_QUERY); String ccAmiQuery = queries.get(PROPERTY_EC2_CC_AMI_QUERY);
ListenableFuture<Iterable<Image>> normalImages = images(regions.get(), amiQuery, PROPERTY_EC2_AMI_QUERY); Set<String> regionIds = regions.get();
ListenableFuture<Iterable<Image>> normalImages = images(regionIds, amiQuery, PROPERTY_EC2_AMI_QUERY);
ImmutableSet<Image> clusterImages; ImmutableSet<Image> clusterImages;
try { try {
clusterImages = ImmutableSet.copyOf(images(clusterRegions, ccAmiQuery, PROPERTY_EC2_CC_AMI_QUERY).get()); clusterImages = ImmutableSet
.copyOf(images(filter(clusterRegions, in(regionIds)), ccAmiQuery, PROPERTY_EC2_CC_AMI_QUERY).get());
} catch (Exception e) { } catch (Exception e) {
logger.warn(e, "Error parsing images in query %s", ccAmiQuery); logger.warn(e, "Error parsing images in query %s", ccAmiQuery);
throw Throwables.propagate(e); throw propagate(e);
} }
Iterables.addAll(clusterComputeIds, transform(clusterImages, new Function<Image, String>() { Iterables.addAll(clusterComputeIds, transform(clusterImages, new Function<Image, String>() {
@ -113,20 +109,21 @@ public class AWSEC2ImageSupplier implements Supplier<Set<? extends Image>> {
} }
})); }));
Iterable<? extends Image> parsedImages; Iterable<? extends Image> parsedImages;
try { try {
parsedImages = ImmutableSet.copyOf(concat(clusterImages, normalImages.get())); parsedImages = ImmutableSet.copyOf(concat(clusterImages, normalImages.get()));
} catch (Exception e) { } catch (Exception e) {
logger.warn(e, "Error parsing images in query %s", amiQuery); logger.warn(e, "Error parsing images in query %s", amiQuery);
throw Throwables.propagate(e); throw propagate(e);
} }
final Map<RegionAndName, ? extends Image> imageMap = ImagesToRegionAndIdMap.imagesToMap(parsedImages); Map<RegionAndName, ? extends Image> imageMap = ImagesToRegionAndIdMap.imagesToMap(parsedImages);
cache.get().invalidateAll(); cache.get().invalidateAll();
cache.get().asMap().putAll(Map.class.cast(imageMap)); cache.get().putAll(Map.class.cast(imageMap));
logger.debug("<< images(%d)", imageMap.size()); logger.debug("<< images(%d)", imageMap.size());
// TODO Used to be mutable; was this assumed anywhere? // Forwarding so that later changes to the underlying cache are visible.
return new ForwardingSet<Image>() { return new ForwardingSet<Image>() {
protected Set<Image> delegate() { protected Set<Image> delegate() {
return ImmutableSet.copyOf(cache.get().asMap().values()); return ImmutableSet.copyOf(cache.get().asMap().values());