Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Jul 13, 2017
1 parent 03c6419 commit 8d27436
Show file tree
Hide file tree
Showing 18 changed files with 845 additions and 571 deletions.
167 changes: 86 additions & 81 deletions cluster/calcium/create_container.go

Large diffs are not rendered by default.

8 changes: 0 additions & 8 deletions cluster/calcium/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ func makeCPUAndMem(nodes []*types.Node) map[string]types.CPUAndMem {
return r
}

func makeCPUMap(nodes map[string]types.CPUAndMem) map[string]types.CPUMap {
r := make(map[string]types.CPUMap)
for key, node := range nodes {
r[key] = node.CpuMap
}
return r
}

// filter nodes
// public is the flag
func filterNodes(nodes []*types.Node, public bool) []*types.Node {
Expand Down
50 changes: 28 additions & 22 deletions cluster/calcium/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"gitlab.ricebook.net/platform/core/utils"
)

func (c *calcium) AllocMemoryPodResource(opts *types.DeployOptions) (map[string]int, error) {
func (c *calcium) allocMemoryPodResource(opts *types.DeployOptions) ([]types.NodeInfo, error) {
lock, err := c.Lock(opts.Podname, 30)
if err != nil {
return nil, err
Expand All @@ -22,36 +22,37 @@ func (c *calcium) AllocMemoryPodResource(opts *types.DeployOptions) (map[string]
if err != nil {
return nil, err
}

nodesInfo := getNodesInfo(cpuandmem)
log.Debugf("Input opts.CPUQuota: %f", opts.CPUQuota)
cpuQuota := int(opts.CPUQuota * float64(utils.CpuPeriodBase))
log.Debugf("Tranfered cpuQuota: %d", cpuQuota)

// Load deploy status
nodesInfo, err = c.store.MakeDeployStatus(opts, nodesInfo)
if err != nil {
return nil, err
}

plan, err := c.scheduler.SelectMemoryNodes(nodesInfo, cpuQuota, opts.Memory, opts.Count) // 还是以 Bytes 作单位, 不转换了
cpuRate := int64(opts.CPUQuota * float64(utils.CpuPeriodBase))
log.Debugf("Input opts.CPUQuota: %f, equal CPURate %d", opts.CPUQuota, cpuRate)
sort.Slice(nodesInfo, func(i, j int) bool { return nodesInfo[i].MemCap < nodesInfo[j].MemCap })
nodesInfo, err = c.scheduler.SelectMemoryNodes(nodesInfo, cpuRate, opts.Memory, opts.Count) // 还是以 Bytes 作单位, 不转换了
if err != nil {
return nil, err
}

// 并发扣除所需资源
wg := sync.WaitGroup{}
wg.Add(len(plan))
for nodename, connum := range plan {
go func(nodename string, connum int) {
wg.Done()
memoryTotal := opts.Memory * int64(connum)
c.store.UpdateNodeMem(opts.Podname, nodename, memoryTotal, "-")
}(nodename, connum)
wg.Add(len(nodesInfo))
for _, nodeInfo := range nodesInfo {
go func(nodeInfo types.NodeInfo) {
defer wg.Done()
memoryTotal := opts.Memory * nodeInfo.Deploy
c.store.UpdateNodeMem(opts.Podname, nodeInfo.Name, memoryTotal, "-")
}(nodeInfo)
}
wg.Wait()
return plan, nil
return nodesInfo, nil
}

func (c *calcium) AllocCPUPodResource(opts *types.DeployOptions) (map[string][]types.CPUMap, error) {
func (c *calcium) allocCPUPodResource(opts *types.DeployOptions) (map[string][]types.CPUMap, error) {
lock, err := c.Lock(opts.Podname, 30)
if err != nil {
return nil, err
Expand All @@ -62,10 +63,15 @@ func (c *calcium) AllocCPUPodResource(opts *types.DeployOptions) (map[string][]t
if err != nil {
return nil, err
}
nodesInfo := getNodesInfo(cpuandmem)

// Load deploy status
nodesInfo, err = c.store.MakeDeployStatus(opts, nodesInfo)
if err != nil {
return nil, err
}

cpumap := makeCPUMap(cpuandmem) // 做这个转换,免得改太多
log.Debugf("Cpumap: %v", cpumap)
result, changed, err := c.scheduler.SelectCPUNodes(cpumap, opts.CPUQuota, opts.Count)
result, changed, err := c.scheduler.SelectCPUNodes(nodesInfo, opts.CPUQuota, opts.Count)
log.Debugf("Result: %v, Changed: %v", result, changed)
if err != nil {
return result, err
Expand Down Expand Up @@ -123,11 +129,11 @@ func (c *calcium) getCPUAndMem(podname, nodename string, quota float64) (map[str
return result, nodes, nil
}

func getNodesInfo(cpumemmap map[string]types.CPUAndMem) []types.NodeInfo {
func getNodesInfo(cpuAndMemData map[string]types.CPUAndMem) []types.NodeInfo {
result := []types.NodeInfo{}
for node, cpuandmem := range cpumemmap {
result = append(result, types.NodeInfo{node, len(cpuandmem.CpuMap) * utils.CpuPeriodBase, cpuandmem.MemCap, 0, 0, 0})
for nodeName, cpuAndMem := range cpuAndMemData {
cpuRate := int64(len(cpuAndMem.CpuMap)) * utils.CpuPeriodBase
result = append(result, types.NodeInfo{cpuAndMem, nodeName, cpuRate, 0, 0, 0})
}
sort.Slice(result, func(i, j int) bool { return result[i].Memory < result[j].Memory })
return result
}
4 changes: 2 additions & 2 deletions rpc/gen/core.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rpc/gen/core.proto
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ message DeployOptions {
string entrypoint = 6;
string extra_args = 7;
double cpu_quota = 8;
int32 count = 9;
int64 count = 9;
int64 memory = 10;
repeated string env = 11;
map<string, string> networks = 12;
Expand Down
2 changes: 1 addition & 1 deletion rpc/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func toCoreDeployOptions(d *pb.DeployOptions) *types.DeployOptions {
Entrypoint: d.Entrypoint,
ExtraArgs: d.ExtraArgs,
CPUQuota: d.CpuQuota,
Count: int(d.Count),
Count: d.Count,
Memory: d.Memory,
Env: d.Env,
Networks: d.Networks,
Expand Down
12 changes: 9 additions & 3 deletions scheduler/complex/mem.go → scheduler/complex/communism.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package complexscheduler

import (
"sort"

"gitlab.ricebook.net/platform/core/types"
)

func equalDivisionPlan(arg []types.NodeInfo, need, volTotal int) ([]types.NodeInfo, error) {
// 吃我一记共产主义大锅饭
func CommunismDivisionPlan(arg []types.NodeInfo, need, volTotal int64) ([]types.NodeInfo, error) {
sort.Slice(arg, func(i, j int) bool { return arg[i].Count < arg[j].Count })
length := len(arg)
i := 0

var deploy, differ int64
for need > 0 && volTotal > 0 {
p := i
deploy := 0
differ := 1
deploy = 0
differ = 1
if i < length-1 {
differ = arg[i+1].Count - arg[i].Count
i++
Expand Down
Loading

0 comments on commit 8d27436

Please sign in to comment.