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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport of grpc: fix data race in balancer registration into release/1.15.x #17351

Conversation

hc-github-team-consul-core
Copy link
Collaborator

Backport

This PR is auto-generated from #16229 to be assessed for backporting due to the inclusion of the label backport/1.15.

馃毃

Warning automatic cherry-pick of commits failed. If the first commit failed,
you will see a blank no-op commit below. If at least one commit succeeded, you
will see the cherry-picked commits up to, not including, the commit where
the merge conflict occurred.

The person who merged in the original PR is:
@boxofrad
This person should manually cherry-pick the original PR into a new backport PR,
and close this one when the manual backport PR is merged in.

merge conflict error: POST https://api.github.com/repos/hashicorp/consul/merges: 409 Merge conflict []

The below text is copied from the body of the original PR.


Description

Registering gRPC balancers is thread-unsafe because they are stored in a global map variable that is accessed without holding a lock. Therefore, it's expected that balancers are registered once at the beginning of your program (e.g. in a package init function) and certainly not after you've started dialing connections, etc.

NOTE: this function must only be called during initialization time (i.e. in an init() function), and is not thread-safe.

While this is fine for us in production, it's challenging for tests that spin up multiple agents in-memory. We currently register a balancer per-agent which holds agent-specific state that cannot safely be shared.

This commit introduces our own registry that is thread-safe, and implements the Builder interface such that we can call gRPC's Register method once, on start-up. It uses the same pattern as our resolver registry where we use the dial target's host (aka "authority"), which is unique per-agent, to determine which builder to use.


Overview of commits

@hc-github-team-consul-core hc-github-team-consul-core force-pushed the backport/boxofrad/thread-safe-balancer-register/strongly-new-jackal branch 2 times, most recently from fd63a22 to 85fbc59 Compare May 15, 2023 10:52
@hashicorp-cla
Copy link

hashicorp-cla commented May 15, 2023

CLA assistant check
All committers have signed the CLA.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Auto approved Consul Bot automated PR

Registering gRPC balancers is thread-unsafe because they are stored in a
global map variable that is accessed without holding a lock. Therefore,
it's expected that balancers are registered _once_ at the beginning of
your program (e.g. in a package `init` function) and certainly not after
you've started dialing connections, etc.

> NOTE: this function must only be called during initialization time
> (i.e. in an init() function), and is not thread-safe.

While this is fine for us in production, it's challenging for tests that
spin up multiple agents in-memory. We currently register a balancer per-
agent which holds agent-specific state that cannot safely be shared.

This commit introduces our own registry that _is_ thread-safe, and
implements the Builder interface such that we can call gRPC's `Register`
method once, on start-up. It uses the same pattern as our resolver
registry where we use the dial target's host (aka "authority"), which is
unique per-agent, to determine which builder to use.
@boxofrad boxofrad force-pushed the backport/boxofrad/thread-safe-balancer-register/strongly-new-jackal branch from daf53d0 to b9cdc79 Compare May 15, 2023 10:55
@boxofrad boxofrad marked this pull request as ready for review May 15, 2023 11:33
@boxofrad boxofrad merged commit 64c29e5 into release/1.15.x May 15, 2023
129 checks passed
@boxofrad boxofrad deleted the backport/boxofrad/thread-safe-balancer-register/strongly-new-jackal branch May 15, 2023 11:34
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.

None yet

4 participants