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

Tiny client race condition fix #2780

Merged
merged 2 commits into from
Jul 6, 2017
Merged
Changes from 1 commit
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
29 changes: 15 additions & 14 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,8 @@ func (c *Client) updateAllocStatus(alloc *structs.Allocation) {
delete(c.blockedAllocations, blockedAlloc.PreviousAllocation)
c.blockedAllocsLock.Unlock()

c.logger.Printf("[DEBUG] client: unblocking alloc %s because alloc %s terminated", blockedAlloc.ID, alloc.ID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%q please for IDs


// Need to call addAlloc without holding the lock
if err := c.addAlloc(blockedAlloc, prevAllocDir); err != nil {
c.logger.Printf("[ERR] client: failed to add alloc which was previously blocked %q: %v",
Expand Down Expand Up @@ -1606,8 +1608,8 @@ func (c *Client) runAllocs(update *allocUpdates) {
// Check if the alloc is already present in the blocked allocations
// map
if _, ok := c.blockedAllocations[add.PreviousAllocation]; !ok {
c.logger.Printf("[DEBUG] client: added alloc %q to blocked queue for previous allocation %q", add.ID,
add.PreviousAllocation)
c.logger.Printf("[DEBUG] client: added alloc %s to blocked queue for previous allocation %s",
add.ID, add.PreviousAllocation)
c.blockedAllocations[add.PreviousAllocation] = add
}
c.blockedAllocsLock.Unlock()
Expand Down Expand Up @@ -1988,29 +1990,28 @@ func (c *Client) addAlloc(alloc *structs.Allocation, prevAllocDir *allocdir.Allo
c.allocLock.Unlock()
return nil
}
c.allocLock.Unlock()

// Make room for the allocation
if err := c.garbageCollector.MakeRoomFor([]*structs.Allocation{alloc}); err != nil {
c.logger.Printf("[ERR] client: error making room for allocation: %v", err)
}

c.allocLock.Lock()
defer c.allocLock.Unlock()

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

// Store the alloc runner.
c.allocs[alloc.ID] = ar

if err := ar.SaveState(); err != nil {
c.logger.Printf("[WARN] client: initial save state for alloc %q failed: %v", alloc.ID, err)
}

go ar.Run()
// Must release allocLock as GC acquires it to count allocs
c.allocLock.Unlock()

// Store the alloc runner.
c.allocs[alloc.ID] = ar
// Make room for the allocation before running it
if err := c.garbageCollector.MakeRoomFor([]*structs.Allocation{alloc}); err != nil {
c.logger.Printf("[ERR] client: error making room for allocation: %v", err)
}

go ar.Run()
return nil
}

Expand Down