Acquiring locks before iterating allocations and tasks

This commit is contained in:
Diptanu Choudhury
2016-05-24 16:42:53 -07:00
parent b755ab9341
commit 2b1f3896cc
2 changed files with 13 additions and 5 deletions

View File

@@ -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

View File

@@ -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()