Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Resource Less/LessEqual #484

Merged
merged 1 commit into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resource1 < resource2 in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, resource1.ScalarResources map is not nil, but value <= minMilliScalarResources, so continue, point 4

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about the others, e.g. cpu, memory? we need to make sure all resources meet LessEqual.

},
{
resource1: &Resource{
Expand Down