Merge pull request #398 from bacongobbler/refactor-scores-to-weighted-node

refactor score to WeightedNode structure
This commit is contained in:
Andrea Luzzardi 2015-02-27 16:47:26 -08:00
commit 4087e022d6
2 changed files with 36 additions and 29 deletions

View File

@ -19,7 +19,7 @@ func (p *BinPackingPlacementStrategy) Initialize() error {
}
func (p *BinPackingPlacementStrategy) PlaceContainer(config *dockerclient.ContainerConfig, nodes []cluster.Node) (cluster.Node, error) {
scores := scores{}
weightedNodes := weightedNodeList{}
for _, node := range nodes {
nodeMemory := node.TotalMemory()
@ -43,39 +43,16 @@ func (p *BinPackingPlacementStrategy) PlaceContainer(config *dockerclient.Contai
}
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
}
sort.Sort(scores)
// sort by highest weight
sort.Sort(sort.Reverse(weightedNodes))
return scores[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
return weightedNodes[0].Node, nil
}

View File

@ -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
}