From b1f443500d7274bb4fdea6b0ff4b682a8a0a6f1a Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Thu, 6 Feb 2020 13:37:42 -0800 Subject: [PATCH] client: fix race accessing Node.status * Call Node.Canonicalize once when Node is created. * Lock when accessing fields mutated by node update goroutine --- client/client.go | 11 +++++++---- command/agent/agent.go | 3 +++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/client/client.go b/client/client.go index 17ef3473b..6996875f4 100644 --- a/client/client.go +++ b/client/client.go @@ -2847,12 +2847,15 @@ func (c *Client) setGaugeForUptime(hStats *stats.HostStats, baseLabels []metrics func (c *Client) emitHostStats() { nodeID := c.NodeID() hStats := c.hostStatsCollector.Stats() - node := c.Node() - node.Canonicalize() + c.configLock.RLock() + nodeStatus := c.configCopy.Node.Status + nodeEligibility := c.configCopy.Node.SchedulingEligibility + c.configLock.RUnlock() + labels := append(c.baseLabels, - metrics.Label{Name: "node_status", Value: node.Status}, - metrics.Label{Name: "node_scheduling_eligibility", Value: node.SchedulingEligibility}, + metrics.Label{Name: "node_status", Value: nodeStatus}, + metrics.Label{Name: "node_scheduling_eligibility", Value: nodeEligibility}, ) c.setGaugeForMemoryStats(nodeID, hStats, labels) diff --git a/command/agent/agent.go b/command/agent/agent.go index 661d7d2b7..fff26e469 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -521,6 +521,9 @@ func convertClientConfig(agentConfig *Config) (*clientconfig.Config, error) { // Set up the HTTP advertise address conf.Node.HTTPAddr = agentConfig.AdvertiseAddrs.HTTP + // Canonicalize Node struct + conf.Node.Canonicalize() + // Reserve resources on the node. // COMPAT(0.10): Remove in 0.10 r := conf.Node.Reserved