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 panic when get job's elastic resource #3106

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions pkg/scheduler/api/job_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,12 +572,15 @@ func (ji *JobInfo) DeductSchGatedResources(res *Resource) *Resource {
return result
}

// GetElasticResources returns those partly resources in allocated which are more than its minResource
func (ji *JobInfo) GetElasticResources() *Resource {
minResource := ji.GetMinResources()
if ji.Allocated.LessEqualPartly(minResource, Zero) {
if ji.Allocated == nil {
return EmptyResource()
}
return ji.Allocated.Clone().Sub(minResource)
minResource := ji.GetMinResources()
elastic := ExceededPart(ji.Allocated, minResource)

return elastic
}

func (ji *JobInfo) addTaskIndex(ti *TaskInfo) {
Expand Down
55 changes: 55 additions & 0 deletions pkg/scheduler/api/job_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,58 @@ func TestJobInfo(t *testing.T) {
}
}
}

func TestGetElasticResources(t *testing.T) {
resNoGPU := BuildResourceList("1", "1G")
resWithGPU := BuildResourceListWithGPU("1", "1G", "1")
wantNoGPU := BuildResourceList("1", "1G", []ScalarResource{{Name: "pods", Value: "1"}}...)
wantWithGPU := BuildResourceListWithGPU("1", "1G", "1", []ScalarResource{{Name: "pods", Value: "1"}}...)
tests := []struct {
pods []*v1.Pod
podgroup scheduling.PodGroup
want *Resource
}{
{
pods: []*v1.Pod{
buildPod("ns1", "task-1", "node1", v1.PodRunning, resWithGPU, nil, make(map[string]string)),
},
podgroup: BuildPodgroup("pg1", "ns1", 1, wantWithGPU),
want: EmptyResource(),
},
{
pods: []*v1.Pod{
buildPod("ns1", "task-1", "node1", v1.PodRunning, resWithGPU, nil, make(map[string]string)),
buildPod("ns1", "task-2", "node2", v1.PodRunning, resNoGPU, nil, make(map[string]string)),
},
podgroup: BuildPodgroup("pg1", "ns1", 1, wantWithGPU),
want: NewResource(wantNoGPU),
},
{
pods: []*v1.Pod{
buildPod("ns1", "task-1", "node1", v1.PodRunning, resNoGPU, nil, make(map[string]string)),
buildPod("ns1", "task-2", "node2", v1.PodRunning, resNoGPU, nil, make(map[string]string)),
},
podgroup: BuildPodgroup("pg1", "ns1", 1, wantWithGPU),
want: NewResource(wantNoGPU),
},
{
pods: []*v1.Pod{
buildPod("ns1", "task-1", "node1", v1.PodRunning, resWithGPU, nil, make(map[string]string)),
buildPod("ns1", "task-2", "node2", v1.PodRunning, resWithGPU, nil, make(map[string]string)),
},
podgroup: BuildPodgroup("pg1", "ns1", 1, wantWithGPU),
want: NewResource(wantWithGPU),
},
}

for i, test := range tests {
job := NewJobInfo("job")
for _, pod := range test.pods {
job.AddTaskInfo(NewTaskInfo(pod))
}
job.SetPodGroup(&PodGroup{PodGroup: test.podgroup})
if elastic := job.GetElasticResources(); !elastic.Equal(test.want, Zero) {
t.Fatalf("case %d:expected %+v, got %+v", i, test.want, elastic)
}
}
}
14 changes: 14 additions & 0 deletions pkg/scheduler/api/resource_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,3 +771,17 @@ func (r ResourceNameList) Contains(rr ResourceNameList) bool {
func IsCountQuota(name v1.ResourceName) bool {
return strings.HasPrefix(string(name), "count/")
}

// ExceededPart returns the partly resource in left which exceed right
func ExceededPart(left, right *Resource) *Resource {
if right == nil {
return left
}

if left == nil {
return EmptyResource()
}

diff, _ := left.Diff(right, Zero)
return diff
}
20 changes: 18 additions & 2 deletions pkg/scheduler/api/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

"volcano.sh/apis/pkg/apis/scheduling"
)

func buildNode(name string, alloc v1.ResourceList) *v1.Node {
Expand Down Expand Up @@ -90,7 +92,7 @@ type ScalarResource struct {
Value string
}

// BuildResourceList builts resource list object
// BuildResourceList builds resource list object
func BuildResourceList(cpu string, memory string, scalarResources ...ScalarResource) v1.ResourceList {
resourceList := v1.ResourceList{
v1.ResourceCPU: resource.MustParse(cpu),
Expand All @@ -103,7 +105,7 @@ func BuildResourceList(cpu string, memory string, scalarResources ...ScalarResou
return resourceList
}

// BuildResourceListWithGPU builts resource list with GPU
// BuildResourceListWithGPU builds resource list with GPU
func BuildResourceListWithGPU(cpu string, memory string, GPU string, scalarResources ...ScalarResource) v1.ResourceList {
resourceList := v1.ResourceList{
v1.ResourceCPU: resource.MustParse(cpu),
Expand All @@ -116,3 +118,17 @@ func BuildResourceListWithGPU(cpu string, memory string, GPU string, scalarResou

return resourceList
}

// BuildPodgroup builds podgroup
func BuildPodgroup(name, ns string, minMember int32, minResource v1.ResourceList) scheduling.PodGroup {
return scheduling.PodGroup{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: name,
},
Spec: scheduling.PodGroupSpec{
MinMember: minMember,
MinResources: &minResource,
},
}
}
2 changes: 1 addition & 1 deletion pkg/scheduler/plugins/capacity/capacity.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (cp *capacityPlugin) OnSessionOpen(ssn *framework.Session) {
if len(queue.Queue.Spec.Guarantee.Resource) != 0 {
attr.guarantee = api.NewResource(queue.Queue.Spec.Guarantee.Resource)
}
realCapability := cp.totalResource.Clone().Sub(cp.totalGuarantee).Add(attr.guarantee)
realCapability := api.ExceededPart(cp.totalResource, cp.totalGuarantee).Add(attr.guarantee)
if attr.capability == nil {
attr.realCapability = realCapability
} else {
Expand Down
2 changes: 1 addition & 1 deletion pkg/scheduler/plugins/proportion/proportion.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (pp *proportionPlugin) OnSessionOpen(ssn *framework.Session) {
if len(queue.Queue.Spec.Guarantee.Resource) != 0 {
attr.guarantee = api.NewResource(queue.Queue.Spec.Guarantee.Resource)
}
realCapability := pp.totalResource.Clone().Sub(pp.totalGuarantee).Add(attr.guarantee)
realCapability := api.ExceededPart(pp.totalResource, pp.totalGuarantee).Add(attr.guarantee)
if attr.capability == nil {
attr.realCapability = realCapability
} else {
Expand Down
Loading