Skip to content

Commit

Permalink
Fix locks and use task runners state not alloc state
Browse files Browse the repository at this point in the history
  • Loading branch information
dadgar committed Feb 1, 2016
1 parent 410ae59 commit 3b90539
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
17 changes: 12 additions & 5 deletions client/alloc_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
}
Expand Down
3 changes: 2 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down

0 comments on commit 3b90539

Please sign in to comment.