Skip to content
This repository has been archived by the owner on Feb 1, 2021. It is now read-only.

refactor score to WeightedNode structure #398

Merged
merged 1 commit into from
Feb 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 6 additions & 29 deletions scheduler/strategy/binpacking.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
30 changes: 30 additions & 0 deletions scheduler/strategy/weighted_node.go
Original file line number Diff line number Diff line change
@@ -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
}