Issue 763: Supplier<Set<Image>> for EC2 returns set backed by on-demand cache if ami-owners is empty

This commit is contained in:
Aled Sage 2011-12-07 23:53:02 +00:00 committed by Adrian Cole
parent 741773f9ab
commit dad5356991
4 changed files with 78 additions and 3 deletions

View File

@ -19,8 +19,10 @@
package org.jclouds.ec2.compute.config;
import static com.google.common.collect.Iterables.toArray;
import static org.jclouds.Constants.PROPERTY_SESSION_INTERVAL;
import static org.jclouds.ec2.reference.EC2Constants.PROPERTY_EC2_AMI_OWNERS;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;
@ -44,7 +46,12 @@ import com.google.common.base.Suppliers;
import com.google.common.base.Throwables;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheLoader;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
/**
* Configures the {@link ComputeServiceContext}; requires {@link EC2ComputeService} bound.
@ -64,6 +71,25 @@ public class EC2ComputeServiceContextModule extends BaseComputeServiceContextMod
install(new EC2ComputeServiceDependenciesModule());
}
@Override
protected boolean shouldParseImagesOnDemand(Injector injector) {
// If no owners to query, then will never lookup all images
String[] amiOwners = injector.getInstance(Key.get(String[].class, Names.named(PROPERTY_EC2_AMI_OWNERS)));
return (amiOwners.length > 0);
}
@Override
protected Supplier<Set<? extends Image>> supplyNonParsingImageCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds,
final Supplier<Set<? extends Image>> imageSupplier, Injector injector) {
final Supplier<Cache<RegionAndName, ? extends Image>> cache = injector.getInstance(Key.get(new TypeLiteral<Supplier<Cache<RegionAndName, ? extends Image>>>() {}));
return new Supplier<Set<? extends Image>>() {
@Override
public Set<? extends Image> get() {
return ImmutableSet.copyOf(cache.get().asMap().values());
}
};
}
@Provides
@Singleton
protected Supplier<Cache<RegionAndName, ? extends Image>> provideRegionAndNameToImageSupplierCache(

View File

@ -8,9 +8,7 @@ import org.jclouds.compute.domain.Image;
import org.jclouds.ec2.compute.domain.RegionAndName;
import com.google.common.base.Function;
import com.google.inject.Singleton;
@Singleton
public class ImagesToRegionAndIdMap implements Function<Iterable<? extends Image>, Map<RegionAndName, ? extends Image>> {
public static Map<RegionAndName, ? extends Image> imagesToMap(Iterable<? extends Image> input) {

View File

@ -231,6 +231,19 @@ public abstract class BaseComputeServiceContextModule extends AbstractModule {
@Provides
@Singleton
@Memoized
protected Supplier<Set<? extends Image>> supplyImageCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds,
final Supplier<Set<? extends Image>> imageSupplier, Injector injector) {
if (shouldParseImagesOnDemand(injector)) {
return supplyImageCache(seconds, imageSupplier);
} else {
return supplyNonParsingImageCache(seconds, imageSupplier, injector);
}
}
protected boolean shouldParseImagesOnDemand(Injector injector) {
return true;
}
protected Supplier<Set<? extends Image>> supplyImageCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds,
final Supplier<Set<? extends Image>> imageSupplier) {
return new MemoizedRetryOnTimeOutButNotOnAuthorizationExceptionSupplier<Set<? extends Image>>(authException,
@ -242,6 +255,14 @@ public abstract class BaseComputeServiceContextModule extends AbstractModule {
});
}
/**
* For overriding; default impl is same as {@link supplyImageCache(seconds, imageSupplier)}
*/
protected Supplier<Set<? extends Image>> supplyNonParsingImageCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds,
final Supplier<Set<? extends Image>> imageSupplier, Injector injector) {
return supplyImageCache(seconds, imageSupplier);
}
@Provides
@Singleton
protected Supplier<Map<String, ? extends Hardware>> provideSizeMap(@Memoized Supplier<Set<? extends Hardware>> sizes) {

View File

@ -18,11 +18,14 @@
*/
package org.jclouds.aws.ec2.compute.config;
import static org.jclouds.Constants.PROPERTY_SESSION_INTERVAL;
import static org.jclouds.compute.domain.OsFamily.AMZN_LINUX;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;
import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.aws.ec2.AWSEC2PropertiesBuilder;
@ -36,6 +39,7 @@ import org.jclouds.aws.ec2.compute.strategy.AWSEC2ListNodesStrategy;
import org.jclouds.aws.ec2.compute.strategy.AWSEC2ReviseParsedImage;
import org.jclouds.aws.ec2.compute.strategy.CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions;
import org.jclouds.aws.ec2.compute.suppliers.AWSEC2HardwareSupplier;
import org.jclouds.aws.ec2.reference.AWSEC2Constants;
import org.jclouds.compute.config.BaseComputeServiceContextModule;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.TemplateBuilder;
@ -64,8 +68,12 @@ import com.google.common.base.Suppliers;
import com.google.common.base.Throwables;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheLoader;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
/**
*
@ -95,7 +103,29 @@ public class AWSEC2ComputeServiceContextModule extends BaseComputeServiceContext
install(new AWSEC2ComputeServiceDependenciesModule());
}
// TODO duplicates EC2ComputeServiceContextModule; but that's easiest thing to do with guice; could extract to common util
@Override
protected boolean shouldParseImagesOnDemand(Injector injector) {
// If no queries defined, then will never lookup all images
String amiQuery = injector.getInstance(Key.get(String.class, Names.named(AWSEC2Constants.PROPERTY_EC2_AMI_QUERY)));
String amiCcQuery = injector.getInstance(Key.get(String.class, Names.named(AWSEC2Constants.PROPERTY_EC2_CC_AMI_QUERY)));
return !(amiQuery.isEmpty() && amiCcQuery.isEmpty());
}
// duplicates EC2ComputeServiceContextModule; but that's easiest thing to do with guice; could extract to common util
@Override
protected Supplier<Set<? extends Image>> supplyNonParsingImageCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds,
final Supplier<Set<? extends Image>> imageSupplier, Injector injector) {
final Supplier<Cache<RegionAndName, ? extends Image>> cache = injector.getInstance(Key.get(new TypeLiteral<Supplier<Cache<RegionAndName, ? extends Image>>>() {}));
return new Supplier<Set<? extends Image>>() {
@Override
public Set<? extends Image> get() {
return ImmutableSet.copyOf(cache.get().asMap().values());
}
};
}
// duplicates EC2ComputeServiceContextModule; but that's easiest thing to do with guice; could extract to common util
@Provides
@Singleton
protected Supplier<CacheLoader<RegionAndName, Image>> provideRegionAndNameToImageSupplierCacheLoader(