Skip to content

Commit 6004c9f

Browse files
authored
chore(spanner): support transaction channel hint for RW mux (#3317)
* chore(spanner): add transaction channel hint for RW to support multiplexed sessions * chore(spanner): lint fix
1 parent cce008d commit 6004c9f

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,8 @@ public void close() {
426426
}
427427
}
428428

429-
ApiFuture<ByteString> beginTransactionAsync(Options transactionOptions, boolean routeToLeader) {
429+
ApiFuture<ByteString> beginTransactionAsync(
430+
Options transactionOptions, boolean routeToLeader, Map<SpannerRpc.Option, ?> channelHint) {
430431
final SettableApiFuture<ByteString> res = SettableApiFuture.create();
431432
final ISpan span = tracer.spanBuilder(SpannerImpl.BEGIN_TRANSACTION);
432433
final BeginTransactionRequest request =
@@ -436,7 +437,7 @@ ApiFuture<ByteString> beginTransactionAsync(Options transactionOptions, boolean
436437
.build();
437438
final ApiFuture<Transaction> requestFuture;
438439
try (IScope ignore = tracer.withSpan(span)) {
439-
requestFuture = spanner.getRpc().beginTransactionAsync(request, getOptions(), routeToLeader);
440+
requestFuture = spanner.getRpc().beginTransactionAsync(request, channelHint, routeToLeader);
440441
}
441442
requestFuture.addListener(
442443
() -> {

google-cloud-spanner/src/main/java/com/google/cloud/spanner/TransactionRunnerImpl.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ ApiFuture<Void> ensureTxnAsync() {
282282

283283
private void createTxnAsync(final SettableApiFuture<Void> res) {
284284
span.addAnnotation("Creating Transaction");
285-
final ApiFuture<ByteString> fut = session.beginTransactionAsync(options, isRouteToLeader());
285+
final ApiFuture<ByteString> fut =
286+
session.beginTransactionAsync(options, isRouteToLeader(), getTransactionChannelHint());
286287
fut.addListener(
287288
() -> {
288289
try {
@@ -427,7 +428,7 @@ public void run() {
427428
final ApiFuture<com.google.spanner.v1.CommitResponse> commitFuture;
428429
final ISpan opSpan = tracer.spanBuilderWithExplicitParent(SpannerImpl.COMMIT, span);
429430
try (IScope ignore = tracer.withSpan(opSpan)) {
430-
commitFuture = rpc.commitAsync(commitRequest, session.getOptions());
431+
commitFuture = rpc.commitAsync(commitRequest, getTransactionChannelHint());
431432
}
432433
session.markUsed(clock.instant());
433434
commitFuture.addListener(
@@ -525,7 +526,7 @@ ApiFuture<Empty> rollbackAsync() {
525526
.setSession(session.getName())
526527
.setTransactionId(transactionId)
527528
.build(),
528-
session.getOptions());
529+
getTransactionChannelHint());
529530
session.markUsed(clock.instant());
530531
return apiFuture;
531532
} else {
@@ -800,7 +801,7 @@ private ResultSet internalExecuteUpdate(
800801
statement, queryMode, options, /* withTransactionSelector = */ true);
801802
try {
802803
com.google.spanner.v1.ResultSet resultSet =
803-
rpc.executeQuery(builder.build(), session.getOptions(), isRouteToLeader());
804+
rpc.executeQuery(builder.build(), getTransactionChannelHint(), isRouteToLeader());
804805
session.markUsed(clock.instant());
805806
if (resultSet.getMetadata().hasTransaction()) {
806807
onTransactionMetadata(
@@ -838,7 +839,8 @@ public ApiFuture<Long> executeUpdateAsync(Statement statement, UpdateOption... u
838839
// commit.
839840
increaseAsyncOperations();
840841
resultSet =
841-
rpc.executeQueryAsync(builder.build(), session.getOptions(), isRouteToLeader());
842+
rpc.executeQueryAsync(
843+
builder.build(), getTransactionChannelHint(), isRouteToLeader());
842844
session.markUsed(clock.instant());
843845
} catch (Throwable t) {
844846
decreaseAsyncOperations();
@@ -926,7 +928,7 @@ public long[] batchUpdate(Iterable<Statement> statements, UpdateOption... update
926928
getExecuteBatchDmlRequestBuilder(statements, options);
927929
try {
928930
com.google.spanner.v1.ExecuteBatchDmlResponse response =
929-
rpc.executeBatchDml(builder.build(), session.getOptions());
931+
rpc.executeBatchDml(builder.build(), getTransactionChannelHint());
930932
session.markUsed(clock.instant());
931933
long[] results = new long[response.getResultSetsCount()];
932934
for (int i = 0; i < response.getResultSetsCount(); ++i) {
@@ -983,7 +985,7 @@ public ApiFuture<long[]> batchUpdateAsync(
983985
// Register the update as an async operation that must finish before the transaction may
984986
// commit.
985987
increaseAsyncOperations();
986-
response = rpc.executeBatchDmlAsync(builder.build(), session.getOptions());
988+
response = rpc.executeBatchDmlAsync(builder.build(), getTransactionChannelHint());
987989
session.markUsed(clock.instant());
988990
} catch (Throwable t) {
989991
decreaseAsyncOperations();

google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionPoolTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,7 @@ public void testSessionNotFoundReadWriteTransaction() {
14971497
.thenReturn(ApiFutures.immediateFuture(Empty.getDefaultInstance()));
14981498
when(closedSession.newTransaction(Options.fromTransactionOptions()))
14991499
.thenReturn(closedTransactionContext);
1500-
when(closedSession.beginTransactionAsync(any(), eq(true))).thenThrow(sessionNotFound);
1500+
when(closedSession.beginTransactionAsync(any(), eq(true), any())).thenThrow(sessionNotFound);
15011501
when(closedSession.getTracer()).thenReturn(tracer);
15021502
TransactionRunnerImpl closedTransactionRunner = new TransactionRunnerImpl(closedSession);
15031503
closedTransactionRunner.setSpan(span);
@@ -1512,7 +1512,7 @@ public void testSessionNotFoundReadWriteTransaction() {
15121512
final TransactionContextImpl openTransactionContext = mock(TransactionContextImpl.class);
15131513
when(openSession.newTransaction(Options.fromTransactionOptions()))
15141514
.thenReturn(openTransactionContext);
1515-
when(openSession.beginTransactionAsync(any(), eq(true)))
1515+
when(openSession.beginTransactionAsync(any(), eq(true), any()))
15161516
.thenReturn(ApiFutures.immediateFuture(ByteString.copyFromUtf8("open-txn")));
15171517
when(openSession.getTracer()).thenReturn(tracer);
15181518
TransactionRunnerImpl openTransactionRunner = new TransactionRunnerImpl(openSession);

0 commit comments

Comments
 (0)