Skip to content

Commit

Permalink
Merge pull request #484 from wangyuqing4/resource
Browse files Browse the repository at this point in the history
fix Resource Less/LessEqual
  • Loading branch information
k82cn authored Oct 17, 2019
2 parents dab3f6e + 06c8fad commit 03b3053
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
49 changes: 37 additions & 12 deletions pkg/scheduler/api/resource_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,24 +224,38 @@ func (r *Resource) Multi(ratio float64) *Resource {

// 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 @@ -251,9 +265,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 @@ -262,12 +284,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 03b3053

Please sign in to comment.