-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update retry logic for operations that can be associated with a …
…transaction (#144) Update retry logic for those operations which can be associated with a transaction such that they will not be retried unnecessarily if the transaction is `ABORTED`. This change ultimately effects those operations which result in a `LookupRequest`, a `CommitRequest` or a `RunQueryRequest` and are part of a transaction.
- Loading branch information
1 parent
5767638
commit 82ee74e
Showing
3 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
68 changes: 68 additions & 0 deletions
68
...astore/src/main/java/com/google/cloud/datastore/TransactionOperationExceptionHandler.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2017 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.datastore; | ||
|
||
import com.google.api.core.BetaApi; | ||
import com.google.cloud.BaseService; | ||
import com.google.cloud.ExceptionHandler; | ||
import com.google.cloud.ExceptionHandler.Interceptor; | ||
|
||
@BetaApi | ||
public class TransactionOperationExceptionHandler { | ||
|
||
public static final Interceptor TRANSACTION_OPERATION_EXCEPTION_HANDLER_INTERCEPTOR = | ||
new Interceptor() { | ||
|
||
private static final long serialVersionUID = -1240723093072535978L; | ||
|
||
private static final int ABORTED_CODE = 10; | ||
|
||
@Override | ||
public RetryResult beforeEval(Exception exception) { | ||
if (exception instanceof DatastoreException) { | ||
DatastoreException e = getInnerException((DatastoreException) exception); | ||
if (e.getCode() == ABORTED_CODE | ||
|| e.getReason() != null && e.getReason().equals("ABORTED")) { | ||
return RetryResult.NO_RETRY; | ||
} | ||
} | ||
return BaseService.EXCEPTION_HANDLER_INTERCEPTOR.beforeEval(exception); | ||
} | ||
|
||
@Override | ||
public RetryResult afterEval(Exception exception, RetryResult retryResult) { | ||
return BaseService.EXCEPTION_HANDLER_INTERCEPTOR.afterEval(exception, retryResult); | ||
} | ||
|
||
private DatastoreException getInnerException(DatastoreException exception) { | ||
while (exception.getCause() instanceof DatastoreException) { | ||
exception = (DatastoreException) exception.getCause(); | ||
} | ||
return exception; | ||
} | ||
}; | ||
|
||
public static ExceptionHandler build() { | ||
return ExceptionHandler.newBuilder() | ||
.abortOn(RuntimeException.class) | ||
.addInterceptors(TRANSACTION_OPERATION_EXCEPTION_HANDLER_INTERCEPTOR) | ||
.build(); | ||
} | ||
|
||
/** Intentionally private empty constructor to disable instantiation of this class. */ | ||
private TransactionOperationExceptionHandler() {} | ||
} |
This file contains 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