Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: spanner: move admin clients to GAPIC stub #3067

Merged
merged 2 commits into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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