diff --git a/client/alloc_runner.go b/client/alloc_runner.go index 6dc47f728..e6f17746a 100644 --- a/client/alloc_runner.go +++ b/client/alloc_runner.go @@ -486,6 +486,8 @@ func (r *AllocRunner) StatsReporter() AllocStatsReporter { // AllocStats returns the stats reporter of all the tasks running in the // allocation func (r *AllocRunner) AllocStats() map[string]TaskStatsReporter { + r.taskLock.Lock() + defer r.taskLock.RUnlock() res := make(map[string]TaskStatsReporter) for task, tr := range r.tasks { res[task] = tr.StatsReporter() @@ -498,7 +500,7 @@ func (r *AllocRunner) AllocStats() map[string]TaskStatsReporter { func (r *AllocRunner) TaskStats(task string) (TaskStatsReporter, error) { tr, ok := r.tasks[task] if !ok { - return nil, fmt.Errorf("task %q not running", task) + return nil, fmt.Errorf("task %q not running in allocation %v", task, r.alloc.ID) } return tr.StatsReporter(), nil diff --git a/client/client.go b/client/client.go index b809248de..1a0bd889e 100644 --- a/client/client.go +++ b/client/client.go @@ -72,6 +72,10 @@ const ( // consulSyncInterval is the interval at which the client syncs with consul // to remove services and checks which are no longer valid consulSyncInterval = 15 * time.Second + + // hostStatsCollectorIntv is the interval on which we collect host resource + // usage stats + hostStatsCollectorIntv = 1 * time.Second ) // DefaultConfig returns the default configuration @@ -213,7 +217,7 @@ func NewClient(cfg *config.Config) (*Client, error) { go c.run() // Start collecting stats - go c.monitorUsage() + go c.monitorHostStats() // Start the consul sync go c.syncConsul() @@ -430,7 +434,8 @@ func (c *Client) StatsReporter() ClientStatsReporter { // Nomad client func (c *Client) AllocStats() map[string]AllocStatsReporter { res := make(map[string]AllocStatsReporter) - for alloc, ar := range c.allocs { + allocRunners := c.getAllocRunners() + for alloc, ar := range allocRunners { res[alloc] = ar } return res @@ -1277,9 +1282,10 @@ func (c *Client) syncConsul() { } } -func (c *Client) monitorUsage() { +// monitorHostStats collects host resource usage stats periodically +func (c *Client) monitorHostStats() { + next := time.NewTimer(hostStatsCollectorIntv) for { - next := time.NewTimer(1 * time.Second) select { case <-next.C: ru, err := c.hostStatsCollector.Collect()