Skip to content

Commit 8c3fc63

Browse files
add strategy method: FILLGLOBAL (#305)
* add strategy method: FILLGLOBAL * pass test
1 parent cf31615 commit 8c3fc63

File tree

5 files changed

+98
-20
lines changed

5 files changed

+98
-20
lines changed

rpc/gen/core.pb.go

+23-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/gen/core.proto

+1
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ message DeployOptions {
408408
FILL = 1;
409409
EACH = 2;
410410
GLOBAL = 3;
411+
FILLGLOBAL = 4;
411412
DUMMY = 99;
412413
}
413414
string name = 1;

strategy/fill_global.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package strategy
2+
3+
import (
4+
"github.com/projecteru2/core/log"
5+
"github.com/projecteru2/core/types"
6+
)
7+
8+
// FillGlobalPlan try fill strategy and fallback to global strategy
9+
func FillGlobalPlan(strategyInfo []Info, need, total, limit int, resourceType types.ResourceType) (map[string]int, error) {
10+
originStrategyInfo := make([]Info, len(strategyInfo))
11+
copy(originStrategyInfo, strategyInfo)
12+
13+
deployMap, err := FillPlan(strategyInfo, need, total, limit, resourceType)
14+
if err == nil {
15+
return deployMap, nil
16+
}
17+
log.Infof("[FillGlobalPlan] fill plan failed, try global fill: %+v", err)
18+
return GlobalPlan(originStrategyInfo, need, total, limit, resourceType)
19+
}

strategy/fillglobal_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package strategy
2+
3+
import (
4+
"testing"
5+
6+
"github.com/projecteru2/core/types"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestFillGlobalPlan(t *testing.T) {
11+
// can fill
12+
strategyInfos := []Info{
13+
{
14+
Nodename: "n1",
15+
Capacity: 10,
16+
Count: 1,
17+
},
18+
{
19+
Nodename: "n2",
20+
Capacity: 10,
21+
Count: 0,
22+
},
23+
}
24+
deployMap, err := FillPlan(strategyInfos, 1, 2, 1, types.ResourceAll)
25+
assert.Nil(t, err)
26+
assert.EqualValues(t, 0, deployMap["n1"])
27+
assert.EqualValues(t, 1, deployMap["n2"])
28+
deployMap, err = FillGlobalPlan(strategyInfos, 1, 2, 1, types.ResourceAll)
29+
assert.Nil(t, err)
30+
assert.EqualValues(t, 0, deployMap["n1"])
31+
assert.EqualValues(t, 1, deployMap["n2"])
32+
33+
// can't fill
34+
strategyInfos = []Info{
35+
{
36+
Nodename: "n1",
37+
Capacity: 10,
38+
Count: 1,
39+
Usages: map[types.ResourceType]float64{types.ResourceAll: 0.1},
40+
Rates: map[types.ResourceType]float64{types.ResourceAll: 0.1},
41+
},
42+
}
43+
_, err = FillPlan(strategyInfos, 1, 2, 1, types.ResourceAll)
44+
assert.EqualError(t, err, "Cannot alloc a fill node plan, each node has enough workloads")
45+
deployMap, err = FillGlobalPlan(strategyInfos, 1, 2, 1, types.ResourceAll)
46+
assert.Nil(t, err)
47+
assert.EqualValues(t, 1, deployMap["n1"])
48+
}

strategy/strategy.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@ const (
1818
Each = "EACH"
1919
// Global .
2020
Global = "GLOBAL"
21+
// FillGlobal .
22+
FillGlobal = "FILLGLOBAL"
2123
// Dummy for calculate capacity
2224
Dummy = "DUMMY"
2325
)
2426

2527
var Plans = map[string]startegyFunc{
26-
Auto: CommunismPlan,
27-
Fill: FillPlan,
28-
Each: AveragePlan,
29-
Global: GlobalPlan,
28+
Auto: CommunismPlan,
29+
Fill: FillPlan,
30+
Each: AveragePlan,
31+
Global: GlobalPlan,
32+
FillGlobal: FillGlobalPlan,
3033
}
3134

3235
type startegyFunc = func(_ []Info, need, total, limit int, _ types.ResourceType) (map[string]int, error)

0 commit comments

Comments
 (0)