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

Improvements to Consul nameresolver #2594

Merged
merged 9 commits into from
Feb 24, 2023
Merged

Conversation

ItalyPaleAle
Copy link
Contributor

@ItalyPaleAle ItalyPaleAle commented Feb 23, 2023

Description

This PR contains improvements to the Consul nameresolver, especially the ResolveID method which was more complex than it needed to be. In particular, the algorithm for picking a random entry from the list of results could be replaced with a much simpler one (complexity: O(n) to O(1), and removes N allocations on the heap), plus other small tweaks.

Checklist

Please make sure you've completed the relevant tasks for this PR, out of the following list:

  • Code compiles correctly
  • Created/updated tests

@ItalyPaleAle ItalyPaleAle requested review from a team as code owners February 23, 2023 21:59
@ItalyPaleAle ItalyPaleAle changed the title Consul: un-register service before Consul: un-register service before registering again Feb 23, 2023
@ItalyPaleAle ItalyPaleAle marked this pull request as draft February 23, 2023 22:25
@ItalyPaleAle
Copy link
Contributor Author

Moving this back to draft as I realized there's a better way to do this: https://developer.hashicorp.com/consul/api-docs/agent/check#deregistercriticalserviceafter

The risk is that if we delete a service, we may delete multiple instances when we just need to delete one. I'll also test in a Load balanced scenario

Signed-off-by: ItalyPaleAle <[email protected]>
@ItalyPaleAle ItalyPaleAle marked this pull request as ready for review February 24, 2023 01:10
@ItalyPaleAle
Copy link
Contributor Author

After further investigation, looks like the initial approach of unregistering the service won't work and can't be made to work. I'm leaving this PR with only the "ancillary" improvements to the component.

if err := r.client.Agent().ServiceRegister(r.config.Registration); err != nil {
agent := r.client.Agent()

err = agent.ServiceRegister(r.config.Registration)
Copy link
Contributor

Choose a reason for hiding this comment

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

I saw your comment about using deregistercriticalserviceafter - but that fix not working.

Should we rather deregister before registering?
Documentation for ServiceDeregister(serviceID string) error (https://developer.hashicorp.com/consul/commands/services/deregister) also recommends that it is meant to be paired with services register.

It may not completely solve the issue but would certainly be a good practice most probably and may also help in solving issues like #2489

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was actually what I first tried doing, and it didn't work because a service can have multiple instances if the app is scaled horizontally. Calling "ServiceDeregister" removed the entire service and so un-registered other instances of the app too.

I spent 2 hours on this yesterday and doesn't really look like Consul has any way to remove only one instance :(

Copy link
Contributor

Choose a reason for hiding this comment

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

Ohh ok, got it.

@ItalyPaleAle ItalyPaleAle changed the title Consul: un-register service before registering again Improvements to Consul nameresolver Feb 24, 2023
Signed-off-by: ItalyPaleAle <[email protected]>
@berndverst berndverst merged commit be5b18e into dapr:master Feb 24, 2023
@ItalyPaleAle ItalyPaleAle added this to the v1.11 milestone Mar 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants