From 4666d85a29555695511ec3c657bb497bcd3219ae Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Mon, 24 Jul 2017 16:48:40 -0700 Subject: [PATCH] Use seen more conservatively --- command/agent/agent.go | 8 -------- command/agent/consul/client.go | 25 +++++++++++-------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index 0f937fb86..251121cdb 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -704,10 +704,8 @@ func (a *Agent) setupConsul(consulConfig *config.ConsulConfig) error { } // Determine version for TLSSkipVerify - seen := false if self, err := client.Agent().Self(); err == nil { a.consulSupportsTLSSkipVerify = consulSupportsTLSSkipVerify(self) - seen = true } // Create Consul Catalog client for service discovery. @@ -716,12 +714,6 @@ func (a *Agent) setupConsul(consulConfig *config.ConsulConfig) error { // Create Consul Service client for service advertisement and checks. a.consulService = consul.NewServiceClient(client.Agent(), a.consulSupportsTLSSkipVerify, a.logger) - // If we've seen the Consul agent already, mark it so future Consul - // errors are logged - if seen { - a.consulService.MarkSeen() - } - // Run the Consul service client's sync'ing main loop go a.consulService.Run() return nil diff --git a/command/agent/consul/client.go b/command/agent/consul/client.go index 1bd89944e..e3e9a9eea 100644 --- a/command/agent/consul/client.go +++ b/command/agent/consul/client.go @@ -147,15 +147,15 @@ func NewServiceClient(consulClient AgentAPI, skipVerifySupport bool, logger *log // seen is used by MarkSeen and Seen const seen = 1 -// MarkSeen marks Consul as having been seen (meaning at least one operation +// markSeen marks Consul as having been seen (meaning at least one operation // has succeeded). -func (c *ServiceClient) MarkSeen() { +func (c *ServiceClient) markSeen() { atomic.StoreInt64(&c.seen, seen) } -// Seen returns true if any Consul operation has ever succeeded. Useful to +// hasSeen returns true if any Consul operation has ever succeeded. Useful to // squelch errors if Consul isn't running. -func (c *ServiceClient) Seen() bool { +func (c *ServiceClient) hasSeen() bool { return atomic.LoadInt64(&c.seen) == seen } @@ -175,11 +175,8 @@ func (c *ServiceClient) Run() { } if err := c.sync(); err != nil { - // Only log and track failures after Consul has been seen - if c.Seen() { - if failures == 0 { - c.logger.Printf("[WARN] consul.sync: failed to update services in Consul: %v", err) - } + if failures == 0 { + c.logger.Printf("[WARN] consul.sync: failed to update services in Consul: %v", err) } failures++ if !retryTimer.Stop() { @@ -264,9 +261,6 @@ func (c *ServiceClient) sync() error { return fmt.Errorf("error querying Consul services: %v", err) } - // A Consul operation has succeeded, mark Consul as having been seen - c.MarkSeen() - consulChecks, err := c.client.Checks() if err != nil { metrics.IncrCounter([]string{"client", "consul", "sync_failure"}, 1) @@ -362,6 +356,9 @@ func (c *ServiceClient) sync() error { } } + // A Consul operation has succeeded, mark Consul as having been seen + c.markSeen() + c.logger.Printf("[DEBUG] consul.sync: registered %d services, %d checks; deregistered %d services, %d checks", sreg, creg, sdereg, cdereg) return nil @@ -692,8 +689,8 @@ func (c *ServiceClient) Shutdown() error { // Don't wait forever though } - // If Consul was never seen, exit early - if !c.Seen() { + // If Consul was never seen nothing could be written so exit early + if !c.hasSeen() { return nil }