Skip to content

Commit 772c04e

Browse files
authored
Merge pull request #1844 from hashicorp/b-alloc-stats
Don't query for stats if the node is down and handle the errors
2 parents b0749c7 + 0553288 commit 772c04e

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

api/allocations.go

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import (
88
"github.com/hashicorp/go-cleanhttp"
99
)
1010

11+
var (
12+
// NodeDownErr marks an operation as not able to complete since the node is
13+
// down.
14+
NodeDownErr = fmt.Errorf("node down")
15+
)
16+
1117
// Allocations is used to query the alloc-related endpoints.
1218
type Allocations struct {
1319
client *Client
@@ -48,6 +54,9 @@ func (a *Allocations) Stats(alloc *Allocation, q *QueryOptions) (*AllocResourceU
4854
if err != nil {
4955
return nil, err
5056
}
57+
if node.Status == "down" {
58+
return nil, NodeDownErr
59+
}
5160
if node.HTTPAddr == "" {
5261
return nil, fmt.Errorf("http addr of the node where alloc %q is running is not advertised", alloc.ID)
5362
}

command/alloc_status.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,11 @@ func (c *AllocStatusCommand) Run(args []string) int {
234234
stats, statsErr = client.Allocations().Stats(alloc, nil)
235235
if statsErr != nil {
236236
c.Ui.Output("")
237-
c.Ui.Error(fmt.Sprintf("couldn't retrieve stats (HINT: ensure Client.Advertise.HTTP is set): %v", statsErr))
237+
if statsErr != api.NodeDownErr {
238+
c.Ui.Error(fmt.Sprintf("couldn't retrieve stats (HINT: ensure Client.Advertise.HTTP is set): %v", statsErr))
239+
} else {
240+
c.Ui.Output("Omitting resource statistics since the node is down.")
241+
}
238242
}
239243
c.outputTaskDetails(alloc, stats, displayStats)
240244
}
@@ -408,12 +412,14 @@ func (c *AllocStatusCommand) outputTaskResources(alloc *api.Allocation, task str
408412
// Display the rolled up stats. If possible prefer the live stastics
409413
cpuUsage := strconv.Itoa(resource.CPU)
410414
memUsage := humanize.IBytes(uint64(resource.MemoryMB * bytesPerMegabyte))
411-
if ru, ok := stats.Tasks[task]; ok && ru != nil && ru.ResourceUsage != nil {
412-
if cs := ru.ResourceUsage.CpuStats; cs != nil {
413-
cpuUsage = fmt.Sprintf("%v/%v", math.Floor(cs.TotalTicks), resource.CPU)
414-
}
415-
if ms := ru.ResourceUsage.MemoryStats; ms != nil {
416-
memUsage = fmt.Sprintf("%v/%v", humanize.IBytes(ms.RSS), memUsage)
415+
if stats != nil {
416+
if ru, ok := stats.Tasks[task]; ok && ru != nil && ru.ResourceUsage != nil {
417+
if cs := ru.ResourceUsage.CpuStats; cs != nil {
418+
cpuUsage = fmt.Sprintf("%v/%v", math.Floor(cs.TotalTicks), resource.CPU)
419+
}
420+
if ms := ru.ResourceUsage.MemoryStats; ms != nil {
421+
memUsage = fmt.Sprintf("%v/%v", humanize.IBytes(ms.RSS), memUsage)
422+
}
417423
}
418424
}
419425
resourcesOutput = append(resourcesOutput, fmt.Sprintf("%v MHz|%v|%v|%v|%v",
@@ -427,9 +433,11 @@ func (c *AllocStatusCommand) outputTaskResources(alloc *api.Allocation, task str
427433
}
428434
c.Ui.Output(formatListWithSpaces(resourcesOutput))
429435

430-
if ru, ok := stats.Tasks[task]; ok && ru != nil && displayStats && ru.ResourceUsage != nil {
431-
c.Ui.Output("")
432-
c.outputVerboseResourceUsage(task, ru.ResourceUsage)
436+
if stats != nil {
437+
if ru, ok := stats.Tasks[task]; ok && ru != nil && displayStats && ru.ResourceUsage != nil {
438+
c.Ui.Output("")
439+
c.outputVerboseResourceUsage(task, ru.ResourceUsage)
440+
}
433441
}
434442
}
435443

0 commit comments

Comments
 (0)