From 10cc50ad2939d213e293c081137d893b2efee9cf Mon Sep 17 00:00:00 2001 From: capone Date: Tue, 23 Aug 2016 19:09:20 +0300 Subject: [PATCH 1/2] First attemt to fix issue #1636 --- command/agent/consul/check.go | 6 ++++++ command/agent/consul/syncer.go | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/command/agent/consul/check.go b/command/agent/consul/check.go index 28df291f6..1b601ea36 100644 --- a/command/agent/consul/check.go +++ b/command/agent/consul/check.go @@ -47,6 +47,12 @@ func (r *CheckRunner) Start() { r.started = true } +func (r *CheckRunner) Started() bool { + r.startedLock.Lock() + defer r.startedLock.Unlock() + return r.started +} + // Stop is used to stop the check. func (r *CheckRunner) Stop() { r.stopLock.Lock() diff --git a/command/agent/consul/syncer.go b/command/agent/consul/syncer.go index 736e15c8e..f7d78f7ef 100644 --- a/command/agent/consul/syncer.go +++ b/command/agent/consul/syncer.go @@ -394,7 +394,7 @@ func (c *Syncer) syncChecks() error { } // Synchronize checks with Consul - missingChecks, _, changedChecks, staleChecks := c.calcChecksDiff(consulChecks) + missingChecks, existingChecks, changedChecks, staleChecks := c.calcChecksDiff(consulChecks) for _, check := range missingChecks { if err := c.registerCheck(check); err != nil { mErr.Errors = append(mErr.Errors, err) @@ -403,6 +403,9 @@ func (c *Syncer) syncChecks() error { c.trackedChecks[consulCheckID(check.ID)] = check c.registryLock.Unlock() } + for _, check := range existingChecks { + c.ensureCheckRunning(check) + } for _, check := range changedChecks { // NOTE(sean@): Do we need to deregister the check before // re-registering it? Not deregistering to avoid missing the @@ -684,6 +687,20 @@ func (c *Syncer) registerCheck(chkReg *consul.AgentCheckRegistration) error { return c.client.Agent().CheckRegister(chkReg) } +// ensureCheckRunning verifies that registerd check is in running state +func (c *Syncer) ensureCheckRunning(chk *consul.AgentCheckRegistration) { + c.registryLock.RLock() + defer c.registryLock.RUnlock() + cr, ok := c.checkRunners[consulCheckID(chk.ID)] + if !ok { + return + } + if !cr.Started() { + c.logger.Printf("[DEBUG] ensureCheckRunning Running existing check. %v", chk) + cr.Start() + } +} + // createCheckReg creates a Check that can be registered with Nomad. It also // creates a Nomad check for the check types that it can handle. func (c *Syncer) createCheckReg(check *structs.ServiceCheck, serviceReg *consul.AgentServiceRegistration) (*consul.AgentCheckRegistration, error) { From ce4d236a4076f7d4b4e48aaa3485cbdcedc29c18 Mon Sep 17 00:00:00 2001 From: capone Date: Wed, 24 Aug 2016 01:33:44 +0300 Subject: [PATCH 2/2] Fixed CR defects --- command/agent/consul/check.go | 1 + command/agent/consul/syncer.go | 10 +++------- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/command/agent/consul/check.go b/command/agent/consul/check.go index 1b601ea36..551f94b6f 100644 --- a/command/agent/consul/check.go +++ b/command/agent/consul/check.go @@ -47,6 +47,7 @@ func (r *CheckRunner) Start() { r.started = true } +// Started returns if the check runner has started running func (r *CheckRunner) Started() bool { r.startedLock.Lock() defer r.startedLock.Unlock() diff --git a/command/agent/consul/syncer.go b/command/agent/consul/syncer.go index f7d78f7ef..8ac66fd9f 100644 --- a/command/agent/consul/syncer.go +++ b/command/agent/consul/syncer.go @@ -687,16 +687,12 @@ func (c *Syncer) registerCheck(chkReg *consul.AgentCheckRegistration) error { return c.client.Agent().CheckRegister(chkReg) } -// ensureCheckRunning verifies that registerd check is in running state +// ensureCheckRunning starts the check runner for a check if it's not already running func (c *Syncer) ensureCheckRunning(chk *consul.AgentCheckRegistration) { c.registryLock.RLock() defer c.registryLock.RUnlock() - cr, ok := c.checkRunners[consulCheckID(chk.ID)] - if !ok { - return - } - if !cr.Started() { - c.logger.Printf("[DEBUG] ensureCheckRunning Running existing check. %v", chk) + if cr, ok := c.checkRunners[consulCheckID(chk.ID)]; ok && !cr.Started() { + c.logger.Printf("[DEBUG] consul.syncer: starting runner for existing check. %v", chk.ID) cr.Start() } }