From 64f80343fcecfc20aca5a8203b87b095db880ea9 Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Mon, 7 Jan 2019 10:01:46 -0500 Subject: [PATCH] drivers: re-export ResourceUsage structs Re-export the ResourceUsage structs in drivers package to avoid drivers directly depending on the internal client/structs package directly. I attempted moving the structs to drivers, but that caused some import cycles that was a bit hard to disentagle. Alternatively, I added an alias here that's sufficient for our purposes of avoiding external drivers depend on internal packages, while allowing us to restructure packages in future without breaking source compatibility. --- drivers/docker/driver.go | 3 +-- drivers/docker/handle.go | 17 +++++++------ drivers/exec/driver.go | 3 +-- drivers/java/driver.go | 3 +-- drivers/lxc/driver.go | 3 +-- drivers/lxc/handle.go | 11 ++++----- drivers/mock/driver.go | 9 ++++--- drivers/qemu/driver.go | 3 +-- drivers/rawexec/driver.go | 3 +-- drivers/rkt/driver.go | 3 +-- drivers/shared/executor/client.go | 3 +-- drivers/shared/executor/executor.go | 7 +++--- drivers/shared/executor/executor_linux.go | 13 +++++----- drivers/shared/executor/pid_collector.go | 22 ++++++++--------- plugins/drivers/client.go | 3 +-- plugins/drivers/cstructs.go | 29 +++++++++++++++++++++++ plugins/drivers/driver.go | 3 +-- plugins/drivers/testutils/testing.go | 5 ++-- plugins/drivers/utils.go | 19 +++++++-------- 19 files changed, 87 insertions(+), 75 deletions(-) create mode 100644 plugins/drivers/cstructs.go diff --git a/drivers/docker/driver.go b/drivers/docker/driver.go index e585fa58b..ccb35b90f 100644 --- a/drivers/docker/driver.go +++ b/drivers/docker/driver.go @@ -16,7 +16,6 @@ import ( "github.com/hashicorp/consul-template/signals" hclog "github.com/hashicorp/go-hclog" multierror "github.com/hashicorp/go-multierror" - "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/client/taskenv" "github.com/hashicorp/nomad/drivers/docker/docklog" "github.com/hashicorp/nomad/drivers/shared/eventer" @@ -1088,7 +1087,7 @@ func (d *Driver) InspectTask(taskID string) (*drivers.TaskStatus, error) { return status, nil } -func (d *Driver) TaskStats(taskID string) (*structs.TaskResourceUsage, error) { +func (d *Driver) TaskStats(taskID string) (*drivers.TaskResourceUsage, error) { h, ok := d.tasks.Get(taskID) if !ok { return nil, drivers.ErrTaskNotFound diff --git a/drivers/docker/handle.go b/drivers/docker/handle.go index 109cc4ed1..96432f58f 100644 --- a/drivers/docker/handle.go +++ b/drivers/docker/handle.go @@ -13,7 +13,6 @@ import ( docker "github.com/fsouza/go-dockerclient" hclog "github.com/hashicorp/go-hclog" plugin "github.com/hashicorp/go-plugin" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/drivers/docker/docklog" "github.com/hashicorp/nomad/helper/stats" "github.com/hashicorp/nomad/plugins/drivers" @@ -31,7 +30,7 @@ type taskHandle struct { containerID string containerImage string resourceUsageLock sync.RWMutex - resourceUsage *cstructs.TaskResourceUsage + resourceUsage *drivers.TaskResourceUsage doneCh chan bool waitCh chan struct{} removeContainerOnExit bool @@ -82,8 +81,8 @@ func (h *taskHandle) Exec(ctx context.Context, cmd string, args []string) (*driv } execResult := &drivers.ExecTaskResult{ExitResult: &drivers.ExitResult{}} - stdout, _ := circbuf.NewBuffer(int64(cstructs.CheckBufSize)) - stderr, _ := circbuf.NewBuffer(int64(cstructs.CheckBufSize)) + stdout, _ := circbuf.NewBuffer(int64(drivers.CheckBufSize)) + stderr, _ := circbuf.NewBuffer(int64(drivers.CheckBufSize)) startOpts := docker.StartExecOptions{ Detach: false, Tty: false, @@ -161,7 +160,7 @@ func (h *taskHandle) Kill(killTimeout time.Duration, signal os.Signal) error { return nil } -func (h *taskHandle) Stats() (*cstructs.TaskResourceUsage, error) { +func (h *taskHandle) Stats() (*drivers.TaskResourceUsage, error) { h.resourceUsageLock.RLock() defer h.resourceUsageLock.RUnlock() var err error @@ -241,7 +240,7 @@ func (h *taskHandle) collectStats() { select { case s := <-statsCh: if s != nil { - ms := &cstructs.MemoryStats{ + ms := &drivers.MemoryStats{ RSS: s.MemoryStats.Stats.Rss, Cache: s.MemoryStats.Stats.Cache, Swap: s.MemoryStats.Stats.Swap, @@ -249,7 +248,7 @@ func (h *taskHandle) collectStats() { Measured: DockerMeasuredMemStats, } - cs := &cstructs.CpuStats{ + cs := &drivers.CpuStats{ ThrottledPeriods: s.CPUStats.ThrottlingData.ThrottledPeriods, ThrottledTime: s.CPUStats.ThrottlingData.ThrottledTime, Measured: DockerMeasuredCpuStats, @@ -268,8 +267,8 @@ func (h *taskHandle) collectStats() { cs.TotalTicks = (cs.Percent / 100) * stats.TotalTicksAvailable() / float64(numCores) h.resourceUsageLock.Lock() - h.resourceUsage = &cstructs.TaskResourceUsage{ - ResourceUsage: &cstructs.ResourceUsage{ + h.resourceUsage = &drivers.TaskResourceUsage{ + ResourceUsage: &drivers.ResourceUsage{ MemoryStats: ms, CpuStats: cs, }, diff --git a/drivers/exec/driver.go b/drivers/exec/driver.go index acb2c93ca..069d27fa8 100644 --- a/drivers/exec/driver.go +++ b/drivers/exec/driver.go @@ -12,7 +12,6 @@ import ( hclog "github.com/hashicorp/go-hclog" plugin "github.com/hashicorp/go-plugin" "github.com/hashicorp/nomad/client/fingerprint" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/drivers/shared/eventer" "github.com/hashicorp/nomad/drivers/shared/executor" "github.com/hashicorp/nomad/plugins/base" @@ -435,7 +434,7 @@ func (d *Driver) InspectTask(taskID string) (*drivers.TaskStatus, error) { return handle.TaskStatus(), nil } -func (d *Driver) TaskStats(taskID string) (*cstructs.TaskResourceUsage, error) { +func (d *Driver) TaskStats(taskID string) (*drivers.TaskResourceUsage, error) { handle, ok := d.tasks.Get(taskID) if !ok { return nil, drivers.ErrTaskNotFound diff --git a/drivers/java/driver.go b/drivers/java/driver.go index 5a126a9b3..23c09d627 100644 --- a/drivers/java/driver.go +++ b/drivers/java/driver.go @@ -13,7 +13,6 @@ import ( "github.com/hashicorp/go-hclog" "github.com/hashicorp/go-plugin" "github.com/hashicorp/nomad/client/fingerprint" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/drivers/shared/eventer" "github.com/hashicorp/nomad/drivers/shared/executor" "github.com/hashicorp/nomad/plugins/base" @@ -497,7 +496,7 @@ func (d *Driver) InspectTask(taskID string) (*drivers.TaskStatus, error) { return handle.TaskStatus(), nil } -func (d *Driver) TaskStats(taskID string) (*cstructs.TaskResourceUsage, error) { +func (d *Driver) TaskStats(taskID string) (*drivers.TaskResourceUsage, error) { handle, ok := d.tasks.Get(taskID) if !ok { return nil, drivers.ErrTaskNotFound diff --git a/drivers/lxc/driver.go b/drivers/lxc/driver.go index 7ea967679..d9d1ea97e 100644 --- a/drivers/lxc/driver.go +++ b/drivers/lxc/driver.go @@ -10,7 +10,6 @@ import ( "github.com/hashicorp/go-hclog" "github.com/hashicorp/nomad/client/stats" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/drivers/shared/eventer" "github.com/hashicorp/nomad/plugins/base" "github.com/hashicorp/nomad/plugins/drivers" @@ -492,7 +491,7 @@ func (d *Driver) InspectTask(taskID string) (*drivers.TaskStatus, error) { return handle.TaskStatus(), nil } -func (d *Driver) TaskStats(taskID string) (*cstructs.TaskResourceUsage, error) { +func (d *Driver) TaskStats(taskID string) (*drivers.TaskResourceUsage, error) { handle, ok := d.tasks.Get(taskID) if !ok { return nil, drivers.ErrTaskNotFound diff --git a/drivers/lxc/handle.go b/drivers/lxc/handle.go index 0e4490369..059f5d3db 100644 --- a/drivers/lxc/handle.go +++ b/drivers/lxc/handle.go @@ -11,7 +11,6 @@ import ( hclog "github.com/hashicorp/go-hclog" "github.com/hashicorp/nomad/client/stats" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/plugins/drivers" lxc "gopkg.in/lxc/go-lxc.v2" ) @@ -87,7 +86,7 @@ func (h *taskHandle) run() { // TODO: detect if the task OOMed } -func (h *taskHandle) stats() (*cstructs.TaskResourceUsage, error) { +func (h *taskHandle) stats() (*drivers.TaskResourceUsage, error) { cpuStats, err := h.container.CPUStats() if err != nil { h.logger.Error("failed to get container cpu stats", "error", err) @@ -104,7 +103,7 @@ func (h *taskHandle) stats() (*cstructs.TaskResourceUsage, error) { // Get the cpu stats system := cpuStats["system"] user := cpuStats["user"] - cs := &cstructs.CpuStats{ + cs := &drivers.CpuStats{ SystemMode: h.systemCpuStats.Percent(float64(system)), UserMode: h.systemCpuStats.Percent(float64(user)), Percent: h.totalCpuStats.Percent(float64(total)), @@ -130,7 +129,7 @@ func (h *taskHandle) stats() (*cstructs.TaskResourceUsage, error) { } } - ms := &cstructs.MemoryStats{ + ms := &drivers.MemoryStats{ RSS: memData["rss"], Cache: memData["cache"], Swap: memData["swap"], @@ -166,8 +165,8 @@ func (h *taskHandle) stats() (*cstructs.TaskResourceUsage, error) { ms.KernelMaxUsage = val } - taskResUsage := cstructs.TaskResourceUsage{ - ResourceUsage: &cstructs.ResourceUsage{ + taskResUsage := drivers.TaskResourceUsage{ + ResourceUsage: &drivers.ResourceUsage{ CpuStats: cs, MemoryStats: ms, }, diff --git a/drivers/mock/driver.go b/drivers/mock/driver.go index e555f9992..7f236cac6 100644 --- a/drivers/mock/driver.go +++ b/drivers/mock/driver.go @@ -11,7 +11,6 @@ import ( "time" hclog "github.com/hashicorp/go-hclog" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/drivers/shared/eventer" "github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/plugins/base" @@ -495,11 +494,11 @@ func (d *Driver) InspectTask(taskID string) (*drivers.TaskStatus, error) { panic("not implemented") } -func (d *Driver) TaskStats(taskID string) (*cstructs.TaskResourceUsage, error) { +func (d *Driver) TaskStats(taskID string) (*drivers.TaskResourceUsage, error) { // Generate random value for the memory usage - s := &cstructs.TaskResourceUsage{ - ResourceUsage: &cstructs.ResourceUsage{ - MemoryStats: &cstructs.MemoryStats{ + s := &drivers.TaskResourceUsage{ + ResourceUsage: &drivers.ResourceUsage{ + MemoryStats: &drivers.MemoryStats{ RSS: rand.Uint64(), Measured: []string{"RSS"}, }, diff --git a/drivers/qemu/driver.go b/drivers/qemu/driver.go index baf036b93..251615a6d 100644 --- a/drivers/qemu/driver.go +++ b/drivers/qemu/driver.go @@ -16,7 +16,6 @@ import ( "github.com/coreos/go-semver/semver" "github.com/hashicorp/go-hclog" "github.com/hashicorp/go-plugin" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/drivers/shared/eventer" "github.com/hashicorp/nomad/drivers/shared/executor" "github.com/hashicorp/nomad/plugins/base" @@ -547,7 +546,7 @@ func (d *Driver) InspectTask(taskID string) (*drivers.TaskStatus, error) { return handle.TaskStatus(), nil } -func (d *Driver) TaskStats(taskID string) (*cstructs.TaskResourceUsage, error) { +func (d *Driver) TaskStats(taskID string) (*drivers.TaskResourceUsage, error) { handle, ok := d.tasks.Get(taskID) if !ok { return nil, drivers.ErrTaskNotFound diff --git a/drivers/rawexec/driver.go b/drivers/rawexec/driver.go index 08b9cc999..fc0b4c00f 100644 --- a/drivers/rawexec/driver.go +++ b/drivers/rawexec/driver.go @@ -13,7 +13,6 @@ import ( "github.com/hashicorp/consul-template/signals" hclog "github.com/hashicorp/go-hclog" plugin "github.com/hashicorp/go-plugin" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/drivers/shared/eventer" "github.com/hashicorp/nomad/drivers/shared/executor" "github.com/hashicorp/nomad/plugins/base" @@ -457,7 +456,7 @@ func (d *Driver) InspectTask(taskID string) (*drivers.TaskStatus, error) { return handle.TaskStatus(), nil } -func (d *Driver) TaskStats(taskID string) (*cstructs.TaskResourceUsage, error) { +func (d *Driver) TaskStats(taskID string) (*drivers.TaskResourceUsage, error) { handle, ok := d.tasks.Get(taskID) if !ok { return nil, drivers.ErrTaskNotFound diff --git a/drivers/rkt/driver.go b/drivers/rkt/driver.go index 15066405e..27856df43 100644 --- a/drivers/rkt/driver.go +++ b/drivers/rkt/driver.go @@ -26,7 +26,6 @@ import ( plugin "github.com/hashicorp/go-plugin" version "github.com/hashicorp/go-version" "github.com/hashicorp/nomad/client/config" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/client/taskenv" "github.com/hashicorp/nomad/drivers/shared/eventer" "github.com/hashicorp/nomad/drivers/shared/executor" @@ -819,7 +818,7 @@ func (d *Driver) InspectTask(taskID string) (*drivers.TaskStatus, error) { return handle.TaskStatus(), nil } -func (d *Driver) TaskStats(taskID string) (*cstructs.TaskResourceUsage, error) { +func (d *Driver) TaskStats(taskID string) (*drivers.TaskResourceUsage, error) { handle, ok := d.tasks.Get(taskID) if !ok { return nil, drivers.ErrTaskNotFound diff --git a/drivers/shared/executor/client.go b/drivers/shared/executor/client.go index 063e30dec..f7f0e7f00 100644 --- a/drivers/shared/executor/client.go +++ b/drivers/shared/executor/client.go @@ -9,7 +9,6 @@ import ( "github.com/LK4D4/joincontext" "github.com/golang/protobuf/ptypes" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/drivers/shared/executor/proto" "github.com/hashicorp/nomad/plugins/drivers" ) @@ -100,7 +99,7 @@ func (c *grpcExecutorClient) Version() (*ExecutorVersion, error) { return &ExecutorVersion{Version: resp.Version}, nil } -func (c *grpcExecutorClient) Stats() (*cstructs.TaskResourceUsage, error) { +func (c *grpcExecutorClient) Stats() (*drivers.TaskResourceUsage, error) { ctx := context.Background() resp, err := c.client.Stats(ctx, &proto.StatsRequest{}) if err != nil { diff --git a/drivers/shared/executor/executor.go b/drivers/shared/executor/executor.go index 5705866b4..f9576e738 100644 --- a/drivers/shared/executor/executor.go +++ b/drivers/shared/executor/executor.go @@ -22,7 +22,6 @@ import ( "github.com/hashicorp/nomad/client/stats" "github.com/hashicorp/nomad/plugins/drivers" - cstructs "github.com/hashicorp/nomad/client/structs" shelpers "github.com/hashicorp/nomad/helper/stats" ) @@ -68,7 +67,7 @@ type Executor interface { Version() (*ExecutorVersion, error) // Stats fetchs process usage stats for the executor and each pid if available - Stats() (*cstructs.TaskResourceUsage, error) + Stats() (*drivers.TaskResourceUsage, error) // Signal sends the given signal to the user process Signal(os.Signal) error @@ -332,7 +331,7 @@ func ExecScript(ctx context.Context, dir string, env []string, attrs *syscall.Sy cmd.Env = env // Capture output - buf, _ := circbuf.NewBuffer(int64(cstructs.CheckBufSize)) + buf, _ := circbuf.NewBuffer(int64(drivers.CheckBufSize)) cmd.Stdout = buf cmd.Stderr = buf @@ -507,7 +506,7 @@ func (e *UniversalExecutor) Signal(s os.Signal) error { return nil } -func (e *UniversalExecutor) Stats() (*cstructs.TaskResourceUsage, error) { +func (e *UniversalExecutor) Stats() (*drivers.TaskResourceUsage, error) { pidStats, err := e.pidCollector.pidStats() if err != nil { return nil, err diff --git a/drivers/shared/executor/executor_linux.go b/drivers/shared/executor/executor_linux.go index 0b0c9dc84..5e179a7b0 100644 --- a/drivers/shared/executor/executor_linux.go +++ b/drivers/shared/executor/executor_linux.go @@ -18,7 +18,6 @@ import ( hclog "github.com/hashicorp/go-hclog" multierror "github.com/hashicorp/go-multierror" "github.com/hashicorp/nomad/client/stats" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/helper/discover" shelpers "github.com/hashicorp/nomad/helper/stats" "github.com/hashicorp/nomad/helper/uuid" @@ -355,7 +354,7 @@ func (l *LibcontainerExecutor) Version() (*ExecutorVersion, error) { } // Stats returns the resource statistics for processes managed by the executor -func (l *LibcontainerExecutor) Stats() (*cstructs.TaskResourceUsage, error) { +func (l *LibcontainerExecutor) Stats() (*drivers.TaskResourceUsage, error) { lstats, err := l.container.Stats() if err != nil { return nil, err @@ -374,7 +373,7 @@ func (l *LibcontainerExecutor) Stats() (*cstructs.TaskResourceUsage, error) { maxUsage := stats.MemoryStats.Usage.MaxUsage rss := stats.MemoryStats.Stats["rss"] cache := stats.MemoryStats.Stats["cache"] - ms := &cstructs.MemoryStats{ + ms := &drivers.MemoryStats{ RSS: rss, Cache: cache, Swap: swap.Usage, @@ -390,7 +389,7 @@ func (l *LibcontainerExecutor) Stats() (*cstructs.TaskResourceUsage, error) { kernelModeTime := float64(stats.CpuStats.CpuUsage.UsageInKernelmode) totalPercent := l.totalCpuStats.Percent(totalProcessCPUUsage) - cs := &cstructs.CpuStats{ + cs := &drivers.CpuStats{ SystemMode: l.systemCpuStats.Percent(kernelModeTime), UserMode: l.userCpuStats.Percent(userModeTime), Percent: totalPercent, @@ -399,8 +398,8 @@ func (l *LibcontainerExecutor) Stats() (*cstructs.TaskResourceUsage, error) { TotalTicks: l.systemCpuStats.TicksConsumed(totalPercent), Measured: ExecutorCgroupMeasuredCpuStats, } - taskResUsage := cstructs.TaskResourceUsage{ - ResourceUsage: &cstructs.ResourceUsage{ + taskResUsage := drivers.TaskResourceUsage{ + ResourceUsage: &drivers.ResourceUsage{ MemoryStats: ms, CpuStats: cs, }, @@ -420,7 +419,7 @@ func (l *LibcontainerExecutor) Signal(s os.Signal) error { func (l *LibcontainerExecutor) Exec(deadline time.Time, cmd string, args []string) ([]byte, int, error) { combined := append([]string{cmd}, args...) // Capture output - buf, _ := circbuf.NewBuffer(int64(cstructs.CheckBufSize)) + buf, _ := circbuf.NewBuffer(int64(drivers.CheckBufSize)) process := &libcontainer.Process{ Args: combined, diff --git a/drivers/shared/executor/pid_collector.go b/drivers/shared/executor/pid_collector.go index 79d4acbf8..3652cf93e 100644 --- a/drivers/shared/executor/pid_collector.go +++ b/drivers/shared/executor/pid_collector.go @@ -8,7 +8,7 @@ import ( hclog "github.com/hashicorp/go-hclog" "github.com/hashicorp/nomad/client/stats" - cstructs "github.com/hashicorp/nomad/client/structs" + "github.com/hashicorp/nomad/plugins/drivers" ps "github.com/mitchellh/go-ps" "github.com/shirou/gopsutil/process" ) @@ -131,8 +131,8 @@ func scanPids(parentPid int, allPids []ps.Process) (map[int]*nomadPid, error) { } // pidStats returns the resource usage stats per pid -func (c *pidCollector) pidStats() (map[string]*cstructs.ResourceUsage, error) { - stats := make(map[string]*cstructs.ResourceUsage) +func (c *pidCollector) pidStats() (map[string]*drivers.ResourceUsage, error) { + stats := make(map[string]*drivers.ResourceUsage) c.pidLock.RLock() pids := make(map[int]*nomadPid, len(c.pids)) for k, v := range c.pids { @@ -145,14 +145,14 @@ func (c *pidCollector) pidStats() (map[string]*cstructs.ResourceUsage, error) { c.logger.Trace("unable to create new process", "pid", pid, "error", err) continue } - ms := &cstructs.MemoryStats{} + ms := &drivers.MemoryStats{} if memInfo, err := p.MemoryInfo(); err == nil { ms.RSS = memInfo.RSS ms.Swap = memInfo.Swap ms.Measured = ExecutorBasicMeasuredMemStats } - cs := &cstructs.CpuStats{} + cs := &drivers.CpuStats{} if cpuStats, err := p.Times(); err == nil { cs.SystemMode = np.cpuStatsSys.Percent(cpuStats.System * float64(time.Second)) cs.UserMode = np.cpuStatsUser.Percent(cpuStats.User * float64(time.Second)) @@ -161,7 +161,7 @@ func (c *pidCollector) pidStats() (map[string]*cstructs.ResourceUsage, error) { // calculate cpu usage percent cs.Percent = np.cpuStatsTotal.Percent(cpuStats.Total() * float64(time.Second)) } - stats[strconv.Itoa(pid)] = &cstructs.ResourceUsage{MemoryStats: ms, CpuStats: cs} + stats[strconv.Itoa(pid)] = &drivers.ResourceUsage{MemoryStats: ms, CpuStats: cs} } return stats, nil @@ -169,7 +169,7 @@ func (c *pidCollector) pidStats() (map[string]*cstructs.ResourceUsage, error) { // aggregatedResourceUsage aggregates the resource usage of all the pids and // returns a TaskResourceUsage data point -func aggregatedResourceUsage(systemCpuStats *stats.CpuStats, pidStats map[string]*cstructs.ResourceUsage) *cstructs.TaskResourceUsage { +func aggregatedResourceUsage(systemCpuStats *stats.CpuStats, pidStats map[string]*drivers.ResourceUsage) *drivers.TaskResourceUsage { ts := time.Now().UTC().UnixNano() var ( systemModeCPU, userModeCPU, percent float64 @@ -185,7 +185,7 @@ func aggregatedResourceUsage(systemCpuStats *stats.CpuStats, pidStats map[string totalSwap += pidStat.MemoryStats.Swap } - totalCPU := &cstructs.CpuStats{ + totalCPU := &drivers.CpuStats{ SystemMode: systemModeCPU, UserMode: userModeCPU, Percent: percent, @@ -193,17 +193,17 @@ func aggregatedResourceUsage(systemCpuStats *stats.CpuStats, pidStats map[string TotalTicks: systemCpuStats.TicksConsumed(percent), } - totalMemory := &cstructs.MemoryStats{ + totalMemory := &drivers.MemoryStats{ RSS: totalRSS, Swap: totalSwap, Measured: ExecutorBasicMeasuredMemStats, } - resourceUsage := cstructs.ResourceUsage{ + resourceUsage := drivers.ResourceUsage{ MemoryStats: totalMemory, CpuStats: totalCPU, } - return &cstructs.TaskResourceUsage{ + return &drivers.TaskResourceUsage{ ResourceUsage: &resourceUsage, Timestamp: ts, Pids: pidStats, diff --git a/plugins/drivers/client.go b/plugins/drivers/client.go index dba508cbc..feed325a6 100644 --- a/plugins/drivers/client.go +++ b/plugins/drivers/client.go @@ -8,7 +8,6 @@ import ( "github.com/LK4D4/joincontext" "github.com/golang/protobuf/ptypes" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/plugins/base" "github.com/hashicorp/nomad/plugins/drivers/proto" @@ -253,7 +252,7 @@ func (d *driverPluginClient) InspectTask(taskID string) (*TaskStatus, error) { } // TaskStats returns resource usage statistics for the task -func (d *driverPluginClient) TaskStats(taskID string) (*cstructs.TaskResourceUsage, error) { +func (d *driverPluginClient) TaskStats(taskID string) (*TaskResourceUsage, error) { req := &proto.TaskStatsRequest{TaskId: taskID} resp, err := d.client.TaskStats(d.doneCtx, req) diff --git a/plugins/drivers/cstructs.go b/plugins/drivers/cstructs.go new file mode 100644 index 000000000..75d3416ef --- /dev/null +++ b/plugins/drivers/cstructs.go @@ -0,0 +1,29 @@ +package drivers + +import ( + cstructs "github.com/hashicorp/nomad/client/structs" +) + +// This files present an indirection layer to client structs used by drivers, +// and represent the public interface for drivers, as client interfaces are +// internal and subject to change. + +// MemoryStats holds memory usage related stats +type MemoryStats = cstructs.MemoryStats + +// CpuStats holds cpu usage related stats +type CpuStats = cstructs.CpuStats + +// ResourceUsage holds information related to cpu and memory stats +type ResourceUsage = cstructs.ResourceUsage + +// TaskResourceUsage holds aggregated resource usage of all processes in a Task +// and the resource usage of the individual pids +type TaskResourceUsage = cstructs.TaskResourceUsage + +// CheckBufSize is the size of the buffer that is used for job output +const CheckBufSize = cstructs.CheckBufSize + +// DriverStatsNotImplemented is the error to be returned if a driver doesn't +// implement stats. +var DriverStatsNotImplemented = cstructs.DriverStatsNotImplemented diff --git a/plugins/drivers/driver.go b/plugins/drivers/driver.go index 219af6b0a..83a6795f2 100644 --- a/plugins/drivers/driver.go +++ b/plugins/drivers/driver.go @@ -11,7 +11,6 @@ import ( "time" "github.com/hashicorp/nomad/client/allocdir" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/helper" "github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/plugins/base" @@ -40,7 +39,7 @@ type DriverPlugin interface { StopTask(taskID string, timeout time.Duration, signal string) error DestroyTask(taskID string, force bool) error InspectTask(taskID string) (*TaskStatus, error) - TaskStats(taskID string) (*cstructs.TaskResourceUsage, error) + TaskStats(taskID string) (*TaskResourceUsage, error) TaskEvents(context.Context) (<-chan *TaskEvent, error) SignalTask(taskID string, signal string) error diff --git a/plugins/drivers/testutils/testing.go b/plugins/drivers/testutils/testing.go index a9f9063e4..c802f4b17 100644 --- a/plugins/drivers/testutils/testing.go +++ b/plugins/drivers/testutils/testing.go @@ -13,7 +13,6 @@ import ( "github.com/hashicorp/nomad/client/allocdir" "github.com/hashicorp/nomad/client/config" "github.com/hashicorp/nomad/client/logmon" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/client/taskenv" "github.com/hashicorp/nomad/helper/testlog" "github.com/hashicorp/nomad/helper/uuid" @@ -199,7 +198,7 @@ type MockDriver struct { StopTaskF func(string, time.Duration, string) error DestroyTaskF func(string, bool) error InspectTaskF func(string) (*drivers.TaskStatus, error) - TaskStatsF func(string) (*cstructs.TaskResourceUsage, error) + TaskStatsF func(string) (*drivers.TaskResourceUsage, error) TaskEventsF func(context.Context) (<-chan *drivers.TaskEvent, error) SignalTaskF func(string, string) error ExecTaskF func(string, []string, time.Duration) (*drivers.ExecTaskResult, error) @@ -226,7 +225,7 @@ func (d *MockDriver) DestroyTask(taskID string, force bool) error { func (d *MockDriver) InspectTask(taskID string) (*drivers.TaskStatus, error) { return d.InspectTaskF(taskID) } -func (d *MockDriver) TaskStats(taskID string) (*cstructs.TaskResourceUsage, error) { +func (d *MockDriver) TaskStats(taskID string) (*drivers.TaskResourceUsage, error) { return d.TaskStats(taskID) } func (d *MockDriver) TaskEvents(ctx context.Context) (<-chan *drivers.TaskEvent, error) { diff --git a/plugins/drivers/utils.go b/plugins/drivers/utils.go index 028a6e14d..a56c2c1ba 100644 --- a/plugins/drivers/utils.go +++ b/plugins/drivers/utils.go @@ -4,7 +4,6 @@ import ( "time" "github.com/golang/protobuf/ptypes" - cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/plugins/drivers/proto" ) @@ -381,7 +380,7 @@ func taskStatusFromProto(pb *proto.TaskStatus) (*TaskStatus, error) { }, nil } -func TaskStatsToProto(stats *cstructs.TaskResourceUsage) (*proto.TaskStats, error) { +func TaskStatsToProto(stats *TaskResourceUsage) (*proto.TaskStats, error) { timestamp, err := ptypes.TimestampProto(time.Unix(0, stats.Timestamp)) if err != nil { return nil, err @@ -399,18 +398,18 @@ func TaskStatsToProto(stats *cstructs.TaskResourceUsage) (*proto.TaskStats, erro }, nil } -func TaskStatsFromProto(pb *proto.TaskStats) (*cstructs.TaskResourceUsage, error) { +func TaskStatsFromProto(pb *proto.TaskStats) (*TaskResourceUsage, error) { timestamp, err := ptypes.Timestamp(pb.Timestamp) if err != nil { return nil, err } - pids := map[string]*cstructs.ResourceUsage{} + pids := map[string]*ResourceUsage{} for pid, ru := range pb.ResourceUsageByPid { pids[pid] = resourceUsageFromProto(ru) } - stats := &cstructs.TaskResourceUsage{ + stats := &TaskResourceUsage{ Timestamp: timestamp.Unix(), ResourceUsage: resourceUsageFromProto(pb.AggResourceUsage), Pids: pids, @@ -419,7 +418,7 @@ func TaskStatsFromProto(pb *proto.TaskStats) (*cstructs.TaskResourceUsage, error return stats, nil } -func resourceUsageToProto(ru *cstructs.ResourceUsage) *proto.TaskResourceUsage { +func resourceUsageToProto(ru *ResourceUsage) *proto.TaskResourceUsage { cpu := &proto.CPUUsage{} for _, field := range ru.CpuStats.Measured { switch field { @@ -471,8 +470,8 @@ func resourceUsageToProto(ru *cstructs.ResourceUsage) *proto.TaskResourceUsage { } } -func resourceUsageFromProto(pb *proto.TaskResourceUsage) *cstructs.ResourceUsage { - cpu := cstructs.CpuStats{} +func resourceUsageFromProto(pb *proto.TaskResourceUsage) *ResourceUsage { + cpu := CpuStats{} if pb.Cpu != nil { for _, field := range pb.Cpu.MeasuredFields { switch field { @@ -498,7 +497,7 @@ func resourceUsageFromProto(pb *proto.TaskResourceUsage) *cstructs.ResourceUsage } } - memory := cstructs.MemoryStats{} + memory := MemoryStats{} if pb.Memory != nil { for _, field := range pb.Memory.MeasuredFields { switch field { @@ -521,7 +520,7 @@ func resourceUsageFromProto(pb *proto.TaskResourceUsage) *cstructs.ResourceUsage } } - return &cstructs.ResourceUsage{ + return &ResourceUsage{ CpuStats: &cpu, MemoryStats: &memory, }