Skip to content

Commit

Permalink
Merge pull request #599 from sivanzcw/lessequal
Browse files Browse the repository at this point in the history
Change lessequal function in Reclaimable function
  • Loading branch information
volcano-sh-bot authored Dec 11, 2019
2 parents 1e0d302 + 31aed62 commit 2eec41d
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 1 deletion.
25 changes: 25 additions & 0 deletions pkg/scheduler/api/resource_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,31 @@ func (r *Resource) Less(rr *Resource) bool {
return true
}

// LessEqualStrict checks whether a resource is less or equal than other
func (r *Resource) LessEqualStrict(rr *Resource) bool {
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
}

for rName, rQuant := range r.ScalarResources {
if !lessFunc(rQuant, rr.ScalarResources[rName]) {
return false
}
}

return true
}

// LessEqual checks whether a resource is less than other resource
func (r *Resource) LessEqual(rr *Resource) bool {
lessEqualFunc := func(l, r, diff float64) bool {
Expand Down
155 changes: 155 additions & 0 deletions pkg/scheduler/api/resource_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,158 @@ func TestLess(t *testing.T) {
}
}
}

func TestLessEqualStrict(t *testing.T) {
tests := []struct {
name string
former *Resource
latter *Resource
expected bool
}{
{
name: "same resource",
former: &Resource{
MilliCPU: 1000,
Memory: 1 * 1024 * 1024,
ScalarResources: map[v1.ResourceName]float64{
"nvidia.com/gpu-tesla-p100-16GB": 8000,
},
},
latter: &Resource{
MilliCPU: 1000,
Memory: 1 * 1024 * 1024,
ScalarResources: map[v1.ResourceName]float64{
"nvidia.com/gpu-tesla-p100-16GB": 8000,
},
},
expected: true,
},
{
name: "cpu less",
former: &Resource{
MilliCPU: 1000 - 1,
Memory: 1 * 1024 * 1024,
ScalarResources: map[v1.ResourceName]float64{
"nvidia.com/gpu-tesla-p100-16GB": 8000,
},
},
latter: &Resource{
MilliCPU: 1000,
Memory: 1 * 1024 * 1024,
ScalarResources: map[v1.ResourceName]float64{
"nvidia.com/gpu-tesla-p100-16GB": 8000,
},
},
expected: true,
},
{
name: "memory less",
former: &Resource{
MilliCPU: 1000,
Memory: 1*1024*1024 - 1,
ScalarResources: map[v1.ResourceName]float64{
"nvidia.com/gpu-tesla-p100-16GB": 8000,
},
},
latter: &Resource{
MilliCPU: 1000,
Memory: 1 * 1024 * 1024,
ScalarResources: map[v1.ResourceName]float64{
"nvidia.com/gpu-tesla-p100-16GB": 8000,
},
},
expected: true,
},
{
name: "scalar resource less",
former: &Resource{
MilliCPU: 1000,
Memory: 1 * 1024 * 1024,
ScalarResources: map[v1.ResourceName]float64{
"nvidia.com/gpu-tesla-p100-16GB": 8000 - 1,
},
},
latter: &Resource{
MilliCPU: 1000,
Memory: 1 * 1024 * 1024,
ScalarResources: map[v1.ResourceName]float64{
"nvidia.com/gpu-tesla-p100-16GB": 8000,
},
},
expected: true,
},
{
name: "memory larger",
former: &Resource{
MilliCPU: 1000,
Memory: 1*1024*1024 + 1,
ScalarResources: map[v1.ResourceName]float64{
"nvidia.com/gpu-tesla-p100-16GB": 8000,
},
},
latter: &Resource{
MilliCPU: 1000,
Memory: 1 * 1024 * 1024,
ScalarResources: map[v1.ResourceName]float64{
"nvidia.com/gpu-tesla-p100-16GB": 8000,
},
},
expected: false,
},
{
name: "scalar larger",
former: &Resource{
MilliCPU: 1000,
Memory: 1 * 1024 * 1024,
ScalarResources: map[v1.ResourceName]float64{
"nvidia.com/gpu-tesla-p100-16GB": 8000 + 1,
},
},
latter: &Resource{
MilliCPU: 1000,
Memory: 1 * 1024 * 1024,
ScalarResources: map[v1.ResourceName]float64{
"nvidia.com/gpu-tesla-p100-16GB": 8000,
},
},
expected: false,
},
{
name: "former does not have scalar resource",
former: &Resource{
MilliCPU: 1000,
Memory: 1 * 1024 * 1024,
},
latter: &Resource{
MilliCPU: 1000,
Memory: 1 * 1024 * 1024,
ScalarResources: map[v1.ResourceName]float64{
"nvidia.com/gpu-tesla-p100-16GB": 8000,
},
},
expected: true,
},
{
name: "latter does not have scalar resource",
former: &Resource{
MilliCPU: 1000,
Memory: 1 * 1024 * 1024,
ScalarResources: map[v1.ResourceName]float64{
"nvidia.com/gpu-tesla-p100-16GB": 8000,
},
},
latter: &Resource{
MilliCPU: 1000,
Memory: 1 * 1024 * 1024,
},
expected: false,
},
}

for _, test := range tests {
result := test.former.LessEqualStrict(test.latter)
if !reflect.DeepEqual(test.expected, result) {
t.Errorf("case %s, expected: %#v, got: %#v", test.name, test.expected, result)
}
}
}
2 changes: 1 addition & 1 deletion pkg/scheduler/plugins/proportion/proportion.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (pp *proportionPlugin) OnSessionOpen(ssn *framework.Session) {
}

allocated.Sub(reclaimee.Resreq)
if attr.deserved.LessEqual(allocated) {
if attr.deserved.LessEqualStrict(allocated) {
victims = append(victims, reclaimee)
}
}
Expand Down

0 comments on commit 2eec41d

Please sign in to comment.