From 86c4acb3572161b85f51394135efb4577060fbef Mon Sep 17 00:00:00 2001 From: Mateusz Date: Tue, 26 Nov 2019 12:01:28 -0800 Subject: [PATCH] fix Resource Less/LessEqual Cherry picked from https://github.com/volcano-sh/volcano/pull/484. --- pkg/scheduler/api/resource_info.go | 51 ++++++++++++++++++------- pkg/scheduler/api/resource_info_test.go | 2 +- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/pkg/scheduler/api/resource_info.go b/pkg/scheduler/api/resource_info.go index 7f38517f3..ecab02461 100644 --- a/pkg/scheduler/api/resource_info.go +++ b/pkg/scheduler/api/resource_info.go @@ -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 } } @@ -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 } @@ -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 } } diff --git a/pkg/scheduler/api/resource_info_test.go b/pkg/scheduler/api/resource_info_test.go index 8b84f2421..dc3832dd4 100644 --- a/pkg/scheduler/api/resource_info_test.go +++ b/pkg/scheduler/api/resource_info_test.go @@ -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{