Skip to content

Commit

Permalink
refactor memory scheduler by sort
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Jul 9, 2018
1 parent 1fa0f42 commit c1d601f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
2 changes: 0 additions & 2 deletions cluster/calcium/resource.go
Expand Up @@ -3,7 +3,6 @@ package calcium
import (
"context"
"fmt"
"sort"
"sync"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -43,7 +42,6 @@ func (c *Calcium) allocMemoryPodResource(ctx context.Context, opts *types.Deploy

cpuRate := int64(opts.CPUQuota * float64(CpuPeriodBase))
log.Debugf("[allocMemoryPodResource] 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
Expand Down
36 changes: 18 additions & 18 deletions scheduler/complex/potassium.go
Expand Up @@ -28,40 +28,40 @@ func (m *potassium) SelectMemoryNodes(nodesInfo []types.NodeInfo, rate, memory i
return nil, fmt.Errorf("memory must positive")
}

p := -1
for i, nodeInfo := range nodesInfo {
if nodeInfo.CPURate >= rate {
p = i
break
}
}
if p == -1 {
nodesInfoLength := len(nodesInfo)

// 筛选出能满足 CPU 需求的
sort.Slice(nodesInfo, func(i, j int) bool { return nodesInfo[i].CPURate < nodesInfo[j].CPURate })
p := sort.Search(nodesInfoLength, func(i int) bool { return nodesInfo[i].CPURate >= rate })
// p 最大也就是 nodesInfoLength - 1
if p == nodesInfoLength {
return nil, fmt.Errorf("Cannot alloc a plan, not enough cpu rate")
}
log.Debugf("[SelectMemoryNodes] The %d th node has enough cpu rate", p)
nodesInfoLength -= p
nodesInfo = nodesInfo[p:]
log.Debugf("[SelectMemoryNodes] %d nodes has enough cpu rate", nodesInfoLength)

// 计算是否有足够的内存满足需求
sort.Slice(nodesInfo, func(i, j int) bool { return nodesInfo[i].MemCap < nodesInfo[j].MemCap })
p = sort.Search(nodesInfoLength, func(i int) bool { return nodesInfo[i].MemCap >= memory })
if p == nodesInfoLength {
return nil, fmt.Errorf("Cannot alloc a plan, not enough memory")
}
nodesInfoLength -= p
nodesInfo = nodesInfo[p:]
log.Debugf("[SelectMemoryNodes] %d nodes has enough memory", nodesInfoLength)

volTotal := 0
capacity := -1
p = -1
for i, nodeInfo := range nodesInfo {
capacity = int(nodeInfo.MemCap / memory)
if capacity > 0 {
volTotal += capacity
nodesInfo[i].Capacity = capacity
if p == -1 {
p = i
}
}
}

if p < 0 {
return nil, fmt.Errorf("No nodes provide enough memory")
}

// 继续裁可用节点池子
nodesInfo = nodesInfo[p:]
log.Debugf("[SelectMemoryNodes] Node info: %v", nodesInfo)
nodesInfo, err := CommunismDivisionPlan(nodesInfo, need, volTotal)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion scheduler/complex/potassium_test.go
Expand Up @@ -641,7 +641,7 @@ func TestTestSelectMemoryNodesNotEnough(t *testing.T) {
_, err = k.SelectMemoryNodes(pod, 1e9, 5*1024*1024*1024, 1)
assert.Error(t, err)
if err != nil {
assert.Equal(t, err.Error(), "No nodes provide enough memory")
assert.Equal(t, err.Error(), "Cannot alloc a plan, not enough memory")
}

// 2 nodes [cpu not enough]
Expand Down

0 comments on commit c1d601f

Please sign in to comment.