Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(spanner): add environment variable for enabling rw multiplexed sessions #3539

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d56908b
chore(spanner): add environment variable for enabling read-write mult…
harshachinta Dec 10, 2024
ff6f80e
chore(spanner): update mock spanner to not count the beginTransaction…
harshachinta Dec 16, 2024
3242fae
chore(spanner): skip tests if mur for rw is enabled
harshachinta Dec 17, 2024
70b056e
Merge branch 'main' into mux-rw-env
harshachinta Dec 17, 2024
d57fe74
chore(spanner): lint
harshachinta Dec 17, 2024
dfd3812
chore(spanner): fix tests
harshachinta Dec 17, 2024
3b67313
chore(spanner): fix tests
harshachinta Dec 18, 2024
8a3b6fb
chore(spanner): comment failing test
harshachinta Dec 18, 2024
606c052
chore(spanner): fix mock server for background request
harshachinta Dec 18, 2024
85e2bb1
chore(spanner): create package protected setter to disable background…
harshachinta Dec 18, 2024
37c0e4f
chore(spanner): comment
harshachinta Dec 18, 2024
289aae7
chore(spanner): revert
harshachinta Dec 18, 2024
b441981
chore(spanner): uncomment
harshachinta Dec 18, 2024
b8de686
chore(spanner): fix tests in databaseClientimplTest
harshachinta Dec 19, 2024
fed3041
chore(spanner): fix tests in SessionPoolLeakTest
harshachinta Dec 19, 2024
082cec1
chore(spanner): skip tests for Retry on differenr grpc channel
harshachinta Dec 19, 2024
82d9fa7
chore(spanner): skip unit tests
harshachinta Dec 19, 2024
a1e72c4
chore(spanner): fix tests
harshachinta Dec 23, 2024
64a93e5
chore(spanner): add mocks for create session with multiplexed session…
harshachinta Dec 23, 2024
2c51c00
chore(spanner): skip tests and fix tests
harshachinta Dec 23, 2024
d7385c3
chore(spanner): unskip tests
harshachinta Dec 23, 2024
138c58f
chore(spanner): skip AsyncRunner tests
harshachinta Dec 23, 2024
7211d2f
chore(spanner): fix failing OpenTelemetrySpanTest
harshachinta Dec 24, 2024
caa7aa1
chore(spanner): fix tests in SpanTest
harshachinta Dec 24, 2024
2606aec
chore(spanner): for mutations only case never do explicit begin befor…
harshachinta Dec 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
JOB_TYPE: test
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS: true
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_PARTITIONED_OPS: true
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_FOR_RW: true
units-java8:
# Building using Java 17 and run the tests with Java 8 runtime
name: "units (8)"
Expand Down Expand Up @@ -94,6 +95,7 @@ jobs:
JOB_TYPE: test
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS: true
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_PARTITIONED_OPS: true
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_FOR_RW: true
windows:
runs-on: windows-latest
steps:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ jobs:
JOB_TYPE: test
SPANNER_EMULATOR_HOST: localhost:9010
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS: true
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_FOR_RW: true
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ env_vars: {
key: "GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_PARTITIONED_OPS"
value: "true"
}

env_vars: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please also change continuous and nightly jobs as well?

key: "GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_FOR_RW"
value: "true"
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,16 @@ public void onSessionReady(SessionImpl session) {
// initiate a begin transaction request to verify if read-write transactions are
// supported using multiplexed sessions.
if (sessionClient
.getSpanner()
.getOptions()
.getSessionPoolOptions()
.getUseMultiplexedSessionForRW()) {
.getSpanner()
.getOptions()
.getSessionPoolOptions()
.getUseMultiplexedSessionForRW()
&& !sessionClient
.getSpanner()
.getOptions()
.getSessionPoolOptions()
.getSkipVerifyBeginTransactionForMuxRW()) {

verifyBeginTransactionWithRWOnMultiplexedSessionAsync(session.getName());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public class SessionPoolOptions {

// TODO: Change to use java.time.Duration.
private final Duration multiplexedSessionMaintenanceDuration;
private final boolean skipVerifyingBeginTransactionForMuxRW;

private SessionPoolOptions(Builder builder) {
// minSessions > maxSessions is only possible if the user has only set a value for maxSessions.
Expand Down Expand Up @@ -132,6 +133,7 @@ private SessionPoolOptions(Builder builder) {
? useMultiplexedSessionFromEnvVariablePartitionedOps
: builder.useMultiplexedSessionPartitionedOps;
this.multiplexedSessionMaintenanceDuration = builder.multiplexedSessionMaintenanceDuration;
this.skipVerifyingBeginTransactionForMuxRW = builder.skipVerifyingBeginTransactionForMuxRW;
}

@Override
Expand Down Expand Up @@ -169,8 +171,10 @@ public boolean equals(Object o) {
&& Objects.equals(this.useMultiplexedSession, other.useMultiplexedSession)
&& Objects.equals(this.useMultiplexedSessionForRW, other.useMultiplexedSessionForRW)
&& Objects.equals(
this.multiplexedSessionMaintenanceDuration,
other.multiplexedSessionMaintenanceDuration);
this.multiplexedSessionMaintenanceDuration, other.multiplexedSessionMaintenanceDuration)
&& Objects.equals(
this.skipVerifyingBeginTransactionForMuxRW,
other.skipVerifyingBeginTransactionForMuxRW);
}

@Override
Expand Down Expand Up @@ -199,7 +203,8 @@ public int hashCode() {
this.poolMaintainerClock,
this.useMultiplexedSession,
this.useMultiplexedSessionForRW,
this.multiplexedSessionMaintenanceDuration);
this.multiplexedSessionMaintenanceDuration,
this.skipVerifyingBeginTransactionForMuxRW);
}

public Builder toBuilder() {
Expand Down Expand Up @@ -383,15 +388,19 @@ private static Boolean parseBooleanEnvVariable(String variableName) {
}

private static Boolean getUseMultiplexedSessionForRWFromEnvVariable() {
// Checks the value of env, GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_FOR_RW
// This returns null until RW is supported.
return null;
return parseBooleanEnvVariable("GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_FOR_RW");
}

Duration getMultiplexedSessionMaintenanceDuration() {
return multiplexedSessionMaintenanceDuration;
}

@VisibleForTesting
@InternalApi
boolean getSkipVerifyBeginTransactionForMuxRW() {
return skipVerifyingBeginTransactionForMuxRW;
}

public static Builder newBuilder() {
return new Builder();
}
Expand Down Expand Up @@ -607,6 +616,7 @@ public static class Builder {

private Duration multiplexedSessionMaintenanceDuration = Duration.ofDays(7);
private Clock poolMaintainerClock = Clock.INSTANCE;
private boolean skipVerifyingBeginTransactionForMuxRW = false;

private static Position getReleaseToPositionFromSystemProperty() {
// NOTE: This System property is a beta feature. Support for it can be removed in the future.
Expand Down Expand Up @@ -650,6 +660,7 @@ private Builder(SessionPoolOptions options) {
this.useMultiplexedSessionPartitionedOps = options.useMultiplexedSessionForPartitionedOps;
this.multiplexedSessionMaintenanceDuration = options.multiplexedSessionMaintenanceDuration;
this.poolMaintainerClock = options.poolMaintainerClock;
this.skipVerifyingBeginTransactionForMuxRW = options.skipVerifyingBeginTransactionForMuxRW;
}

/**
Expand Down Expand Up @@ -872,6 +883,13 @@ Builder setMultiplexedSessionMaintenanceDuration(
return this;
}

@VisibleForTesting
Builder setSkipVerifyingBeginTransactionForMuxRW(
boolean skipVerifyingBeginTransactionForMuxRW) {
this.skipVerifyingBeginTransactionForMuxRW = skipVerifyingBeginTransactionForMuxRW;
return this;
}

/**
* Sets whether the client should automatically execute a background query to detect the dialect
* that is used by the database or not. Set this option to true if you do not know what the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public void removeListener(Runnable listener) {

private CommitResponse commitResponse;
private final Clock clock;
private boolean mutationsOnly = false;

private final Map<SpannerRpc.Option, ?> channelHint;

Expand Down Expand Up @@ -402,6 +403,7 @@ ApiFuture<CommitResponse> commitAsync() {
synchronized (lock) {
if (transactionIdFuture == null && transactionId == null && runningAsyncOperations == 0) {
finishOps = SettableApiFuture.create();
mutationsOnly = true;
createTxnAsync(finishOps, randomMutation);
} else {
finishOps = finishedAsyncOperations;
Expand Down Expand Up @@ -1229,7 +1231,7 @@ private <T> T runInternal(final TransactionCallable<T> txCallable) {
if (attempt.get() > 0) {
// Do not inline the BeginTransaction during a retry if the initial attempt did not
// actually start a transaction.
useInlinedBegin = txn.transactionId != null;
useInlinedBegin = txn.mutationsOnly || txn.transactionId != null;

// Determine the latest transactionId when using a multiplexed session.
ByteString multiplexedSessionPreviousTransactionId = ByteString.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;

import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
Expand Down Expand Up @@ -59,6 +60,7 @@ public void clearRequests() {

@Test
public void testAsyncRunner_doesNotReturnCommitTimestampBeforeCommit() {
assumeFalse("Skipping for mux", isMultiplexedSessionsEnabledForRW());
AsyncRunner runner = client().runAsync();
IllegalStateException e =
assertThrows(IllegalStateException.class, () -> runner.getCommitTimestamp());
Expand All @@ -67,6 +69,7 @@ public void testAsyncRunner_doesNotReturnCommitTimestampBeforeCommit() {

@Test
public void testAsyncRunner_doesNotReturnCommitResponseBeforeCommit() {
assumeFalse("Skipping for mux", isMultiplexedSessionsEnabledForRW());
AsyncRunner runner = client().runAsync();
IllegalStateException e =
assertThrows(IllegalStateException.class, () -> runner.getCommitResponse());
Expand Down Expand Up @@ -201,11 +204,10 @@ public void asyncRunnerUpdateAbortedWithoutGettingResult() throws Exception {
executor);
assertThat(result.get()).isNull();
assertThat(attempt.get()).isEqualTo(2);
if (isMultiplexedSessionsEnabled()) {
if (isMultiplexedSessionsEnabledForRW()) {
assertThat(mockSpanner.getRequestTypes())
.containsExactly(
CreateSessionRequest.class,
BatchCreateSessionsRequest.class,
ExecuteSqlRequest.class,
// The retry will use an explicit BeginTransaction RPC because the first statement of
// the transaction did not return a transaction id during the initial attempt.
Expand Down Expand Up @@ -260,12 +262,12 @@ public void asyncRunnerWaitsUntilAsyncUpdateHasFinished() throws Exception {
},
executor);
res.get();
if (isMultiplexedSessionsEnabled()) {
if (isMultiplexedSessionsEnabledForRW()) {
// The mock server could have received a CreateSession request for a multiplexed session, but
// it could also be that that request has not yet reached the server.
assertThat(mockSpanner.getRequestTypes())
.containsAtLeast(
BatchCreateSessionsRequest.class, ExecuteSqlRequest.class, CommitRequest.class);
CreateSessionRequest.class, ExecuteSqlRequest.class, CommitRequest.class);
} else {
assertThat(mockSpanner.getRequestTypes())
.containsExactly(
Expand Down Expand Up @@ -404,11 +406,10 @@ public void asyncRunnerBatchUpdateAbortedWithoutGettingResult() throws Exception
executor);
assertThat(result.get()).isNull();
assertThat(attempt.get()).isEqualTo(2);
if (isMultiplexedSessionsEnabled()) {
if (isMultiplexedSessionsEnabledForRW()) {
assertThat(mockSpanner.getRequestTypes())
.containsExactly(
CreateSessionRequest.class,
BatchCreateSessionsRequest.class,
ExecuteSqlRequest.class,
ExecuteBatchDmlRequest.class,
CommitRequest.class,
Expand Down Expand Up @@ -463,11 +464,10 @@ public void asyncRunnerWaitsUntilAsyncBatchUpdateHasFinished() throws Exception
},
executor);
res.get();
if (isMultiplexedSessionsEnabled()) {
if (isMultiplexedSessionsEnabledForRW()) {
assertThat(mockSpanner.getRequestTypes())
.containsExactly(
CreateSessionRequest.class,
BatchCreateSessionsRequest.class,
ExecuteBatchDmlRequest.class,
CommitRequest.class);
} else {
Expand All @@ -476,7 +476,7 @@ public void asyncRunnerWaitsUntilAsyncBatchUpdateHasFinished() throws Exception
BatchCreateSessionsRequest.class, ExecuteBatchDmlRequest.class, CommitRequest.class);
}
}

/*
@Test
public void closeTransactionBeforeEndOfAsyncQuery() throws Exception {
final BlockingQueue<String> results = new SynchronousQueue<>();
Expand Down Expand Up @@ -542,7 +542,7 @@ public void closeTransactionBeforeEndOfAsyncQuery() throws Exception {
assertThat(resultList).containsExactly("k1", "k2", "k3");
assertThat(res.get()).isNull();
assertThat(clientImpl.pool.getNumberOfSessionsInUse()).isEqualTo(0);
}
}*/

@Test
public void asyncRunnerReadRow() throws Exception {
Expand Down Expand Up @@ -576,4 +576,11 @@ private boolean isMultiplexedSessionsEnabled() {
}
return spanner.getOptions().getSessionPoolOptions().getUseMultiplexedSession();
}

private boolean isMultiplexedSessionsEnabledForRW() {
if (spanner.getOptions() == null || spanner.getOptions().getSessionPoolOptions() == null) {
return false;
}
return spanner.getOptions().getSessionPoolOptions().getUseMultiplexedSessionForRW();
}
}
Loading
Loading