diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerImpl.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerImpl.java index 225926a10e25..b645b75e056a 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerImpl.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerImpl.java @@ -36,6 +36,7 @@ import com.google.cloud.spanner.Options.ListOption; import com.google.cloud.spanner.Options.QueryOption; import com.google.cloud.spanner.Options.ReadOption; +import com.google.cloud.spanner.spi.v1.GapicSpannerRpc; import com.google.cloud.spanner.spi.v1.SpannerRpc; import com.google.cloud.spanner.spi.v1.SpannerRpc.Paginated; import com.google.common.annotations.VisibleForTesting; @@ -81,7 +82,6 @@ import io.opencensus.trace.Span; import io.opencensus.trace.Tracer; import io.opencensus.trace.Tracing; - import java.io.IOException; import java.io.Serializable; import java.util.AbstractList; @@ -134,7 +134,8 @@ class SpannerImpl extends BaseService implements Spanner { } private final Random random = new Random(); - private final SpannerRpc rpc; + private final SpannerRpc rawGrpcRpc; + private final SpannerRpc gapicRpc; private final int defaultPrefetchChunks; @GuardedBy("this") @@ -146,16 +147,26 @@ class SpannerImpl extends BaseService implements Spanner { @GuardedBy("this") private boolean spannerIsClosed = false; - SpannerImpl(SpannerRpc rpc, int defaultPrefetchChunks, SpannerOptions options) { + SpannerImpl( + SpannerRpc rawGrpcRpc, + SpannerRpc gapicRpc, + int defaultPrefetchChunks, + SpannerOptions options) { super(options); - this.rpc = rpc; + this.rawGrpcRpc = rawGrpcRpc; + this.gapicRpc = gapicRpc; this.defaultPrefetchChunks = defaultPrefetchChunks; - this.dbAdminClient = new DatabaseAdminClientImpl(options.getProjectId(), rpc); - this.instanceClient = new InstanceAdminClientImpl(options.getProjectId(), rpc, dbAdminClient); + this.dbAdminClient = new DatabaseAdminClientImpl(options.getProjectId(), gapicRpc); + this.instanceClient = + new InstanceAdminClientImpl(options.getProjectId(), gapicRpc, dbAdminClient); } SpannerImpl(SpannerOptions options) { - this(options.getSpannerRpcV1(), options.getPrefetchChunks(), options); + this( + options.getSpannerRpcV1(), + GapicSpannerRpc.create(options), + options.getPrefetchChunks(), + options); } private static ExponentialBackOff newBackOff() { @@ -255,7 +266,8 @@ Session createSession(final DatabaseId db) throws SpannerException { new Callable() { @Override public com.google.spanner.v1.Session call() throws Exception { - return rpc.createSession(db.getName(), getOptions().getSessionLabels(), options); + return rawGrpcRpc.createSession( + db.getName(), getOptions().getSessionLabels(), options); } }); span.end(); @@ -794,7 +806,7 @@ public Timestamp writeAtLeastOnce(Iterable mutations) throws SpannerEx new Callable() { @Override public CommitResponse call() throws Exception { - return rpc.commit(request, options); + return rawGrpcRpc.commit(request, options); } }); Timestamp t = Timestamp.fromProto(response.getCommitTimestamp()); @@ -816,7 +828,7 @@ public ReadContext singleUse() { @Override public ReadContext singleUse(TimestampBound bound) { - return setActive(new SingleReadContext(this, bound, rpc, defaultPrefetchChunks)); + return setActive(new SingleReadContext(this, bound, rawGrpcRpc, defaultPrefetchChunks)); } @Override @@ -826,7 +838,8 @@ public ReadOnlyTransaction singleUseReadOnlyTransaction() { @Override public ReadOnlyTransaction singleUseReadOnlyTransaction(TimestampBound bound) { - return setActive(new SingleUseReadOnlyTransaction(this, bound, rpc, defaultPrefetchChunks)); + return setActive( + new SingleUseReadOnlyTransaction(this, bound, rawGrpcRpc, defaultPrefetchChunks)); } @Override @@ -836,12 +849,13 @@ public ReadOnlyTransaction readOnlyTransaction() { @Override public ReadOnlyTransaction readOnlyTransaction(TimestampBound bound) { - return setActive(new MultiUseReadOnlyTransaction(this, bound, rpc, defaultPrefetchChunks)); + return setActive( + new MultiUseReadOnlyTransaction(this, bound, rawGrpcRpc, defaultPrefetchChunks)); } @Override public TransactionRunner readWriteTransaction() { - return setActive(new TransactionRunnerImpl(this, rpc, defaultPrefetchChunks)); + return setActive(new TransactionRunnerImpl(this, rawGrpcRpc, defaultPrefetchChunks)); } @Override @@ -858,7 +872,7 @@ public void close() { new Callable() { @Override public Void call() throws Exception { - rpc.deleteSession(name, options); + rawGrpcRpc.deleteSession(name, options); return null; } }); @@ -884,7 +898,7 @@ ByteString beginTransaction() { new Callable() { @Override public Transaction call() throws Exception { - return rpc.beginTransaction(request, options); + return rawGrpcRpc.beginTransaction(request, options); } }); if (txn.getId().isEmpty()) { diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/GapicSpannerRpc.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/GapicSpannerRpc.java index 7bc6b47702a3..0872a84f08d9 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/GapicSpannerRpc.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/GapicSpannerRpc.java @@ -98,6 +98,14 @@ public class GapicSpannerRpc implements SpannerRpc { private final String projectName; private final SpannerMetadataProvider metadataProvider; + public static GapicSpannerRpc create(SpannerOptions options) { + try { + return new GapicSpannerRpc(options); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + public GapicSpannerRpc(SpannerOptions options) throws IOException { this.projectId = options.getProjectId(); this.projectName = PROJECT_NAME_TEMPLATE.instantiate("project", this.projectId); diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/BatchClientImplTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/BatchClientImplTest.java index f7a0c95acfe3..da9a39cb057d 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/BatchClientImplTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/BatchClientImplTest.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + package com.google.cloud.spanner; import static com.google.common.truth.Truth.assertThat; @@ -59,7 +59,7 @@ public final class BatchClientImplTest { public void setUp() { initMocks(this); DatabaseId db = DatabaseId.of(DB_NAME); - SpannerImpl spanner = new SpannerImpl(rpc, 1, spannerOptions); + SpannerImpl spanner = new SpannerImpl(rpc, rpc, 1, spannerOptions); client = new BatchClientImpl(db, spanner); } diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionImplTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionImplTest.java index e9dd56fea827..08e1bf1e14f3 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionImplTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionImplTest.java @@ -69,7 +69,7 @@ public class SessionImplTest { @Before public void setUp() { MockitoAnnotations.initMocks(this); - SpannerImpl spanner = new SpannerImpl(rpc, 1, spannerOptions); + SpannerImpl spanner = new SpannerImpl(rpc, rpc, 1, spannerOptions); String dbName = "projects/p1/instances/i1/databases/d1"; String sessionName = dbName + "/sessions/s1"; DatabaseId db = DatabaseId.of(dbName); diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java index 71b0a1569a2d..a075a4615384 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java @@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat; import com.google.cloud.spanner.spi.v1.SpannerRpc; - import java.util.HashMap; import java.util.Map; import org.junit.Before; @@ -44,7 +43,7 @@ public class SpannerImplTest { @Before public void setUp() { MockitoAnnotations.initMocks(this); - impl = new SpannerImpl(rpc, 1, spannerOptions); + impl = new SpannerImpl(rpc, rpc, 1, spannerOptions); } @Test