Add the Stats api to driverhandle

This commit is contained in:
Diptanu Choudhury 2016-04-28 16:06:01 -07:00
parent bc81cae12c
commit 6c1b60ff07
12 changed files with 78 additions and 1 deletions

View file

@ -908,6 +908,19 @@ func (h *DockerHandle) Kill() error {
return nil
}
func (h *DockerHandle) Stats() (*cstructs.TaskResourceUsage, error) {
stats := make(chan *docker.Stats)
done := make(chan bool)
statsOpts := docker.StatsOptions{ID: h.containerID, Done: done, Stats: stats, Stream: false, Timeout: 2 * time.Second}
if err := h.client.Stats(statsOpts); err != nil {
return nil, err
}
containerStats := <-stats
close(done)
resourceUsage := cstructs.TaskResourceUsage{MemoryStats: &cstructs.MemoryStats{RSS: containerStats.MemoryStats.Stats.Rss}}
return &resourceUsage, nil
}
func (h *DockerHandle) run() {
// Wait for it...
exitCode, err := h.client.WaitContainer(h.containerID)

View file

@ -113,6 +113,8 @@ type DriverHandle interface {
// Kill is used to stop the task
Kill() error
Stats() (*cstructs.TaskResourceUsage, error)
}
// ExecContext is shared between drivers within an allocation

View file

@ -280,6 +280,10 @@ func (h *execHandle) Kill() error {
}
}
func (h *execHandle) Stats() (*cstructs.TaskResourceUsage, error) {
return h.executor.Stats()
}
func (h *execHandle) run() {
ps, err := h.executor.Wait()
close(h.doneCh)

View file

@ -38,6 +38,7 @@ type Executor interface {
SyncServices(ctx *ConsulContext) error
DeregisterServices() error
Version() (*ExecutorVersion, error)
Stats() (*cstructs.TaskResourceUsage, error)
}
// ConsulContext holds context to configure the consul client and run checks

View file

@ -2,7 +2,10 @@
package executor
import cgroupConfig "github.com/opencontainers/runc/libcontainer/configs"
import (
cstructs "github.com/hashicorp/nomad/client/driver/structs"
cgroupConfig "github.com/opencontainers/runc/libcontainer/configs"
)
func (e *UniversalExecutor) configureChroot() error {
return nil
@ -27,3 +30,7 @@ func (e *UniversalExecutor) applyLimits(pid int) error {
func (e *UniversalExecutor) configureIsolation() error {
return nil
}
func (e *UniversalExecutor) Stats() (*cstructs.TaskResourceUsage, error) {
return nil, nil
}

View file

@ -15,6 +15,7 @@ import (
cgroupConfig "github.com/opencontainers/runc/libcontainer/configs"
"github.com/hashicorp/nomad/client/allocdir"
cstructs "github.com/hashicorp/nomad/client/driver/structs"
"github.com/hashicorp/nomad/nomad/structs"
)
@ -116,6 +117,16 @@ func (e *UniversalExecutor) configureCgroups(resources *structs.Resources) error
return nil
}
func (e *UniversalExecutor) Stats() (*cstructs.TaskResourceUsage, error) {
manager := getCgroupManager(e.groups, e.cgPaths)
stats, err := manager.GetStats()
if err != nil {
return nil, err
}
e.logger.Printf("DIPTANU stats %#v", stats.MemoryStats.Stats)
return &cstructs.TaskResourceUsage{}, nil
}
// runAs takes a user id as a string and looks up the user, and sets the command
// to execute as that user.
func (e *UniversalExecutor) runAs(userid string) error {

View file

@ -7,6 +7,7 @@ import (
"github.com/hashicorp/go-plugin"
"github.com/hashicorp/nomad/client/driver/executor"
cstructs "github.com/hashicorp/nomad/client/driver/structs"
"github.com/hashicorp/nomad/nomad/structs"
)
@ -88,6 +89,12 @@ func (e *ExecutorRPC) Version() (*executor.ExecutorVersion, error) {
return &version, err
}
func (e *ExecutorRPC) Stats() (*cstructs.TaskResourceUsage, error) {
var resourceUsage cstructs.TaskResourceUsage
err := e.client.Call("Plugin.Stats", new(interface{}), &resourceUsage)
return &resourceUsage, err
}
type ExecutorRPCServer struct {
Impl executor.Executor
logger *log.Logger
@ -149,6 +156,14 @@ func (e *ExecutorRPCServer) Version(args interface{}, version *executor.Executor
return err
}
func (e *ExecutorRPCServer) Stats(args interface{}, resourceUsage *cstructs.TaskResourceUsage) error {
ru, err := e.Impl.Stats()
if ru != nil {
*resourceUsage = *ru
}
return err
}
type ExecutorPlugin struct {
logger *log.Logger
Impl *ExecutorRPCServer

View file

@ -380,6 +380,10 @@ func (h *javaHandle) Kill() error {
}
}
func (h *javaHandle) Stats() (*cstructs.TaskResourceUsage, error) {
return h.executor.Stats()
}
func (h *javaHandle) run() {
ps, err := h.executor.Wait()
close(h.doneCh)

View file

@ -375,6 +375,10 @@ func (h *qemuHandle) Kill() error {
}
}
func (h *qemuHandle) Stats() (*cstructs.TaskResourceUsage, error) {
return h.executor.Stats()
}
func (h *qemuHandle) run() {
ps, err := h.executor.Wait()
if ps.ExitCode == 0 && err != nil {

View file

@ -277,6 +277,10 @@ func (h *rawExecHandle) Kill() error {
}
}
func (h *rawExecHandle) Stats() (*cstructs.TaskResourceUsage, error) {
return h.executor.Stats()
}
func (h *rawExecHandle) run() {
ps, err := h.executor.Wait()
close(h.doneCh)

View file

@ -399,6 +399,10 @@ func (h *rktHandle) Kill() error {
}
}
func (h *rktHandle) Stats() (*cstructs.TaskResourceUsage, error) {
return nil, fmt.Errorf("stats not implemented for rkt")
}
func (h *rktHandle) run() {
ps, err := h.executor.Wait()
close(h.doneCh)

View file

@ -84,3 +84,11 @@ type CheckResult struct {
// Err is the error that a check returned
Err error
}
type MemoryStats struct {
RSS uint64
}
type TaskResourceUsage struct {
MemoryStats *MemoryStats
}