From 08b0ddbd14b1be5db5e4f2a0be2b468537653c18 Mon Sep 17 00:00:00 2001 From: Matthew Fisher Date: Fri, 13 Feb 2015 03:57:26 -0700 Subject: [PATCH] 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 --- scheduler/strategy/binpacking.go | 35 +++++------------------------ scheduler/strategy/weighted_node.go | 30 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 29 deletions(-) create mode 100644 scheduler/strategy/weighted_node.go diff --git a/scheduler/strategy/binpacking.go b/scheduler/strategy/binpacking.go index d3e109c1b4..d22028fb32 100644 --- a/scheduler/strategy/binpacking.go +++ b/scheduler/strategy/binpacking.go @@ -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 } diff --git a/scheduler/strategy/weighted_node.go b/scheduler/strategy/weighted_node.go new file mode 100644 index 0000000000..74abec165d --- /dev/null +++ b/scheduler/strategy/weighted_node.go @@ -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 +}