Skip to content

Commit

Permalink
Merge pull request #2096 from hashicorp/b-addAlloc
Browse files Browse the repository at this point in the history
Fix race and remove panic
  • Loading branch information
dadgar committed Dec 13, 2016
2 parents 9c31a41 + da3e51a commit 8b6fcd3
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1397,8 +1397,13 @@ func (c *Client) runAllocs(update *allocUpdates) {
// have already blocked
c.migratingAllocsLock.Lock()
if _, ok := c.migratingAllocs[add.ID]; !ok {
c.migratingAllocs[add.ID] = make(chan struct{})
go c.blockForRemoteAlloc(add)
// Check that we don't have an alloc runner already. This
// prevents a race between a finishing blockForRemoteAlloc and
// another invocation of runAllocs
if _, ok := c.getAllocRunners()[add.PreviousAllocation]; !ok {
c.migratingAllocs[add.ID] = make(chan struct{})
go c.blockForRemoteAlloc(add)
}
}
c.migratingAllocsLock.Unlock()
continue
Expand Down Expand Up @@ -1747,20 +1752,23 @@ func (c *Client) updateAlloc(exist, update *structs.Allocation) error {

// addAlloc is invoked when we should add an allocation
func (c *Client) addAlloc(alloc *structs.Allocation, prevAllocDir *allocdir.AllocDir) error {
c.allocLock.Lock()
defer c.allocLock.Unlock()

// Check if we already have an alloc runner
if _, ok := c.allocs[alloc.ID]; ok {
c.logger.Printf("[DEBUG]: client: dropping duplicate add allocation request: %q", alloc.ID)
return nil
}

c.configLock.RLock()
ar := NewAllocRunner(c.logger, c.configCopy, c.updateAllocStatus, alloc, c.vaultClient)
ar.SetPreviousAllocDir(prevAllocDir)
c.configLock.RUnlock()
go ar.Run()

// Store the alloc runner.
c.allocLock.Lock()
if _, ok := c.allocs[alloc.ID]; ok {
c.allocLock.Unlock()
panic(fmt.Sprintf("Alloc Runner already exists for alloc %q", alloc.ID))
}
c.allocs[alloc.ID] = ar
c.allocLock.Unlock()
return nil
}

Expand Down

0 comments on commit 8b6fcd3

Please sign in to comment.