forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
retry datastore transactions (googleapis#1242)
- Loading branch information
Showing
5 changed files
with
156 additions
and
5 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
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
57 changes: 57 additions & 0 deletions
57
...cloud-datastore/src/main/java/com/google/cloud/datastore/TransactionExceptionHandler.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,57 @@ | ||
/* | ||
* Copyright 2017 Google Inc. All Rights Reserved. | ||
* | ||
* 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.cloud.ExceptionHandler; | ||
import com.google.cloud.ExceptionHandler.Interceptor; | ||
|
||
public class TransactionExceptionHandler { | ||
public static final Interceptor TRANSACTION_EXCEPTION_HANDLER_INTERCEPTOR = | ||
new Interceptor() { | ||
@Override | ||
public RetryResult beforeEval(Exception exception) { | ||
if (exception instanceof DatastoreException) { | ||
DatastoreException e = getInnerException((DatastoreException) exception); | ||
if (e.getCode() == 10 || e.getReason() == "ABORTED") { | ||
return RetryResult.RETRY; | ||
} | ||
} | ||
return Interceptor.RetryResult.CONTINUE_EVALUATION; | ||
} | ||
|
||
@Override | ||
public RetryResult afterEval(Exception exception, RetryResult retryResult) { | ||
return Interceptor.RetryResult.CONTINUE_EVALUATION; | ||
} | ||
|
||
private DatastoreException getInnerException(DatastoreException exception) { | ||
DatastoreException innerException = exception; | ||
while (innerException.getCause() instanceof DatastoreException) { | ||
innerException = (DatastoreException) innerException.getCause(); | ||
} | ||
return innerException; | ||
} | ||
}; | ||
|
||
public static ExceptionHandler build() { | ||
return ExceptionHandler.newBuilder() | ||
.abortOn(RuntimeException.class) | ||
.addInterceptors( | ||
DatastoreImpl.EXCEPTION_HANDLER_INTERCEPTOR, TRANSACTION_EXCEPTION_HANDLER_INTERCEPTOR) | ||
.build(); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...d-datastore/src/test/java/com/google/cloud/datastore/TransactionExceptionHandlerTest.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,60 @@ | ||
/* | ||
* Copyright 2017 Google Inc. All Rights Reserved. | ||
* | ||
* 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 static junit.framework.TestCase.assertFalse; | ||
import static junit.framework.TestCase.assertTrue; | ||
|
||
import com.google.cloud.BaseServiceException; | ||
import com.google.cloud.ExceptionHandler; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
/** Tests for {@link TransactionExceptionHandler}. */ | ||
public class TransactionExceptionHandlerTest { | ||
|
||
@Rule public ExpectedException thrown = ExpectedException.none(); | ||
|
||
@Test | ||
public void testShouldTry() { | ||
ExceptionHandler handler = | ||
ExceptionHandler.newBuilder() | ||
.abortOn(RuntimeException.class) | ||
.addInterceptors(DatastoreImpl.EXCEPTION_HANDLER_INTERCEPTOR) | ||
.build(); | ||
ExceptionHandler transactionHandler = TransactionExceptionHandler.build(); | ||
|
||
assertFalse(handler.accept(new DatastoreException(10, "", "ABORTED", false, null))); | ||
assertFalse(handler.accept(new DatastoreException(10, "", "", false, null))); | ||
assertFalse(handler.accept(new DatastoreException(0, "", "", false, null))); | ||
|
||
assertTrue(transactionHandler.accept(new DatastoreException(10, "", "ABORTED", false, null))); | ||
assertTrue(transactionHandler.accept(new DatastoreException(10, "", "", false, null))); | ||
assertFalse(transactionHandler.accept(new DatastoreException(0, "", "", false, null))); | ||
|
||
DatastoreException nestedDatastoreException = | ||
new DatastoreException( | ||
BaseServiceException.UNKNOWN_CODE, | ||
"", | ||
null, | ||
new DatastoreException(10, "", "ABORTED", false, null)); | ||
|
||
assertTrue(transactionHandler.accept(nestedDatastoreException)); | ||
assertFalse(handler.accept(nestedDatastoreException)); | ||
} | ||
} |
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