Skip to content

Commit b3b7f04

Browse files
authored
spanner: move admin clients to GAPIC stub (#3067)
1 parent ba0cf43 commit b3b7f04

5 files changed

Lines changed: 41 additions & 20 deletions

File tree

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.google.cloud.spanner.Options.ListOption;
3737
import com.google.cloud.spanner.Options.QueryOption;
3838
import com.google.cloud.spanner.Options.ReadOption;
39+
import com.google.cloud.spanner.spi.v1.GapicSpannerRpc;
3940
import com.google.cloud.spanner.spi.v1.SpannerRpc;
4041
import com.google.cloud.spanner.spi.v1.SpannerRpc.Paginated;
4142
import com.google.common.annotations.VisibleForTesting;
@@ -81,7 +82,6 @@
8182
import io.opencensus.trace.Span;
8283
import io.opencensus.trace.Tracer;
8384
import io.opencensus.trace.Tracing;
84-
8585
import java.io.IOException;
8686
import java.io.Serializable;
8787
import java.util.AbstractList;
@@ -134,7 +134,8 @@ class SpannerImpl extends BaseService<SpannerOptions> implements Spanner {
134134
}
135135

136136
private final Random random = new Random();
137-
private final SpannerRpc rpc;
137+
private final SpannerRpc rawGrpcRpc;
138+
private final SpannerRpc gapicRpc;
138139
private final int defaultPrefetchChunks;
139140

140141
@GuardedBy("this")
@@ -146,16 +147,26 @@ class SpannerImpl extends BaseService<SpannerOptions> implements Spanner {
146147
@GuardedBy("this")
147148
private boolean spannerIsClosed = false;
148149

149-
SpannerImpl(SpannerRpc rpc, int defaultPrefetchChunks, SpannerOptions options) {
150+
SpannerImpl(
151+
SpannerRpc rawGrpcRpc,
152+
SpannerRpc gapicRpc,
153+
int defaultPrefetchChunks,
154+
SpannerOptions options) {
150155
super(options);
151-
this.rpc = rpc;
156+
this.rawGrpcRpc = rawGrpcRpc;
157+
this.gapicRpc = gapicRpc;
152158
this.defaultPrefetchChunks = defaultPrefetchChunks;
153-
this.dbAdminClient = new DatabaseAdminClientImpl(options.getProjectId(), rpc);
154-
this.instanceClient = new InstanceAdminClientImpl(options.getProjectId(), rpc, dbAdminClient);
159+
this.dbAdminClient = new DatabaseAdminClientImpl(options.getProjectId(), gapicRpc);
160+
this.instanceClient =
161+
new InstanceAdminClientImpl(options.getProjectId(), gapicRpc, dbAdminClient);
155162
}
156163

157164
SpannerImpl(SpannerOptions options) {
158-
this(options.getSpannerRpcV1(), options.getPrefetchChunks(), options);
165+
this(
166+
options.getSpannerRpcV1(),
167+
GapicSpannerRpc.create(options),
168+
options.getPrefetchChunks(),
169+
options);
159170
}
160171

161172
private static ExponentialBackOff newBackOff() {
@@ -255,7 +266,8 @@ Session createSession(final DatabaseId db) throws SpannerException {
255266
new Callable<com.google.spanner.v1.Session>() {
256267
@Override
257268
public com.google.spanner.v1.Session call() throws Exception {
258-
return rpc.createSession(db.getName(), getOptions().getSessionLabels(), options);
269+
return rawGrpcRpc.createSession(
270+
db.getName(), getOptions().getSessionLabels(), options);
259271
}
260272
});
261273
span.end();
@@ -794,7 +806,7 @@ public Timestamp writeAtLeastOnce(Iterable<Mutation> mutations) throws SpannerEx
794806
new Callable<CommitResponse>() {
795807
@Override
796808
public CommitResponse call() throws Exception {
797-
return rpc.commit(request, options);
809+
return rawGrpcRpc.commit(request, options);
798810
}
799811
});
800812
Timestamp t = Timestamp.fromProto(response.getCommitTimestamp());
@@ -816,7 +828,7 @@ public ReadContext singleUse() {
816828

817829
@Override
818830
public ReadContext singleUse(TimestampBound bound) {
819-
return setActive(new SingleReadContext(this, bound, rpc, defaultPrefetchChunks));
831+
return setActive(new SingleReadContext(this, bound, rawGrpcRpc, defaultPrefetchChunks));
820832
}
821833

822834
@Override
@@ -826,7 +838,8 @@ public ReadOnlyTransaction singleUseReadOnlyTransaction() {
826838

827839
@Override
828840
public ReadOnlyTransaction singleUseReadOnlyTransaction(TimestampBound bound) {
829-
return setActive(new SingleUseReadOnlyTransaction(this, bound, rpc, defaultPrefetchChunks));
841+
return setActive(
842+
new SingleUseReadOnlyTransaction(this, bound, rawGrpcRpc, defaultPrefetchChunks));
830843
}
831844

832845
@Override
@@ -836,12 +849,13 @@ public ReadOnlyTransaction readOnlyTransaction() {
836849

837850
@Override
838851
public ReadOnlyTransaction readOnlyTransaction(TimestampBound bound) {
839-
return setActive(new MultiUseReadOnlyTransaction(this, bound, rpc, defaultPrefetchChunks));
852+
return setActive(
853+
new MultiUseReadOnlyTransaction(this, bound, rawGrpcRpc, defaultPrefetchChunks));
840854
}
841855

842856
@Override
843857
public TransactionRunner readWriteTransaction() {
844-
return setActive(new TransactionRunnerImpl(this, rpc, defaultPrefetchChunks));
858+
return setActive(new TransactionRunnerImpl(this, rawGrpcRpc, defaultPrefetchChunks));
845859
}
846860

847861
@Override
@@ -858,7 +872,7 @@ public void close() {
858872
new Callable<Void>() {
859873
@Override
860874
public Void call() throws Exception {
861-
rpc.deleteSession(name, options);
875+
rawGrpcRpc.deleteSession(name, options);
862876
return null;
863877
}
864878
});
@@ -884,7 +898,7 @@ ByteString beginTransaction() {
884898
new Callable<Transaction>() {
885899
@Override
886900
public Transaction call() throws Exception {
887-
return rpc.beginTransaction(request, options);
901+
return rawGrpcRpc.beginTransaction(request, options);
888902
}
889903
});
890904
if (txn.getId().isEmpty()) {

google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/GapicSpannerRpc.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ public class GapicSpannerRpc implements SpannerRpc {
9898
private final String projectName;
9999
private final SpannerMetadataProvider metadataProvider;
100100

101+
public static GapicSpannerRpc create(SpannerOptions options) {
102+
try {
103+
return new GapicSpannerRpc(options);
104+
} catch (IOException e) {
105+
throw new IllegalStateException(e);
106+
}
107+
}
108+
101109
public GapicSpannerRpc(SpannerOptions options) throws IOException {
102110
this.projectId = options.getProjectId();
103111
this.projectName = PROJECT_NAME_TEMPLATE.instantiate("project", this.projectId);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
16+
1717
package com.google.cloud.spanner;
1818

1919
import static com.google.common.truth.Truth.assertThat;
@@ -59,7 +59,7 @@ public final class BatchClientImplTest {
5959
public void setUp() {
6060
initMocks(this);
6161
DatabaseId db = DatabaseId.of(DB_NAME);
62-
SpannerImpl spanner = new SpannerImpl(rpc, 1, spannerOptions);
62+
SpannerImpl spanner = new SpannerImpl(rpc, rpc, 1, spannerOptions);
6363
client = new BatchClientImpl(db, spanner);
6464
}
6565

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class SessionImplTest {
6969
@Before
7070
public void setUp() {
7171
MockitoAnnotations.initMocks(this);
72-
SpannerImpl spanner = new SpannerImpl(rpc, 1, spannerOptions);
72+
SpannerImpl spanner = new SpannerImpl(rpc, rpc, 1, spannerOptions);
7373
String dbName = "projects/p1/instances/i1/databases/d1";
7474
String sessionName = dbName + "/sessions/s1";
7575
DatabaseId db = DatabaseId.of(dbName);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020

2121
import com.google.cloud.spanner.spi.v1.SpannerRpc;
22-
2322
import java.util.HashMap;
2423
import java.util.Map;
2524
import org.junit.Before;
@@ -44,7 +43,7 @@ public class SpannerImplTest {
4443
@Before
4544
public void setUp() {
4645
MockitoAnnotations.initMocks(this);
47-
impl = new SpannerImpl(rpc, 1, spannerOptions);
46+
impl = new SpannerImpl(rpc, rpc, 1, spannerOptions);
4847
}
4948

5049
@Test

0 commit comments

Comments
 (0)