Search in sources :

Example 1 with ITConnection

use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection in project java-spanner by googleapis.

the class AbortedTest method testAbortedDuringRetryOfFailedBatchUpdateAsFirstStatement.

@Test
public void testAbortedDuringRetryOfFailedBatchUpdateAsFirstStatement() {
    final Statement invalidStatement = Statement.of("INSERT INTO FOO");
    StatusRuntimeException notFound = Status.NOT_FOUND.withDescription("Table not found").asRuntimeException();
    mockSpanner.putStatementResult(StatementResult.exception(invalidStatement, notFound));
    try (ITConnection connection = createConnection(createAbortFirstRetryListener(invalidStatement, notFound))) {
        try {
            connection.executeBatchUpdate(Collections.singletonList(invalidStatement));
            fail("missing expected exception");
        } catch (SpannerException e) {
            assertThat(e.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
        }
        connection.execute(INSERT_STATEMENT);
        // Force an abort and retry.
        mockSpanner.abortNextStatement();
        connection.commit();
    }
    assertThat(mockSpanner.countRequestsOfType(CommitRequest.class)).isEqualTo(2);
    assertThat(mockSpanner.countRequestsOfType(ExecuteBatchDmlRequest.class)).isEqualTo(6);
}
Also used : ITConnection(com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection) Statement(com.google.cloud.spanner.Statement) StatusRuntimeException(io.grpc.StatusRuntimeException) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 2 with ITConnection

use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection in project java-spanner by googleapis.

the class AbortedTest method testCommitAborted.

@Test
public void testCommitAborted() {
    // transaction is the most recent transaction of that session.
    for (int i = 0; i < 2; i++) {
        mockSpanner.putStatementResult(StatementResult.query(SELECT_COUNT_STATEMENT, SELECT_COUNT_RESULTSET_BEFORE_INSERT));
        mockSpanner.putStatementResult(StatementResult.update(INSERT_STATEMENT, UPDATE_COUNT));
        AbortInterceptor interceptor = new AbortInterceptor(0);
        try (ITConnection connection = createConnection(interceptor, new CountTransactionRetryListener())) {
            // verify that the there is no test record
            try (ResultSet rs = connection.executeQuery(Statement.of("SELECT COUNT(*) AS C FROM TEST WHERE ID=1"))) {
                assertThat(rs.next(), is(true));
                assertThat(rs.getLong("C"), is(equalTo(0L)));
                assertThat(rs.next(), is(false));
            }
            // do an insert
            connection.executeUpdate(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (1, 'test aborted')"));
            // indicate that the next statement should abort
            interceptor.setProbability(1.0);
            interceptor.setOnlyInjectOnce(true);
            // do a commit that will first abort, and then on retry will succeed
            connection.commit();
            mockSpanner.putStatementResult(StatementResult.query(SELECT_COUNT_STATEMENT, SELECT_COUNT_RESULTSET_AFTER_INSERT));
            // verify that the insert succeeded
            try (ResultSet rs = connection.executeQuery(Statement.of("SELECT COUNT(*) AS C FROM TEST WHERE ID=1"))) {
                assertThat(rs.next(), is(true));
                assertThat(rs.getLong("C"), is(equalTo(1L)));
                assertThat(rs.next(), is(false));
            }
        }
    }
}
Also used : ITConnection(com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection) AbortInterceptor(com.google.cloud.spanner.connection.ITAbstractSpannerTest.AbortInterceptor) ResultSet(com.google.cloud.spanner.ResultSet) CountTransactionRetryListener(com.google.cloud.spanner.connection.it.ITTransactionRetryTest.CountTransactionRetryListener) Test(org.junit.Test)

Example 3 with ITConnection

use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection in project java-spanner by googleapis.

the class AbortedTest method testAbortedDuringRetryOfFailedUpdate.

@Test
public void testAbortedDuringRetryOfFailedUpdate() {
    final Statement invalidStatement = Statement.of("INSERT INTO FOO");
    StatusRuntimeException notFound = Status.NOT_FOUND.withDescription("Table not found").asRuntimeException();
    mockSpanner.putStatementResult(StatementResult.exception(invalidStatement, notFound));
    try (ITConnection connection = createConnection(createAbortFirstRetryListener(invalidStatement, notFound))) {
        connection.execute(INSERT_STATEMENT);
        try {
            connection.execute(invalidStatement);
            fail("missing expected exception");
        } catch (SpannerException e) {
            assertThat(e.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
        }
        // Force an abort and retry.
        mockSpanner.abortNextStatement();
        connection.commit();
    }
    assertThat(mockSpanner.countRequestsOfType(CommitRequest.class)).isEqualTo(2);
    // The transaction will be executed 3 times, which means that there will be 6
    // ExecuteSqlRequests:
    // 1. The initial attempt.
    // 2. The first retry attempt. This will fail on the invalid statement as it is aborted.
    // 3. the second retry attempt. This will succeed.
    assertThat(mockSpanner.countRequestsOfType(ExecuteSqlRequest.class)).isEqualTo(6);
}
Also used : ITConnection(com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection) Statement(com.google.cloud.spanner.Statement) StatusRuntimeException(io.grpc.StatusRuntimeException) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 4 with ITConnection

use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection in project java-spanner by googleapis.

the class AbortedTest method testAbortedDuringRetryOfFailedBatchUpdate.

@Test
public void testAbortedDuringRetryOfFailedBatchUpdate() {
    final Statement invalidStatement = Statement.of("INSERT INTO FOO");
    StatusRuntimeException notFound = Status.NOT_FOUND.withDescription("Table not found").asRuntimeException();
    mockSpanner.putStatementResult(StatementResult.exception(invalidStatement, notFound));
    try (ITConnection connection = createConnection(createAbortFirstRetryListener(invalidStatement, notFound))) {
        connection.execute(INSERT_STATEMENT);
        try {
            connection.executeBatchUpdate(Collections.singletonList(invalidStatement));
            fail("missing expected exception");
        } catch (SpannerException e) {
            assertThat(e.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
        }
        // Force an abort and retry.
        mockSpanner.abortNextStatement();
        connection.commit();
    }
    assertThat(mockSpanner.countRequestsOfType(CommitRequest.class)).isEqualTo(2);
    assertThat(mockSpanner.countRequestsOfType(ExecuteBatchDmlRequest.class)).isEqualTo(3);
}
Also used : ITConnection(com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection) Statement(com.google.cloud.spanner.Statement) StatusRuntimeException(io.grpc.StatusRuntimeException) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 5 with ITConnection

use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection in project java-spanner by googleapis.

the class ConnectionAsyncApiAbortedTest method createConnection.

ITConnection createConnection(TransactionRetryListener listener) {
    ITConnection connection = super.createConnection(ImmutableList.of(), ImmutableList.of(listener));
    connection.setAutocommit(false);
    return connection;
}
Also used : ITConnection(com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection)

Aggregations

ITConnection (com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection)11 Test (org.junit.Test)8 SpannerException (com.google.cloud.spanner.SpannerException)7 Statement (com.google.cloud.spanner.Statement)7 StatusRuntimeException (io.grpc.StatusRuntimeException)7 ResultSet (com.google.cloud.spanner.ResultSet)4 AbortInterceptor (com.google.cloud.spanner.connection.ITAbstractSpannerTest.AbortInterceptor)2 CountTransactionRetryListener (com.google.cloud.spanner.connection.it.ITTransactionRetryTest.CountTransactionRetryListener)2 Timestamp (com.google.cloud.Timestamp)1 ErrorCode (com.google.cloud.spanner.ErrorCode)1 StatementResult (com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult)1 ImmutableList (com.google.common.collect.ImmutableList)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 ByteString (com.google.protobuf.ByteString)1 CommitRequest (com.google.spanner.v1.CommitRequest)1 ExecuteBatchDmlRequest (com.google.spanner.v1.ExecuteBatchDmlRequest)1 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)1 Status (io.grpc.Status)1 Collections (java.util.Collections)1 CoreMatchers.equalTo (org.hamcrest.CoreMatchers.equalTo)1