Add concurrency and id flag for daemon

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2015-12-07 15:19:56 -08:00
parent 9415a4210c
commit d9e8fe62cb
5 changed files with 40 additions and 10 deletions

View file

@ -198,6 +198,7 @@ func (s *server) writeState(w http.ResponseWriter, e *containerd.Event) error {
state := State{ state := State{
Containers: []Container{}, Containers: []Container{},
Machine: Machine{ Machine: Machine{
ID: m.ID,
Cpus: m.Cpus, Cpus: m.Cpus,
Memory: m.Memory, Memory: m.Memory,
}, },

View file

@ -15,6 +15,7 @@ const (
) )
type Machine struct { type Machine struct {
ID string `json:"id"`
Cpus int `json:"cpus"` Cpus int `json:"cpus"`
Memory int64 `json:"memory"` Memory int64 `json:"memory"`
} }

View file

@ -29,6 +29,11 @@ func main() {
}, },
} }
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
cli.StringFlag{
Name: "id",
Value: getDefaultID(),
Usage: "unique containerd id to identify the instance",
},
cli.BoolFlag{ cli.BoolFlag{
Name: "debug", Name: "debug",
Usage: "enable debug output in the logs", Usage: "enable debug output in the logs",
@ -43,6 +48,11 @@ func main() {
Value: 2048, Value: 2048,
Usage: "set the channel buffer size for events and signals", 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 { app.Before = func(context *cli.Context) error {
if context.GlobalBool("debug") { if context.GlobalBool("debug") {
@ -65,7 +75,12 @@ func main() {
return nil return nil
} }
app.Action = func(context *cli.Context) { 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) 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) tasks := make(chan *containerd.StartTask, concurrency*100)
supervisor, err := containerd.NewSupervisor(stateDir, tasks) supervisor, err := containerd.NewSupervisor(id, stateDir, tasks)
if err != nil { if err != nil {
return err return err
} }
@ -100,3 +115,12 @@ func daemon(stateDir string, concurrency, bufferSize int) error {
server := v1.NewServer(supervisor) server := v1.NewServer(supervisor)
return http.ListenAndServe("localhost:8888", server) 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
}

View file

@ -3,12 +3,15 @@ package containerd
import "github.com/cloudfoundry/gosigar" import "github.com/cloudfoundry/gosigar"
type Machine struct { type Machine struct {
ID string
Cpus int Cpus int
Memory int64 Memory int64
} }
func CollectMachineInformation() (Machine, error) { func CollectMachineInformation(id string) (Machine, error) {
m := Machine{} m := Machine{
ID: id,
}
cpu := sigar.CpuList{} cpu := sigar.CpuList{}
if err := cpu.Get(); err != nil { if err := cpu.Get(); err != nil {
return m, err return m, err

View file

@ -3,6 +3,7 @@ package containerd
import ( import (
"os" "os"
"os/signal" "os/signal"
"path/filepath"
goruntime "runtime" goruntime "runtime"
"sync" "sync"
"syscall" "syscall"
@ -13,16 +14,16 @@ import (
) )
// NewSupervisor returns an initialized Process supervisor. // 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 { if err := os.MkdirAll(stateDir, 0755); err != nil {
return nil, err return nil, err
} }
// register counters // register counters
r, err := newRuntime(stateDir) r, err := newRuntime(filepath.Join(stateDir, id))
if err != nil { if err != nil {
return nil, err return nil, err
} }
machine, err := CollectMachineInformation() machine, err := CollectMachineInformation(id)
if err != nil { if err != nil {
return nil, err return nil, err
} }