Skip to content
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

Log a better message on trying to deregister a nonexistent service #2492

Merged
merged 2 commits into from
Nov 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,14 @@ func (a *Agent) RemoveService(serviceID string, persist bool) error {
}

// Remove service immediately
a.state.RemoveService(serviceID)
err := a.state.RemoveService(serviceID)

// TODO: Return the error instead of just logging here in Consul 0.8
// For now, keep the current idempotent behavior on deleting a nonexistent service
if err != nil {
a.logger.Printf("[WARN] agent: Failed to deregister service %q: %s", serviceID, err)
return nil
}

// Remove the service from the data dir
if persist {
Expand Down
5 changes: 5 additions & 0 deletions command/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,11 @@ func TestAgent_PurgeService(t *testing.T) {
t.Fatalf("err: %s", err)
}

// Re-add the service
if err := agent.AddService(svc, nil, true, ""); err != nil {
t.Fatalf("err: %v", err)
}

// Removed
if err := agent.RemoveService(svc.ID, true); err != nil {
t.Fatalf("err: %s", err)
Expand Down
16 changes: 11 additions & 5 deletions command/agent/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,20 @@ func (l *localState) AddService(service *structs.NodeService, token string) {

// RemoveService is used to remove a service entry from the local state.
// The agent will make a best effort to ensure it is deregistered
func (l *localState) RemoveService(serviceID string) {
func (l *localState) RemoveService(serviceID string) error {
l.Lock()
defer l.Unlock()

delete(l.services, serviceID)
delete(l.serviceTokens, serviceID)
l.serviceStatus[serviceID] = syncStatus{inSync: false}
l.changeMade()
if _, ok := l.services[serviceID]; ok {
delete(l.services, serviceID)
delete(l.serviceTokens, serviceID)
l.serviceStatus[serviceID] = syncStatus{inSync: false}
l.changeMade()
} else {
return fmt.Errorf("Service does not exist")
}

return nil
}

// Services returns the locally registered services that the
Expand Down
4 changes: 4 additions & 0 deletions command/agent/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,10 @@ func TestAgent_serviceTokens(t *testing.T) {
l := new(localState)
l.Init(config, nil)

l.AddService(&structs.NodeService{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this related to the log change?

Copy link
Contributor Author

@kyhavlov kyhavlov Nov 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because if we don't add the service initially, it'll panic on trying to log the warning with the nil logger we initialize with above

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh makes sense - this test was kind of bogus w/o the registration.

ID: "redis",
}, "")

// Returns default when no token is set
if token := l.ServiceToken("redis"); token != "default" {
t.Fatalf("bad: %s", token)
Expand Down