use of com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture 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();
}
}
}
}
use of com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture 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));
}
}
use of com.google.cloud.spanner.AsyncTransactionManager.TransactionContextFuture 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.TransactionContextFuture 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.TransactionContextFuture 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();
}
}
}
}
Aggregations