Skip to content

Commit

Permalink
[SPCLD-6491] fix bugs of cpu realloc algorithm (#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
DuodenumL authored Jun 21, 2022
1 parent 91cd77c commit 2091d84
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
8 changes: 4 additions & 4 deletions scheduler/complex/potassium.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (m *Potassium) ReselectCPUNodes(ctx context.Context, scheduleInfo resourcet

func cpuReallocPlan(scheduleInfo resourcetypes.ScheduleInfo, quota float64, CPU types.CPUMap, sharebase int64) (resourcetypes.ScheduleInfo, float64, types.CPUMap) {
affinityPlan := make(types.CPUMap)
diff := int64(quota*float64(sharebase)) - CPU.Total()
diff := types.RoundToInt(quota*float64(sharebase)) - CPU.Total()
// sort by pieces
cpuIDs := []string{}
for cpuID := range CPU {
Expand Down Expand Up @@ -226,7 +226,7 @@ func cpuReallocPlan(scheduleInfo resourcetypes.ScheduleInfo, quota float64, CPU
}

// expand, prioritize full cpus
needPieces := int64(quota * float64(sharebase))
needPieces := types.RoundToInt(quota * float64(sharebase))
for i := len(cpuIDs) - 1; i >= 0; i-- {
cpuID := cpuIDs[i]
if needPieces == 0 {
Expand All @@ -243,10 +243,10 @@ func cpuReallocPlan(scheduleInfo resourcetypes.ScheduleInfo, quota float64, CPU

// fragments, try to find complement
if available := scheduleInfo.CPU[cpuID]; available == sharebase-CPU[cpuID] {
expand := utils.Min64(available, needPieces)
expand := utils.Min64(available, needPieces-CPU[cpuID])
affinityPlan[cpuID] = CPU[cpuID] + expand
scheduleInfo.CPU[cpuID] -= expand
needPieces -= sharebase
needPieces -= affinityPlan[cpuID]
continue
}

Expand Down
17 changes: 12 additions & 5 deletions scheduler/complex/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sort"

"github.com/projecteru2/core/types"
"github.com/projecteru2/core/utils"
)

type resourceInfo struct {
Expand Down Expand Up @@ -121,7 +122,10 @@ func (h *host) getFragmentResult(fragment int64, resources []resourceInfo) []typ
resourceMaps := h.getFragmentsResult(resources, fragment)
result := make([]types.ResourceMap, len(resourceMaps))
for i, resourceMap := range resourceMaps {
result[i] = resourceMap[0]
result[i] = types.ResourceMap{}
for id, pieces := range resourceMap[0] {
result[i][id] = pieces
}
}

// to pass tests due to new algorithm returns unsorted list
Expand Down Expand Up @@ -259,16 +263,19 @@ func (h *host) getFullResult(full int, resources []resourceInfo) []types.Resourc

func (h *host) distributeOneRation(ration float64, maxShare int) []types.ResourceMap {
ration *= float64(h.share)
fullRequire := int64(ration) / int64(h.share)
fragmentRequire := int64(ration) % int64(h.share)
fullRequire := types.RoundToInt(ration) / int64(h.share)
fragmentRequire := types.RoundToInt(ration) % int64(h.share)

if fullRequire == 0 {
if maxShare == -1 {
if maxShare < 0 {
// 这个时候就把所有的资源都当成碎片
maxShare = len(h.full) + len(h.fragment)
}
maxShare = utils.Min(maxShare, len(h.full)+len(h.fragment))
diff := maxShare - len(h.fragment)
h.fragment = append(h.fragment, h.full[:diff]...)
if diff > 0 {
h.fragment = append(h.fragment, h.full[:diff]...)
}

return h.getFragmentResult(fragmentRequire, h.fragment)
}
Expand Down
5 changes: 5 additions & 0 deletions types/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ import "math"
func Round(f float64) float64 {
return math.Round(f*1000000000) / 1000000000
}

// RoundToInt for float64 to int
func RoundToInt(f float64) int64 {
return int64(math.Round(f))
}
9 changes: 9 additions & 0 deletions types/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ func TestRound(t *testing.T) {
a = 19.99998
assert.InDelta(t, (Round(a)), 19.99998, 1e-6)
}

func TestRoundToInt(t *testing.T) {
a := 0.0199999998
assert.EqualValues(t, RoundToInt(a), 0)
a = 0.1999998
assert.EqualValues(t, RoundToInt(a), 0)
a = 1.999998
assert.EqualValues(t, RoundToInt(a), 2)
}

0 comments on commit 2091d84

Please sign in to comment.