From d9e8fe62cb6877815fa2e8ed20cb3f811596d77b Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Mon, 7 Dec 2015 15:19:56 -0800 Subject: [PATCH] Add concurrency and id flag for daemon Signed-off-by: Michael Crosby --- api/v1/server.go | 1 + api/v1/types.go | 5 +++-- containerd/main.go | 30 +++++++++++++++++++++++++++--- machine.go | 7 +++++-- supervisor.go | 7 ++++--- 5 files changed, 40 insertions(+), 10 deletions(-) diff --git a/api/v1/server.go b/api/v1/server.go index 2713929..f92b745 100644 --- a/api/v1/server.go +++ b/api/v1/server.go @@ -198,6 +198,7 @@ func (s *server) writeState(w http.ResponseWriter, e *containerd.Event) error { state := State{ Containers: []Container{}, Machine: Machine{ + ID: m.ID, Cpus: m.Cpus, Memory: m.Memory, }, diff --git a/api/v1/types.go b/api/v1/types.go index 3618a8f..4946bb0 100644 --- a/api/v1/types.go +++ b/api/v1/types.go @@ -15,8 +15,9 @@ const ( ) type Machine struct { - Cpus int `json:"cpus"` - Memory int64 `json:"memory"` + ID string `json:"id"` + Cpus int `json:"cpus"` + Memory int64 `json:"memory"` } type ContainerState struct { diff --git a/containerd/main.go b/containerd/main.go index 918255c..6e9af13 100644 --- a/containerd/main.go +++ b/containerd/main.go @@ -29,6 +29,11 @@ func main() { }, } app.Flags = []cli.Flag{ + cli.StringFlag{ + Name: "id", + Value: getDefaultID(), + Usage: "unique containerd id to identify the instance", + }, cli.BoolFlag{ Name: "debug", Usage: "enable debug output in the logs", @@ -43,6 +48,11 @@ func main() { Value: 2048, Usage: "set the channel buffer size for events and signals", }, + cli.IntFlag{ + Name: "c,concurrency", + Value: 10, + Usage: "set the concurrency level for tasks", + }, } app.Before = func(context *cli.Context) error { if context.GlobalBool("debug") { @@ -65,7 +75,12 @@ func main() { return nil } app.Action = func(context *cli.Context) { - if err := daemon(context.String("state-dir"), 10, context.Int("buffer-size")); err != nil { + if err := daemon( + context.String("id"), + context.String("state-dir"), + context.Int("concurrency"), + context.Int("buffer-size"), + ); err != nil { logrus.Fatal(err) } } @@ -74,9 +89,9 @@ func main() { } } -func daemon(stateDir string, concurrency, bufferSize int) error { +func daemon(id, stateDir string, concurrency, bufferSize int) error { tasks := make(chan *containerd.StartTask, concurrency*100) - supervisor, err := containerd.NewSupervisor(stateDir, tasks) + supervisor, err := containerd.NewSupervisor(id, stateDir, tasks) if err != nil { return err } @@ -100,3 +115,12 @@ func daemon(stateDir string, concurrency, bufferSize int) error { server := v1.NewServer(supervisor) return http.ListenAndServe("localhost:8888", server) } + +// getDefaultID returns the hostname for the instance host +func getDefaultID() string { + hostname, err := os.Hostname() + if err != nil { + panic(err) + } + return hostname +} diff --git a/machine.go b/machine.go index 746d827..7d5cee3 100644 --- a/machine.go +++ b/machine.go @@ -3,12 +3,15 @@ package containerd import "github.com/cloudfoundry/gosigar" type Machine struct { + ID string Cpus int Memory int64 } -func CollectMachineInformation() (Machine, error) { - m := Machine{} +func CollectMachineInformation(id string) (Machine, error) { + m := Machine{ + ID: id, + } cpu := sigar.CpuList{} if err := cpu.Get(); err != nil { return m, err diff --git a/supervisor.go b/supervisor.go index ede024e..d411a30 100644 --- a/supervisor.go +++ b/supervisor.go @@ -3,6 +3,7 @@ package containerd import ( "os" "os/signal" + "path/filepath" goruntime "runtime" "sync" "syscall" @@ -13,16 +14,16 @@ import ( ) // NewSupervisor returns an initialized Process supervisor. -func NewSupervisor(stateDir string, tasks chan *StartTask) (*Supervisor, error) { +func NewSupervisor(id, stateDir string, tasks chan *StartTask) (*Supervisor, error) { if err := os.MkdirAll(stateDir, 0755); err != nil { return nil, err } // register counters - r, err := newRuntime(stateDir) + r, err := newRuntime(filepath.Join(stateDir, id)) if err != nil { return nil, err } - machine, err := CollectMachineInformation() + machine, err := CollectMachineInformation(id) if err != nil { return nil, err }