Skip to content

Commit

Permalink
Spanner: Handle AbortedExceptions that occur before commit (#4181)
Browse files Browse the repository at this point in the history
* spanner: Handle AbortedExceptions that occur before commit

The current TransactionRunner code checks for aborted transactions by
using the isAborted() function. This function returns the value of
"aborted" that is only set when processing resultSets. This does not
work for DML which only uses the resultSet stats and does not actually
call ResultSet.next().

This patch adds an additional check for the aborted exception type and
retries such transactions.

* throw before executeUpdate, verify flag is false after test

* Update google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITDMLTest.java

Co-Authored-By: nithinsujir <[email protected]>
  • Loading branch information
nithinsujir authored and chingor13 committed Dec 5, 2018
1 parent 97b1efc commit 90f8c98
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ private <T> T runInternal(TransactionCallable<T> callable) {
shouldRollback = false;
} catch (Exception e) {
txnLogger.log(Level.FINE, "User-provided TransactionCallable raised exception", e);
if (txn.isAborted()) {
if (txn.isAborted() || (e instanceof AbortedException)) {
span.addAnnotation(
"Transaction Attempt Aborted in user operation. Retrying",
ImmutableMap.of("Attempt", AttributeValue.longAttributeValue(attempt)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.google.cloud.spanner.AbortedException;
import com.google.cloud.spanner.Database;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.ErrorCode;
Expand All @@ -30,6 +31,7 @@
import com.google.cloud.spanner.Mutation;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.TimestampBound;
import com.google.cloud.spanner.TransactionContext;
Expand Down Expand Up @@ -59,6 +61,8 @@ public final class ITDMLTest {
private static final String DELETE_DML = "DELETE FROM T WHERE T.K like 'boo%';";
private static final long DML_COUNT = 4;

private static boolean throwAbortOnce = false;

@BeforeClass
public static void setUpDatabase() {
db =
Expand All @@ -82,6 +86,12 @@ private void executeUpdate(long expectedCount, final String... stmts) {
public Long run(TransactionContext transaction) {
long rowCount = 0;
for (String stmt : stmts) {
if (throwAbortOnce) {
throwAbortOnce = false;
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.ABORTED, "Abort in test");
}

rowCount += transaction.executeUpdate(Statement.of(stmt));
}
return rowCount;
Expand All @@ -92,6 +102,17 @@ public Long run(TransactionContext transaction) {
assertThat(rowCount).isEqualTo(expectedCount);
}

@Test
public void abortOnceShouldSucceedAfterRetry() {
try {
throwAbortOnce = true;
executeUpdate(DML_COUNT, INSERT_DML);
assertThat(throwAbortOnce).isFalse();
} catch (AbortedException e) {
fail("Abort Exception not caught and retried");
}
}

@Test
public void partitionedDML() {
executeUpdate(DML_COUNT, INSERT_DML);
Expand Down

0 comments on commit 90f8c98

Please sign in to comment.