-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(spanner): add R/W multiplexed session support (#3381)
This PR introduces support for using multiplexed sessions in R/W transactions for the following methods: 1. write 2. writeWithOptions 3. readWriteTransaction 4. transactionManager 5. runAsync 6. transactionManagerAsync
- Loading branch information
1 parent
4f73bdb
commit 1f53d38
Showing
17 changed files
with
768 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
google-cloud-spanner/src/main/java/com/google/cloud/spanner/DelayedAsyncRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.spanner; | ||
|
||
import com.google.api.core.ApiFuture; | ||
import com.google.api.core.ApiFutures; | ||
import com.google.cloud.Timestamp; | ||
import com.google.common.util.concurrent.MoreExecutors; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Executor; | ||
|
||
/** | ||
* Represents a {@link AsyncRunner} using a multiplexed session that is not yet ready. The execution | ||
* will be delayed until the multiplexed session has been created and is ready. This class is only | ||
* used during the startup of the client and the multiplexed session has not yet been created. | ||
*/ | ||
public class DelayedAsyncRunner implements AsyncRunner { | ||
|
||
private final ApiFuture<AsyncRunner> asyncRunnerFuture; | ||
|
||
public DelayedAsyncRunner(ApiFuture<AsyncRunner> asyncRunnerFuture) { | ||
this.asyncRunnerFuture = asyncRunnerFuture; | ||
} | ||
|
||
ApiFuture<AsyncRunner> getAsyncRunner() { | ||
return ApiFutures.catchingAsync( | ||
asyncRunnerFuture, | ||
Exception.class, | ||
exception -> { | ||
if (exception instanceof InterruptedException) { | ||
throw SpannerExceptionFactory.propagateInterrupt((InterruptedException) exception); | ||
} | ||
if (exception instanceof ExecutionException) { | ||
throw SpannerExceptionFactory.causeAsRunTimeException((ExecutionException) exception); | ||
} | ||
throw exception; | ||
}, | ||
MoreExecutors.directExecutor()); | ||
} | ||
|
||
@Override | ||
public <R> ApiFuture<R> runAsync(AsyncWork<R> work, Executor executor) { | ||
return ApiFutures.transformAsync( | ||
getAsyncRunner(), | ||
asyncRunner -> asyncRunner.runAsync(work, executor), | ||
MoreExecutors.directExecutor()); | ||
} | ||
|
||
@Override | ||
public ApiFuture<Timestamp> getCommitTimestamp() { | ||
return ApiFutures.transformAsync( | ||
getAsyncRunner(), AsyncRunner::getCommitTimestamp, MoreExecutors.directExecutor()); | ||
} | ||
|
||
@Override | ||
public ApiFuture<CommitResponse> getCommitResponse() { | ||
return ApiFutures.transformAsync( | ||
getAsyncRunner(), AsyncRunner::getCommitResponse, MoreExecutors.directExecutor()); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...-cloud-spanner/src/main/java/com/google/cloud/spanner/DelayedAsyncTransactionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.spanner; | ||
|
||
import com.google.api.core.ApiFuture; | ||
import com.google.cloud.spanner.TransactionManager.TransactionState; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
/** | ||
* Represents a {@link AsyncTransactionManager} using a multiplexed session that is not yet ready. | ||
* The execution will be delayed until the multiplexed session has been created and is ready. This | ||
* class is only used during the startup of the client and the multiplexed session has not yet been | ||
* created. | ||
*/ | ||
public class DelayedAsyncTransactionManager implements AsyncTransactionManager { | ||
|
||
private final ApiFuture<AsyncTransactionManager> asyncTransactionManagerApiFuture; | ||
|
||
DelayedAsyncTransactionManager( | ||
ApiFuture<AsyncTransactionManager> asyncTransactionManagerApiFuture) { | ||
this.asyncTransactionManagerApiFuture = asyncTransactionManagerApiFuture; | ||
} | ||
|
||
AsyncTransactionManager getAsyncTransactionManager() { | ||
try { | ||
return this.asyncTransactionManagerApiFuture.get(); | ||
} catch (ExecutionException executionException) { | ||
throw SpannerExceptionFactory.causeAsRunTimeException(executionException); | ||
} catch (InterruptedException interruptedException) { | ||
throw SpannerExceptionFactory.propagateInterrupt(interruptedException); | ||
} | ||
} | ||
|
||
@Override | ||
public TransactionContextFuture beginAsync() { | ||
return getAsyncTransactionManager().beginAsync(); | ||
} | ||
|
||
@Override | ||
public ApiFuture<Void> rollbackAsync() { | ||
return getAsyncTransactionManager().rollbackAsync(); | ||
} | ||
|
||
@Override | ||
public TransactionContextFuture resetForRetryAsync() { | ||
return getAsyncTransactionManager().resetForRetryAsync(); | ||
} | ||
|
||
@Override | ||
public TransactionState getState() { | ||
return getAsyncTransactionManager().getState(); | ||
} | ||
|
||
@Override | ||
public ApiFuture<CommitResponse> getCommitResponse() { | ||
return getAsyncTransactionManager().getCommitResponse(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
getAsyncTransactionManager().close(); | ||
} | ||
|
||
@Override | ||
public ApiFuture<Void> closeAsync() { | ||
return getAsyncTransactionManager().closeAsync(); | ||
} | ||
} |
Oops, something went wrong.