From 3b90539fe9f2ad576afe691179dafd7162d01a5b Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Mon, 1 Feb 2016 15:43:43 -0800 Subject: [PATCH] Fix locks and use task runners state not alloc state --- client/alloc_runner.go | 17 ++++++++++++----- client/client.go | 3 ++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/client/alloc_runner.go b/client/alloc_runner.go index f17a420b4d3..2d52c07f6e0 100644 --- a/client/alloc_runner.go +++ b/client/alloc_runner.go @@ -156,6 +156,8 @@ func (r *AllocRunner) SaveState() error { func (r *AllocRunner) saveAllocRunnerState() error { r.taskStatusLock.RLock() defer r.taskStatusLock.RUnlock() + r.allocLock.Lock() + defer r.allocLock.Unlock() snap := allocRunnerState{ Alloc: r.alloc, RestartPolicy: r.RestartPolicy, @@ -224,7 +226,8 @@ func (r *AllocRunner) syncStatus() error { // Scan the task states to determine the status of the alloc var pending, running, dead, failed bool r.taskStatusLock.RLock() - for _, state := range r.alloc.TaskStates { + for _, tr := range r.tasks { + state := tr.state switch state.State { case structs.TaskStateRunning: running = true @@ -239,13 +242,17 @@ func (r *AllocRunner) syncStatus() error { } } } + r.taskStatusLock.RUnlock() + + // Determine the alloc status + r.allocLock.Lock() + defer r.allocLock.Unlock() + if len(r.alloc.TaskStates) > 0 { taskDesc, _ := json.Marshal(r.alloc.TaskStates) r.alloc.ClientDescription = string(taskDesc) } - r.taskStatusLock.RUnlock() - // Determine the alloc status if failed { r.alloc.ClientStatus = structs.AllocClientStatusFailed } else if running { @@ -379,8 +386,8 @@ OUTER: } // Destroy each sub-task - r.taskLock.RLock() - defer r.taskLock.RUnlock() + r.taskLock.Lock() + defer r.taskLock.Unlock() for _, tr := range r.tasks { tr.Destroy() } diff --git a/client/client.go b/client/client.go index c0b7cb70338..8a51fc07336 100644 --- a/client/client.go +++ b/client/client.go @@ -795,8 +795,9 @@ func (c *Client) watchAllocations(updates chan *allocUpdates) { runner, ok := c.allocs[allocID] if !ok || runner.shouldUpdate(modifyIndex) { pull = append(pull, allocID) + } else { + filtered[allocID] = struct{}{} } - filtered[allocID] = struct{}{} } c.allocLock.Unlock() c.logger.Printf("[DEBUG] client: updated allocations at index %d (pulled %d) (filtered %d)",