Skip to content

Commit 2091d84

Browse files
authored
[SPCLD-6491] fix bugs of cpu realloc algorithm (#579)
1 parent 91cd77c commit 2091d84

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

scheduler/complex/potassium.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func (m *Potassium) ReselectCPUNodes(ctx context.Context, scheduleInfo resourcet
197197

198198
func cpuReallocPlan(scheduleInfo resourcetypes.ScheduleInfo, quota float64, CPU types.CPUMap, sharebase int64) (resourcetypes.ScheduleInfo, float64, types.CPUMap) {
199199
affinityPlan := make(types.CPUMap)
200-
diff := int64(quota*float64(sharebase)) - CPU.Total()
200+
diff := types.RoundToInt(quota*float64(sharebase)) - CPU.Total()
201201
// sort by pieces
202202
cpuIDs := []string{}
203203
for cpuID := range CPU {
@@ -226,7 +226,7 @@ func cpuReallocPlan(scheduleInfo resourcetypes.ScheduleInfo, quota float64, CPU
226226
}
227227

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

244244
// fragments, try to find complement
245245
if available := scheduleInfo.CPU[cpuID]; available == sharebase-CPU[cpuID] {
246-
expand := utils.Min64(available, needPieces)
246+
expand := utils.Min64(available, needPieces-CPU[cpuID])
247247
affinityPlan[cpuID] = CPU[cpuID] + expand
248248
scheduleInfo.CPU[cpuID] -= expand
249-
needPieces -= sharebase
249+
needPieces -= affinityPlan[cpuID]
250250
continue
251251
}
252252

scheduler/complex/resource.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"sort"
66

77
"github.com/projecteru2/core/types"
8+
"github.com/projecteru2/core/utils"
89
)
910

1011
type resourceInfo struct {
@@ -121,7 +122,10 @@ func (h *host) getFragmentResult(fragment int64, resources []resourceInfo) []typ
121122
resourceMaps := h.getFragmentsResult(resources, fragment)
122123
result := make([]types.ResourceMap, len(resourceMaps))
123124
for i, resourceMap := range resourceMaps {
124-
result[i] = resourceMap[0]
125+
result[i] = types.ResourceMap{}
126+
for id, pieces := range resourceMap[0] {
127+
result[i][id] = pieces
128+
}
125129
}
126130

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

260264
func (h *host) distributeOneRation(ration float64, maxShare int) []types.ResourceMap {
261265
ration *= float64(h.share)
262-
fullRequire := int64(ration) / int64(h.share)
263-
fragmentRequire := int64(ration) % int64(h.share)
266+
fullRequire := types.RoundToInt(ration) / int64(h.share)
267+
fragmentRequire := types.RoundToInt(ration) % int64(h.share)
264268

265269
if fullRequire == 0 {
266-
if maxShare == -1 {
270+
if maxShare < 0 {
267271
// 这个时候就把所有的资源都当成碎片
268272
maxShare = len(h.full) + len(h.fragment)
269273
}
274+
maxShare = utils.Min(maxShare, len(h.full)+len(h.fragment))
270275
diff := maxShare - len(h.fragment)
271-
h.fragment = append(h.fragment, h.full[:diff]...)
276+
if diff > 0 {
277+
h.fragment = append(h.fragment, h.full[:diff]...)
278+
}
272279

273280
return h.getFragmentResult(fragmentRequire, h.fragment)
274281
}

types/helper.go

+5
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ import "math"
66
func Round(f float64) float64 {
77
return math.Round(f*1000000000) / 1000000000
88
}
9+
10+
// RoundToInt for float64 to int
11+
func RoundToInt(f float64) int64 {
12+
return int64(math.Round(f))
13+
}

types/helper_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,12 @@ func TestRound(t *testing.T) {
1616
a = 19.99998
1717
assert.InDelta(t, (Round(a)), 19.99998, 1e-6)
1818
}
19+
20+
func TestRoundToInt(t *testing.T) {
21+
a := 0.0199999998
22+
assert.EqualValues(t, RoundToInt(a), 0)
23+
a = 0.1999998
24+
assert.EqualValues(t, RoundToInt(a), 0)
25+
a = 1.999998
26+
assert.EqualValues(t, RoundToInt(a), 2)
27+
}

0 commit comments

Comments
 (0)