-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(Datastore): Swap the default transport from HttpJson to gRPC #12977
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
base: main
Are you sure you want to change the base?
Changes from all commits
80dcf14
1550342
93ce220
6696b85
9bd06ce
01c5184
0271060
df95ceb
5665355
6f98817
d839776
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,7 +19,6 @@ | |||||||||
| import static com.google.cloud.datastore.Validator.validateNamespace; | ||||||||||
|
|
||||||||||
| import com.google.api.core.BetaApi; | ||||||||||
| import com.google.api.core.ObsoleteApi; | ||||||||||
| import com.google.api.gax.grpc.ChannelPoolSettings; | ||||||||||
| import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; | ||||||||||
| import com.google.api.gax.rpc.TransportChannelProvider; | ||||||||||
|
|
@@ -35,18 +34,21 @@ | |||||||||
| import com.google.cloud.grpc.GrpcTransportOptions; | ||||||||||
| import com.google.cloud.http.HttpTransportOptions; | ||||||||||
| import com.google.common.base.MoreObjects; | ||||||||||
| import com.google.common.base.Preconditions; | ||||||||||
| import com.google.common.collect.ImmutableSet; | ||||||||||
| import java.io.IOException; | ||||||||||
| import java.lang.reflect.Method; | ||||||||||
| import java.util.Objects; | ||||||||||
| import java.util.Set; | ||||||||||
| import java.util.logging.Logger; | ||||||||||
| import javax.annotation.Nonnull; | ||||||||||
| import javax.annotation.Nullable; | ||||||||||
|
|
||||||||||
| public class DatastoreOptions extends ServiceOptions<Datastore, DatastoreOptions> { | ||||||||||
|
|
||||||||||
| private static final long serialVersionUID = -1018382430058137336L; | ||||||||||
| private static final String API_SHORT_NAME = "Datastore"; | ||||||||||
| private static final Logger logger = Logger.getLogger(DatastoreOptions.class.getName()); | ||||||||||
| private static final String DATASTORE_SCOPE = "https://www.googleapis.com/auth/datastore"; | ||||||||||
| private static final Set<String> SCOPES = ImmutableSet.of(DATASTORE_SCOPE); | ||||||||||
| private static final String DEFAULT_DATABASE_ID = ""; | ||||||||||
|
|
@@ -72,9 +74,7 @@ public class DatastoreOptions extends ServiceOptions<Datastore, DatastoreOptions | |||||||||
| /** | ||||||||||
| * @deprecated This constant is obsolete and will be removed in a future version. | ||||||||||
| */ | ||||||||||
| @ObsoleteApi("This constant is obsolete and will be removed in a future version.") | ||||||||||
| @Deprecated | ||||||||||
| public static final int MAX_CHANNEL_COUNT = 10; | ||||||||||
| @Deprecated public static final int MAX_CHANNEL_COUNT = 10; | ||||||||||
|
|
||||||||||
| private transient TransportChannelProvider channelProvider = null; | ||||||||||
|
|
||||||||||
|
|
@@ -129,7 +129,10 @@ public static class Builder extends ServiceOptions.Builder<Datastore, DatastoreO | |||||||||
| private String databaseId; | ||||||||||
| private TransportChannelProvider channelProvider = null; | ||||||||||
| private String host; | ||||||||||
| private TransportOptions transportOptions; | ||||||||||
|
|
||||||||||
| @Nonnull | ||||||||||
| private TransportOptions transportOptions = | ||||||||||
| new DatastoreDefaults().getDefaultTransportOptions(); | ||||||||||
|
|
||||||||||
| @Nullable private DatastoreOpenTelemetryOptions openTelemetryOptions = null; | ||||||||||
|
|
||||||||||
|
|
@@ -141,25 +144,51 @@ private Builder(DatastoreOptions options) { | |||||||||
| this.databaseId = options.databaseId; | ||||||||||
| this.openTelemetryOptions = options.openTelemetryOptions; | ||||||||||
| this.channelProvider = validateChannelProvider(options.channelProvider); | ||||||||||
| this.host = options.getHost(); | ||||||||||
| this.transportOptions = options.getTransportOptions(); | ||||||||||
|
Comment on lines
146
to
+148
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These assignments are redundant and potentially introduce a bug. The
Suggested change
References
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| private TransportChannelProvider validateChannelProvider( | ||||||||||
| TransportChannelProvider channelProvider) { | ||||||||||
| Preconditions.checkNotNull(channelProvider, "TransportChannelProvider cannot be null"); | ||||||||||
| if (!(channelProvider instanceof InstantiatingGrpcChannelProvider)) { | ||||||||||
| throw new IllegalArgumentException( | ||||||||||
| "Only GRPC channels are allowed for " + API_SHORT_NAME + "."); | ||||||||||
| } | ||||||||||
| return channelProvider; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Sets the transport options. | ||||||||||
| * | ||||||||||
| * @param transportOptions the transport options to set, must be {@link HttpTransportOptions} or | ||||||||||
| * {@link GrpcTransportOptions} | ||||||||||
| * @return the builder | ||||||||||
| * @throws IllegalArgumentException if the transport options are not supported | ||||||||||
| */ | ||||||||||
| @Override | ||||||||||
| public Builder setTransportOptions(TransportOptions transportOptions) { | ||||||||||
| if (!(transportOptions instanceof HttpTransportOptions)) { | ||||||||||
| public Builder setTransportOptions(@Nonnull TransportOptions transportOptions) { | ||||||||||
| Preconditions.checkNotNull(transportOptions, "TransportOptions cannot be null"); | ||||||||||
| if (!(transportOptions instanceof HttpTransportOptions) | ||||||||||
| && !(transportOptions instanceof GrpcTransportOptions)) { | ||||||||||
| throw new IllegalArgumentException( | ||||||||||
| "Only http transport is allowed for " + API_SHORT_NAME + "."); | ||||||||||
| "Only http and grpc transport are allowed for " + API_SHORT_NAME + "."); | ||||||||||
| } | ||||||||||
| this.transportOptions = transportOptions; | ||||||||||
| return super.setTransportOptions(transportOptions); | ||||||||||
|
Comment on lines
177
to
178
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The assignment
Suggested change
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Sets the transport to gRPC. Note this functionality is experimental and subject to change. | ||||||||||
| * This method deprecated. Prefer to use {@link #setTransportOptions(TransportOptions)} instead. | ||||||||||
| * When using the transport-neutral variant, you may need to cast to TransportOptions when using | ||||||||||
| * a GrpcTransportOptions class, otherwise it will default to the deprecated method. | ||||||||||
| * | ||||||||||
| * <p>Sets the transport to gRPC. Note this functionality is experimental and subject to change. | ||||||||||
| */ | ||||||||||
| @Deprecated | ||||||||||
| @BetaApi | ||||||||||
| public Builder setTransportOptions(GrpcTransportOptions transportOptions) { | ||||||||||
| this.transportOptions = transportOptions; | ||||||||||
| return super.setTransportOptions(transportOptions); | ||||||||||
| return setTransportOptions((TransportOptions) transportOptions); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Override | ||||||||||
|
|
@@ -185,10 +214,28 @@ public Builder setChannelProvider(TransportChannelProvider channelProvider) { | |||||||||
| return this; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Builds the {@link DatastoreOptions} instance. | ||||||||||
| * | ||||||||||
| * <p>If the host is not explicitly set, it defaults to the transport-specific default host: | ||||||||||
| * | ||||||||||
| * <ul> | ||||||||||
| * <li>gRPC: {@code datastore.googleapis.com:443} | ||||||||||
| * <li>HTTP: {@code https://datastore.googleapis.com} | ||||||||||
| * </ul> | ||||||||||
| * | ||||||||||
| * @return the {@link DatastoreOptions} instance | ||||||||||
| */ | ||||||||||
| @Override | ||||||||||
| public DatastoreOptions build() { | ||||||||||
| if (this.host == null && this.transportOptions instanceof GrpcTransportOptions) { | ||||||||||
| this.setHost(DatastoreSettings.getDefaultEndpoint()); | ||||||||||
| if (this.host == null) { | ||||||||||
| // Use whatever host value the user passes in, otherwise use the transport specific default | ||||||||||
| // host values | ||||||||||
| if (this.transportOptions instanceof GrpcTransportOptions) { | ||||||||||
| this.setHost(DatastoreSettings.getDefaultEndpoint()); | ||||||||||
| } else if (this.transportOptions instanceof HttpTransportOptions) { | ||||||||||
| this.setHost(com.google.datastore.v1.client.DatastoreFactory.DEFAULT_HOST); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| return new DatastoreOptions(this); | ||||||||||
| } | ||||||||||
|
|
@@ -218,15 +265,6 @@ public Builder setOpenTelemetryOptions( | |||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private static TransportChannelProvider validateChannelProvider( | ||||||||||
| TransportChannelProvider channelProvider) { | ||||||||||
| if (channelProvider != null && !(channelProvider instanceof InstantiatingGrpcChannelProvider)) { | ||||||||||
| throw new IllegalArgumentException( | ||||||||||
| "Only GRPC channels are allowed for " + API_SHORT_NAME + "."); | ||||||||||
| } | ||||||||||
| return channelProvider; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private DatastoreOptions(Builder builder) { | ||||||||||
| super(DatastoreFactory.class, DatastoreRpcFactory.class, builder, new DatastoreDefaults()); | ||||||||||
|
|
||||||||||
|
|
@@ -239,9 +277,9 @@ private DatastoreOptions(Builder builder) { | |||||||||
| namespace = MoreObjects.firstNonNull(builder.namespace, defaultNamespace()); | ||||||||||
| databaseId = MoreObjects.firstNonNull(builder.databaseId, DEFAULT_DATABASE_ID); | ||||||||||
|
|
||||||||||
| // ChannelProvider is used by GAX but HttpJson does not use it so we safely ignore it. | ||||||||||
| if (getTransportOptions() instanceof HttpTransportOptions && builder.channelProvider != null) { | ||||||||||
| throw new IllegalArgumentException( | ||||||||||
| "Only gRPC transport allows setting of channel provider or credentials provider"); | ||||||||||
| logger.warning("Channel provider is ignored for HttpJson transport."); | ||||||||||
| } else if (getTransportOptions() instanceof GrpcTransportOptions) { | ||||||||||
| if (builder.channelProvider == null) { | ||||||||||
| // Set the default gRPC connection pool to be configured with a minimum of 1 channel. | ||||||||||
|
|
@@ -310,8 +348,8 @@ public TransportOptions getDefaultTransportOptions() { | |||||||||
| return TRANSPORT_OPTIONS; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public static HttpTransportOptions.Builder getDefaultTransportOptionsBuilder() { | ||||||||||
| return HttpTransportOptions.newBuilder(); | ||||||||||
| public static GrpcTransportOptions.Builder getDefaultTransportOptionsBuilder() { | ||||||||||
| return GrpcTransportOptions.newBuilder(); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
||||||||||
Uh oh!
There was an error while loading. Please reload this page.