agent: do not deregister service checks twice (#6168)

Deregistering a service from the catalog automatically deregisters its
checks, however the agent still performs a deregister call for each
service checks even after the service has been deregistered.
With ACLs enabled this results in logs like:
"message:consul: "Catalog.Deregister" RPC failed to server
server_ip:8300: rpc error making call: rpc error making call: Unknown
check 'check_id'"
This change removes associated checks from the agent state when
deregistering a service, which results in less calls to the servers and
supresses the error logs.
This commit is contained in:
Aestek 2020-01-17 14:26:53 +01:00 committed by Hans Hasselberg
parent ce023359fe
commit 5dc8875bd3
1 changed files with 15 additions and 5 deletions

View File

@ -1091,6 +1091,12 @@ func (l *State) deleteService(key structs.ServiceID) error {
switch {
case err == nil || strings.Contains(err.Error(), "Unknown service"):
delete(l.services, key)
// service deregister also deletes associated checks
for _, c := range l.checks {
if c.Deleted && c.Check.ServiceID == key.ID {
l.pruneCheck(c.Check.CompoundCheckID())
}
}
l.logger.Printf("[INFO] agent: Deregistered service %q", key.ID)
return nil
@ -1125,11 +1131,7 @@ func (l *State) deleteCheck(key structs.CheckID) error {
err := l.Delegate.RPC("Catalog.Deregister", &req, &out)
switch {
case err == nil || strings.Contains(err.Error(), "Unknown check"):
c := l.checks[key]
if c != nil && c.DeferCheck != nil {
c.DeferCheck.Stop()
}
delete(l.checks, key)
l.pruneCheck(key)
l.logger.Printf("[INFO] agent: Deregistered check %q", key.String())
return nil
@ -1147,6 +1149,14 @@ func (l *State) deleteCheck(key structs.CheckID) error {
}
}
func (l *State) pruneCheck(id structs.CheckID) {
c := l.checks[id]
if c != nil && c.DeferCheck != nil {
c.DeferCheck.Stop()
}
delete(l.checks, id)
}
// syncService is used to sync a service to the server
func (l *State) syncService(key structs.ServiceID) error {
// If the service has associated checks that are out of sync,