From 6c36f76534a90c05f3d3f4f5b1d938fb6b5bb7bd Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Fri, 27 Oct 2017 17:00:11 -0700 Subject: [PATCH] Fix regression by returning error on unknown alloc --- client/client.go | 7 ++++--- client/gc.go | 8 +++++--- command/agent/alloc_endpoint.go | 5 ++++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/client/client.go b/client/client.go index 381442f40f0..3c0faa5f1ef 100644 --- a/client/client.go +++ b/client/client.go @@ -487,9 +487,10 @@ func (c *Client) Stats() map[string]map[string]string { return stats } -// CollectAllocation garbage collects a single allocation -func (c *Client) CollectAllocation(allocID string) { - c.garbageCollector.Collect(allocID) +// CollectAllocation garbage collects a single allocation on a node. Returns +// true if alloc was found and garbage collected; otherwise false. +func (c *Client) CollectAllocation(allocID string) bool { + return c.garbageCollector.Collect(allocID) } // CollectAllAllocs garbage collects all allocations on a node in the terminal diff --git a/client/gc.go b/client/gc.go index 54873e159ca..a7332ebc4cf 100644 --- a/client/gc.go +++ b/client/gc.go @@ -196,14 +196,16 @@ func (a *AllocGarbageCollector) Stop() { close(a.shutdownCh) } -// Collect garbage collects a single allocation on a node -func (a *AllocGarbageCollector) Collect(allocID string) { +// Collect garbage collects a single allocation on a node. Returns true if +// alloc was found and garbage collected; otherwise false. +func (a *AllocGarbageCollector) Collect(allocID string) bool { if gcAlloc := a.allocRunners.Remove(allocID); gcAlloc != nil { a.destroyAllocRunner(gcAlloc.allocRunner, "forced collection") - return + return true } a.logger.Printf("[DEBUG] client.gc: alloc %s is invalid or was already garbage collected", allocID) + return false } // CollectAll garbage collects all termianated allocations on a node diff --git a/command/agent/alloc_endpoint.go b/command/agent/alloc_endpoint.go index 41f5d9721a5..65bcbd0116f 100644 --- a/command/agent/alloc_endpoint.go +++ b/command/agent/alloc_endpoint.go @@ -133,7 +133,10 @@ func (s *HTTPServer) allocGC(allocID string, resp http.ResponseWriter, req *http return nil, structs.ErrPermissionDenied } - s.agent.Client().CollectAllocation(allocID) + if !s.agent.Client().CollectAllocation(allocID) { + // Could not find alloc + return nil, fmt.Errorf("unable to collect allocation: not present") + } return nil, nil }