-
Notifications
You must be signed in to change notification settings - Fork 26
[PECOBLR-1219] Added Api retriable codes URL param #1110
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
nishkarsh-db
wants to merge
11
commits into
databricks:main
Choose a base branch
from
nishkarsh-db:apiRetriableCodes
base: main
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.
+126
−4
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b25fbf1
Added New URL Param to retry without retry-after header
nishkarsh-db 9db1a81
Rplaced hard-coded check in testApiRetriableCodesWithoutRetryAfterHeader
nishkarsh-db b187146
added imports
nishkarsh-db 0c8c199
added test for ApiretriableCodes in connectioncontext
nishkarsh-db 9ba776b
Removed unnecessary null check
nishkarsh-db bd74599
Added new param for API_CODES_RETRY_TIMEOUT
nishkarsh-db 0173f30
Added a util for parsing Set<Integer>, renamed ApiRetry Params
nishkarsh-db 56a0505
Edited URL param detail
nishkarsh-db 7d638df
Merge branch 'main' into apiRetriableCodes
nishkarsh-db cbdc315
Merge branch 'main' into apiRetriableCodes
nishkarsh-db b4dd423
Updated changelog
nishkarsh-db 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
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 |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| import com.google.common.annotations.VisibleForTesting; | ||
| import java.io.IOException; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import org.apache.http.HttpResponse; | ||
| import org.apache.http.HttpResponseInterceptor; | ||
| import org.apache.http.HttpStatus; | ||
|
|
@@ -28,6 +29,7 @@ public class DatabricksHttpRetryHandler | |
| private static final String TEMP_UNAVAILABLE_ACCUMULATED_TIME_KEY = | ||
| "tempUnavailableAccumulatedTime"; | ||
| private static final String RATE_LIMIT_ACCUMULATED_TIME_KEY = "rateLimitAccumulatedTime"; | ||
| private static final String API_CODES_ACCUMULATED_TIME_KEY = "apiCodesAccumulatedTime"; | ||
| static final String RETRY_AFTER_HEADER = "Retry-After"; | ||
| private static final int DEFAULT_BACKOFF_FACTOR = 2; // Exponential factor | ||
| private static final int MIN_BACKOFF_INTERVAL = 1000; // 1s | ||
|
|
@@ -134,10 +136,12 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte | |
|
|
||
| // check if retry interval is valid for 503 and 429 | ||
| int retryInterval = (int) context.getAttribute(RETRY_INTERVAL_KEY); | ||
| if ((statusCode == HttpStatus.SC_SERVICE_UNAVAILABLE | ||
| || statusCode == HttpStatus.SC_TOO_MANY_REQUESTS) | ||
| && retryInterval == -1) { | ||
|
|
||
| Set<Integer> apiRetriableCodes = connectionContext.getApiRetriableHttpCodes(); | ||
| boolean isInCustomRetriableCodes = apiRetriableCodes.contains(statusCode); | ||
| if (retryInterval == -1 && !isInCustomRetriableCodes) { | ||
| // This case arises when the server does not send the retryAfter header | ||
| // and the status code is not in the custom retriable codes list | ||
| LOGGER.warn( | ||
| "Invalid retry interval in the context " | ||
| + context | ||
|
|
@@ -146,9 +150,15 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte | |
| return false; | ||
| } | ||
|
|
||
| // If no retry-after header (retryInterval == -1), calculate delay using exponential backoff | ||
| if (retryInterval == -1) { | ||
| retryInterval = (int) (calculateExponentialBackoff(executionCount) / 1000); | ||
| } | ||
|
|
||
| long tempUnavailableAccumulatedTime = | ||
| getAccumulatedTime(context, TEMP_UNAVAILABLE_ACCUMULATED_TIME_KEY); | ||
| long rateLimitAccumulatedTime = getAccumulatedTime(context, RATE_LIMIT_ACCUMULATED_TIME_KEY); | ||
| long apiCodesAccumulatedTime = getAccumulatedTime(context, API_CODES_ACCUMULATED_TIME_KEY); | ||
|
|
||
| // check if retry timeout has been hit for error code 503 | ||
| if (statusCode == HttpStatus.SC_SERVICE_UNAVAILABLE | ||
|
|
@@ -174,6 +184,19 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte | |
| return false; | ||
| } | ||
|
|
||
| // check if retry timeout has been hit for custom API retriable codes | ||
| if (apiRetriableCodes.contains(statusCode) | ||
| && statusCode != HttpStatus.SC_SERVICE_UNAVAILABLE | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this filtering for 403 and 429 here? Why not also include 429 and 503
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because there are seperate url params for 429 and 503. |
||
| && statusCode != HttpStatus.SC_TOO_MANY_REQUESTS | ||
| && apiCodesAccumulatedTime + retryInterval > connectionContext.getApiRetryTimeout()) { | ||
| LOGGER.warn( | ||
| "ApiRetry timeout " | ||
| + connectionContext.getApiRetryTimeout() | ||
| + " has been hit for the error: " | ||
| + exception.getMessage()); | ||
| return false; | ||
| } | ||
|
|
||
| // check if request method is retryable | ||
| boolean isRequestMethodRetryable = | ||
| isRequestMethodRetryable( | ||
|
|
@@ -190,6 +213,8 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte | |
| } else if (statusCode == HttpStatus.SC_TOO_MANY_REQUESTS) { | ||
| context.setAttribute( | ||
| RATE_LIMIT_ACCUMULATED_TIME_KEY, rateLimitAccumulatedTime + retryInterval); | ||
| } else if (apiRetriableCodes.contains(statusCode)) { | ||
| context.setAttribute(API_CODES_ACCUMULATED_TIME_KEY, apiCodesAccumulatedTime + retryInterval); | ||
| } | ||
|
|
||
| // calculate the delay and sleep for that duration | ||
|
|
@@ -237,6 +262,9 @@ private static void initializeRetryAccumulatedTimeIfNotExist(HttpContext httpCon | |
| if (httpContext.getAttribute(RATE_LIMIT_ACCUMULATED_TIME_KEY) == null) { | ||
| httpContext.setAttribute(RATE_LIMIT_ACCUMULATED_TIME_KEY, 0L); | ||
| } | ||
| if (httpContext.getAttribute(API_CODES_ACCUMULATED_TIME_KEY) == null) { | ||
| httpContext.setAttribute(API_CODES_ACCUMULATED_TIME_KEY, 0L); | ||
| } | ||
| } | ||
|
|
||
| private static long getAccumulatedTime(HttpContext context, String key) { | ||
|
|
@@ -256,6 +284,11 @@ protected void doSleepForDelay(long delayMillis) { | |
|
|
||
| /** Check if the request is retryable based on the status code and any connection preferences. */ | ||
| private boolean isStatusCodeRetryable(int statusCode) { | ||
| Set<Integer> apiRetriableCodes = connectionContext.getApiRetriableHttpCodes(); | ||
| if (apiRetriableCodes.contains(statusCode)) { | ||
| return true; | ||
| } | ||
|
|
||
| switch (statusCode) { | ||
| case HttpStatus.SC_SERVICE_UNAVAILABLE: | ||
| return connectionContext.shouldRetryTemporarilyUnavailableError(); | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This exponential backoff is applied unconditionally for all cases where
retryInterval == -1, including status codes not inapiRetriableCodes. The logic should only apply exponential backoff whenisInCustomRetriableCodesis true. For other cases withretryInterval == -1, the method should return false as per the existing logic (lines 149-161).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition is never reached. And if it reaches (in future) then this will not be a problem.