Skip to content

Commit c1c125b

Browse files
chore(spanner): add package protected setter to enable mux for RW (#3327)
* chore(spanner): add package protected setter to enable mux for RW * Update google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPoolOptions.java Co-authored-by: Knut Olav Løite <[email protected]> --------- Co-authored-by: Knut Olav Løite <[email protected]>
1 parent afda386 commit c1c125b

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public class SessionPoolOptions {
7373

7474
private final boolean useMultiplexedSession;
7575

76+
private final boolean useMultiplexedSessionForRW;
77+
7678
// TODO: Change to use java.time.Duration.
7779
private final Duration multiplexedSessionMaintenanceDuration;
7880

@@ -108,6 +110,13 @@ private SessionPoolOptions(Builder builder) {
108110
(useMultiplexedSessionFromEnvVariable != null)
109111
? useMultiplexedSessionFromEnvVariable
110112
: builder.useMultiplexedSession;
113+
// useMultiplexedSessionForRW priority => Environment var > private setter > client default
114+
Boolean useMultiplexedSessionForRWFromEnvVariable =
115+
getUseMultiplexedSessionForRWFromEnvVariable();
116+
this.useMultiplexedSessionForRW =
117+
(useMultiplexedSessionForRWFromEnvVariable != null)
118+
? useMultiplexedSessionForRWFromEnvVariable
119+
: builder.useMultiplexedSessionForRW;
111120
this.multiplexedSessionMaintenanceDuration = builder.multiplexedSessionMaintenanceDuration;
112121
}
113122

@@ -144,6 +153,7 @@ public boolean equals(Object o) {
144153
this.inactiveTransactionRemovalOptions, other.inactiveTransactionRemovalOptions)
145154
&& Objects.equals(this.poolMaintainerClock, other.poolMaintainerClock)
146155
&& Objects.equals(this.useMultiplexedSession, other.useMultiplexedSession)
156+
&& Objects.equals(this.useMultiplexedSessionForRW, other.useMultiplexedSessionForRW)
147157
&& Objects.equals(
148158
this.multiplexedSessionMaintenanceDuration,
149159
other.multiplexedSessionMaintenanceDuration);
@@ -174,6 +184,7 @@ public int hashCode() {
174184
this.inactiveTransactionRemovalOptions,
175185
this.poolMaintainerClock,
176186
this.useMultiplexedSession,
187+
this.useMultiplexedSessionForRW,
177188
this.multiplexedSessionMaintenanceDuration);
178189
}
179190

@@ -307,6 +318,14 @@ public boolean getUseMultiplexedSession() {
307318
return useMultiplexedSession;
308319
}
309320

321+
@VisibleForTesting
322+
@InternalApi
323+
public boolean getUseMultiplexedSessionForRW() {
324+
// Multiplexed sessions for R/W are enabled only if both global multiplexed sessions and
325+
// read-write multiplexed session flags are set to true.
326+
return getUseMultiplexedSession() && useMultiplexedSessionForRW;
327+
}
328+
310329
private static Boolean getUseMultiplexedSessionFromEnvVariable() {
311330
String useMultiplexedSessionFromEnvVariable =
312331
System.getenv("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS");
@@ -323,6 +342,12 @@ private static Boolean getUseMultiplexedSessionFromEnvVariable() {
323342
return null;
324343
}
325344

345+
private static Boolean getUseMultiplexedSessionForRWFromEnvVariable() {
346+
// Checks the value of env, GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_FOR_RW
347+
// This returns null until RW is supported.
348+
return null;
349+
}
350+
326351
Duration getMultiplexedSessionMaintenanceDuration() {
327352
return multiplexedSessionMaintenanceDuration;
328353
}
@@ -529,6 +554,12 @@ public static class Builder {
529554
// Set useMultiplexedSession to true to make multiplexed session the default.
530555
private boolean useMultiplexedSession = false;
531556

557+
// This field controls the default behavior of session management for RW operations in Java
558+
// client.
559+
// Set useMultiplexedSessionForRW to true to make multiplexed session for RW operations the
560+
// default.
561+
private boolean useMultiplexedSessionForRW = false;
562+
532563
private Duration multiplexedSessionMaintenanceDuration = Duration.ofDays(7);
533564
private Clock poolMaintainerClock = Clock.INSTANCE;
534565

@@ -570,6 +601,7 @@ private Builder(SessionPoolOptions options) {
570601
this.randomizePositionQPSThreshold = options.randomizePositionQPSThreshold;
571602
this.inactiveTransactionRemovalOptions = options.inactiveTransactionRemovalOptions;
572603
this.useMultiplexedSession = options.useMultiplexedSession;
604+
this.useMultiplexedSessionForRW = options.useMultiplexedSessionForRW;
573605
this.multiplexedSessionMaintenanceDuration = options.multiplexedSessionMaintenanceDuration;
574606
this.poolMaintainerClock = options.poolMaintainerClock;
575607
}
@@ -757,6 +789,15 @@ Builder setUseMultiplexedSession(boolean useMultiplexedSession) {
757789
return this;
758790
}
759791

792+
/**
793+
* Sets whether the client should use multiplexed session for R/W operations or not. This method
794+
* is intentionally package-private and intended for internal use.
795+
*/
796+
Builder setUseMultiplexedSessionForRW(boolean useMultiplexedSessionForRW) {
797+
this.useMultiplexedSessionForRW = useMultiplexedSessionForRW;
798+
return this;
799+
}
800+
760801
@VisibleForTesting
761802
Builder setMultiplexedSessionMaintenanceDuration(
762803
Duration multiplexedSessionMaintenanceDuration) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ public DatabaseClient getDatabaseClient(DatabaseId db) {
277277

278278
boolean useMultiplexedSession =
279279
getOptions().getSessionPoolOptions().getUseMultiplexedSession();
280+
boolean useMultiplexedSessionForRW =
281+
getOptions().getSessionPoolOptions().getUseMultiplexedSessionForRW();
282+
280283
MultiplexedSessionDatabaseClient multiplexedSessionDatabaseClient =
281284
useMultiplexedSession
282285
? new MultiplexedSessionDatabaseClient(SpannerImpl.this.getSessionClient(db))

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,52 @@ public void testUseMultiplexedSession() {
299299
.getUseMultiplexedSession());
300300
}
301301

302+
@Test
303+
public void testUseMultiplexedSessionForRW() {
304+
// skip these tests since this configuration can have dual behaviour in different test-runners
305+
assumeFalse(SessionPoolOptions.newBuilder().build().getUseMultiplexedSession());
306+
assumeFalse(SessionPoolOptions.newBuilder().build().getUseMultiplexedSessionForRW());
307+
308+
// Verify default client behavior for multiplexed sessions in R/W transactions
309+
assertEquals(false, SessionPoolOptions.newBuilder().build().getUseMultiplexedSessionForRW());
310+
311+
// Client will use multiplexed sessions for R/W transactions if both the fields are set to true.
312+
assertEquals(
313+
true,
314+
SessionPoolOptions.newBuilder()
315+
.setUseMultiplexedSession(true)
316+
.setUseMultiplexedSessionForRW(true)
317+
.build()
318+
.getUseMultiplexedSessionForRW());
319+
// Client will not use multiplexed sessions for R/W transactions, since one of the field is set
320+
// to false.
321+
assertEquals(
322+
false,
323+
SessionPoolOptions.newBuilder()
324+
.setUseMultiplexedSession(true)
325+
.setUseMultiplexedSessionForRW(false)
326+
.build()
327+
.getUseMultiplexedSessionForRW());
328+
// Client will not use multiplexed sessions for R/W transactions, since one of the field is set
329+
// to false.
330+
assertEquals(
331+
false,
332+
SessionPoolOptions.newBuilder()
333+
.setUseMultiplexedSession(false)
334+
.setUseMultiplexedSessionForRW(true)
335+
.build()
336+
.getUseMultiplexedSessionForRW());
337+
// Client will not use multiplexed sessions for R/W transactions, since both the fields are set
338+
// to false.
339+
assertEquals(
340+
false,
341+
SessionPoolOptions.newBuilder()
342+
.setUseMultiplexedSession(false)
343+
.setUseMultiplexedSessionForRW(false)
344+
.build()
345+
.getUseMultiplexedSessionForRW());
346+
}
347+
302348
@Test
303349
public void testMultiplexedSessionMaintenanceDuration() {
304350
assertEquals(
@@ -326,6 +372,10 @@ public void testToBuilder() {
326372
SessionPoolOptions.newBuilder()
327373
.setUseMultiplexedSession(ThreadLocalRandom.current().nextBoolean())
328374
.build());
375+
assertToBuilderRoundtrip(
376+
SessionPoolOptions.newBuilder()
377+
.setUseMultiplexedSessionForRW(ThreadLocalRandom.current().nextBoolean())
378+
.build());
329379
assertToBuilderRoundtrip(
330380
SessionPoolOptions.newBuilder()
331381
.setMinSessions(ThreadLocalRandom.current().nextInt(400))

0 commit comments

Comments
 (0)