Move overcommit outside of binpacking and use 105 instead of 0.05

Signed-off-by: Victor Vieux <vieux@docker.com>
This commit is contained in:
Victor Vieux 2015-01-08 20:13:11 +00:00
parent 2e156fb692
commit d8b609c161
7 changed files with 20 additions and 27 deletions

View File

@ -50,10 +50,15 @@ var (
Name: "tlsverify", Name: "tlsverify",
Usage: "use TLS and verify the remote", Usage: "use TLS and verify the remote",
} }
flOverCommit = cli.IntFlag{
Name: "overcommit, oc",
Usage: "overcommit to apply on resources",
Value: 105,
}
flStrategy = cli.StringFlag{ flStrategy = cli.StringFlag{
Name: "strategy", Name: "strategy",
Usage: "placement strategy to use [binpacking, random]", Usage: "placement strategy to use [binpacking, random]",
Value: "binpacking:0.05", Value: "binpacking",
} }
flFilter = cli.StringSliceFlag{ flFilter = cli.StringSliceFlag{
Name: "filter, f", Name: "filter, f",

View File

@ -87,7 +87,7 @@ func main() {
Flags: []cli.Flag{ Flags: []cli.Flag{
flDiscovery, flDiscovery,
flStrategy, flFilter, flStrategy, flFilter,
flHosts, flHeartBeat, flHosts, flHeartBeat, flOverCommit,
flTls, flTlsCaCert, flTlsCert, flTlsKey, flTlsVerify, flTls, flTlsCaCert, flTlsCert, flTlsKey, flTlsVerify,
flEnableCors}, flEnableCors},
Action: manage, Action: manage,

View File

@ -78,7 +78,7 @@ func manage(c *cli.Context) {
log.Fatal("--discovery required to manage a cluster") 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -3,7 +3,6 @@ package strategy
import ( import (
"errors" "errors"
"sort" "sort"
"strconv"
"github.com/docker/swarm/cluster" "github.com/docker/swarm/cluster"
"github.com/samalba/dockerclient" "github.com/samalba/dockerclient"
@ -14,21 +13,20 @@ var (
) )
type BinPackingPlacementStrategy struct { type BinPackingPlacementStrategy struct {
ratio int64 overcommitRatio int64
} }
func (p *BinPackingPlacementStrategy) Initialize(opts string) error { func (p *BinPackingPlacementStrategy) Initialize(overcommitRatio int64) error {
overcommitRatio, err := strconv.ParseFloat(opts, 64) p.overcommitRatio = overcommitRatio - 100
p.ratio = int64(overcommitRatio * 100) return nil
return err
} }
func (p *BinPackingPlacementStrategy) PlaceContainer(config *dockerclient.ContainerConfig, nodes []*cluster.Node) (*cluster.Node, error) { func (p *BinPackingPlacementStrategy) PlaceContainer(config *dockerclient.ContainerConfig, nodes []*cluster.Node) (*cluster.Node, error) {
scores := scores{} scores := scores{}
for _, node := range nodes { for _, node := range nodes {
nodeMemory := node.Memory + (node.Memory * p.ratio / 100) nodeMemory := node.Memory + (node.Memory * p.overcommitRatio / 100)
nodeCpus := node.Cpus + (node.Cpus * p.ratio / 100) nodeCpus := node.Cpus + (node.Cpus * p.overcommitRatio / 100)
// Skip nodes that are smaller than the requested resources. // Skip nodes that are smaller than the requested resources.
if nodeMemory < int64(config.Memory) || nodeCpus < config.CpuShares { if nodeMemory < int64(config.Memory) || nodeCpus < config.CpuShares {

View File

@ -114,7 +114,7 @@ func TestPlaceContainerHuge(t *testing.T) {
} }
func TestPlaceContainerOvercommit(t *testing.T) { func TestPlaceContainerOvercommit(t *testing.T) {
s, err := New("binpacking:0.05") s, err := New("binpacking", 105)
assert.NoError(t, err) assert.NoError(t, err)
nodes := []*cluster.Node{createNode("node-1", 0, 1)} nodes := []*cluster.Node{createNode("node-1", 0, 1)}

View File

@ -13,7 +13,7 @@ import (
type RandomPlacementStrategy struct { type RandomPlacementStrategy struct {
} }
func (p *RandomPlacementStrategy) Initialize(_ string) error { func (p *RandomPlacementStrategy) Initialize(_ int64) error {
rand.Seed(time.Now().UTC().UnixNano()) rand.Seed(time.Now().UTC().UnixNano())
return nil return nil
} }

View File

@ -2,7 +2,6 @@ package strategy
import ( import (
"errors" "errors"
"strings"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/docker/swarm/cluster" "github.com/docker/swarm/cluster"
@ -10,7 +9,7 @@ import (
) )
type PlacementStrategy interface { type PlacementStrategy interface {
Initialize(string) error Initialize(overcommitRatio int64) error
// Given a container configuration and a set of nodes, select the target // Given a container configuration and a set of nodes, select the target
// node where the container should be scheduled. // node where the container should be scheduled.
PlaceContainer(config *dockerclient.ContainerConfig, nodes []*cluster.Node) (*cluster.Node, error) PlaceContainer(config *dockerclient.ContainerConfig, nodes []*cluster.Node) (*cluster.Node, error)
@ -28,19 +27,10 @@ func init() {
} }
} }
func New(nameAndOpts string) (PlacementStrategy, error) { func New(name string, overcommitRatio int64) (PlacementStrategy, error) {
var (
parts = strings.SplitN(nameAndOpts, ":", 2)
name = parts[0]
opts string
)
if len(parts) == 2 {
opts = parts[1]
}
if strategy, exists := strategies[name]; exists { if strategy, exists := strategies[name]; exists {
log.Debugf("Initializing %q strategy with %q", name, opts) log.Debugf("Initializing %q strategy", name)
err := strategy.Initialize(opts) err := strategy.Initialize(overcommitRatio)
return strategy, err return strategy, err
} }