-
Notifications
You must be signed in to change notification settings - Fork 119
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
feat: Retry API calls that return a 500 error #2041
Conversation
ebc9d0a
to
5faf4ec
Compare
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.
couple nits, will let @enocom review for Java readability
should we make this a PR a fix:
?
* response with an error code in the 500 range. | ||
* | ||
* @param e the exception | ||
* @return false if this is a http response with a 500 status code, otherwise true. |
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.
* response with an error code in the 500 range. | |
* | |
* @param e the exception | |
* @return false if this is a http response with a 500 status code, otherwise true. | |
* response with an error code in the 5xx range. | |
* | |
* @param e the exception | |
* @return false if this is a http response with a 5xx status code, otherwise true. |
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.
Fixed.
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.
Fixed.
* | ||
* <p>The formula is: base * multi^(attempt + 1 + random) | ||
* | ||
* <p>With base = 200ms and multi = 1.1618, and random = [0.0, 1.0), the backoff values would fall |
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.
nit: value of multi should be 1.618
* <p>With base = 200ms and multi = 1.1618, and random = [0.0, 1.0), the backoff values would fall | |
* <p>With base = 200ms and multi = 1.618, and random = [0.0, 1.0), the backoff values would fall |
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.
Fixed.
new ApiClientRetryingCallable<>( | ||
() -> { | ||
int attempt = counter.incrementAndGet(); | ||
if (attempt < 3) { |
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.
any reason for choosing 3 instead of 5 here?
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.
I wanted to demonstrate that it returns after a successful attempt before exhausting the total number of retries.
I added another test that always fails to show that it stops at 5 attempts and then fails.
This should be a "feature" -- it will have a noticeable usability change. |
// Only retry if the error is an HTTP response with a 5xx error code. | ||
if (e instanceof HttpResponseException) { | ||
HttpResponseException re = (HttpResponseException) e; | ||
return re.getStatusCode() < 500 || re.getStatusCode() > 599; |
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.
When would re.getStatusCode() > 599
apply? I think re.getStatusCode() < 500
will get the job done.
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.
Fixed.
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.
I also added a new test to ConnectorTest
. This test the SqlAdmin client library actually responds correctly and the ApiClientRetryingCallable works as expected when the service intermittently responds with 502 errors.
* response with an error code in the 500 range. | ||
* | ||
* @param e the exception | ||
* @return false if this is a http response with a 500 status code, otherwise true. |
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.
Fixed.
* response with an error code in the 500 range. | ||
* | ||
* @param e the exception | ||
* @return false if this is a http response with a 500 status code, otherwise true. |
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.
Fixed.
// Only retry if the error is an HTTP response with a 5xx error code. | ||
if (e instanceof HttpResponseException) { | ||
HttpResponseException re = (HttpResponseException) e; | ||
return re.getStatusCode() < 500 || re.getStatusCode() > 599; |
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.
Fixed.
* | ||
* <p>The formula is: base * multi^(attempt + 1 + random) | ||
* | ||
* <p>With base = 200ms and multi = 1.1618, and random = [0.0, 1.0), the backoff values would fall |
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.
Fixed.
new ApiClientRetryingCallable<>( | ||
() -> { | ||
int attempt = counter.incrementAndGet(); | ||
if (attempt < 3) { |
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.
I wanted to demonstrate that it returns after a successful attempt before exhausting the total number of retries.
I added another test that always fails to show that it stops at 5 attempts and then fails.
5faf4ec
to
2e8c190
Compare
2e8c190
to
e1380fe
Compare
e1380fe
to
cf3d027
Compare
This updates the retry logic for API calls made by the connector:
This also implements the exponential backoff logic defined used in the go connector.
See: GoogleCloudPlatform/cloud-sql-go-connector#781