Skip to content

Commit

Permalink
Agent state: do not deregister service checks twice
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Thibault Gilles committed Jan 13, 2020
1 parent 27f49ee commit b86b9b4
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions agent/local/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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,
Expand Down

0 comments on commit b86b9b4

Please sign in to comment.