Search in sources :

Example 6 with UPDATE_STATEMENT

use of com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_STATEMENT in project java-spanner by googleapis.

the class AsyncTransactionManagerTest method asyncTransactionManagerWithBatchUpdateCommitAborted.

@Test
public void asyncTransactionManagerWithBatchUpdateCommitAborted() throws Exception {
    try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
        // Temporarily set the result of the update to 2 rows.
        mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT + 1L));
        final AtomicInteger attempt = new AtomicInteger();
        TransactionContextFuture txn = manager.beginAsync();
        while (true) {
            try {
                AsyncTransactionStep<Void, long[]> updateCounts = txn.then((ignored1, ignored2) -> {
                    if (attempt.get() > 0) {
                        // Set the result of the update statement back to 1 row.
                        mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
                    }
                    return ApiFutures.<Void>immediateFuture(null);
                }, executor).then((transactionContext, ignored) -> transactionContext.batchUpdateAsync(ImmutableList.of(UPDATE_STATEMENT, UPDATE_STATEMENT)), executor);
                updateCounts.then((transaction, ignored) -> {
                    if (attempt.incrementAndGet() == 1) {
                        mockSpanner.abortTransaction(transaction);
                    }
                    return ApiFutures.immediateFuture(null);
                }, executor).commitAsync().get();
                assertThat(updateCounts.get()).asList().containsExactly(UPDATE_COUNT, UPDATE_COUNT);
                assertThat(attempt.get()).isEqualTo(2);
                break;
            } catch (AbortedException e) {
                txn = manager.resetForRetryAsync();
            }
        }
    } finally {
        mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
    }
    assertThat(mockSpanner.getRequestTypes()).containsExactly(BatchCreateSessionsRequest.class, ExecuteBatchDmlRequest.class, CommitRequest.class, BeginTransactionRequest.class, ExecuteBatchDmlRequest.class, CommitRequest.class);
}
Also used : Arrays(java.util.Arrays) BatchCreateSessionsRequest(com.google.spanner.v1.BatchCreateSessionsRequest) TimeoutException(java.util.concurrent.TimeoutException) StatementResult(com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UPDATE_STATEMENT(com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_STATEMENT) TransactionSelector(com.google.spanner.v1.TransactionSelector) Status(io.grpc.Status) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Parameterized(org.junit.runners.Parameterized) CommitTimestampFuture(com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture) ApiFutures(com.google.api.core.ApiFutures) SpannerApiFutures.get(com.google.cloud.spanner.SpannerApiFutures.get) Collection(java.util.Collection) READ_COLUMN_NAMES(com.google.cloud.spanner.MockSpannerTestUtil.READ_COLUMN_NAMES) Range(com.google.common.collect.Range) ApiFutureCallback(com.google.api.core.ApiFutureCallback) Executors(java.util.concurrent.Executors) ApiFuture(com.google.api.core.ApiFuture) SettableApiFuture(com.google.api.core.SettableApiFuture) UPDATE_COUNT(com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_COUNT) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) Iterables(com.google.common.collect.Iterables) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) INVALID_UPDATE_STATEMENT(com.google.cloud.spanner.MockSpannerTestUtil.INVALID_UPDATE_STATEMENT) READ_TABLE_NAME(com.google.cloud.spanner.MockSpannerTestUtil.READ_TABLE_NAME) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) CommitRequest(com.google.spanner.v1.CommitRequest) ImmutableList(com.google.common.collect.ImmutableList) SimulatedExecutionTime(com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) AbstractMessage(com.google.protobuf.AbstractMessage) Executor(java.util.concurrent.Executor) Assert.assertNotNull(org.junit.Assert.assertNotNull) Parameter(org.junit.runners.Parameterized.Parameter) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) RollbackRequest(com.google.spanner.v1.RollbackRequest) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ReadOption(com.google.cloud.spanner.Options.ReadOption) AsyncTransactionFunction(com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionFunction) AsyncTransactionStep(com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionStep) SessionPoolTransactionContext(com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext) UPDATE_ABORTED_STATEMENT(com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_ABORTED_STATEMENT) TransactionContextImpl(com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) Test(org.junit.Test)

Example 7 with UPDATE_STATEMENT

use of com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_STATEMENT in project java-spanner by googleapis.

the class AsyncTransactionManagerTest method asyncTransactionManagerUpdateAbortedWithoutGettingResult.

@Test
public void asyncTransactionManagerUpdateAbortedWithoutGettingResult() throws Exception {
    final AtomicInteger attempt = new AtomicInteger();
    try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
        TransactionContextFuture transactionContextFuture = manager.beginAsync();
        while (true) {
            try {
                CommitTimestampFuture commitTimestampFuture = transactionContextFuture.then((transaction, ignored) -> {
                    if (attempt.incrementAndGet() == 1) {
                        mockSpanner.abortNextStatement();
                    }
                    // This update statement will be aborted, but the error will not
                    // propagated to the transaction runner and cause the transaction to
                    // retry. Instead, the commit call will do that.
                    transaction.executeUpdateAsync(UPDATE_STATEMENT);
                    // has actually finished successfully.
                    return ApiFutures.immediateFuture(null);
                }, executor).commitAsync();
                assertThat(commitTimestampFuture.get()).isNotNull();
                assertThat(attempt.get()).isEqualTo(2);
                // The server may receive 1 or 2 commit requests depending on whether the call to
                // commitAsync() already knows that the transaction has aborted. If it does, it will not
                // attempt to call the Commit RPC and instead directly propagate the Aborted error.
                assertThat(mockSpanner.getRequestTypes()).containsAtLeast(BatchCreateSessionsRequest.class, ExecuteSqlRequest.class, // The retry will use a BeginTransaction RPC.
                BeginTransactionRequest.class, ExecuteSqlRequest.class, CommitRequest.class);
                break;
            } catch (AbortedException e) {
                transactionContextFuture = manager.resetForRetryAsync();
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) BatchCreateSessionsRequest(com.google.spanner.v1.BatchCreateSessionsRequest) TimeoutException(java.util.concurrent.TimeoutException) StatementResult(com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UPDATE_STATEMENT(com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_STATEMENT) TransactionSelector(com.google.spanner.v1.TransactionSelector) Status(io.grpc.Status) BeginTransactionRequest(com.google.spanner.v1.BeginTransactionRequest) Parameterized(org.junit.runners.Parameterized) CommitTimestampFuture(com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture) ApiFutures(com.google.api.core.ApiFutures) SpannerApiFutures.get(com.google.cloud.spanner.SpannerApiFutures.get) Collection(java.util.Collection) READ_COLUMN_NAMES(com.google.cloud.spanner.MockSpannerTestUtil.READ_COLUMN_NAMES) Range(com.google.common.collect.Range) ApiFutureCallback(com.google.api.core.ApiFutureCallback) Executors(java.util.concurrent.Executors) ApiFuture(com.google.api.core.ApiFuture) SettableApiFuture(com.google.api.core.SettableApiFuture) UPDATE_COUNT(com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_COUNT) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) Iterables(com.google.common.collect.Iterables) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) INVALID_UPDATE_STATEMENT(com.google.cloud.spanner.MockSpannerTestUtil.INVALID_UPDATE_STATEMENT) READ_TABLE_NAME(com.google.cloud.spanner.MockSpannerTestUtil.READ_TABLE_NAME) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) CommitRequest(com.google.spanner.v1.CommitRequest) ImmutableList(com.google.common.collect.ImmutableList) SimulatedExecutionTime(com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) AbstractMessage(com.google.protobuf.AbstractMessage) Executor(java.util.concurrent.Executor) Assert.assertNotNull(org.junit.Assert.assertNotNull) Parameter(org.junit.runners.Parameterized.Parameter) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) RollbackRequest(com.google.spanner.v1.RollbackRequest) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ReadOption(com.google.cloud.spanner.Options.ReadOption) AsyncTransactionFunction(com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionFunction) AsyncTransactionStep(com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionStep) SessionPoolTransactionContext(com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext) UPDATE_ABORTED_STATEMENT(com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_ABORTED_STATEMENT) TransactionContextImpl(com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl) ExecuteBatchDmlRequest(com.google.spanner.v1.ExecuteBatchDmlRequest) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) CommitTimestampFuture(com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture) Test(org.junit.Test)

Aggregations

ApiFuture (com.google.api.core.ApiFuture)7 ApiFutureCallback (com.google.api.core.ApiFutureCallback)7 ApiFutures (com.google.api.core.ApiFutures)7 SettableApiFuture (com.google.api.core.SettableApiFuture)7 AsyncTransactionFunction (com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionFunction)7 AsyncTransactionStep (com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionStep)7 CommitTimestampFuture (com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture)7 TransactionContextFuture (com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture)7 SimulatedExecutionTime (com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime)7 StatementResult (com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult)7 INVALID_UPDATE_STATEMENT (com.google.cloud.spanner.MockSpannerTestUtil.INVALID_UPDATE_STATEMENT)7 READ_COLUMN_NAMES (com.google.cloud.spanner.MockSpannerTestUtil.READ_COLUMN_NAMES)7 READ_TABLE_NAME (com.google.cloud.spanner.MockSpannerTestUtil.READ_TABLE_NAME)7 UPDATE_ABORTED_STATEMENT (com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_ABORTED_STATEMENT)7 UPDATE_COUNT (com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_COUNT)7 UPDATE_STATEMENT (com.google.cloud.spanner.MockSpannerTestUtil.UPDATE_STATEMENT)7 ReadOption (com.google.cloud.spanner.Options.ReadOption)7 SessionPoolTransactionContext (com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext)7 SpannerApiFutures.get (com.google.cloud.spanner.SpannerApiFutures.get)7 TransactionContextImpl (com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl)7