Skip to content

Commit

Permalink
Merge pull request #6207 from hashicorp/b-gc-destroyed-allocs-rerun
Browse files Browse the repository at this point in the history
Don't persist allocs of destroyed alloc runners
  • Loading branch information
Mahmood Ali committed Sep 18, 2019
1 parent bb0e721 commit 6b25c05
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
27 changes: 23 additions & 4 deletions client/allocrunner/alloc_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,14 +763,15 @@ func (ar *allocRunner) destroyImpl() {
// state if Run() ran at all.
<-ar.taskStateUpdateHandlerCh

// Cleanup state db
// Mark alloc as destroyed
ar.destroyedLock.Lock()

// Cleanup state db; while holding the lock to avoid
// a race periodic PersistState that may resurrect the alloc
if err := ar.stateDB.DeleteAllocationBucket(ar.id); err != nil {
ar.logger.Warn("failed to delete allocation state", "error", err)
}

// Mark alloc as destroyed
ar.destroyedLock.Lock()

if !ar.shutdown {
ar.shutdown = true
close(ar.shutdownCh)
Expand All @@ -782,6 +783,24 @@ func (ar *allocRunner) destroyImpl() {
ar.destroyedLock.Unlock()
}

func (ar *allocRunner) PersistState() error {
ar.destroyedLock.Lock()
defer ar.destroyedLock.Unlock()

if ar.destroyed {
err := ar.stateDB.DeleteAllocationBucket(ar.id)
if err != nil {
ar.logger.Warn("failed to delete allocation bucket", "error", err)
}
return nil
}

// TODO: consider persisting deployment state along with task status.
// While we study why only the alloc is persisted, I opted to maintain current
// behavior and not risk adding yet more IO calls unnecessarily.
return ar.stateDB.PutAllocation(ar.Alloc())
}

// Destroy the alloc runner by stopping it if it is still running and cleaning
// up all of its resources.
//
Expand Down
3 changes: 2 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type AllocRunner interface {
ShutdownCh() <-chan struct{}
Signal(taskName, signal string) error
GetTaskEventHandler(taskName string) drivermanager.EventHandler
PersistState() error

RestartTask(taskName string, taskEvent *structs.TaskEvent) error
RestartAll(taskEvent *structs.TaskEvent) error
Expand Down Expand Up @@ -1121,7 +1122,7 @@ func (c *Client) saveState() error {

for id, ar := range runners {
go func(id string, ar AllocRunner) {
err := c.stateDB.PutAllocation(ar.Alloc())
err := ar.PersistState()
if err != nil {
c.logger.Error("error saving alloc state", "error", err, "alloc_id", id)
l.Lock()
Expand Down

0 comments on commit 6b25c05

Please sign in to comment.