Description:
Fallback cost sort to mem, cpu-power, cpu-cores
Not all environments provide a cost option, so when computing an
appropriate
instance type from provided constraints, use mem, cpu-power and
cpu-cores as
fallback options. When any two of them are the same, we fallback to the
next
option.
Reviewers: mp+179191_ code.launchpad. net,
Message:
Please take a look.
Description:
Fallback cost sort to mem, cpu-power, cpu-cores
Not all environments provide a cost option, so when computing an
appropriate
instance type from provided constraints, use mem, cpu-power and
cpu-cores as
fallback options. When any two of them are the same, we fallback to the
next
option.
https:/ /code.launchpad .net/~sidnei/ juju-core/ sort-by- cost-lowest- fallback/ +merge/ 179191
(do not edit description out of merge proposal)
Please review this at https:/ /codereview. appspot. com/12569044/
Affected files: instances/ instancetype. go instances/ instancetype_ test.go openstack/ image.go
A [revision details]
M environs/
M environs/
M environs/
Index: [revision details] 20130808095436- ia7cq6t6ybdpo2h f
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: tarmac-
+New revision: <email address hidden>
Index: environs/ instances/ instancetype. go instances/ instancetype. go' instances/ instancetype. go 2013-05-20 23:33:29 +0000 instances/ instancetype. go 2013-08-08 13:36:50 +0000
=== modified file 'environs/
--- environs/
+++ environs/
@@ -126,8 +126,27 @@
// byCost is used to sort a slice of instance types by Cost.
type byCost []InstanceType
-func (bc byCost) Len() int { return len(bc) }
-func (bc byCost) Less(i, j int) bool { return bc[i].Cost < bc[j].Cost }
+func (bc byCost) Len() int { return len(bc) }
+func (bc byCost) Less(i, j int) bool {
+ if bc[i].Cost != bc[j].Cost {
+ return bc[i].Cost < bc[j].Cost
+ }
+ if bc[i].Mem != bc[j].Mem {
+ return bc[i].Mem < bc[j].Mem
+ }
+ iCpuPower := uint64(0)
+ jCpuPower := uint64(0)
+ if bc[i].CpuPower != nil {
+ iCpuPower = *bc[i].CpuPower
+ }
+ if bc[j].CpuPower != nil {
+ jCpuPower = *bc[j].CpuPower
+ }
+ if iCpuPower != jCpuPower {
+ return iCpuPower < jCpuPower
+ }
+ return bc[i].CpuCores < bc[j].CpuCores
+}
func (bc byCost) Swap(i, j int) {
bc[i], bc[j] = bc[j], bc[i]
}
Index: environs/ instances/ instancetype_ test.go instances/ instancetype_ test.go' instances/ instancetype_ test.go 2013-05-17 06:34:24 +0000 instances/ instancetype_ test.go 2013-08-08 13:36:50 +0000
=== modified file 'environs/
--- environs/
+++ environs/
@@ -4,6 +4,8 @@
package instances
import ( net/gocheck" net/juju- core/constraint s" net/juju- core/testing" byCost( t.itypesToUse) )
+ "sort"
+
. "launchpad.
"launchpad.
"launchpad.
@@ -231,3 +233,68 @@
}
}
}
+
+var byCostTest = []struct {
+ info string
+ itypesToUse []InstanceType
+ expectedItypes []string
+}{
+ {
+ info: "default to lowest cost",
+ itypesToUse: []InstanceType{
+ {Id: "2", Name: "it-2", CpuCores: 2, Mem: 4096, Cost: 240},
+ {Id: "1", Name: "it-1", CpuCores: 1, Mem: 2048, Cost: 241},
+ },
+ expectedItypes: []string{
+ "it-2", "it-1",
+ },
+ }, {
+ info: "when no cost associated, pick lowest ram",
+ itypesToUse: []InstanceType{
+ {Id: "2", Name: "it-2", CpuCores: 2, Mem: 4096},
+ {Id: "1", Name: "it-1", CpuCores: 1, Mem: 2048},
+ },
+ expectedItypes: []string{
+ "it-1", "it-2",
+ },
+ }, {
+ info: "when cost is the same, pick lowest ram",
+ itypesToUse: []InstanceType{
+ {Id: "2", Name: "it-2", CpuCores: 2, Mem: 4096, Cost: 240},
+ {Id: "1", Name: "it-1", CpuCores: 1, Mem: 2048, Cost: 240},
+ },
+ expectedItypes: []string{
+ "it-1", "it-2",
+ },
+ }, {
+ info: "when cost and ram is the same, pick lowest cpu power",
+ itypesToUse: []InstanceType{
+ {Id: "2", Name: "it-2", CpuCores: 2, CpuPower: CpuPower(200)},
+ {Id: "1", Name: "it-1", CpuCores: 1, CpuPower: CpuPower(100)},
+ },
+ expectedItypes: []string{
+ "it-1", "it-2",
+ },
+ }, {
+ info: "when cpu power is the same, pick the lowest cores",
+ itypesToUse: []InstanceType{
+ {Id: "2", Name: "it-2", CpuCores: 2, CpuPower: CpuPower(200)},
+ {Id: "1", Name: "it-1", CpuCores: 1, CpuPower: CpuPower(200)},
+ },
+ expectedItypes: []string{
+ "it-1", "it-2",
+ },
+ },
+}
+
+func (s *instanceTypeSuite) TestSortByCost(c *C) {
+ for i, t := range byCostTest {
+ c.Logf("test %d: %s", i, t.info)
+ sort.Sort(
+ names := make([]string, len(t.itypesToUse))
+ for i, itype := range t.itypesToUse {
+ names[i] = itype.Name
+ }
+ c.Check(names, DeepEquals, t.expectedItypes)
+ }
+}
Index: environs/ openstack/ image.go openstack/ image.go' openstack/ image.go 2013-07-31 10:42:27 +0000 openstack/ image.go 2013-08-08 13:36:50 +0000 flavor. VCPUs), Types = append( allInstanceType s, instanceType)
=== modified file 'environs/
--- environs/
+++ environs/
@@ -25,7 +25,6 @@
Arches: ic.Arches,
Mem: uint64(flavor.RAM),
CpuCores: uint64(
- Cost: uint64(flavor.RAM),
}
allInstance
}