Skip to content

retry datastore transactions (#1242)#1932

Merged
neozwu merged 4 commits intogoogleapis:masterfrom
neozwu:retry-runintransaction
Apr 18, 2017
Merged

retry datastore transactions (#1242)#1932
neozwu merged 4 commits intogoogleapis:masterfrom
neozwu:retry-runintransaction

Conversation

@neozwu
Copy link
Contributor

@neozwu neozwu commented Apr 17, 2017

enable retry for datastore transactions.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Apr 17, 2017
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.

return RetryResult.RETRY;
}
}
return Interceptor.RetryResult.CONTINUE_EVALUATION;

This comment was marked as spam.

This comment was marked as spam.

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.

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.

}
};
int result = DATASTORE.runInTransaction(callable);
assertTrue(result == 1);

This comment was marked as spam.

This comment was marked as spam.

}

@Test
public void testRunInTransaction() {

This comment was marked as spam.

This comment was marked as spam.

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling 8c4b537 on neozwu:retry-runintransaction into ** on GoogleCloudPlatform:master**.

@neozwu
Copy link
Contributor Author

neozwu commented Apr 17, 2017

@garrettjonesgoogle PTAL

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.007%) to 80.934% when pulling 0625f80 on neozwu:retry-runintransaction into b6a87b0 on GoogleCloudPlatform:master.

Copy link
Member

@garrettjonesgoogle garrettjonesgoogle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.

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.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.01%) to 80.928% when pulling 47e086d on neozwu:retry-runintransaction into b6a87b0 on GoogleCloudPlatform:master.

Copy link
Contributor

@vam-google vam-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.


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.


public Integer run(DatastoreReaderWriter transaction) {
transaction.get(KEY1);
if (attempts < 1) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@garrettjonesgoogle
Copy link
Member

LGTM after tests pass

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.002%) to 80.939% when pulling e0aa253 on neozwu:retry-runintransaction into b6a87b0 on GoogleCloudPlatform:master.

@neozwu neozwu merged commit b355f4d into googleapis:master Apr 18, 2017
chingor13 pushed a commit that referenced this pull request Jan 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants