From 7a01a96cc2d6c9c9ff39bc39d49184b5139af9f9 Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Tue, 11 Jun 2019 10:30:09 -0400 Subject: [PATCH] Fallback to `alloc.TaskResources` for old allocs When a client is running against an old server (e.g. running 0.8), `alloc.AllocatedResources` may be nil, and we need to check the deprecated `alloc.TaskResources` instead. Fixes https://github.com/hashicorp/nomad/issues/5810 --- client/allocrunner/taskrunner/task_runner.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/client/allocrunner/taskrunner/task_runner.go b/client/allocrunner/taskrunner/task_runner.go index 51c4f58b0d9d..352815654651 100644 --- a/client/allocrunner/taskrunner/task_runner.go +++ b/client/allocrunner/taskrunner/task_runner.go @@ -1181,9 +1181,15 @@ func (tr *TaskRunner) UpdateStats(ru *cstructs.TaskResourceUsage) { func (tr *TaskRunner) setGaugeForMemory(ru *cstructs.TaskResourceUsage) { alloc := tr.Alloc() var allocatedMem float32 - if taskRes := alloc.AllocatedResources.Tasks[tr.taskName]; taskRes != nil { - // Convert to bytes to match other memory metrics - allocatedMem = float32(taskRes.Memory.MemoryMB) * 1024 * 1024 + if alloc.AllocatedResources != nil { + if taskRes := alloc.AllocatedResources.Tasks[tr.taskName]; taskRes != nil { + // Convert to bytes to match other memory metrics + allocatedMem = float32(taskRes.Memory.MemoryMB) * 1024 * 1024 + } + } else if taskRes := alloc.TaskResources[tr.taskName]; taskRes != nil { + // COMPAT(0.11) Remove in 0.11 when TaskResources is removed + allocatedMem = float32(taskRes.MemoryMB) * 1024 * 1024 + } if !tr.clientConfig.DisableTaggedMetrics {