Skip to content

Commit

Permalink
revise auto distributor
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Mar 8, 2019
1 parent b5f0af0 commit f5789fe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
41 changes: 21 additions & 20 deletions scheduler/complex/communism.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,32 @@ import (
func CommunismDivisionPlan(arg []types.NodeInfo, need int) ([]types.NodeInfo, error) {
sort.Slice(arg, func(i, j int) bool { return arg[i].Count < arg[j].Count })
length := len(arg)
i := 0

for need > 0 {
p := i
deploy := 0
differ := 1
for i := 0; i < length; i++ {
if need <= 0 {
break
}
req := need
if i < length-1 {
differ = arg[i+1].Count - arg[i].Count
i++
req = (arg[i+1].Count - arg[i].Count) * (i + 1)
}
for j := 0; j <= p && need > 0 && differ > 0; j++ {
// 减枝
if arg[j].Capacity == 0 {
continue
}
deploy = differ
if deploy > arg[j].Capacity {
deploy = arg[j].Capacity
if req > need {
req = need
}
for j := 0; j < i+1; j++ {
deploy := req / (i + 1 - j)
tail := req % (i + 1 - j)
d := deploy
if tail > 0 {
d++
}
if deploy > need {
deploy = need
if d > arg[j].Capacity {
d = arg[j].Capacity
}
arg[j].Deploy += deploy
arg[j].Capacity -= deploy
need -= deploy
arg[j].Deploy += d
arg[j].Capacity -= d
need -= d
req -= d
}
}
// 这里 need 一定会为 0 出来,因为 volTotal 保证了一定大于 need
Expand Down
16 changes: 15 additions & 1 deletion scheduler/complex/communism_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,26 @@ func TestCommunismDivisionPlan(t *testing.T) {
nodes = deployedNodes()
r, err = CommunismDivisionPlan(nodes, 3)
assert.NoError(t, err)
assert.Equal(t, r[0].Deploy, 3)
assert.Equal(t, r[0].Deploy, 2)
assert.Equal(t, r[1].Deploy, 1)
nodes = deployedNodes()
r, err = CommunismDivisionPlan(nodes, 4)
assert.NoError(t, err)
assert.Equal(t, r[0].Deploy, 3)
assert.Equal(t, r[1].Deploy, 1)
nodes = deployedNodes()
r, err = CommunismDivisionPlan(nodes, 29)
assert.NoError(t, err)
assert.Equal(t, r[0].Deploy, 10)
assert.Equal(t, r[1].Deploy, 9)
assert.Equal(t, r[2].Deploy, 6)
assert.Equal(t, r[3].Deploy, 4)
nodes = deployedNodes()
r, err = CommunismDivisionPlan(nodes, 37)
assert.NoError(t, err)
nodes = deployedNodes()
r, err = CommunismDivisionPlan(nodes, 40)
assert.NoError(t, err)
}

func randomDeployStatus(nodesInfo []types.NodeInfo, maxDeployed int) []types.NodeInfo {
Expand Down

0 comments on commit f5789fe

Please sign in to comment.