Skip to content
Permalink
Browse files
feat: transaction callable as functional interface (#1066)
* feat: transaction callable as functional interface

Marks the transaction callable as a functional interface.

* samples: uses lambdas in samples
  • Loading branch information
thiagotnunes committed Apr 15, 2021
1 parent 1d4eed4 commit b036a77196886f16d2738e70f676ccc99a52874c
Showing with 952 additions and 1,591 deletions.
  1. +7 −11 google-cloud-spanner/src/main/java/com/google/cloud/spanner/AsyncRunnerImpl.java
  2. +3 −6 google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionImpl.java
  3. +1 −0 google-cloud-spanner/src/main/java/com/google/cloud/spanner/TransactionRunner.java
  4. +24 −37 google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/SingleUseTransaction.java
  5. +1 −8 google-cloud-spanner/src/test/java/com/google/cloud/spanner/BackendExhaustedTest.java
  6. +90 −181 google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java
  7. +4 −15 google-cloud-spanner/src/test/java/com/google/cloud/spanner/InlineBeginBenchmark.java
  8. +182 −290 google-cloud-spanner/src/test/java/com/google/cloud/spanner/InlineBeginTransactionTest.java
  9. +3 −8 google-cloud-spanner/src/test/java/com/google/cloud/spanner/MockSpannerServiceImpl.java
  10. +120 −176 ...cloud-spanner/src/test/java/com/google/cloud/spanner/ReadWriteTransactionWithInlineBeginTest.java
  11. +33 −73 google-cloud-spanner/src/test/java/com/google/cloud/spanner/RetryOnInvalidatedSessionTest.java
  12. +15 −40 google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionImplTest.java
  13. +4 −15 google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionPoolBenchmark.java
  14. +1 −10 google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionPoolLeakTest.java
  15. +4 −15 google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionPoolMaintainerBenchmark.java
  16. +6 −13 google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpanTest.java
  17. +18 −51 google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerGaxRetryTest.java
  18. +21 −48 google-cloud-spanner/src/test/java/com/google/cloud/spanner/TransactionRunnerImplTest.java
  19. +39 −55 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITBatchDmlTest.java
  20. +12 −19 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITClosedSessionTest.java
  21. +43 −60 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITDMLTest.java
  22. +11 −25 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITQueryOptionsTest.java
  23. +120 −180 google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITTransactionTest.java
  24. +2 −16 google-cloud-spanner/src/test/java/com/google/cloud/spanner/spi/v1/GapicSpannerRpcTest.java
  25. +9 −14 samples/snippets/src/main/java/com/example/spanner/CustomTimeoutAndRetrySettingsExample.java
  26. +173 −215 samples/snippets/src/main/java/com/example/spanner/SpannerSample.java
  27. +6 −10 samples/snippets/src/main/java/com/example/spanner/StatementTimeoutExample.java
@@ -23,7 +23,6 @@
import com.google.api.core.ApiFutures;
import com.google.api.core.SettableApiFuture;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.TransactionRunner.TransactionCallable;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.concurrent.ExecutionException;
@@ -60,16 +59,13 @@ public void run() {

private <R> R runTransaction(final AsyncWork<R> work) {
return delegate.run(
new TransactionCallable<R>() {
@Override
public R run(TransactionContext transaction) throws Exception {
try {
return work.doWorkAsync(transaction).get();
} catch (ExecutionException e) {
throw SpannerExceptionFactory.newSpannerException(e.getCause());
} catch (InterruptedException e) {
throw SpannerExceptionFactory.propagateInterrupt(e);
}
transaction -> {
try {
return work.doWorkAsync(transaction).get();
} catch (ExecutionException e) {
throw SpannerExceptionFactory.newSpannerException(e.getCause());
} catch (InterruptedException e) {
throw SpannerExceptionFactory.propagateInterrupt(e);
}
});
}
@@ -141,12 +141,9 @@ public CommitResponse writeWithOptions(Iterable<Mutation> mutations, Transaction
? (Collection<Mutation>) mutations
: Lists.newArrayList(mutations);
runner.run(
new TransactionRunner.TransactionCallable<Void>() {
@Override
public Void run(TransactionContext ctx) {
ctx.buffer(finalMutations);
return null;
}
ctx -> {
ctx.buffer(finalMutations);
return null;
});
return runner.getCommitResponse();
}
@@ -30,6 +30,7 @@
*/
public interface TransactionRunner {
/** A unit of work to be performed in the context of a transaction. */
@FunctionalInterface
interface TransactionCallable<T> {
/**
* Invoked by the library framework to perform a single attempt of a transaction. This method
@@ -34,9 +34,7 @@
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.TimestampBound;
import com.google.cloud.spanner.TransactionContext;
import com.google.cloud.spanner.TransactionRunner;
import com.google.cloud.spanner.TransactionRunner.TransactionCallable;
import com.google.cloud.spanner.connection.StatementParser.ParsedStatement;
import com.google.cloud.spanner.connection.StatementParser.StatementType;
import com.google.common.base.Function;
@@ -357,12 +355,7 @@ public Long call() throws Exception {
writeTransaction = createWriteTransaction();
Long res =
writeTransaction.run(
new TransactionCallable<Long>() {
@Override
public Long run(TransactionContext transaction) throws Exception {
return transaction.executeUpdate(update.getStatement());
}
});
transaction -> transaction.executeUpdate(update.getStatement()));
state = UnitOfWorkState.COMMITTED;
return res;
} catch (Throwable t) {
@@ -404,31 +397,28 @@ private ApiFuture<long[]> executeTransactionalBatchUpdateAsync(
public long[] call() throws Exception {
writeTransaction = createWriteTransaction();
return writeTransaction.run(
new TransactionCallable<long[]>() {
@Override
public long[] run(TransactionContext transaction) throws Exception {
try {
long[] res =
transaction.batchUpdate(
Iterables.transform(
updates,
new Function<ParsedStatement, Statement>() {
@Override
public Statement apply(ParsedStatement input) {
return input.getStatement();
}
}));
transaction -> {
try {
long[] res =
transaction.batchUpdate(
Iterables.transform(
updates,
new Function<ParsedStatement, Statement>() {
@Override
public Statement apply(ParsedStatement input) {
return input.getStatement();
}
}));
state = UnitOfWorkState.COMMITTED;
return res;
} catch (Throwable t) {
if (t instanceof SpannerBatchUpdateException) {
// Batch update exceptions does not cause a rollback.
state = UnitOfWorkState.COMMITTED;
return res;
} catch (Throwable t) {
if (t instanceof SpannerBatchUpdateException) {
// Batch update exceptions does not cause a rollback.
state = UnitOfWorkState.COMMITTED;
} else {
state = UnitOfWorkState.COMMIT_FAILED;
}
throw t;
} else {
state = UnitOfWorkState.COMMIT_FAILED;
}
throw t;
}
});
}
@@ -455,12 +445,9 @@ public Void call() throws Exception {
writeTransaction = createWriteTransaction();
Void res =
writeTransaction.run(
new TransactionCallable<Void>() {
@Override
public Void run(TransactionContext transaction) throws Exception {
transaction.buffer(mutations);
return null;
}
transaction -> {
transaction.buffer(mutations);
return null;
});
state = UnitOfWorkState.COMMITTED;
return res;
@@ -24,7 +24,6 @@
import com.google.cloud.grpc.GrpcTransportOptions.ExecutorFactory;
import com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime;
import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult;
import com.google.cloud.spanner.TransactionRunner.TransactionCallable;
import com.google.protobuf.ListValue;
import com.google.spanner.v1.ResultSetMetadata;
import com.google.spanner.v1.StructType;
@@ -205,13 +204,7 @@ private final class WriteRunnable implements Runnable {
@Override
public void run() {
TransactionRunner runner = client.readWriteTransaction();
runner.run(
new TransactionCallable<Long>() {
@Override
public Long run(TransactionContext transaction) {
return transaction.executeUpdate(UPDATE_STATEMENT);
}
});
runner.run(transaction -> transaction.executeUpdate(UPDATE_STATEMENT));
}
}
}

0 comments on commit b036a77

Please sign in to comment.