mirror of https://github.com/docker/docs.git
Merge pull request #398 from bacongobbler/refactor-scores-to-weighted-node
refactor score to WeightedNode structure
This commit is contained in:
commit
4087e022d6
|
|
@ -19,7 +19,7 @@ func (p *BinPackingPlacementStrategy) Initialize() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BinPackingPlacementStrategy) PlaceContainer(config *dockerclient.ContainerConfig, nodes []cluster.Node) (cluster.Node, error) {
|
func (p *BinPackingPlacementStrategy) PlaceContainer(config *dockerclient.ContainerConfig, nodes []cluster.Node) (cluster.Node, error) {
|
||||||
scores := scores{}
|
weightedNodes := weightedNodeList{}
|
||||||
|
|
||||||
for _, node := range nodes {
|
for _, node := range nodes {
|
||||||
nodeMemory := node.TotalMemory()
|
nodeMemory := node.TotalMemory()
|
||||||
|
|
@ -43,39 +43,16 @@ func (p *BinPackingPlacementStrategy) PlaceContainer(config *dockerclient.Contai
|
||||||
}
|
}
|
||||||
|
|
||||||
if cpuScore <= 100 && memoryScore <= 100 {
|
if cpuScore <= 100 && memoryScore <= 100 {
|
||||||
scores = append(scores, &score{node: node, score: cpuScore + memoryScore})
|
weightedNodes = append(weightedNodes, &weightedNode{Node: node, Weight: cpuScore + memoryScore})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(scores) == 0 {
|
if len(weightedNodes) == 0 {
|
||||||
return nil, ErrNoResourcesAvailable
|
return nil, ErrNoResourcesAvailable
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Sort(scores)
|
// sort by highest weight
|
||||||
|
sort.Sort(sort.Reverse(weightedNodes))
|
||||||
|
|
||||||
return scores[0].node, nil
|
return weightedNodes[0].Node, nil
|
||||||
}
|
|
||||||
|
|
||||||
type score struct {
|
|
||||||
node cluster.Node
|
|
||||||
score int64
|
|
||||||
}
|
|
||||||
|
|
||||||
type scores []*score
|
|
||||||
|
|
||||||
func (s scores) Len() int {
|
|
||||||
return len(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s scores) Swap(i, j int) {
|
|
||||||
s[i], s[j] = s[j], s[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s scores) Less(i, j int) bool {
|
|
||||||
var (
|
|
||||||
ip = s[i]
|
|
||||||
jp = s[j]
|
|
||||||
)
|
|
||||||
|
|
||||||
return ip.score > jp.score
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
package strategy
|
||||||
|
|
||||||
|
import "github.com/docker/swarm/cluster"
|
||||||
|
|
||||||
|
// WeightedNode represents a node in the cluster with a given weight, typically used for sorting
|
||||||
|
// purposes.
|
||||||
|
type weightedNode struct {
|
||||||
|
Node cluster.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
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue