From d8b609c16117b6db5bbbc6a1a79fcbae20a8bf72 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Thu, 8 Jan 2015 20:13:11 +0000 Subject: [PATCH] Move overcommit outside of binpacking and use 105 instead of 0.05 Signed-off-by: Victor Vieux --- flags.go | 7 ++++++- main.go | 2 +- manage.go | 2 +- scheduler/strategy/binpacking.go | 14 ++++++-------- scheduler/strategy/binpacking_test.go | 2 +- scheduler/strategy/random.go | 2 +- scheduler/strategy/strategy.go | 18 ++++-------------- 7 files changed, 20 insertions(+), 27 deletions(-) diff --git a/flags.go b/flags.go index 21e068ea00..ca6d916c26 100644 --- a/flags.go +++ b/flags.go @@ -50,10 +50,15 @@ var ( Name: "tlsverify", Usage: "use TLS and verify the remote", } + flOverCommit = cli.IntFlag{ + Name: "overcommit, oc", + Usage: "overcommit to apply on resources", + Value: 105, + } flStrategy = cli.StringFlag{ Name: "strategy", Usage: "placement strategy to use [binpacking, random]", - Value: "binpacking:0.05", + Value: "binpacking", } flFilter = cli.StringSliceFlag{ Name: "filter, f", diff --git a/main.go b/main.go index 5b40d44721..e91072ad27 100644 --- a/main.go +++ b/main.go @@ -87,7 +87,7 @@ func main() { Flags: []cli.Flag{ flDiscovery, flStrategy, flFilter, - flHosts, flHeartBeat, + flHosts, flHeartBeat, flOverCommit, flTls, flTlsCaCert, flTlsCert, flTlsKey, flTlsVerify, flEnableCors}, Action: manage, diff --git a/manage.go b/manage.go index 63395cebe4..e725b96a3b 100644 --- a/manage.go +++ b/manage.go @@ -78,7 +78,7 @@ func manage(c *cli.Context) { log.Fatal("--discovery required to manage a cluster") } - s, err := strategy.New(c.String("strategy")) + s, err := strategy.New(c.String("strategy"), int64(c.Int("overcommit"))) if err != nil { log.Fatal(err) } diff --git a/scheduler/strategy/binpacking.go b/scheduler/strategy/binpacking.go index ccd38ceb2c..3cfa2175ef 100644 --- a/scheduler/strategy/binpacking.go +++ b/scheduler/strategy/binpacking.go @@ -3,7 +3,6 @@ package strategy import ( "errors" "sort" - "strconv" "github.com/docker/swarm/cluster" "github.com/samalba/dockerclient" @@ -14,21 +13,20 @@ var ( ) type BinPackingPlacementStrategy struct { - ratio int64 + overcommitRatio int64 } -func (p *BinPackingPlacementStrategy) Initialize(opts string) error { - overcommitRatio, err := strconv.ParseFloat(opts, 64) - p.ratio = int64(overcommitRatio * 100) - return err +func (p *BinPackingPlacementStrategy) Initialize(overcommitRatio int64) error { + p.overcommitRatio = overcommitRatio - 100 + return nil } func (p *BinPackingPlacementStrategy) PlaceContainer(config *dockerclient.ContainerConfig, nodes []*cluster.Node) (*cluster.Node, error) { scores := scores{} for _, node := range nodes { - nodeMemory := node.Memory + (node.Memory * p.ratio / 100) - nodeCpus := node.Cpus + (node.Cpus * p.ratio / 100) + nodeMemory := node.Memory + (node.Memory * p.overcommitRatio / 100) + nodeCpus := node.Cpus + (node.Cpus * p.overcommitRatio / 100) // Skip nodes that are smaller than the requested resources. if nodeMemory < int64(config.Memory) || nodeCpus < config.CpuShares { diff --git a/scheduler/strategy/binpacking_test.go b/scheduler/strategy/binpacking_test.go index d62bf317f6..6e0c3cca87 100644 --- a/scheduler/strategy/binpacking_test.go +++ b/scheduler/strategy/binpacking_test.go @@ -114,7 +114,7 @@ func TestPlaceContainerHuge(t *testing.T) { } func TestPlaceContainerOvercommit(t *testing.T) { - s, err := New("binpacking:0.05") + s, err := New("binpacking", 105) assert.NoError(t, err) nodes := []*cluster.Node{createNode("node-1", 0, 1)} diff --git a/scheduler/strategy/random.go b/scheduler/strategy/random.go index 8cdcc1f872..6adf80e431 100644 --- a/scheduler/strategy/random.go +++ b/scheduler/strategy/random.go @@ -13,7 +13,7 @@ import ( type RandomPlacementStrategy struct { } -func (p *RandomPlacementStrategy) Initialize(_ string) error { +func (p *RandomPlacementStrategy) Initialize(_ int64) error { rand.Seed(time.Now().UTC().UnixNano()) return nil } diff --git a/scheduler/strategy/strategy.go b/scheduler/strategy/strategy.go index 2be75ffe91..a32bbc4e4b 100644 --- a/scheduler/strategy/strategy.go +++ b/scheduler/strategy/strategy.go @@ -2,7 +2,6 @@ package strategy import ( "errors" - "strings" log "github.com/Sirupsen/logrus" "github.com/docker/swarm/cluster" @@ -10,7 +9,7 @@ import ( ) type PlacementStrategy interface { - Initialize(string) error + Initialize(overcommitRatio int64) error // Given a container configuration and a set of nodes, select the target // node where the container should be scheduled. PlaceContainer(config *dockerclient.ContainerConfig, nodes []*cluster.Node) (*cluster.Node, error) @@ -28,19 +27,10 @@ func init() { } } -func New(nameAndOpts string) (PlacementStrategy, error) { - var ( - parts = strings.SplitN(nameAndOpts, ":", 2) - name = parts[0] - opts string - ) - if len(parts) == 2 { - opts = parts[1] - } - +func New(name string, overcommitRatio int64) (PlacementStrategy, error) { if strategy, exists := strategies[name]; exists { - log.Debugf("Initializing %q strategy with %q", name, opts) - err := strategy.Initialize(opts) + log.Debugf("Initializing %q strategy", name) + err := strategy.Initialize(overcommitRatio) return strategy, err }