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

SNOW-577665: Wrong retry 403 logic #916

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/net/snowflake/client/jdbc/RestRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public static CloseableHttpResponse execute(
* If we got a response and the status code is not one of those
* transient failures, no more retry
*/
if (isCertificateRevoked(savedEx) || isNonretryableHTTPCode(response, retryHTTP403)) {
if (isCertificateRevoked(savedEx) || isNonRetryableHTTPCode(response, retryHTTP403)) {
String msg = "Unknown cause";
if (response != null) {
logger.debug("HTTP response code: {}", response.getStatusLine().getStatusCode());
Expand Down Expand Up @@ -439,15 +439,15 @@ public static CloseableHttpResponse execute(
return response;
}

static boolean isNonretryableHTTPCode(CloseableHttpResponse response, boolean retryHTTP403) {
static boolean isNonRetryableHTTPCode(CloseableHttpResponse response, boolean retryHTTP403) {
return response != null
&& (response.getStatusLine().getStatusCode() < 500
|| // service unavailable
response.getStatusLine().getStatusCode() >= 600)
&& // gateway timeout
response.getStatusLine().getStatusCode() != 408
&& // request timeout
(retryHTTP403 || response.getStatusLine().getStatusCode() != 403);
(!retryHTTP403 || response.getStatusLine().getStatusCode() != 403);
Copy link
Contributor Author

@sfc-gh-wshangguan sfc-gh-wshangguan Apr 19, 2022

Choose a reason for hiding this comment

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

This is the fix. Reviewers please carefully review whether this change is correct since it's error prone. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

This makes sense!

}

private static boolean isCertificateRevoked(Exception ex) {
Expand Down
17 changes: 9 additions & 8 deletions src/test/java/net/snowflake/client/jdbc/RestRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ private CloseableHttpResponse anyStatusCodeResponse(int statusCode) {
}

@Test
public void testIsRetryableHTTPCode() throws Exception {
public void testIsNonRetryableHTTPCode() throws Exception {
class TestCase {
TestCase(int statusCode, boolean retryHTTP403, boolean result) {
this.statusCode = statusCode;
this.retryHTTP403 = retryHTTP403;
this.result = result;
this.result = result; // expected result of calling isNonRetryableHTTPCode()
}

public int statusCode;
Expand All @@ -172,6 +172,7 @@ class TestCase {
}
List<TestCase> testCases = new ArrayList<>();
// no retry on HTTP 403 option

testCases.add(new TestCase(100, false, true));
testCases.add(new TestCase(101, false, true));
testCases.add(new TestCase(103, false, true));
Expand All @@ -191,12 +192,12 @@ class TestCase {
testCases.add(new TestCase(308, false, true));
testCases.add(new TestCase(400, false, true));
testCases.add(new TestCase(401, false, true));
testCases.add(new TestCase(403, false, false)); // no retry on HTTP 403
testCases.add(new TestCase(403, false, true)); // no retry on HTTP 403
testCases.add(new TestCase(404, false, true));
testCases.add(new TestCase(405, false, true));
testCases.add(new TestCase(406, false, true));
testCases.add(new TestCase(407, false, true));
testCases.add(new TestCase(408, false, false)); // no retry on HTTP 408
testCases.add(new TestCase(408, false, false)); // do retry on HTTP 408
testCases.add(new TestCase(410, false, true));
testCases.add(new TestCase(411, false, true));
testCases.add(new TestCase(412, false, true));
Expand Down Expand Up @@ -244,12 +245,12 @@ class TestCase {
testCases.add(new TestCase(308, true, true));
testCases.add(new TestCase(400, true, true));
testCases.add(new TestCase(401, true, true));
testCases.add(new TestCase(403, true, true)); // do retry on HTTP 403
testCases.add(new TestCase(403, true, false)); // do retry on HTTP 403
testCases.add(new TestCase(404, true, true));
testCases.add(new TestCase(405, true, true));
testCases.add(new TestCase(406, true, true));
testCases.add(new TestCase(407, true, true));
testCases.add(new TestCase(408, true, false)); // no retry on HTTP 408
testCases.add(new TestCase(408, true, false)); // do retry on HTTP 408
testCases.add(new TestCase(410, true, true));
testCases.add(new TestCase(411, true, true));
testCases.add(new TestCase(412, true, true));
Expand Down Expand Up @@ -284,14 +285,14 @@ class TestCase {
String.format(
"Result must be true but false: HTTP Code: %d, RetryHTTP403: %s",
t.statusCode, t.retryHTTP403),
RestRequest.isNonretryableHTTPCode(
RestRequest.isNonRetryableHTTPCode(
anyStatusCodeResponse(t.statusCode), t.retryHTTP403));
} else {
assertFalse(
String.format(
"Result must be false but true: HTTP Code: %d, RetryHTTP403: %s",
t.statusCode, t.retryHTTP403),
RestRequest.isNonretryableHTTPCode(
RestRequest.isNonRetryableHTTPCode(
anyStatusCodeResponse(t.statusCode), t.retryHTTP403));
}
}
Expand Down