Skip to content

Commit

Permalink
spanner: move admin clients to GAPIC stub (googleapis#3067)
Browse files Browse the repository at this point in the history
  • Loading branch information
pongad authored and yihanzhen committed Apr 5, 2018
1 parent 18e61d2 commit a1e6133
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -134,7 +134,8 @@ class SpannerImpl extends BaseService<SpannerOptions> 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")
Expand All @@ -146,16 +147,26 @@ class SpannerImpl extends BaseService<SpannerOptions> 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() {
Expand Down Expand Up @@ -255,7 +266,8 @@ Session createSession(final DatabaseId db) throws SpannerException {
new Callable<com.google.spanner.v1.Session>() {
@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();
Expand Down Expand Up @@ -794,7 +806,7 @@ public Timestamp writeAtLeastOnce(Iterable<Mutation> mutations) throws SpannerEx
new Callable<CommitResponse>() {
@Override
public CommitResponse call() throws Exception {
return rpc.commit(request, options);
return rawGrpcRpc.commit(request, options);
}
});
Timestamp t = Timestamp.fromProto(response.getCommitTimestamp());
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -858,7 +872,7 @@ public void close() {
new Callable<Void>() {
@Override
public Void call() throws Exception {
rpc.deleteSession(name, options);
rawGrpcRpc.deleteSession(name, options);
return null;
}
});
Expand All @@ -884,7 +898,7 @@ ByteString beginTransaction() {
new Callable<Transaction>() {
@Override
public Transaction call() throws Exception {
return rpc.beginTransaction(request, options);
return rawGrpcRpc.beginTransaction(request, options);
}
});
if (txn.getId().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit a1e6133

Please sign in to comment.