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();
}
}
}
}
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);
}
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();
}
}
}
}
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();
}
}
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();
}
}
Aggregations