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

Fix: adjusting retry logic to avoid retrying successful job creation #1879

Merged
merged 5 commits into from Mar 3, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -52,7 +52,7 @@ If you are using Maven without BOM, add this to your dependencies:
If you are using Gradle 5.x or later, add this to your dependencies

```Groovy
implementation platform('com.google.cloud:libraries-bom:24.3.0')
implementation platform('com.google.cloud:libraries-bom:24.4.0')

implementation 'com.google.cloud:google-cloud-bigquery'
```
Expand Down
Expand Up @@ -25,6 +25,8 @@
import com.google.api.gax.retrying.TimedAttemptSettings;
import com.google.api.gax.retrying.TimedRetryAlgorithm;
import com.google.api.gax.retrying.TimedRetryAlgorithmWithContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.CancellationException;
Expand Down Expand Up @@ -107,9 +109,10 @@ private boolean shouldRetryBasedOnBigQueryRetryConfig(
/*
In some cases error messages may come without an exception
e.g. status code 200 with a rate limit exceeded for job create
in these cases there is now previousThrowable so we need to check previousResponse
in these cases there is no previousThrowable so we need
to check for error messages in previousResponse
*/
errorDesc = previousResponse.toString();
errorDesc = getErrorDescFromResponse(previousResponse);
}

if (errorDesc != null) {
Expand Down Expand Up @@ -212,4 +215,28 @@ private TimedAttemptSettings createNextAttemptBasedOnTiming(
}
return getTimedAlgorithm().createNextAttempt(previousSettings);
}

private String getErrorDescFromResponse(ResponseT previousResponse) {
/*
error messages may come without an exception and must be extracted from response
following logic based on response body of jobs.insert method, so far the only
known case where a response with status code 200 may contain an error message
*/
try {
JsonObject responseJson =
JsonParser.parseString(previousResponse.toString()).getAsJsonObject();
if (responseJson.has("status") && responseJson.getAsJsonObject("status").has("errorResult")) {
return responseJson
.getAsJsonObject("status")
.getAsJsonObject("errorResult")
.get("message")
.toString();
} else {
return null;
}
} catch (Exception e) {
// exceptions here implies no error message present in response, returning null
return null;
}
}
}