Skip to content

Commit 818d2fc

Browse files
committed
feat: addressed review comments
1 parent a92033c commit 818d2fc

16 files changed

+469
-877
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,8 @@ private ResultSet executeQueryInternal(
641641
* <li>Specific {@link QueryOptions} passed in for this query.
642642
* <li>Any value specified in a valid environment variable when the {@link SpannerOptions}
643643
* instance was created.
644-
* <li>The default {@link SpannerOptions#getDefaultQueryOptions()} specified for the database
645-
* where the query is executed.
644+
* <li>The default {@link SpannerOptions#getDefaultQueryOptions(DatabaseId)} ()} specified for
645+
* the database where the query is executed.
646646
* </ol>
647647
*/
648648
@VisibleForTesting

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

+9-24
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.cloud.spanner.Options.TransactionOption;
2323
import com.google.cloud.spanner.Options.UpdateOption;
2424
import com.google.spanner.v1.BatchWriteResponse;
25+
import com.google.spanner.v1.TransactionOptions.IsolationLevel;
2526

2627
/**
2728
* Interface for all the APIs that are used to read/write data into a Cloud Spanner database. An
@@ -111,10 +112,6 @@ default String getDatabaseRole() {
111112
* applied to any other requests on the transaction.
112113
* <li>{@link Options#commitStats()}: Request that the server includes commit statistics in the
113114
* {@link CommitResponse}.
114-
* <li>{@link Options#repeatableReadIsolationLevel()}: Request Repeatable Read Isolation Level
115-
* from the backend.
116-
* <li>{@link Options#serializableIsolationLevel()}: Request Serializable Isolation Level from
117-
* the backend.
118115
* </ul>
119116
*
120117
* @return a response with the timestamp at which the write was committed
@@ -190,10 +187,6 @@ CommitResponse writeWithOptions(Iterable<Mutation> mutations, TransactionOption.
190187
* applied to any other requests on the transaction.
191188
* <li>{@link Options#commitStats()}: Request that the server includes commit statistics in the
192189
* {@link CommitResponse}.
193-
* <li>{@link Options#repeatableReadIsolationLevel()}: Request Repeatable Read Isolation Level
194-
* from the backend.
195-
* <li>{@link Options#serializableIsolationLevel()}: Request Serializable Isolation Level from
196-
* the backend.
197190
* </ul>
198191
*
199192
* @return a response with the timestamp at which the write was committed
@@ -422,10 +415,8 @@ ServerStream<BatchWriteResponse> batchWriteAtLeastOnce(
422415
* applied to any other requests on the transaction.
423416
* <li>{@link Options#commitStats()}: Request that the server includes commit statistics in the
424417
* {@link CommitResponse}.
425-
* <li>{@link Options#repeatableReadIsolationLevel()}: Request Repeatable Read Isolation Level
426-
* from the backend.
427-
* <li>{@link Options#serializableIsolationLevel()}: Request Serializable Isolation Level from
428-
* the backend.
418+
* <li>{@link Options#isolationLevelOption(IsolationLevel)}: The isolation level for the
419+
* transaction
429420
* </ul>
430421
*/
431422
TransactionRunner readWriteTransaction(TransactionOption... options);
@@ -466,10 +457,8 @@ ServerStream<BatchWriteResponse> batchWriteAtLeastOnce(
466457
* applied to any other requests on the transaction.
467458
* <li>{@link Options#commitStats()}: Request that the server includes commit statistics in the
468459
* {@link CommitResponse}.
469-
* <li>{@link Options#repeatableReadIsolationLevel()}: Request Repeatable Read Isolation Level
470-
* from the backend.
471-
* <li>{@link Options#serializableIsolationLevel()}: Request Serializable Isolation Level from
472-
* the backend.
460+
* <li>{@link Options#isolationLevelOption(IsolationLevel)}: The isolation level for the
461+
* transaction
473462
* </ul>
474463
*/
475464
TransactionManager transactionManager(TransactionOption... options);
@@ -510,10 +499,8 @@ ServerStream<BatchWriteResponse> batchWriteAtLeastOnce(
510499
* applied to any other requests on the transaction.
511500
* <li>{@link Options#commitStats()}: Request that the server includes commit statistics in the
512501
* {@link CommitResponse}.
513-
* <li>{@link Options#repeatableReadIsolationLevel()}: Request Repeatable Read Isolation Level
514-
* from the backend.
515-
* <li>{@link Options#serializableIsolationLevel()}: Request Serializable Isolation Level from
516-
* the backend.
502+
* <li>{@link Options#isolationLevelOption(IsolationLevel)}: The isolation level for the
503+
* transaction
517504
* </ul>
518505
*/
519506
AsyncRunner runAsync(TransactionOption... options);
@@ -568,10 +555,8 @@ ServerStream<BatchWriteResponse> batchWriteAtLeastOnce(
568555
* applied to any other requests on the transaction.
569556
* <li>{@link Options#commitStats()}: Request that the server includes commit statistics in the
570557
* {@link CommitResponse}.
571-
* <li>{@link Options#repeatableReadIsolationLevel()}: Request Repeatable Read Isolation Level
572-
* from the backend.
573-
* <li>{@link Options#serializableIsolationLevel()}: Request Serializable Isolation Level from
574-
* the backend.
558+
* <li>{@link Options#isolationLevelOption(IsolationLevel)}: The isolation level for the
559+
* transaction
575560
* </ul>
576561
*/
577562
AsyncTransactionManager transactionManagerAsync(TransactionOption... options);

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

+12-49
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.spanner;
1818

19+
import com.google.cloud.spanner.SpannerOptions.Builder.TransactionOptions;
1920
import com.google.common.base.Preconditions;
2021
import com.google.spanner.v1.DirectedReadOptions;
2122
import com.google.spanner.v1.ReadRequest.LockHint;
@@ -26,7 +27,6 @@
2627
import java.time.Duration;
2728
import java.util.Arrays;
2829
import java.util.Objects;
29-
import java.util.function.Predicate;
3030
import java.util.stream.Stream;
3131

3232
/** Specifies options for various spanner operations */
@@ -135,29 +135,7 @@ public interface UpdateAdminApiOption extends AdminApiOption {}
135135
public interface QueryOption {}
136136

137137
/** Marker interface to mark options applicable to write operations */
138-
public interface TransactionOption {
139-
Predicate<TransactionOption> isValidIsolationLevelOption =
140-
txnOption ->
141-
txnOption instanceof RepeatableReadOption || txnOption instanceof SerializableOption;
142-
143-
/**
144-
* Combines two arrays of TransactionOption, with primaryOptions taking precedence in case of
145-
* conflicts. Note that {@link
146-
* com.google.cloud.spanner.SpannerOptions.Builder.TransactionOptions} supports only the {@link
147-
* IsolationLevel} TransactionOption, meaning spannerOptions will contain a maximum of one
148-
* TransactionOption.
149-
*/
150-
static TransactionOption[] combine(
151-
TransactionOption[] primaryOptions, TransactionOption[] spannerOptions) {
152-
if (spannerOptions == null
153-
|| Arrays.stream(primaryOptions).anyMatch(isValidIsolationLevelOption)) {
154-
return primaryOptions;
155-
} else {
156-
return Stream.concat(Arrays.stream(primaryOptions), Arrays.stream(spannerOptions))
157-
.toArray(TransactionOption[]::new);
158-
}
159-
}
160-
}
138+
public interface TransactionOption {}
161139

162140
/** Marker interface to mark options applicable to update operation. */
163141
public interface UpdateOption {}
@@ -186,19 +164,10 @@ public static TransactionOption optimisticLock() {
186164
}
187165

188166
/**
189-
* Specifying this instructs the transaction to request Repeatable Read Isolation Level from the
190-
* backend.
191-
*/
192-
public static TransactionOption repeatableReadIsolationLevel() {
193-
return REPEATABLE_READ_OPTION;
194-
}
195-
196-
/**
197-
* Specifying this instructs the transaction to request Serializable Isolation Level from the
198-
* backend.
167+
* Specifying this instructs the transaction to request {@link IsolationLevel} from the backend.
199168
*/
200-
public static TransactionOption serializableIsolationLevel() {
201-
return SERIALIZABLE_OPTION;
169+
public static IsolationLevelOption isolationLevelOption(IsolationLevel isolationLevel) {
170+
return new IsolationLevelOption(isolationLevel);
202171
}
203172

204173
/**
@@ -532,26 +501,20 @@ void appendToOptions(Options options) {
532501
}
533502
}
534503

535-
/** Option to request REPEATABLE READ isolation level for read/write transactions. */
536-
static final class RepeatableReadOption extends InternalOption implements TransactionOption {
537-
@Override
538-
void appendToOptions(Options options) {
539-
options.isolationLevel = IsolationLevel.REPEATABLE_READ;
540-
}
541-
}
504+
/** Option to set isolation level for read/write transactions. */
505+
static final class IsolationLevelOption extends InternalOption implements TransactionOption {
506+
private final IsolationLevel isolationLevel;
542507

543-
static final RepeatableReadOption REPEATABLE_READ_OPTION = new RepeatableReadOption();
508+
public IsolationLevelOption(IsolationLevel isolationLevel) {
509+
this.isolationLevel = isolationLevel;
510+
}
544511

545-
/** Option to request SERIALIZABLE isolation level for read/write transactions. */
546-
static final class SerializableOption extends InternalOption implements TransactionOption {
547512
@Override
548513
void appendToOptions(Options options) {
549-
options.isolationLevel = IsolationLevel.SERIALIZABLE;
514+
options.isolationLevel = isolationLevel;
550515
}
551516
}
552517

553-
static final SerializableOption SERIALIZABLE_OPTION = new SerializableOption();
554-
555518
private boolean withCommitStats;
556519

557520
private Duration maxCommitDelay;

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

+31-34
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.google.cloud.spanner.Options.TransactionOption;
3131
import com.google.cloud.spanner.Options.UpdateOption;
3232
import com.google.cloud.spanner.SessionClient.SessionOption;
33+
import com.google.cloud.spanner.SpannerOptions.Builder.TransactionOptions;
3334
import com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl;
3435
import com.google.cloud.spanner.spi.v1.SpannerRpc;
3536
import com.google.common.base.Ticker;
@@ -44,7 +45,6 @@
4445
import com.google.spanner.v1.CommitRequest;
4546
import com.google.spanner.v1.RequestOptions;
4647
import com.google.spanner.v1.Transaction;
47-
import com.google.spanner.v1.TransactionOptions;
4848
import java.time.Instant;
4949
import java.util.ArrayList;
5050
import java.util.Collection;
@@ -69,15 +69,18 @@ static void throwIfTransactionsPending() {
6969
}
7070
}
7171

72-
static TransactionOptions createReadWriteTransactionOptions(
72+
static com.google.spanner.v1.TransactionOptions createReadWriteTransactionOptions(
7373
Options options, ByteString previousTransactionId) {
74-
TransactionOptions.Builder transactionOptions = TransactionOptions.newBuilder();
74+
com.google.spanner.v1.TransactionOptions.Builder transactionOptions =
75+
com.google.spanner.v1.TransactionOptions.newBuilder();
7576
if (options.withExcludeTxnFromChangeStreams() == Boolean.TRUE) {
7677
transactionOptions.setExcludeTxnFromChangeStreams(true);
7778
}
78-
TransactionOptions.ReadWrite.Builder readWrite = TransactionOptions.ReadWrite.newBuilder();
79+
com.google.spanner.v1.TransactionOptions.ReadWrite.Builder readWrite =
80+
com.google.spanner.v1.TransactionOptions.ReadWrite.newBuilder();
7981
if (options.withOptimisticLock() == Boolean.TRUE) {
80-
readWrite.setReadLockMode(TransactionOptions.ReadWrite.ReadLockMode.OPTIMISTIC);
82+
readWrite.setReadLockMode(
83+
com.google.spanner.v1.TransactionOptions.ReadWrite.ReadLockMode.OPTIMISTIC);
8184
}
8285
if (previousTransactionId != null
8386
&& previousTransactionId != com.google.protobuf.ByteString.EMPTY) {
@@ -196,6 +199,14 @@ void markUsed(Instant instant) {
196199
sessionReference.markUsed(instant);
197200
}
198201

202+
com.google.spanner.v1.TransactionOptions defaultTransactionOptions() {
203+
TransactionOptions transactionOptions =
204+
this.spanner.getOptions().getDefaultTransactionOptions();
205+
return transactionOptions != null
206+
? transactionOptions.getTransactionOptions()
207+
: com.google.spanner.v1.TransactionOptions.getDefaultInstance();
208+
}
209+
199210
public DatabaseId getDatabaseId() {
200211
return sessionReference.getDatabaseId();
201212
}
@@ -242,26 +253,24 @@ public CommitResponse writeAtLeastOnceWithOptions(
242253
setActive(null);
243254
List<com.google.spanner.v1.Mutation> mutationsProto = new ArrayList<>();
244255
Mutation.toProtoAndReturnRandomMutation(mutations, mutationsProto);
245-
Options options =
246-
Options.fromTransactionOptions(
247-
TransactionOption.combine(
248-
transactionOptions, this.spanner.getOptions().getTransactionOptions()));
256+
Options options = Options.fromTransactionOptions(transactionOptions);
249257
final CommitRequest.Builder requestBuilder =
250258
CommitRequest.newBuilder()
251259
.setSession(getName())
252260
.setReturnCommitStats(options.withCommitStats())
253261
.addAllMutations(mutationsProto);
254262

255-
TransactionOptions.Builder transactionOptionsBuilder =
256-
TransactionOptions.newBuilder()
257-
.setReadWrite(TransactionOptions.ReadWrite.getDefaultInstance());
263+
com.google.spanner.v1.TransactionOptions.Builder transactionOptionsBuilder =
264+
com.google.spanner.v1.TransactionOptions.newBuilder()
265+
.setReadWrite(com.google.spanner.v1.TransactionOptions.ReadWrite.getDefaultInstance());
258266
if (options.withExcludeTxnFromChangeStreams() == Boolean.TRUE) {
259267
transactionOptionsBuilder.setExcludeTxnFromChangeStreams(true);
260268
}
261269
if (options.isolationLevel() != null) {
262270
transactionOptionsBuilder.setIsolationLevel(options.isolationLevel());
263271
}
264-
requestBuilder.setSingleUseTransaction(transactionOptionsBuilder);
272+
requestBuilder.setSingleUseTransaction(
273+
this.defaultTransactionOptions().toBuilder().mergeFrom(transactionOptionsBuilder.build()));
265274

266275
if (options.hasMaxCommitDelay()) {
267276
requestBuilder.setMaxCommitDelay(
@@ -405,37 +414,22 @@ public ReadOnlyTransaction readOnlyTransaction(TimestampBound bound) {
405414

406415
@Override
407416
public TransactionRunner readWriteTransaction(TransactionOption... options) {
408-
return setActive(
409-
new TransactionRunnerImpl(
410-
this,
411-
TransactionOption.combine(options, this.spanner.getOptions().getTransactionOptions())));
417+
return setActive(new TransactionRunnerImpl(this, options));
412418
}
413419

414420
@Override
415421
public AsyncRunner runAsync(TransactionOption... options) {
416-
return new AsyncRunnerImpl(
417-
setActive(
418-
new TransactionRunnerImpl(
419-
this,
420-
TransactionOption.combine(
421-
options, this.spanner.getOptions().getTransactionOptions()))));
422+
return new AsyncRunnerImpl(setActive(new TransactionRunnerImpl(this, options)));
422423
}
423424

424425
@Override
425426
public TransactionManager transactionManager(TransactionOption... options) {
426-
return new TransactionManagerImpl(
427-
this,
428-
currentSpan,
429-
tracer,
430-
TransactionOption.combine(options, this.spanner.getOptions().getTransactionOptions()));
427+
return new TransactionManagerImpl(this, currentSpan, tracer, options);
431428
}
432429

433430
@Override
434431
public AsyncTransactionManagerImpl transactionManagerAsync(TransactionOption... options) {
435-
return new AsyncTransactionManagerImpl(
436-
this,
437-
currentSpan,
438-
TransactionOption.combine(options, this.spanner.getOptions().getTransactionOptions()));
432+
return new AsyncTransactionManagerImpl(this, currentSpan, options);
439433
}
440434

441435
@Override
@@ -468,7 +462,11 @@ ApiFuture<Transaction> beginTransactionAsync(
468462
BeginTransactionRequest.newBuilder()
469463
.setSession(getName())
470464
.setOptions(
471-
createReadWriteTransactionOptions(transactionOptions, previousTransactionId));
465+
defaultTransactionOptions()
466+
.toBuilder()
467+
.mergeFrom(
468+
createReadWriteTransactionOptions(
469+
transactionOptions, previousTransactionId)));
472470
if (sessionReference.getIsMultiplexed() && mutation != null) {
473471
requestBuilder.setMutationKey(mutation);
474472
}
@@ -513,7 +511,6 @@ TransactionContextImpl newTransaction(Options options, ByteString previousTransa
513511
.setOptions(options)
514512
.setTransactionId(null)
515513
.setPreviousTransactionId(previousTransactionId)
516-
.setOptions(options)
517514
.setTrackTransactionStarter(spanner.getOptions().isTrackTransactionStarter())
518515
.setRpc(spanner.getRpc())
519516
.setDefaultQueryOptions(spanner.getDefaultQueryOptions(getDatabaseId()))

0 commit comments

Comments
 (0)