retry datastore transactions (#1242)#1932
Conversation
| public RetryResult beforeEval(Exception exception) { | ||
| if (exception instanceof DatastoreException) { | ||
| DatastoreException e = getInnerException((DatastoreException) exception); | ||
| if (e.getCode() == 10 || e.getReason() == "ABORTED") { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| return RetryResult.RETRY; | ||
| } | ||
| } | ||
| return Interceptor.RetryResult.CONTINUE_EVALUATION; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| DatastoreImpl.EXCEPTION_HANDLER_INTERCEPTOR, TRANSACTION_EXCEPTION_HANDLER_INTERCEPTOR) | ||
| .build(); | ||
| } | ||
| } No newline at end of file |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| transaction.get(KEY1); | ||
| if (attempts < 1) { | ||
| ++attempts; | ||
| throw new DatastoreException(10,"","ABORTED",false,null); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| } | ||
| }; | ||
| int result = DATASTORE.runInTransaction(callable); | ||
| assertTrue(result == 1); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| } | ||
|
|
||
| @Test | ||
| public void testRunInTransaction() { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
Changes Unknown when pulling 8c4b537 on neozwu:retry-runintransaction into ** on GoogleCloudPlatform:master**. |
|
@garrettjonesgoogle PTAL |
garrettjonesgoogle
left a comment
There was a problem hiding this comment.
LGTM after the suggestions below are addressed, and after @vam-google has also given approval.
| import com.google.cloud.ExceptionHandler; | ||
| import com.google.cloud.ExceptionHandler.Interceptor; | ||
|
|
||
| public class TransactionExceptionHandler { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| public class TransactionExceptionHandler { | ||
| public static final Interceptor TRANSACTION_EXCEPTION_HANDLER_INTERCEPTOR = | ||
| new Interceptor() { | ||
| private final int ABORTED_CODE = 10; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| public RetryResult beforeEval(Exception exception) { | ||
| if (exception instanceof DatastoreException) { | ||
| DatastoreException e = getInnerException((DatastoreException) exception); | ||
| if (e.getCode() == ABORTED_CODE || e.getReason() == "ABORTED") { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
vam-google
left a comment
There was a problem hiding this comment.
LGTM after the review comments are addressed.
| public <T> T runInTransaction(TransactionCallable<T> callable) { | ||
| return DatastoreHelper.runInTransaction(this, callable); | ||
| public <T> T runInTransaction(final TransactionCallable<T> callable) { | ||
| ExceptionHandler TRANSACTION_EXCEPTION_HANDLER = TransactionExceptionHandler.build(); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
|
||
| public class TransactionExceptionHandler { | ||
| public static final Interceptor TRANSACTION_EXCEPTION_HANDLER_INTERCEPTOR = | ||
| new Interceptor() { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
|
||
| public Integer run(DatastoreReaderWriter transaction) { | ||
| transaction.get(KEY1); | ||
| if (attempts < 1) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
LGTM after tests pass |
🤖 I have created a release *beep* *boop* --- ## [2.28.2](https://togithub.com/googleapis/java-bigquerystorage/compare/v2.28.1...v2.28.2) (2023-01-18) ### Bug Fixes * Add a timeout on retry for retryable errors ([#1930](https://togithub.com/googleapis/java-bigquerystorage/issues/1930)) ([2d648cf](https://togithub.com/googleapis/java-bigquerystorage/commit/2d648cf9706a6e7bc155e8769ba7dda2a6bc3061)) * Add precision overwritten to 9 digit if the passed in JSON type is FLOAT or DOUBLE ([#1932](https://togithub.com/googleapis/java-bigquerystorage/issues/1932)) ([417bc6c](https://togithub.com/googleapis/java-bigquerystorage/commit/417bc6c76f7b9fa602721c3c183c487c5aab2e09)) ### Dependencies * Update dependency com.google.cloud:google-cloud-bigquery to v2.20.2 ([#1929](https://togithub.com/googleapis/java-bigquerystorage/issues/1929)) ([a95ae9d](https://togithub.com/googleapis/java-bigquerystorage/commit/a95ae9d708bd3e2d8f55297a6004a0d937c8d83f)) --- This PR was generated with [Release Please](https://togithub.com/googleapis/release-please). See [documentation](https://togithub.com/googleapis/release-please#release-please).
enable retry for datastore transactions.