Skip to content

feat: allow opt-in for gRPC, have datastore-v1-proto-client as default #1400

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

Merged
merged 7 commits into from
Apr 23, 2024
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ If you are using Maven without the BOM, add this to your dependencies:
If you are using Gradle 5.x or later, add this to your dependencies:

```Groovy
implementation platform('com.google.cloud:libraries-bom:26.34.0')
implementation platform('com.google.cloud:libraries-bom:26.37.0')

implementation 'com.google.cloud:google-cloud-datastore'
```
If you are using Gradle without BOM, add this to your dependencies:

```Groovy
implementation 'com.google.cloud:google-cloud-datastore:2.18.5'
implementation 'com.google.cloud:google-cloud-datastore:2.19.0'
```

If you are using SBT, add this to your dependencies:

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-datastore" % "2.18.5"
libraryDependencies += "com.google.cloud" % "google-cloud-datastore" % "2.19.0"
```
<!-- {x-version-update-end} -->

Expand Down Expand Up @@ -380,7 +380,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-datastore/java11.html
[stability-image]: https://img.shields.io/badge/stability-stable-green
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-datastore.svg
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-datastore/2.18.5
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-datastore/2.19.0
[authentication]: https://github.com/googleapis/google-cloud-java#authentication
[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles
Expand Down
8 changes: 8 additions & 0 deletions google-cloud-datastore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-cloud-datastore-admin-v1</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud.datastore</groupId>
<artifactId>datastore-v1-proto-client</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-api</artifactId>
Expand Down Expand Up @@ -102,6 +106,10 @@
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.cloud.datastore.Validator.validateNamespace;

import com.google.api.core.BetaApi;
import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.api.gax.rpc.TransportChannelProvider;
Expand Down Expand Up @@ -74,15 +75,8 @@ public ServiceRpc create(DatastoreOptions options) {
try {
if (options.getTransportOptions() instanceof GrpcTransportOptions) {
return new GrpcDatastoreRpc(options);
} else if (options.getTransportOptions() instanceof HttpTransportOptions) {
// todo see if we can remove this check
if (DatastoreUtils.isEmulator(options)) {
throw new IllegalArgumentException("Only GRPC channels are allowed for emulator.");
}
return new HttpDatastoreRpc(options);
} else {
throw new IllegalArgumentException(
"unknown transport type: " + options.getTransportOptions());
return new HttpDatastoreRpc(options);
}
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -96,6 +90,8 @@ public static class Builder extends ServiceOptions.Builder<Datastore, DatastoreO
private String databaseId;
private TransportChannelProvider channelProvider = null;
private CredentialsProvider credentialsProvider = null;
private String host;
private TransportOptions transportOptions;

private Builder() {}

Expand All @@ -109,15 +105,33 @@ private Builder(DatastoreOptions options) {

@Override
public Builder setTransportOptions(TransportOptions transportOptions) {
if (!(transportOptions instanceof HttpTransportOptions)) {
throw new IllegalArgumentException(
"Only http transport is allowed for " + API_SHORT_NAME + ".");
}
this.transportOptions = transportOptions;
return super.setTransportOptions(transportOptions);
}

@BetaApi
public Builder setTransportOptions(GrpcTransportOptions transportOptions) {
this.transportOptions = transportOptions;
return super.setTransportOptions(transportOptions);
}

@Override
public Builder setHost(String host) {
this.host = host;
return super.setHost(host);
}

/**
* Sets the {@link TransportChannelProvider} to use with this Datastore client.
*
* @param channelProvider A InstantiatingGrpcChannelProvider object that defines the transport
* provider for this client.
*/
@BetaApi
public Builder setChannelProvider(TransportChannelProvider channelProvider) {
this.channelProvider = validateChannelProvider(channelProvider);
return this;
Expand All @@ -129,13 +143,17 @@ public Builder setChannelProvider(TransportChannelProvider channelProvider) {
* @param credentialsProvider A CredentialsProvider object that defines the credential provider
* for this client.
*/
@BetaApi
public Builder setCredentialsProvider(CredentialsProvider credentialsProvider) {
this.credentialsProvider = credentialsProvider;
return this;
}

@Override
public DatastoreOptions build() {
if (this.host == null && this.transportOptions instanceof GrpcTransportOptions) {
this.setHost(DatastoreSettings.getDefaultEndpoint());
}
return new DatastoreOptions(this);
}

Expand All @@ -153,7 +171,7 @@ public Builder setDatabaseId(String databaseId) {

private static TransportChannelProvider validateChannelProvider(
TransportChannelProvider channelProvider) {
if (!(channelProvider instanceof InstantiatingGrpcChannelProvider)) {
if (channelProvider != null && !(channelProvider instanceof InstantiatingGrpcChannelProvider)) {
throw new IllegalArgumentException(
"Only GRPC channels are allowed for " + API_SHORT_NAME + ".");
}
Expand All @@ -165,7 +183,6 @@ private DatastoreOptions(Builder builder) {
namespace = MoreObjects.firstNonNull(builder.namespace, defaultNamespace());
databaseId = MoreObjects.firstNonNull(builder.databaseId, DEFAULT_DATABASE_ID);

// todo see if we can update this
if (getTransportOptions() instanceof HttpTransportOptions
&& (builder.channelProvider != null || builder.credentialsProvider != null)) {
throw new IllegalArgumentException(
Expand Down Expand Up @@ -195,7 +212,7 @@ public TransportChannelProvider getTransportChannelProvider() {
@Override
protected String getDefaultHost() {
String host = System.getProperty(LOCAL_HOST_ENV_VAR, System.getenv(LOCAL_HOST_ENV_VAR));
return host != null ? host : DatastoreSettings.getDefaultEndpoint();
return host != null ? host : com.google.datastore.v1.client.DatastoreFactory.DEFAULT_HOST;
}

@Override
Expand All @@ -222,8 +239,8 @@ public TransportOptions getDefaultTransportOptions() {
return TRANSPORT_OPTIONS;
}

public static GrpcTransportOptions.Builder getDefaultTransportOptionsBuilder() {
return GrpcTransportOptions.newBuilder();
public static HttpTransportOptions.Builder getDefaultTransportOptionsBuilder() {
return HttpTransportOptions.newBuilder();
}
}

Expand Down
Loading
Loading