Search in sources :

Example 6 with CommitTimestampFuture

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

the class AsyncTransactionManagerTest method asyncTransactionManagerIsNonBlockingWithBatchUpdate.

@Test
public void asyncTransactionManagerIsNonBlockingWithBatchUpdate() throws Exception {
    mockSpanner.freeze();
    try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
        TransactionContextFuture transactionContextFuture = manager.beginAsync();
        while (true) {
            try {
                AsyncTransactionStep<Void, long[]> updateCounts = transactionContextFuture.then((transactionContext, ignored) -> transactionContext.batchUpdateAsync(Collections.singleton(UPDATE_STATEMENT)), executor);
                CommitTimestampFuture commitTimestampFuture = updateCounts.commitAsync();
                mockSpanner.unfreeze();
                assertThat(commitTimestampFuture.get()).isNotNull();
                assertThat(updateCounts.get()).asList().containsExactly(UPDATE_COUNT);
                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 7 with CommitTimestampFuture

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

the class AsyncTransactionManagerTest method asyncTransactionManagerFireAndForgetInvalidUpdate.

@Test
public void asyncTransactionManagerFireAndForgetInvalidUpdate() throws Exception {
    try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
        TransactionContextFuture transactionContextFuture = manager.beginAsync();
        while (true) {
            try {
                AsyncTransactionStep<Void, Long> transaction = transactionContextFuture.then((transactionContext, ignored) -> {
                    // This fire-and-forget update statement should not fail the transaction.
                    // The exception will however cause the transaction to be retried, as the
                    // statement will not return a transaction id.
                    transactionContext.executeUpdateAsync(INVALID_UPDATE_STATEMENT);
                    return transactionContext.executeUpdateAsync(UPDATE_STATEMENT);
                }, executor);
                CommitTimestampFuture commitTimestamp = transaction.commitAsync();
                assertThat(commitTimestamp.get()).isNotNull();
                assertThat(transaction.get()).isEqualTo(UPDATE_COUNT);
                break;
            } catch (AbortedException e) {
                transactionContextFuture = manager.resetForRetryAsync();
            }
        }
    }
    assertThat(mockSpanner.getRequestTypes()).containsExactly(BatchCreateSessionsRequest.class, // The first update that fails. This will cause a transaction retry.
    ExecuteSqlRequest.class, // The retry will use an explicit BeginTransaction call.
    BeginTransactionRequest.class, // transaction can continue.
    ExecuteSqlRequest.class, ExecuteSqlRequest.class, CommitRequest.class);
}
Also used : TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) CommitTimestampFuture(com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture) Test(org.junit.Test)

Example 8 with CommitTimestampFuture

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

the class AsyncTransactionManagerTest method asyncTransactionManagerChain.

@Test
public void asyncTransactionManagerChain() throws Exception {
    try (AsyncTransactionManager manager = client().transactionManagerAsync()) {
        TransactionContextFuture transactionContextFuture = manager.beginAsync();
        while (true) {
            try {
                CommitTimestampFuture commitTimestamp = transactionContextFuture.then((transaction, ignored) -> transaction.executeUpdateAsync(UPDATE_STATEMENT), executor).then((transactionContext, ignored) -> transactionContext.readRowAsync(READ_TABLE_NAME, Key.of(1L), READ_COLUMN_NAMES), executor).then((ignored, input) -> ApiFutures.immediateFuture(input.getString("Value")), executor).then((ignored, input) -> {
                    assertThat(input).isEqualTo("v1");
                    return ApiFutures.immediateFuture(null);
                }, executor).commitAsync();
                assertThat(commitTimestamp.get()).isNotNull();
                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 9 with CommitTimestampFuture

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

the class RetryOnInvalidatedSessionTest method asyncTransactionManager_readAsync.

private void asyncTransactionManager_readAsync(final Function<TransactionContext, AsyncResultSet> fn) throws InterruptedException {
    final ExecutorService queryExecutor = Executors.newSingleThreadExecutor();
    try (AsyncTransactionManager manager = client.transactionManagerAsync()) {
        TransactionContextFuture context = manager.beginAsync();
        while (true) {
            try {
                final AtomicLong counter = new AtomicLong();
                AsyncTransactionStep<Void, Long> count = context.then((transaction, ignored) -> {
                    AsyncResultSet rs = fn.apply(transaction);
                    ApiFuture<Void> fut = rs.setCallback(queryExecutor, resultSet -> {
                        while (true) {
                            switch(resultSet.tryNext()) {
                                case OK:
                                    counter.incrementAndGet();
                                    break;
                                case DONE:
                                    return CallbackResponse.DONE;
                                case NOT_READY:
                                    return CallbackResponse.CONTINUE;
                            }
                        }
                    });
                    return ApiFutures.transform(fut, input -> counter.get(), MoreExecutors.directExecutor());
                }, executor);
                CommitTimestampFuture ts = count.commitAsync();
                assertThrowsSessionNotFoundIfShouldFail(() -> get(ts));
                break;
            } catch (AbortedException e) {
                context = manager.resetForRetryAsync();
            }
        }
    } finally {
        queryExecutor.shutdown();
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ExecutorService(java.util.concurrent.ExecutorService) AtomicLong(java.util.concurrent.atomic.AtomicLong) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) CommitTimestampFuture(com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture)

Example 10 with CommitTimestampFuture

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

the class RetryOnInvalidatedSessionTest method asyncTransactionManager_readRowFunction.

private void asyncTransactionManager_readRowFunction(final Function<TransactionContext, ApiFuture<Struct>> fn) throws InterruptedException {
    final ExecutorService queryExecutor = Executors.newSingleThreadExecutor();
    try (AsyncTransactionManager manager = client.transactionManagerAsync()) {
        TransactionContextFuture context = manager.beginAsync();
        while (true) {
            try {
                AsyncTransactionStep<Void, Struct> row = context.then((transaction, ignored) -> fn.apply(transaction), executor);
                CommitTimestampFuture ts = row.commitAsync();
                assertThrowsSessionNotFoundIfShouldFail(() -> get(ts));
                break;
            } catch (AbortedException e) {
                context = manager.resetForRetryAsync();
            }
        }
    } finally {
        queryExecutor.shutdown();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) TransactionContextFuture(com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture) CommitTimestampFuture(com.google.cloud.spanner.AsyncTransactionManager.CommitTimestampFuture)

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