Lock around saving state

Prevent interleaving state syncs as it could conceivably lead to
empty state files as per #1367
This commit is contained in:
Michael Schurter 2016-09-02 15:39:22 -07:00
parent e7dd443447
commit 6cb6d9cdf1
2 changed files with 13 additions and 0 deletions

View File

@ -66,6 +66,9 @@ type AllocRunner struct {
destroyCh chan struct{} destroyCh chan struct{}
destroyLock sync.Mutex destroyLock sync.Mutex
waitCh chan struct{} waitCh chan struct{}
// serialize saveAllocRunnerState calls
persistLock sync.Mutex
} }
// allocRunnerState is used to snapshot the state of the alloc runner // allocRunnerState is used to snapshot the state of the alloc runner
@ -179,8 +182,12 @@ func (r *AllocRunner) SaveState() error {
} }
func (r *AllocRunner) saveAllocRunnerState() error { func (r *AllocRunner) saveAllocRunnerState() error {
r.persistLock.Lock()
defer r.persistLock.Unlock()
// Create the snapshot. // Create the snapshot.
alloc := r.Alloc() alloc := r.Alloc()
r.allocLock.Lock() r.allocLock.Lock()
states := alloc.TaskStates states := alloc.TaskStates
allocClientStatus := r.allocClientStatus allocClientStatus := r.allocClientStatus

View File

@ -69,6 +69,9 @@ type TaskRunner struct {
destroyLock sync.Mutex destroyLock sync.Mutex
destroyEvent *structs.TaskEvent destroyEvent *structs.TaskEvent
waitCh chan struct{} waitCh chan struct{}
// serialize SaveState calls
persistLock sync.Mutex
} }
// taskRunnerState is used to snapshot the state of the task runner // taskRunnerState is used to snapshot the state of the task runner
@ -186,6 +189,9 @@ func (r *TaskRunner) RestoreState() error {
// SaveState is used to snapshot our state // SaveState is used to snapshot our state
func (r *TaskRunner) SaveState() error { func (r *TaskRunner) SaveState() error {
r.persistLock.Lock()
defer r.persistLock.Unlock()
snap := taskRunnerState{ snap := taskRunnerState{
Task: r.task, Task: r.task,
Version: r.config.Version, Version: r.config.Version,