From 144f27722646b75c6d74b385d5c58fc10bfe423e Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Mon, 20 Jun 2016 10:19:06 -0700 Subject: [PATCH] finer grain locking --- client/alloc_runner.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/client/alloc_runner.go b/client/alloc_runner.go index c5f68c2dc35..5ecbbf2f106 100644 --- a/client/alloc_runner.go +++ b/client/alloc_runner.go @@ -497,16 +497,15 @@ func (r *AllocRunner) StatsReporter() AllocStatsReporter { // LatestAllocStats returns the latest allocation stats. If the optional taskFilter is set // the allocation stats will only include the given task. func (r *AllocRunner) LatestAllocStats(taskFilter string) (*cstructs.AllocResourceUsage, error) { - r.taskLock.RLock() - defer r.taskLock.RUnlock() - astat := &cstructs.AllocResourceUsage{ Tasks: make(map[string]*cstructs.TaskResourceUsage), } var flat []*cstructs.TaskResourceUsage if taskFilter != "" { + r.taskLock.RLock() tr, ok := r.tasks[taskFilter] + r.taskLock.RUnlock() if !ok { return nil, fmt.Errorf("allocation %q has no task %q", r.alloc.ID, taskFilter) } @@ -517,10 +516,18 @@ func (r *AllocRunner) LatestAllocStats(taskFilter string) (*cstructs.AllocResour astat.Timestamp = l.Timestamp } } else { - for task, tr := range r.tasks { + // Get the task runners + r.taskLock.RLock() + runners := make([]*TaskRunner, 0, len(r.tasks)) + for _, tr := range r.tasks { + runners = append(runners, tr) + } + r.taskLock.RUnlock() + + for _, tr := range runners { l := tr.LatestResourceUsage() if l != nil { - astat.Tasks[task] = l + astat.Tasks[tr.task.Name] = l flat = append(flat, l) if l.Timestamp > astat.Timestamp { astat.Timestamp = l.Timestamp