Skip to content

Commit a463917

Browse files
committed
chore(spanner): add delayed transaction support for mux R/W
1 parent 4bf1c98 commit a463917

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.spanner;
18+
19+
import com.google.api.core.ApiFuture;
20+
import com.google.cloud.Timestamp;
21+
import java.util.concurrent.ExecutionException;
22+
23+
class DelayedTransactionManager implements TransactionManager {
24+
25+
private final ApiFuture<TransactionManager> transactionManagerFuture;
26+
27+
DelayedTransactionManager(ApiFuture<TransactionManager> transactionManagerFuture) {
28+
this.transactionManagerFuture = transactionManagerFuture;
29+
}
30+
31+
TransactionManager getTransactionManager() {
32+
try {
33+
return this.transactionManagerFuture.get();
34+
} catch (ExecutionException executionException) {
35+
// Propagate the underlying exception as a RuntimeException (SpannerException is also a
36+
// RuntimeException).
37+
if (executionException.getCause() instanceof RuntimeException) {
38+
throw (RuntimeException) executionException.getCause();
39+
}
40+
throw SpannerExceptionFactory.asSpannerException(executionException.getCause());
41+
} catch (InterruptedException interruptedException) {
42+
throw SpannerExceptionFactory.propagateInterrupt(interruptedException);
43+
}
44+
}
45+
46+
@Override
47+
public TransactionContext begin() {
48+
return getTransactionManager().begin();
49+
}
50+
51+
@Override
52+
public void commit() {
53+
getTransactionManager().commit();
54+
}
55+
56+
@Override
57+
public void rollback() {
58+
getTransactionManager().rollback();
59+
}
60+
61+
@Override
62+
public TransactionContext resetForRetry() {
63+
return getTransactionManager().resetForRetry();
64+
}
65+
66+
@Override
67+
public Timestamp getCommitTimestamp() {
68+
return getTransactionManager().getCommitTimestamp();
69+
}
70+
71+
@Override
72+
public CommitResponse getCommitResponse() {
73+
return getTransactionManager().getCommitResponse();
74+
}
75+
76+
@Override
77+
public TransactionState getState() {
78+
return getTransactionManager().getState();
79+
}
80+
81+
@Override
82+
public void close() {
83+
getTransactionManager().close();
84+
}
85+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.spanner;
18+
19+
import com.google.api.core.ApiFuture;
20+
import com.google.cloud.Timestamp;
21+
import java.util.concurrent.ExecutionException;
22+
import javax.annotation.Nullable;
23+
24+
/**
25+
* Represents a {@link TransactionRunner} using a multiplexed session that is not yet ready. The
26+
* execution will be delayed until the multiplexed session has been created and is ready. This class
27+
* is only used during the startup of the client and the multiplexed session has not yet been
28+
* created.
29+
*/
30+
class DelayedTransactionRunner implements TransactionRunner {
31+
private final ApiFuture<TransactionRunner> transactionRunnerFuture;
32+
33+
DelayedTransactionRunner(ApiFuture<TransactionRunner> transactionRunnerFuture) {
34+
this.transactionRunnerFuture = transactionRunnerFuture;
35+
}
36+
37+
TransactionRunner getTransactionRunner() {
38+
try {
39+
return this.transactionRunnerFuture.get();
40+
} catch (ExecutionException executionException) {
41+
// Propagate the underlying exception as a RuntimeException (SpannerException is also a
42+
// RuntimeException).
43+
if (executionException.getCause() instanceof RuntimeException) {
44+
throw (RuntimeException) executionException.getCause();
45+
}
46+
throw SpannerExceptionFactory.asSpannerException(executionException.getCause());
47+
} catch (InterruptedException interruptedException) {
48+
throw SpannerExceptionFactory.propagateInterrupt(interruptedException);
49+
}
50+
}
51+
52+
@Nullable
53+
@Override
54+
public <T> T run(TransactionCallable<T> callable) {
55+
return getTransactionRunner().run(callable);
56+
}
57+
58+
@Override
59+
public Timestamp getCommitTimestamp() {
60+
return getTransactionRunner().getCommitTimestamp();
61+
}
62+
63+
@Override
64+
public CommitResponse getCommitResponse() {
65+
return getTransactionRunner().getCommitResponse();
66+
}
67+
68+
@Override
69+
public TransactionRunner allowNestedTransaction() {
70+
return getTransactionRunner().allowNestedTransaction();
71+
}
72+
}

0 commit comments

Comments
 (0)