Fix locks and use task runners state not alloc state

This commit is contained in:
Alex Dadgar 2016-02-01 15:43:43 -08:00
parent 2d98c0eadd
commit b5260fc14e
2 changed files with 14 additions and 6 deletions

View File

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

View File

@ -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)",