Skip to content

Commit

Permalink
fix Resource Less/LessEqual
Browse files Browse the repository at this point in the history
Cherry picked from volcano-sh/volcano#484.
  • Loading branch information
mateuszlitwin committed Nov 26, 2019
1 parent 4fc3170 commit 86c4acb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
51 changes: 38 additions & 13 deletions pkg/scheduler/api/resource_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,26 +223,40 @@ func (r *Resource) Multi(ratio float64) *Resource {
return r
}

// Less checks whether a resource is less than other
// Less checks whether a resource is less than other.
func (r *Resource) Less(rr *Resource) bool {
if !(r.MilliCPU < rr.MilliCPU && r.Memory < rr.Memory) {
lessFunc := func(l, r float64) bool {
if l < r {
return true
}
return false
}

if !lessFunc(r.MilliCPU, rr.MilliCPU) {
return false
}
if !lessFunc(r.Memory, rr.Memory) {
return false
}

if r.ScalarResources == nil {
if rr.ScalarResources == nil {
return false
if rr.ScalarResources != nil {
for _, rrQuant := range rr.ScalarResources {
if rrQuant <= minMilliScalarResources {
return false
}
}
}
return true
}

for rName, rQuant := range r.ScalarResources {
if rr.ScalarResources == nil {
return false
}
if rr.ScalarResources == nil {
return false
}

for rName, rQuant := range r.ScalarResources {
rrQuant := rr.ScalarResources[rName]
if rQuant >= rrQuant {
if !lessFunc(rQuant, rrQuant) {
return false
}
}
Expand All @@ -252,9 +266,17 @@ func (r *Resource) Less(rr *Resource) bool {

// LessEqual checks whether a resource is less than other resource
func (r *Resource) LessEqual(rr *Resource) bool {
isLess := (r.MilliCPU < rr.MilliCPU || math.Abs(rr.MilliCPU-r.MilliCPU) < minMilliCPU) &&
(r.Memory < rr.Memory || math.Abs(rr.Memory-r.Memory) < minMemory)
if !isLess {
lessEqualFunc := func(l, r, diff float64) bool {
if l < r || math.Abs(l-r) < diff {
return true
}
return false
}

if !lessEqualFunc(r.MilliCPU, rr.MilliCPU, minMilliCPU) {
return false
}
if !lessEqualFunc(r.Memory, rr.Memory, minMemory) {
return false
}

Expand All @@ -263,12 +285,15 @@ func (r *Resource) LessEqual(rr *Resource) bool {
}

for rName, rQuant := range r.ScalarResources {
if rQuant <= minMilliScalarResources {
continue
}
if rr.ScalarResources == nil {
return false
}

rrQuant := rr.ScalarResources[rName]
if !(rQuant < rrQuant || math.Abs(rrQuant-rQuant) < minMilliScalarResources) {
if !lessEqualFunc(rQuant, rrQuant, minMilliScalarResources) {
return false
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/scheduler/api/resource_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func TestLessEqual(t *testing.T) {
ScalarResources: map[v1.ResourceName]float64{"scalar.test/scalar1": 1},
},
resource2: &Resource{},
expected: false,
expected: true,
},
{
resource1: &Resource{
Expand Down

0 comments on commit 86c4acb

Please sign in to comment.