Changed the instance Size to support floating-point type of # of cores (ex, 6.5 cores). Propogated the change to template builder and related ComputeService usages.

This commit is contained in:
Alex Yarmula 2010-02-23 13:24:57 -08:00
parent c0b5d832b5
commit d71b439981
7 changed files with 60 additions and 52 deletions

View File

@ -39,13 +39,18 @@ public class EC2Size extends SizeImpl {
private static final long serialVersionUID = 8605688733788974797L;
private final InstanceType instanceType;
EC2Size(InstanceType instanceType, Integer cores, Integer ram, Integer disk,
EC2Size(InstanceType instanceType, Double cores, Integer ram, Integer disk,
Iterable<Architecture> supportedArchitectures) {
super(instanceType.toString(), instanceType.toString(), null, null, ImmutableMap
.<String, String> of(),cores, ram, disk, supportedArchitectures);
this.instanceType = instanceType;
}
EC2Size(InstanceType instanceType, Integer cores, Integer ram, Integer disk,
Iterable<Architecture> supportedArchitectures) {
this(instanceType, cores.doubleValue(), ram, disk, supportedArchitectures);
}
/**
* Returns the EC2 InstanceType associated with this size.
*/
@ -71,7 +76,7 @@ public class EC2Size extends SizeImpl {
/**
* @see InstanceType#M2_XLARGE
*/
public static final EC2Size M2_XLARGE = new EC2Size(InstanceType.M2_XLARGE, 6 /*TODO: 6.5*/, 17510, 420,
public static final EC2Size M2_XLARGE = new EC2Size(InstanceType.M2_XLARGE, 6.5, 17510, 420,
ImmutableSet.of(Architecture.X86_64));
/**
* @see InstanceType#M2_2XLARGE

View File

@ -60,80 +60,82 @@ public class EC2ComputeServiceTest {
* {@link org.jclouds.compute.domain.Size} from {@link EC2Size}.
*
* Expected size: m2.xlarge
*
* @throws Exception if non-test-related exception arises
*/
@Test
public void testTemplateChoiceForInstanceBySizeId() throws Exception {
//create an instance of TemplateBuilderImpl
Location location = new LocationImpl(LocationScope.REGION, "us-east-1", "us east", null, true);
Image image = new ImageImpl("ami-image", "image", "us-east-1", new URI("http"),
Maps.<String,String>newHashMap(), "description", "1.0",
null, "ubuntu", Architecture.X86_64);
TemplateBuilderImpl templateBuilder =
new TemplateBuilderImpl(ImmutableMap.of("us-east-1", location),
ImmutableMap.of("ami-image", image),
ImmutableMap.of("m2.xlarge", EC2Size.M2_XLARGE,
"m2.2xlarge", EC2Size.M2_2XLARGE,
"m2.4xlarge", EC2Size.M2_4XLARGE),
location);
//find the matching template
Template template = templateBuilder.
Template template = newTemplateBuilder().
architecture(Architecture.X86_64).sizeId("m2.xlarge").
locationId("us-east-1").
build();
//assert the template is correct
assert template != null : "The returned template was null, but it should have a value.";
assert EC2Size.M2_XLARGE.equals(template.getSize()) :
format("Incorrect image determined by the template. Expected: %s. Found: %s.",
"m2.xlarge", String.valueOf(template.getSize()));
}
/**
* Verifies that {@link TemplateBuilderImpl} would
* choose the correct size of the instance, based on
* physical attributes (# of cores, ram, etc).
*
* Expected size: m2.xlarge
*
* @throws Exception if non-test-related exception arises
*/
@Test
public void testTemplateChoiceForInstanceByAttributes() throws Exception {
Template template = newTemplateBuilder().
architecture(Architecture.X86_64).
minRam(17510).minCores(6.5).smallest().
locationId("us-east-1").
build();
//create an instance of TemplateBuilderImpl
assert template != null : "The returned template was null, but it should have a value.";
assert EC2Size.M2_XLARGE.equals(template.getSize()) :
format("Incorrect image determined by the template. Expected: %s. Found: %s.",
"m2.xlarge", String.valueOf(template.getSize()));
}
/**
* Negative test version of {@link #testTemplateChoiceForInstanceByAttributes}.
*
* Verifies that {@link TemplateBuilderImpl} would
* not choose the insufficient size of the instance, based on
* physical attributes (# of cores, ram, etc).
*
* Expected size: anything but m2.xlarge
*/
@Test
public void testNegativeTemplateChoiceForInstanceByAttributes() throws Exception {
Template template = newTemplateBuilder().
architecture(Architecture.X86_64).
minRam(17510).minCores(6.7).smallest().
locationId("us-east-1").
build();
assert template != null : "The returned template was null, but it should have a value.";
assert ! EC2Size.M2_XLARGE.equals(template.getSize()) :
format("Incorrect image determined by the template. Expected: not %s. Found: %s.",
"m2.xlarge", String.valueOf(template.getSize()));
}
private TemplateBuilder newTemplateBuilder() {
Location location = new LocationImpl(LocationScope.REGION, "us-east-1", "us east", null, true);
Image image = new ImageImpl("ami-image", "image", "us-east-1", new URI("http"),
Image image = new ImageImpl("ami-image", "image", "us-east-1", null,
Maps.<String,String>newHashMap(), "description", "1.0",
null, "ubuntu", Architecture.X86_64);
TemplateBuilderImpl templateBuilder =
new TemplateBuilderImpl(ImmutableMap.of("us-east-1", location),
return new TemplateBuilderImpl(ImmutableMap.of("us-east-1", location),
ImmutableMap.of("ami-image", image),
Maps.uniqueIndex(ImmutableSet.of(EC2Size.C1_MEDIUM, EC2Size.C1_XLARGE,
EC2Size.M1_LARGE, EC2Size.M1_SMALL, EC2Size.M1_XLARGE, EC2Size.M2_XLARGE,
EC2Size.M2_2XLARGE, EC2Size.M2_4XLARGE), indexer()),
location);
//find the matching template
Template template = templateBuilder.
architecture(Architecture.X86_64).
minRam(17510).minCores(6).smallest().
locationId("us-east-1").
build();
//assert the template is correct
assert template != null : "The returned template was null, but it should have a value.";
assert EC2Size.M2_XLARGE.equals(template.getSize()) :
format("Incorrect image determined by the template. Expected: %s. Found: %s.",
"m2.xlarge", String.valueOf(template.getSize()));
}
Function<ComputeMetadata, String> indexer() {
return new Function<ComputeMetadata, String>() {
@Override

View File

@ -40,7 +40,7 @@ public interface Size extends ComputeMetadata {
/**
* Amount of virtual or physical cores provided
*/
int getCores();
double getCores();
/**
* Amount of RAM provided in MB (256M, 1740)

View File

@ -111,7 +111,7 @@ public interface TemplateBuilder {
/**
* Configure this template to require the minimum cores below
*/
TemplateBuilder minCores(int minCores);
TemplateBuilder minCores(double minCores);
/**
* Configure this template to require the minimum ram in megabytes below

View File

@ -41,14 +41,14 @@ import com.google.common.collect.Sets;
public class SizeImpl extends ComputeMetadataImpl implements Size {
/** The serialVersionUID */
private static final long serialVersionUID = 8994255275911717567L;
private final int cores;
private final double cores;
private final int ram;
private final int disk;
private final Set<Architecture> supportedArchitectures = Sets.newHashSet();
public SizeImpl(String id, String name, @Nullable String location, URI uri,
Map<String, String> userMetadata, int cores, int ram, int disk,
Map<String, String> userMetadata, double cores, int ram, int disk,
Iterable<Architecture> supportedArchitectures) {
super(ComputeType.SIZE, id, name, location, uri, userMetadata);
this.cores = cores;
@ -62,7 +62,7 @@ public class SizeImpl extends ComputeMetadataImpl implements Size {
* {@inheritDoc}
*/
@Override
public int getCores() {
public double getCores() {
return cores;
}

View File

@ -10,6 +10,7 @@ import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import com.google.common.primitives.Doubles;
import org.jclouds.compute.domain.Architecture;
import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.compute.domain.Image;
@ -54,7 +55,7 @@ public class TemplateBuilderImpl implements TemplateBuilder {
private String imageVersion;
private String imageName;
private int minCores;
private double minCores;
private int minRam;
private boolean biggest;
@ -212,7 +213,7 @@ public class TemplateBuilderImpl implements TemplateBuilder {
};
static final Ordering<Size> BY_CORES_ORDERING = new Ordering<Size>() {
public int compare(Size left, Size right) {
return Ints.compare(left.getCores(), right.getCores());
return Doubles.compare(left.getCores(), right.getCores());
}
};
static final Ordering<Image> DEFAULT_IMAGE_ORDERING = new Ordering<Image>() {
@ -386,7 +387,7 @@ public class TemplateBuilderImpl implements TemplateBuilder {
* {@inheritDoc}
*/
@Override
public TemplateBuilder minCores(int minCores) {
public TemplateBuilder minCores(double minCores) {
this.minCores = minCores;
return this;
}

View File

@ -180,7 +180,7 @@ public class VCloudComputeServiceContextModule extends VCloudContextModule {
@Override
public NodeMetadata execute(String tag, String name, Template template) {
Map<String, String> metaMap = computeClient.start(template.getLocation().getId(), name,
template.getImage().getId(), template.getSize().getCores(), template.getSize()
template.getImage().getId(), Double.valueOf(template.getSize().getCores()).intValue(), template.getSize()
.getRam(), template.getSize().getDisk() * 1024 * 1024l, ImmutableMap
.<String, String> of(), template.getOptions().getInboundPorts());
VApp vApp = client.getVApp(metaMap.get("id"));