Skip to content

Commit b3eee97

Browse files
committed
Add cost as tie breaker
1 parent 702a439 commit b3eee97

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

environs/instances/instancetype.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,11 @@ type byMemory []InstanceType
177177
func (s byMemory) Len() int { return len(s) }
178178
func (s byMemory) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
179179
func (s byMemory) Less(i, j int) bool {
180-
return s[i].Mem < s[j].Mem
180+
inst0, inst1 := &s[i], &s[j]
181+
if inst0.Mem != inst1.Mem {
182+
return s[i].Mem < s[j].Mem
183+
}
184+
// Memory is equal, so use cost as a tie breaker.
185+
// Result is in descending order of cost so instance with lowest cost is used.
186+
return inst0.Cost > inst1.Cost
181187
}

environs/instances/instancetype_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ var getInstanceTypesTest = []struct {
182182
},
183183
expectedItypes: []string{"it-1"},
184184
},
185+
{
186+
about: "largest mem available matching other constraints if mem not specified, cost is tie breaker",
187+
cons: "cpu-cores=4",
188+
itypesToUse: []InstanceType{
189+
{Id: "4", Name: "it-4", Arches: []string{"amd64"}, Mem: 1024, CpuCores: 2},
190+
{Id: "3", Name: "it-3", Arches: []string{"amd64"}, Mem: 256, CpuCores: 4},
191+
{Id: "2", Name: "it-2", Arches: []string{"amd64"}, Mem: 512, CpuCores: 4, Cost: 50},
192+
{Id: "1", Name: "it-1", Arches: []string{"amd64"}, Mem: 512, CpuCores: 4, Cost: 100},
193+
},
194+
expectedItypes: []string{"it-2"},
195+
},
185196
}
186197

187198
func constraint(region, cons string) *InstanceConstraint {

0 commit comments

Comments
 (0)