Search in sources :

Example 1 with CommitTimestampFuture

use of com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture in project java-spanner by googleapis.

the class AsyncTransactionManagerTest method asyncTransactionManagerUpdate.

@Test
public void asyncTransactionManagerUpdate() throws Exception {
    try (AsyncTransactionManager manager = client().transactionManagerAsync()) {
        TransactionContextFuture transactionContextFuture = manager.beginAsync();
        while (true) {
            try {
                AsyncTransactionStep<Void, Long> updateCount = transactionContextFuture.then((transactionContext, ignored) -> transactionContext.executeUpdateAsync(UPDATE_STATEMENT), executor);
                CommitTimestampFuture commitTimestamp = updateCount.commitAsync();
                assertThat(updateCount.get()).isEqualTo(UPDATE_COUNT);
                assertThat(commitTimestamp.get()).isNotNull();
                break;
            } catch (AbortedException e) {
                transactionContextFuture = manager.resetForRetryAsync();
            }
        }
    }
}
Also used : TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) CommitTimestampFuture(com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture) Test(org.junit.Test)

Example 2 with CommitTimestampFuture

use of com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture in project java-spanner by googleapis.

the class AsyncTransactionManagerTest method asyncTransactionManagerQuery.

@Test
public void asyncTransactionManagerQuery() throws Exception {
    mockSpanner.putStatementResult(StatementResult.query(Statement.of("SELECT FirstName FROM Singers WHERE ID=1"), MockSpannerTestUtil.READ_FIRST_NAME_SINGERS_RESULTSET));
    final long singerId = 1L;
    try (AsyncTransactionManager manager = client().transactionManagerAsync()) {
        TransactionContextFuture transactionContextFuture = manager.beginAsync();
        while (true) {
            final String column = "FirstName";
            CommitTimestampFuture commitTimestamp = transactionContextFuture.then((transactionContext, ignored) -> transactionContext.readRowAsync("Singers", Key.of(singerId), Collections.singleton(column)), executor).then((transaction, input) -> {
                String name = input.getString(column);
                return transaction.bufferAsync(Mutation.newUpdateBuilder("Singers").set(column).to(name.toUpperCase()).build());
            }, executor).commitAsync();
            try {
                commitTimestamp.get();
                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) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) CommitTimestampFuture(com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture) Test(org.junit.Test)

Example 3 with CommitTimestampFuture

use of com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture in project java-spanner by googleapis.

the class AsyncTransactionManagerTest method asyncTransactionManagerIsNonBlocking.

@Test
public void asyncTransactionManagerIsNonBlocking() throws Exception {
    mockSpanner.freeze();
    try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
        TransactionContextFuture transactionContextFuture = manager.beginAsync();
        while (true) {
            try {
                AsyncTransactionStep<Void, Long> updateCount = transactionContextFuture.then((transactionContext, ignored) -> transactionContext.executeUpdateAsync(UPDATE_STATEMENT), executor);
                CommitTimestampFuture commitTimestamp = updateCount.commitAsync();
                mockSpanner.unfreeze();
                assertThat(updateCount.get(10L, TimeUnit.SECONDS)).isEqualTo(UPDATE_COUNT);
                assertThat(commitTimestamp.get(10L, TimeUnit.SECONDS)).isNotNull();
                break;
            } catch (AbortedException e) {
                transactionContextFuture = manager.resetForRetryAsync();
            }
        }
    }
}
Also used : TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) CommitTimestampFuture(com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture) Test(org.junit.Test)

Example 4 with CommitTimestampFuture

use of com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture in project java-spanner by googleapis.

the class AsyncTransactionManagerTest method asyncTransactionManagerCommitAborted.

@Test
public void asyncTransactionManagerCommitAborted() throws Exception {
    final AtomicInteger attempt = new AtomicInteger();
    CountDownLatch abortedLatch = new CountDownLatch(1);
    try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
        TransactionContextFuture transactionContextFuture = manager.beginAsync();
        while (true) {
            try {
                attempt.incrementAndGet();
                AsyncTransactionStep<Void, Long> updateCount = transactionContextFuture.then((transaction, ignored) -> transaction.executeUpdateAsync(UPDATE_STATEMENT), executor);
                updateCount.then((transaction, ignored) -> {
                    if (attempt.get() == 1) {
                        mockSpanner.abortTransaction(transaction);
                        abortedLatch.countDown();
                    }
                    return ApiFutures.immediateFuture(null);
                }, executor);
                abortedLatch.await(10L, TimeUnit.SECONDS);
                CommitTimestampFuture commitTimestamp = updateCount.commitAsync();
                assertThat(updateCount.get()).isEqualTo(UPDATE_COUNT);
                assertThat(commitTimestamp.get()).isNotNull();
                assertThat(attempt.get()).isEqualTo(2);
                break;
            } catch (AbortedException e) {
                transactionContextFuture = manager.resetForRetryAsync();
            }
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) CountDownLatch(java.util.concurrent.CountDownLatch) CommitTimestampFuture(com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture) Test(org.junit.Test)

Example 5 with CommitTimestampFuture

use of com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture in project java-spanner by googleapis.

the class AsyncTransactionManagerTest method asyncTransactionManagerUpdateAborted.

@Test
public void asyncTransactionManagerUpdateAborted() throws Exception {
    try (AsyncTransactionManager manager = client().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 transactionContextFuture = manager.beginAsync();
        while (true) {
            try {
                CommitTimestampFuture commitTimestampFuture = transactionContextFuture.then((ignored1, ignored2) -> {
                    if (attempt.incrementAndGet() == 1) {
                        // Abort the first attempt.
                        mockSpanner.abortNextStatement();
                    } else {
                        // Set the result of the update statement back to 1 row.
                        mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
                    }
                    return ApiFutures.immediateFuture(null);
                }, executor).then((transactionContext, ignored) -> transactionContext.executeUpdateAsync(UPDATE_STATEMENT), executor).commitAsync();
                assertThat(commitTimestampFuture.get()).isNotNull();
                break;
            } catch (AbortedException e) {
                transactionContextFuture = manager.resetForRetryAsync();
            }
        }
        assertThat(attempt.get()).isEqualTo(2);
    } finally {
        mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
    }
}
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

CommitTimestampFuture (com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture)17 TransactionContextFuture (com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture)17 ApiFuture (com.google.api.core.ApiFuture)8 ApiFutures (com.google.api.core.ApiFutures)8 AsyncTransactionStep (com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionStep)8 ImmutableList (com.google.common.collect.ImmutableList)8 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)8 Arrays (java.util.Arrays)8 List (java.util.List)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 ExecutionException (java.util.concurrent.ExecutionException)8 Test (org.junit.Test)8 ApiFutureCallback (com.google.api.core.ApiFutureCallback)7 SettableApiFuture (com.google.api.core.SettableApiFuture)7 AsyncTransactionFunction (com.google.cloud.spanner.AsyncTransactionManager.AsyncTransactionFunction)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