Skip to content

detector: handle AlreadyExists errors as transient during concurrent reconciliations#7170

Open
arnavgogia20 wants to merge 4 commits intokarmada-io:masterfrom
arnavgogia20:fix/issue-7120-concurrent-reconcile
Open

detector: handle AlreadyExists errors as transient during concurrent reconciliations#7170
arnavgogia20 wants to merge 4 commits intokarmada-io:masterfrom
arnavgogia20:fix/issue-7120-concurrent-reconcile

Conversation

@arnavgogia20
Copy link
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:

This PR fixes race conditions in the detector controller where concurrent reconciliations can trigger AlreadyExists errors during ResourceBinding and ClusterResourceBinding creation.

Previously, only conflict errors were retried. However, under concurrent execution, AlreadyExists is also a transient and expected outcome that should not be treated as a terminal failure. This PR updates the retry logic to explicitly retry on both Conflict and AlreadyExists errors, embracing eventual consistency and preventing unnecessary reconcile failures that violate SLOs.

Which issue(s) this PR fixes:

Fixes #7120

Special notes for your reviewer:

  • Replaced retry.RetryOnConflict with retry.OnError using a named predicate to retry on both Conflict and AlreadyExists errors.
  • Applied consistently across ApplyPolicy and ApplyClusterPolicy paths.
  • Added focused unit tests using an intercepting fake client to deterministically simulate AlreadyExists race conditions.
  • Verified with:
    go test ./pkg/detector/...
    

Does this PR introduce a user-facing change?:

NONE

Replace retry.RetryOnConflict with retry.OnError and introduce a named retry predicate handling Conflict + AlreadyExists. This ensures transient AlreadyExists errors during concurrent reconciliations are retried instead of failing.

Signed-off-by: arnavgogia20 <arnavgogia404@gmail.com>
Add pkg/detector/detector_retry_test.go with InterceptingClient to simulate race conditions and verify correct retry behavior for ApplyPolicy and ApplyClusterPolicy.

Signed-off-by: arnavgogia20 <arnavgogia404@gmail.com>
@karmada-bot karmada-bot added the kind/bug Categorizes issue or PR as related to a bug. label Feb 3, 2026
@karmada-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign jabellard for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@gemini-code-assist
Copy link

Summary of Changes

Hello @arnavgogia20, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a bug where concurrent operations in the detector controller could lead to reconciliation failures due to AlreadyExists errors when creating resource bindings. By modifying the retry mechanism to consider AlreadyExists errors as transient, the system can now gracefully handle these race conditions, promoting eventual consistency and improving the robustness of resource binding creation.

Highlights

  • Error Handling: The detector controller now treats AlreadyExists API errors as transient, similar to Conflict errors, during concurrent reconciliations for ResourceBinding and ClusterResourceBinding creation.
  • Retry Logic Update: The retry.RetryOnConflict function has been replaced with retry.OnError in ApplyPolicy and ApplyClusterPolicy methods. A new helper function, isConflictOrAlreadyExists, was introduced to explicitly check for both Conflict and AlreadyExists errors, ensuring proper retry behavior.
  • New Unit Tests: Dedicated unit tests (TestApplyPolicy_RetryOnAlreadyExists and TestApplyClusterPolicy_RetryOnAlreadyExists) have been added to deterministically simulate AlreadyExists race conditions and verify the updated retry logic.
Changelog
  • pkg/detector/detector.go
    • Replaced retry.RetryOnConflict with retry.OnError in ApplyPolicy and ApplyClusterPolicy functions.
    • Introduced a new predicate function isConflictOrAlreadyExists to check for both Conflict and AlreadyExists API errors.
    • Added comments explaining the rationale for retrying on AlreadyExists errors during concurrent reconciliations.
  • pkg/detector/detector_retry_test.go
    • Added a new test file to include unit tests for the updated retry logic.
    • Implemented InterceptingClient to simulate AlreadyExists errors during Create operations.
    • Added TestApplyPolicy_RetryOnAlreadyExists to verify retry behavior for ResourceBinding creation.
    • Added TestApplyClusterPolicy_RetryOnAlreadyExists to verify retry behavior for ClusterResourceBinding creation.
Activity
  • The pull request addresses and fixes issue [Observability] Detector Controller Concurrent Reconciliation Issues Trigger SLO violations #7120.
  • The author replaced retry.RetryOnConflict with retry.OnError using a named predicate to handle both Conflict and AlreadyExists errors.
  • The changes were consistently applied across both ApplyPolicy and ApplyClusterPolicy paths.
  • New unit tests were added to simulate AlreadyExists race conditions using an intercepting fake client.
  • The author verified the changes by running go test ./pkg/detector/....
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@karmada-bot karmada-bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Feb 3, 2026
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly addresses a race condition by updating the retry logic to handle AlreadyExists errors in addition to Conflict errors during the creation of ResourceBinding and ClusterResourceBinding. The change from retry.RetryOnConflict to retry.OnError with a custom predicate is a clean solution. The addition of unit tests using an intercepting client to simulate the race condition is excellent and ensures the fix is robust. I have a couple of minor suggestions to improve comment accuracy and adhere to the project's style guide.

Signed-off-by: arnavgogia20 <arnavgogia404@gmail.com>
Signed-off-by: arnavgogia20 <arnavgogia404@gmail.com>
@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 46.57%. Comparing base (44cbc1d) to head (ed5fac7).
⚠️ Report is 24 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7170      +/-   ##
==========================================
+ Coverage   46.56%   46.57%   +0.01%     
==========================================
  Files         700      700              
  Lines       48139    48141       +2     
==========================================
+ Hits        22414    22421       +7     
+ Misses      24040    24036       -4     
+ Partials     1685     1684       -1     
Flag Coverage Δ
unittests 46.57% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@arnavgogia20
Copy link
Contributor Author

Hii @RainbowMango @Garrybest @whitewindmills , can u kindly review the PR......thankyouu

@whitewindmills
Copy link
Member

Sounds weird; a workqueue guarantees that the same object won't be dequeued again until it's been processed. Could you explain how concurrent reconciliation of the same object occurs?

@arnavgogia20
Copy link
Contributor Author

Sounds weird; a workqueue guarantees that the same object won't be dequeued again until it's been processed. Could you explain how concurrent reconciliation of the same object occurs?

Hii @whitewindmills
Thanks for raising this.
You're right that the workqueue guarantees per-key serialization, so the same object won't be processed concurrently by multiple workers.
The scenario here is not true concurrent reconciliation of the same key, but rather eventual consistency behavior:
A ResourceBinding is created successfully.
Before the informer cache reflects that creation, the reconcile loop is triggered again (due to requeue or related event).
The controller attempts to create the binding again based on stale cache state.
The API server returns AlreadyExists.
In this case, AlreadyExists is effectively transient and expected, similar to a conflict during optimistic concurrency.
The change ensures we treat AlreadyExists consistently with conflict errors and retry instead of treating it as a terminal failure.
Please let me know if this reasoning aligns with your expectations, or if there’s a better way to structure this.

bindingCopy := binding.DeepCopy()
err = retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
// RetryOnConflict handles AlreadyExists as well because during concurrent reconciliations,
// another controller might have created the binding between our Get and Create calls.
Copy link
Member

Choose a reason for hiding this comment

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

@arnavgogia20 thanks for your explanation. This will never happen; a binding will only be created by one controller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Categorizes issue or PR as related to a bug. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Observability] Detector Controller Concurrent Reconciliation Issues Trigger SLO violations

4 participants