mirror of https://github.com/docker/docs.git
refactor score to weightedNode structure
In many different scheduling strategies, a node is typically given a certain weight based upon its values that are important to the overall strategy. Exposing a weightedNode structure as well as a new weightedNodeList type allows one to operate upon a set of nodes for sorting purposes by assigning weights to each node and calling sort.Sort() on a weightedNodeList. Signed-off-by: Matthew Fisher <matthewf@opdemand.com>
This commit is contained in:
parent
db97473b40
commit
08b0ddbd14
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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