Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race and remove panic #2096

Merged
merged 1 commit into from
Dec 13, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1744,20 +1749,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