Skip to content

Commit

Permalink
fix panic when sub total guarantee
Browse files Browse the repository at this point in the history
Signed-off-by: lowang-bh <lhui_wang@163.com>
  • Loading branch information
lowang-bh committed Nov 25, 2023
1 parent faa3b78 commit 17a6e2f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
23 changes: 2 additions & 21 deletions pkg/scheduler/api/job_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,30 +492,11 @@ func (ji *JobInfo) GetMinResources() *Resource {

// GetElasticResources returns those partly resources in allocated which are more than its minResource
func (ji *JobInfo) GetElasticResources() *Resource {
elastic := EmptyResource()
if ji.Allocated == nil {
return elastic
return EmptyResource()
}
minResource := ji.GetMinResources()
if ji.Allocated.MilliCPU > minResource.MilliCPU {
elastic.MilliCPU = ji.Allocated.MilliCPU - minResource.MilliCPU
}
if ji.Allocated.Memory > minResource.Memory {
elastic.Memory = ji.Allocated.Memory - minResource.Memory
}

for k, v := range ji.Allocated.ScalarResources {
if k == v1.ResourcePods{
continue
}
minv := minResource.ScalarResources[k]
if v > minv {
if elastic.ScalarResources == nil {
elastic.ScalarResources = map[v1.ResourceName]float64{}
}
elastic.ScalarResources[k] = v - minv
}
}
elastic := ExceedPart(ji.Allocated, minResource)

return elastic
}
Expand Down
31 changes: 31 additions & 0 deletions pkg/scheduler/api/resource_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,34 @@ func (r ResourceNameList) Contains(rr ResourceNameList) bool {
func IsCountQuota(name v1.ResourceName) bool {
return strings.HasPrefix(string(name), "count/")
}

// ExceedPart returns the partly resource in left which exceed right
func ExceedPart(left, right *Resource) *Resource {
if right == nil {
return left
}
diff := EmptyResource()
if left == nil {
return diff
}
if left.MilliCPU > right.MilliCPU {
diff.MilliCPU = left.MilliCPU - right.MilliCPU
}
if left.Memory > right.Memory {
diff.Memory = left.Memory - right.Memory
}

for k, v := range left.ScalarResources {
if k == v1.ResourcePods {
continue
}
minv := right.ScalarResources[k]
if v > minv {
if diff.ScalarResources == nil {
diff.ScalarResources = map[v1.ResourceName]float64{}
}
diff.ScalarResources[k] = v - minv
}
}
return diff
}
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.ExceedPart(pp.totalResource, pp.totalGuarantee).Add(attr.guarantee)
if attr.capability == nil {
attr.realCapability = realCapability
} else {
Expand Down

0 comments on commit 17a6e2f

Please sign in to comment.