-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix old services not getting removed from consul on update #1668
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
53d7dfb
Remove unused embedded lock on ExecContext
schmichael a63b2cc
Fix old services not getting removed from consul on update
schmichael 1d8d6b5
Remove extra consul service tracking entirely
schmichael bcbe49e
Assert syncer state is consistent with consul's state
schmichael 75e18f2
Fix error message when querying consul fails
schmichael 8c9f5b7
Make comment more precise
schmichael ff21ded
Query consul without helpers in test to be safe
schmichael File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,10 +119,6 @@ type Syncer struct { | |
// Checks all guarded by the registryLock. | ||
registryLock sync.RWMutex | ||
|
||
// trackedChecks and trackedServices are registered with consul | ||
trackedChecks map[consulCheckID]*consul.AgentCheckRegistration | ||
trackedServices map[consulServiceID]*consul.AgentServiceRegistration | ||
|
||
// checkRunners are delegated Consul checks being ran by the Syncer | ||
checkRunners map[consulCheckID]*CheckRunner | ||
|
||
|
@@ -170,10 +166,10 @@ func NewSyncer(consulConfig *config.ConsulConfig, shutdownCh chan struct{}, logg | |
shutdownCh: shutdownCh, | ||
servicesGroups: make(map[ServiceDomain]map[ServiceKey]*consul.AgentServiceRegistration), | ||
checkGroups: make(map[ServiceDomain]map[ServiceKey][]*consul.AgentCheckRegistration), | ||
trackedServices: make(map[consulServiceID]*consul.AgentServiceRegistration), | ||
trackedChecks: make(map[consulCheckID]*consul.AgentCheckRegistration), | ||
checkRunners: make(map[consulCheckID]*CheckRunner), | ||
periodicCallbacks: make(map[string]types.PeriodicCallback), | ||
// default noop implementation of addrFinder | ||
addrFinder: func(string) (string, int) { return "", 0 }, | ||
} | ||
|
||
return &consulSyncer, nil | ||
|
@@ -264,22 +260,47 @@ func (c *Syncer) SetServices(domain ServiceDomain, services map[ServiceKey]*stru | |
return mErr.ErrorOrNil() | ||
} | ||
|
||
// Update the services and checks groups for this domain | ||
c.groupsLock.Lock() | ||
for serviceKey, service := range registeredServices { | ||
serviceKeys, ok := c.servicesGroups[domain] | ||
if !ok { | ||
serviceKeys = make(map[ServiceKey]*consul.AgentServiceRegistration, len(registeredServices)) | ||
c.servicesGroups[domain] = serviceKeys | ||
|
||
// Create map for service group if it doesn't exist | ||
serviceKeys, ok := c.servicesGroups[domain] | ||
if !ok { | ||
serviceKeys = make(map[ServiceKey]*consul.AgentServiceRegistration, len(registeredServices)) | ||
c.servicesGroups[domain] = serviceKeys | ||
} | ||
|
||
// Remove stale services | ||
for existingServiceKey := range serviceKeys { | ||
if _, ok := registeredServices[existingServiceKey]; !ok { | ||
// Exisitng service needs to be removed | ||
delete(serviceKeys, existingServiceKey) | ||
} | ||
} | ||
|
||
// Add registered services | ||
for serviceKey, service := range registeredServices { | ||
serviceKeys[serviceKey] = service | ||
} | ||
for serviceKey, checks := range registeredChecks { | ||
serviceKeys, ok := c.checkGroups[domain] | ||
if !ok { | ||
serviceKeys = make(map[ServiceKey][]*consul.AgentCheckRegistration, len(registeredChecks)) | ||
c.checkGroups[domain] = serviceKeys | ||
|
||
// Create map for check group if it doesn't exist | ||
checkKeys, ok := c.checkGroups[domain] | ||
if !ok { | ||
checkKeys = make(map[ServiceKey][]*consul.AgentCheckRegistration, len(registeredChecks)) | ||
c.checkGroups[domain] = checkKeys | ||
} | ||
|
||
// Remove stale checks | ||
for existingCheckKey := range checkKeys { | ||
if _, ok := registeredChecks[existingCheckKey]; !ok { | ||
// Exisitng check needs to be removed | ||
delete(checkKeys, existingCheckKey) | ||
} | ||
serviceKeys[serviceKey] = checks | ||
} | ||
|
||
// Add registered checks | ||
for checkKey, checks := range registeredChecks { | ||
checkKeys[checkKey] = checks | ||
} | ||
c.groupsLock.Unlock() | ||
|
||
|
@@ -355,7 +376,12 @@ func (c *Syncer) Shutdown() error { | |
} | ||
|
||
// De-register all the services from Consul | ||
for serviceID := range c.trackedServices { | ||
services, err := c.queryAgentServices() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would change the comment above:
|
||
if err != nil { | ||
c.logger.Printf("[WARN] consul.syncer: failed to register services due to error: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This message is wrong |
||
mErr.Errors = append(mErr.Errors, err) | ||
} | ||
for serviceID := range services { | ||
convertedID := string(serviceID) | ||
if err := c.client.Agent().ServiceDeregister(convertedID); err != nil { | ||
c.logger.Printf("[WARN] consul.syncer: failed to deregister service ID %+q: %v", convertedID, err) | ||
|
@@ -399,9 +425,6 @@ func (c *Syncer) syncChecks() error { | |
if err := c.registerCheck(check); err != nil { | ||
mErr.Errors = append(mErr.Errors, err) | ||
} | ||
c.registryLock.Lock() | ||
c.trackedChecks[consulCheckID(check.ID)] = check | ||
c.registryLock.Unlock() | ||
} | ||
for _, check := range existingChecks { | ||
c.ensureCheckRunning(check) | ||
|
@@ -423,9 +446,6 @@ func (c *Syncer) syncChecks() error { | |
if err := c.deregisterCheck(consulCheckID(check.ID)); err != nil { | ||
mErr.Errors = append(mErr.Errors, err) | ||
} | ||
c.registryLock.Lock() | ||
delete(c.trackedChecks, consulCheckID(check.ID)) | ||
c.registryLock.Unlock() | ||
} | ||
return mErr.ErrorOrNil() | ||
} | ||
|
@@ -444,8 +464,8 @@ func compareConsulCheck(localCheck *consul.AgentCheckRegistration, consulCheck * | |
} | ||
|
||
// calcChecksDiff takes the argument (consulChecks) and calculates the delta | ||
// between the consul.Syncer's list of known checks (c.trackedChecks). Three | ||
// arrays are returned: | ||
// between the consul.Syncer's list of known checks (c.flattenedChecks()). | ||
// Four arrays are returned: | ||
// | ||
// 1) a slice of checks that exist only locally in the Syncer and are missing | ||
// from the Consul Agent (consulChecks) and therefore need to be registered. | ||
|
@@ -479,13 +499,12 @@ func (c *Syncer) calcChecksDiff(consulChecks map[consulCheckID]*consul.AgentChec | |
changedChecksCount = 0 | ||
agentChecks = 0 | ||
) | ||
c.registryLock.RLock() | ||
localChecks := make(map[string]*mergedCheck, len(c.trackedChecks)+len(consulChecks)) | ||
for _, localCheck := range c.flattenedChecks() { | ||
flattenedChecks := c.flattenedChecks() | ||
localChecks := make(map[string]*mergedCheck, len(flattenedChecks)+len(consulChecks)) | ||
for _, localCheck := range flattenedChecks { | ||
localChecksCount++ | ||
localChecks[localCheck.ID] = &mergedCheck{localCheck, 'l'} | ||
} | ||
c.registryLock.RUnlock() | ||
for _, consulCheck := range consulChecks { | ||
if localCheck, found := localChecks[consulCheck.CheckID]; found { | ||
localChecksCount-- | ||
|
@@ -561,7 +580,7 @@ func compareConsulService(localService *consul.AgentServiceRegistration, consulS | |
|
||
// calcServicesDiff takes the argument (consulServices) and calculates the | ||
// delta between the consul.Syncer's list of known services | ||
// (c.trackedServices). Four arrays are returned: | ||
// (c.flattenedServices()). Four arrays are returned: | ||
// | ||
// 1) a slice of services that exist only locally in the Syncer and are | ||
// missing from the Consul Agent (consulServices) and therefore need to be | ||
|
@@ -591,10 +610,9 @@ func (c *Syncer) calcServicesDiff(consulServices map[consulServiceID]*consul.Age | |
changedServicesCount = 0 | ||
agentServices = 0 | ||
) | ||
c.registryLock.RLock() | ||
localServices := make(map[string]*mergedService, len(c.trackedServices)+len(consulServices)) | ||
c.registryLock.RUnlock() | ||
for _, localService := range c.flattenedServices() { | ||
flattenedServices := c.flattenedServices() | ||
localServices := make(map[string]*mergedService, len(flattenedServices)+len(consulServices)) | ||
for _, localService := range flattenedServices { | ||
localServicesCount++ | ||
localServices[localService.ID] = &mergedService{localService, 'l'} | ||
} | ||
|
@@ -656,9 +674,6 @@ func (c *Syncer) syncServices() error { | |
if err := c.client.Agent().ServiceRegister(service); err != nil { | ||
mErr.Errors = append(mErr.Errors, err) | ||
} | ||
c.registryLock.Lock() | ||
c.trackedServices[consulServiceID(service.ID)] = service | ||
c.registryLock.Unlock() | ||
} | ||
for _, service := range changedServices { | ||
// Re-register the local service | ||
|
@@ -670,9 +685,6 @@ func (c *Syncer) syncServices() error { | |
if err := c.deregisterService(service.ID); err != nil { | ||
mErr.Errors = append(mErr.Errors, err) | ||
} | ||
c.registryLock.Lock() | ||
delete(c.trackedServices, consulServiceID(service.ID)) | ||
c.registryLock.Unlock() | ||
} | ||
return mErr.ErrorOrNil() | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is a safe or wise thing to do.