-
Notifications
You must be signed in to change notification settings - Fork 110
CCXDEV-15602: Retry refactor #1271
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
Open
katushiik11
wants to merge
7
commits into
openshift:master
Choose a base branch
from
katushiik11:CCXDEV-15602-retry-refactor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
882c6a4
refactor: Some retry logic into 1 package
katushiik11 5013b93
test: Retry logic
katushiik11 54ada83
fix: lint issues
katushiik11 e4022c1
fix: review fixes
katushiik11 13a7a8f
test: fix tests
katushiik11 a0fd48f
refactor: Use Result struct with retry
katushiik11 5eebc1b
fix: HTTP pointer error & context
katushiik11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| // Package retry provides shared retry logic with exponential backoff for HTTP operations. | ||
| // | ||
| // Usage example: | ||
| // | ||
| // result, err := retry.RetryWithExpBackOff( | ||
| // ctx, | ||
| // wait.Backoff{ | ||
| // Duration: interval/32, | ||
| // Factor: 2, | ||
| // Steps: ocm.FailureCountThreshold, | ||
| // Cap: interval, | ||
| // }, | ||
| // retry.RetryOn50xHTTP, | ||
| // func() (retry.Result, error) { | ||
| // data, err := client.RecvSCACerts(ctx, endpoint, nodeArchs) | ||
| // return retry.Result{Data: data}, err | ||
| // }, | ||
| // ) | ||
| package retry | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net/http" | ||
|
|
||
| "github.com/openshift/insights-operator/pkg/insights/insightsclient" | ||
| "k8s.io/apimachinery/pkg/util/wait" | ||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| type RetryStrategy int64 | ||
|
|
||
| const ( | ||
| // RetryOn50xHTTP retries only on HTTP 500+ errors, skips retry for non-HTTP errors (disconnected env) | ||
| // Used by: sca.go, clustertransfer.go | ||
| RetryOn50xHTTP RetryStrategy = iota | ||
|
|
||
| // RetryOnNon200HTTP retries on any non-200 HTTP status code | ||
| // Used by: conditional_gatherer.go | ||
| RetryOnNon200HTTP | ||
|
|
||
| // RetryOnAll retries on all errors | ||
| // Used by: insightsuploader.go | ||
| RetryOnAll | ||
| ) | ||
|
|
||
| // Result holds the response data from retry operations | ||
| type Result struct { | ||
| Data []byte | ||
| StatusCode int | ||
| RequestID string | ||
| } | ||
|
|
||
| // shouldRetry determines if an error should be retried based on the strategy. | ||
| // Returns true if retry should be attempted (when steps remain). | ||
| // Returns false immediately if the context is canceled or deadline exceeded. | ||
| func shouldRetry(ctx context.Context, err error, strategy RetryStrategy) bool { | ||
| // Don't retry if context is canceled or deadline exceeded | ||
| if ctx.Err() != nil { | ||
| return false | ||
| } | ||
|
|
||
| // Don't retry context cancellation or deadline errors | ||
| if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { | ||
| return false | ||
| } | ||
|
|
||
| // Extract status code from HttpError (handles both pointer and non-pointer) | ||
| var statusCode int | ||
| var isHTTPError bool | ||
|
|
||
| switch e := err.(type) { | ||
| case *insightsclient.HttpError: | ||
| // Pointer - real-world case from newHTTPErrorFromResponse | ||
| statusCode = e.StatusCode | ||
| isHTTPError = true | ||
| case insightsclient.HttpError: | ||
| // Non-pointer - test case | ||
| statusCode = e.StatusCode | ||
| isHTTPError = true | ||
| } | ||
|
|
||
| switch strategy { | ||
| case RetryOn50xHTTP: | ||
| // Only retry HTTP 500+ errors, skip non-HTTP errors (disconnected env) | ||
| if !isHTTPError { | ||
| return false | ||
| } | ||
| return statusCode >= http.StatusInternalServerError | ||
|
|
||
| case RetryOnNon200HTTP: | ||
| // Retry on any non-200 HTTP status, or non-HTTP errors | ||
| if !isHTTPError { | ||
| return true // retry non-HTTP errors | ||
| } | ||
| return statusCode != http.StatusOK | ||
|
|
||
| case RetryOnAll: | ||
| // Retry on all errors | ||
| return true | ||
|
katushiik11 marked this conversation as resolved.
|
||
|
|
||
| default: | ||
| // Unknown strategy, don't retry | ||
| klog.Infof("Unknown strategy %d for retry mechanism", strategy) | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| func RetryWithExpBackOff(ctx context.Context, bo wait.Backoff, strategy RetryStrategy, operation func() (Result, error)) (Result, error) { | ||
| var lastErr error | ||
| var result Result | ||
|
|
||
| attempt := 0 | ||
| maxAttempts := bo.Steps | ||
|
|
||
| err := wait.ExponentialBackoffWithContext(ctx, bo, func(context.Context) (bool, error) { | ||
| attempt++ | ||
| result, lastErr = operation() | ||
| if lastErr != nil { | ||
| // Use strategy to determine if we should retry | ||
| if shouldRetry(ctx, lastErr, strategy) { | ||
| klog.Errorf("%v. Retrying (attempt %d/%d)", lastErr, attempt, maxAttempts) | ||
| return false, nil | ||
| } | ||
| // Don't retry based on strategy | ||
| return true, lastErr | ||
| } | ||
|
|
||
| return true, nil | ||
| }) | ||
|
|
||
| // If we exhausted retries, return the last operation error instead of the timeout error | ||
| if wait.Interrupted(err) && lastErr != nil { | ||
| return result, lastErr | ||
| } | ||
|
|
||
| return result, err | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.