From 2ca8d1fb10e926b76eba0b90adedf52dbac7a432 Mon Sep 17 00:00:00 2001 From: CMGS Date: Wed, 1 Nov 2017 12:27:49 +0800 Subject: [PATCH] refactor favor to scheduler --- cluster/calcium/create_container.go | 9 +++++---- cluster/calcium/realloc.go | 5 +++-- scheduler/scheduler.go | 7 +++++++ store/etcd/pod.go | 5 +++-- types/pod.go | 5 ----- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/cluster/calcium/create_container.go b/cluster/calcium/create_container.go index ead57ac2a..a6131deff 100644 --- a/cluster/calcium/create_container.go +++ b/cluster/calcium/create_container.go @@ -13,6 +13,7 @@ import ( enginenetwork "github.com/docker/docker/api/types/network" engineslice "github.com/docker/docker/api/types/strslice" "github.com/docker/go-units" + "github.com/projecteru2/core/scheduler" "github.com/projecteru2/core/stats" "github.com/projecteru2/core/types" "github.com/projecteru2/core/utils" @@ -32,7 +33,7 @@ func (c *calcium) CreateContainer(opts *types.DeployOptions) (chan *types.Create log.Errorf("[CreateContainer] Error during GetPod for %s: %v", opts.Podname, err) return nil, err } - if pod.Favor == types.CPU_PRIOR { + if pod.Favor == scheduler.CPU_PRIOR { return c.createContainerWithCPUPrior(opts) } log.Infof("[CreateContainer] Creating container with options: %v", opts) @@ -97,7 +98,7 @@ func (c *calcium) doCreateContainerWithMemoryPrior(nodeInfo types.NodeInfo, opts for i := 0; i < nodeInfo.Deploy; i++ { // createAndStartContainer will auto cleanup - ms[i] = c.createAndStartContainer(i+index, node, opts, nil, types.MEMORY_PRIOR) + ms[i] = c.createAndStartContainer(i+index, node, opts, nil, scheduler.MEMORY_PRIOR) if !ms[i].Success { log.Errorf("[doCreateContainerWithMemoryPrior] Error when create and start a container, %v", ms[i].Error) continue @@ -162,7 +163,7 @@ func (c *calcium) doCreateContainerWithCPUPrior(nodeName string, cpuMap []types. for i, quota := range cpuMap { // createAndStartContainer will auto cleanup - ms[i] = c.createAndStartContainer(i+index, node, opts, quota, types.CPU_PRIOR) + ms[i] = c.createAndStartContainer(i+index, node, opts, quota, scheduler.CPU_PRIOR) if !ms[i].Success { log.Errorf("[doCreateContainerWithCPUPrior] Error when create and start a container, %v", ms[i].Error) continue @@ -291,7 +292,7 @@ func (c *calcium) makeContainerOptions(index int, quota types.CPUMap, opts *type } var resource enginecontainer.Resources - if favor == types.CPU_PRIOR { + if favor == scheduler.CPU_PRIOR { resource = c.makeCPUPriorSetting(quota) } else { resource = c.makeMemoryPriorSetting(opts.Memory, opts.CPUQuota) diff --git a/cluster/calcium/realloc.go b/cluster/calcium/realloc.go index db1dab3a7..2cfa117e7 100644 --- a/cluster/calcium/realloc.go +++ b/cluster/calcium/realloc.go @@ -7,6 +7,7 @@ import ( log "github.com/Sirupsen/logrus" enginecontainer "github.com/docker/docker/api/types/container" + "github.com/projecteru2/core/scheduler" "github.com/projecteru2/core/types" "github.com/projecteru2/core/utils" ) @@ -52,7 +53,7 @@ func (c *calcium) ReallocResource(ids []string, cpu float64, mem int64) (chan *t } containersInfo[pod][node] = append(containersInfo[pod][node], container) - if pod.Favor == types.CPU_PRIOR { + if pod.Favor == scheduler.CPU_PRIOR { if _, ok := cpuContainersInfo[pod]; !ok { cpuContainersInfo[pod] = CPUNodeContainers{} } @@ -75,7 +76,7 @@ func (c *calcium) ReallocResource(ids []string, cpu float64, mem int64) (chan *t wg := sync.WaitGroup{} wg.Add(len(containersInfo)) for pod, nodeContainers := range containersInfo { - if pod.Favor == types.CPU_PRIOR { + if pod.Favor == scheduler.CPU_PRIOR { nodeCPUContainersInfo := cpuContainersInfo[pod] go func(pod *types.Pod, nodeCPUContainersInfo CPUNodeContainers) { defer wg.Done() diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index 762b85e89..bf94f9d03 100644 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -2,6 +2,13 @@ package scheduler import "github.com/projecteru2/core/types" +const ( + //CPU_PRIOR define cpu select + CPU_PRIOR = "CPU" + //MEMORY_PRIOR define mem select + MEMORY_PRIOR = "MEM" +) + // A scheduler is used to determine which nodes are we gonna use. // `types.CPUMap` represents the CPU label and remaining quota. // `nodes` represents node name and the corresponding CPUMap. diff --git a/store/etcd/pod.go b/store/etcd/pod.go index f6c94d28f..35c07dfe2 100644 --- a/store/etcd/pod.go +++ b/store/etcd/pod.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/coreos/etcd/client" + "github.com/projecteru2/core/scheduler" "github.com/projecteru2/core/types" "github.com/projecteru2/core/utils" ) @@ -38,8 +39,8 @@ func (k *krypton) AddPod(name, favor, desc string) (*types.Pod, error) { key := fmt.Sprintf(podInfoKey, name) favor = strings.ToUpper(favor) if favor == "" { - favor = types.MEMORY_PRIOR - } else if favor != types.MEMORY_PRIOR && favor != types.CPU_PRIOR { + favor = scheduler.MEMORY_PRIOR + } else if favor != scheduler.MEMORY_PRIOR && favor != scheduler.CPU_PRIOR { return nil, fmt.Errorf("favor should be either CPU or MEM, got %s", favor) } pod := &types.Pod{Name: name, Desc: desc, Favor: favor} diff --git a/types/pod.go b/types/pod.go index 8e818e4ec..be3f904b7 100644 --- a/types/pod.go +++ b/types/pod.go @@ -1,10 +1,5 @@ package types -const ( - CPU_PRIOR = "CPU" - MEMORY_PRIOR = "MEM" -) - type Pod struct { Name string `json:"name"` Desc string `json:"desc"`