nomad: Resource Superset ignores network in favor of NetworkIndex

This commit is contained in:
Armon Dadgar
2015-09-13 14:59:34 -07:00
parent 6da83f1e1b
commit ecb60cc766
2 changed files with 2 additions and 81 deletions

View File

@@ -602,7 +602,8 @@ func (r *Resources) NetIndexByIP(ip string) int {
}
// Superset checks if one set of resources is a superset
// of another.
// of another. This ignores network resources, and the NetworkIndex
// should be used for that.
func (r *Resources) Superset(other *Resources) bool {
if r.CPU < other.CPU {
return false
@@ -616,29 +617,6 @@ func (r *Resources) Superset(other *Resources) bool {
if r.IOPS < other.IOPS {
return false
}
// Compute the MBits available by index
mbitsByIdx := make(map[int]int)
for idx, n := range r.Networks {
mbitsByIdx[idx] = n.MBits
}
// Ensure all networks exist and do not exhaust bandwidth
for _, n := range other.Networks {
// Find the matching interface by IP or CIDR
idx := r.NetIndex(n)
if idx == -1 {
return false
}
// Deduct the allocation
mbitsByIdx[idx] -= n.MBits
// Check if we've exhaused our allocation
if mbitsByIdx[idx] < 0 {
return false
}
}
return true
}

View File

@@ -47,24 +47,12 @@ func TestResource_Superset(t *testing.T) {
MemoryMB: 2048,
DiskMB: 10000,
IOPS: 100,
Networks: []*NetworkResource{
&NetworkResource{
CIDR: "10.0.0.0/8",
MBits: 100,
},
},
}
r2 := &Resources{
CPU: 1.0,
MemoryMB: 1024,
DiskMB: 5000,
IOPS: 50,
Networks: []*NetworkResource{
&NetworkResource{
CIDR: "10.0.0.0/8",
MBits: 50,
},
},
}
if !r1.Superset(r1) {
@@ -81,51 +69,6 @@ func TestResource_Superset(t *testing.T) {
}
}
func TestResource_Superset_IPCIDR(t *testing.T) {
r1 := &Resources{
CPU: 2.0,
MemoryMB: 2048,
DiskMB: 10000,
IOPS: 100,
Networks: []*NetworkResource{
&NetworkResource{
CIDR: "10.0.0.0/8",
MBits: 100,
},
},
}
r2 := &Resources{
CPU: 1.0,
MemoryMB: 1024,
DiskMB: 5000,
IOPS: 50,
Networks: []*NetworkResource{
&NetworkResource{
IP: "10.0.0.5",
MBits: 50,
},
&NetworkResource{
IP: "10.0.0.6",
MBits: 50,
},
},
}
if !r1.Superset(r2) {
t.Fatalf("bad")
}
// Use more network
r2.Networks = append(r2.Networks, &NetworkResource{
IP: "10.0.0.7",
MBits: 50,
})
if r1.Superset(r2) {
t.Fatalf("bad")
}
}
func TestResource_Add(t *testing.T) {
r1 := &Resources{
CPU: 2.0,