forked from docker-archive/classicswarm
/
weighted_node.go
69 lines (54 loc) · 1.47 KB
/
weighted_node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package strategy
import (
"github.com/docker/swarm/cluster"
"github.com/docker/swarm/scheduler/node"
)
// WeightedNode represents a node in the cluster with a given weight, typically used for sorting
// purposes.
type weightedNode struct {
Node *node.Node
// Weight is the inherent value of this node.
Weight int64
}
type weightedNodeList []*weightedNode
func (n weightedNodeList) Len() int {
return len(n)
}
func (n weightedNodeList) Swap(i, j int) {
n[i], n[j] = n[j], n[i]
}
func (n weightedNodeList) Less(i, j int) bool {
var (
ip = n[i]
jp = n[j]
)
return ip.Weight < jp.Weight
}
func weighNodes(config *cluster.ContainerConfig, nodes []*node.Node) (weightedNodeList, error) {
weightedNodes := weightedNodeList{}
for _, node := range nodes {
nodeMemory := node.TotalMemory
nodeCpus := node.TotalCpus
// Skip nodes that are smaller than the requested resources.
if nodeMemory < int64(config.Memory) || nodeCpus < config.CpuShares {
continue
}
var (
cpuScore int64 = 100
memoryScore int64 = 100
)
if config.CpuShares > 0 {
cpuScore = (node.UsedCpus + config.CpuShares) * 100 / nodeCpus
}
if config.Memory > 0 {
memoryScore = (node.UsedMemory + config.Memory) * 100 / nodeMemory
}
if cpuScore <= 100 && memoryScore <= 100 {
weightedNodes = append(weightedNodes, &weightedNode{Node: node, Weight: cpuScore + memoryScore})
}
}
if len(weightedNodes) == 0 {
return nil, ErrNoResourcesAvailable
}
return weightedNodes, nil
}