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

fix(manager) lock client creation #2070

Merged
merged 4 commits into from
Dec 9, 2021
Merged

fix(manager) lock client creation #2070

merged 4 commits into from
Dec 9, 2021

Conversation

rainest
Copy link
Contributor

@rainest rainest commented Dec 9, 2021

What this PR does / why we need it:
Add a mutex and lock it when creating Kong admin clients. Attempting to
create multiple workspaced clients simultaneously can result in a race
condition where both clients check for the workspace before either
creates it, both clients will attempt to create the workspace, and the
second client to attempt creation will hit a unique violation.

Which issue this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close that issue when PR gets merged): fixes CI errors like:

panic: creating workspace: HTTP status 409 (message: "UNIQUE violation detected on '{name=\"notdefault\"}'")

Special notes for your reviewer:

PR Readiness Checklist:

Complete these before marking the PR as ready to review:

  • the CHANGELOG.md release notes have been updated to reflect any significant (and particularly user-facing) changes introduced by this PR

@rainest rainest requested a review from a team as a code owner December 9, 2021 01:12
@rainest rainest temporarily deployed to Configure ci December 9, 2021 01:12 Inactive
@rainest rainest mentioned this pull request Dec 9, 2021
1 task
@shaneutt shaneutt added bug Something isn't working priority/medium labels Dec 9, 2021
Copy link
Contributor

@shaneutt shaneutt left a comment

Choose a reason for hiding this comment

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

This patch does seem to fix the immediate issue, however having the calling code being responsible for managing the locks for the underlying race condition makes maintenance and future contributions a bit more difficult. The burden should be on the implementation of the functionality, I suggest:

  • move the locking mechanism into the GetKongClient() method (OR the lowest level needed to avoid the collision)
  • make the mutex a part of the Config rather than a glob if the lock is moved into a Config method
  • whatever method or function the lock ends up moving to add godoc documentation to it to help provide some additional context

CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Contributor

@mflendrich mflendrich left a comment

Choose a reason for hiding this comment

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

We can get away without locks altogether if we follow the dependency inversion pattern and instantiate the client only once, and pass it down to the callers (e.g. as a function argument) for reuse.

@shaneutt
Copy link
Contributor

shaneutt commented Dec 9, 2021

We can get away without locks altogether if we follow the dependency inversion pattern and instantiate the client only once, and pass it down to the callers (e.g. as a function argument) for reuse.

Having a method called GetKongClient() which by name would appear to get you an HTTP client for Kong not be thread-safe is not particularly axiomatic. Since the implementation has this underlying race condition I still think it's best that we add the lock to it, as close to the actual issue as we can, as we should be ensuring thread-safety at the boundary of our APIs in a way that's opaque to callers. That said, I'm not against also sharing a single client to reduce waste.

@rainest rainest temporarily deployed to Configure ci December 9, 2021 17:42 Inactive
Add a mutex and lock it when creating Kong admin clients. Attempting to
create multiple workspaced clients simultaneously can result in a race
condition where both clients check for the workspace before either
creates it, both clients will attempt to create the workspace, and the
second client to attempt creation will hit a unique violation.
@rainest
Copy link
Contributor Author

rainest commented Dec 9, 2021

Went with the lock within the function. We don't currently create meaningfully different clients, but potentially could if we start interacting with separate Kong instances and/or workspaces from a single controller, which we've discussed in the past.

@rainest rainest temporarily deployed to Configure ci December 9, 2021 18:08 Inactive
@shaneutt shaneutt self-requested a review December 9, 2021 18:09
internal/manager/config.go Outdated Show resolved Hide resolved
@rainest rainest temporarily deployed to Configure ci December 9, 2021 18:22 Inactive
@rainest rainest temporarily deployed to Configure ci December 9, 2021 18:44 Inactive
@rainest rainest enabled auto-merge (squash) December 9, 2021 18:45
@rainest rainest requested a review from shaneutt December 9, 2021 18:45
shaneutt
shaneutt previously approved these changes Dec 9, 2021
Copy link
Contributor

@shaneutt shaneutt left a comment

Choose a reason for hiding this comment

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

One suggestion and then LGTM

internal/manager/config.go Outdated Show resolved Hide resolved
Co-authored-by: Shane Utt <shaneutt@linux.com>
@rainest rainest temporarily deployed to Configure ci December 9, 2021 18:48 Inactive
@rainest rainest requested a review from shaneutt December 9, 2021 18:48
@rainest rainest temporarily deployed to Configure ci December 9, 2021 19:56 Inactive
@rainest rainest merged commit 94f4dcd into main Dec 9, 2021
@rainest rainest deleted the fix/client-race branch December 9, 2021 20:08
@rainest rainest temporarily deployed to Configure ci December 9, 2021 20:09 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants