Skip to content
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 @@ -226,11 +226,11 @@ public Builder setChannelProvider(TransportChannelProvider channelProvider) {
*/
@Override
public DatastoreOptions build() {
if (this.host == null) {
// Use whatever host value the user passes in, otherwise use the transport specific default
// host values
// Normalize null or transport-default hosts to the active transport's default so switching
// transports via toBuilder() never carries over a stale default endpoint.
if (this.host == null || DatastoreUtils.isDefaultHost(this.host)) {
if (this.transportOptions instanceof GrpcTransportOptions) {
this.setHost(DatastoreSettings.getDefaultEndpoint());
this.setHost(DatastoreUtils.DEFAULT_GRPC_DATASTORE_ENDPOINT);
} else if (this.transportOptions instanceof HttpTransportOptions) {
this.setHost(DEFAULT_HOST);
}
Expand Down Expand Up @@ -317,7 +317,7 @@ private DatastoreOptions(Builder builder) {
.setChannelPoolSettings(datastoreChannelPoolSettings);
// Only pin the endpoint when a custom host is configured; otherwise leave it unset
// so GAX resolves datastore.<universeDomain>:443 automatically.
if (getHost() != null && !DatastoreSettings.getDefaultEndpoint().equals(getHost())) {
if (getHost() != null && !DatastoreUtils.isDefaultHost(getHost())) {
channelProviderBuilder.setEndpoint(getHost());
}
if (traceUtil.getChannelConfigurator() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
public class DatastoreUtils {

private static final String DEFAULT_DATASTORE_HOSTNAME = "datastore.googleapis.com";
// Default gRPC endpoint kept as a constant to avoid depending on the obsolete
// DatastoreStubSettings.getDefaultEndpoint().
public static final String DEFAULT_GRPC_DATASTORE_ENDPOINT = DEFAULT_DATASTORE_HOSTNAME + ":443";

public static boolean isEmulator(DatastoreOptions datastoreOptions) {
// Short-circuit on NoCredentials before inspecting the host to avoid URL parsing or DNS
Expand All @@ -37,6 +40,24 @@ public static boolean isEmulator(DatastoreOptions datastoreOptions) {
|| isLocalHost(datastoreOptions.getHost());
}

/**
* Returns {@code true} if {@code host} matches the default Datastore service host for either HTTP
* ({@code https://datastore.googleapis.com}) or gRPC ({@code datastore.googleapis.com:443}).
*
* <p>Recognizing both transport defaults allows transport switches via {@code toBuilder()} and
* custom universe-domain endpoint resolution to distinguish default hosts from explicit custom
* host overrides.
*/
public static boolean isDefaultHost(String host) {
if (Strings.isNullOrEmpty(host)) {
return false;
}
// removeScheme() lowercases the input before stripping http:// or https://.
String normalized = removeScheme(host);
return DEFAULT_DATASTORE_HOSTNAME.equals(normalized)
|| DEFAULT_GRPC_DATASTORE_ENDPOINT.equals(normalized);
}

public static boolean isLocalHost(String host) {
if (Strings.isNullOrEmpty(host)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.datastore.spi.v1;

import static com.google.cloud.datastore.DatastoreUtils.isDefaultHost;
import static com.google.cloud.datastore.DatastoreUtils.isEmulator;
import static com.google.cloud.datastore.DatastoreUtils.removeScheme;
import static com.google.cloud.datastore.spi.v1.RpcUtils.retrySettingSetter;
Expand Down Expand Up @@ -205,8 +206,7 @@ ClientContext getClientContext(DatastoreOptions datastoreOptions) throws IOExcep
// and validates credentials against it.
settingsBuilder.setUniverseDomain(datastoreOptions.getUniverseDomain());
// Preserve custom host overrides; default hosts are resolved by GAX from the universe domain.
if (datastoreOptions.getHost() != null
&& !DatastoreSettings.getDefaultEndpoint().equals(datastoreOptions.getHost())) {
if (datastoreOptions.getHost() != null && !isDefaultHost(datastoreOptions.getHost())) {
settingsBuilder.setEndpoint(datastoreOptions.getHost());
}
return ClientContext.create(settingsBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.datastore.spi.v1;

import static com.google.cloud.datastore.DatastoreUtils.isDefaultHost;
import static com.google.cloud.datastore.DatastoreUtils.isLocalHost;
import static com.google.cloud.datastore.DatastoreUtils.removeScheme;

Expand Down Expand Up @@ -116,9 +117,9 @@ static com.google.datastore.v1.client.DatastoreOptions.Builder createClientOptio

if (isLocalHost(normalizedHost)) {
clientBuilder = clientBuilder.localHost(removeScheme(normalizedHost));
} else if (!removeScheme(normalizedHost)
.equals(removeScheme(com.google.datastore.v1.client.DatastoreFactory.DEFAULT_HOST))
&& !normalizedHost.isEmpty()) {
} else if (!isDefaultHost(normalizedHost) && !normalizedHost.isEmpty()) {
Comment thread
jinseopkim0 marked this conversation as resolved.
Resolved
// Only treat non-default hosts as custom overrides; both HTTP and gRPC default hosts fall
// through to universe-domain host resolution below.
// DatastoreFactory.create(...) automatically builds the valid endpoint URL using the host,
// so we only need to set the host without any suffix.
clientBuilder = clientBuilder.host(removeScheme(normalizedHost));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.api.gax.rpc.TransportChannel;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.cloud.NoCredentials;
import com.google.cloud.TransportOptions;
import com.google.cloud.datastore.spi.DatastoreRpcFactory;
import com.google.cloud.datastore.spi.v1.DatastoreRpc;
import com.google.cloud.datastore.v1.DatastoreSettings;
Expand Down Expand Up @@ -438,4 +439,63 @@ public void testGrpcDefaultChannelProviderDefersEndpointToGax() {
assertFalse(customChannelProvider.needsEndpoint());
assertEquals(customHost, customChannelProvider.getEndpoint());
}

@Test
public void testToBuilder_grpcToHttpResetsDefaultHost() {
DatastoreOptions grpcDefault =
DatastoreOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setCredentials(NoCredentials.getInstance())
.build();
assertThat(grpcDefault.getHost()).isEqualTo(DatastoreUtils.DEFAULT_GRPC_DATASTORE_ENDPOINT);

DatastoreOptions switchedToHttp =
grpcDefault.toBuilder()
.setTransportOptions(HttpTransportOptions.newBuilder().build())
.setUniverseDomain("custom.example.com")
.build();
assertThat(switchedToHttp.getTransportOptions()).isInstanceOf(HttpTransportOptions.class);
assertThat(switchedToHttp.getHost()).isEqualTo(DatastoreFactory.DEFAULT_HOST);
assertThat(switchedToHttp.getUniverseDomain()).isEqualTo("custom.example.com");
}

@Test
public void testToBuilder_httpToGrpcResetsDefaultHost() {
DatastoreOptions httpDefault =
DatastoreOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setCredentials(NoCredentials.getInstance())
.setTransportOptions(HttpTransportOptions.newBuilder().build())
.build();
assertThat(httpDefault.getHost()).isEqualTo(DatastoreFactory.DEFAULT_HOST);

DatastoreOptions switchedToGrpc =
httpDefault.toBuilder()
.setTransportOptions((TransportOptions) GrpcTransportOptions.newBuilder().build())
Comment thread
jinseopkim0 marked this conversation as resolved.
Resolved
.setUniverseDomain("custom.example.com")
.build();
assertThat(switchedToGrpc.getTransportOptions()).isInstanceOf(GrpcTransportOptions.class);
assertThat(switchedToGrpc.getHost()).isEqualTo(DatastoreUtils.DEFAULT_GRPC_DATASTORE_ENDPOINT);
InstantiatingGrpcChannelProvider channelProvider =
(InstantiatingGrpcChannelProvider) switchedToGrpc.getTransportChannelProvider();
assertTrue(channelProvider.needsEndpoint());
assertThat(channelProvider.getEndpoint()).isNull();
}

@Test
public void testToBuilder_customHostPreservedAcrossTransportSwitch() {
String customHost = "custom-datastore.example.com:443";
DatastoreOptions grpcCustom =
DatastoreOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setCredentials(NoCredentials.getInstance())
.setHost(customHost)
.build();

DatastoreOptions switchedToHttp =
grpcCustom.toBuilder()
.setTransportOptions(HttpTransportOptions.newBuilder().build())
.build();
assertThat(switchedToHttp.getHost()).isEqualTo(customHost);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.datastore;

import static com.google.cloud.datastore.DatastoreUtils.isDefaultHost;
import static com.google.cloud.datastore.DatastoreUtils.isEmulator;
import static com.google.cloud.datastore.DatastoreUtils.isLocalHost;
import static com.google.cloud.datastore.DatastoreUtils.removeScheme;
Expand All @@ -39,6 +40,20 @@ public void testIsEmulator() {
assertThat(isEmulator(noCredsRemoteHost)).isTrue();
}

@Test
public void testIsDefaultHost() {
assertThat(isDefaultHost(null)).isFalse();
assertThat(isDefaultHost("")).isFalse();
assertThat(isDefaultHost(DatastoreUtils.DEFAULT_GRPC_DATASTORE_ENDPOINT)).isTrue();
assertThat(isDefaultHost(DatastoreFactory.DEFAULT_HOST)).isTrue();
assertThat(isDefaultHost("https://datastore.googleapis.com:443")).isTrue();
assertThat(isDefaultHost("DATASTORE.GOOGLEAPIS.COM:443")).isTrue();
assertThat(isDefaultHost("datastore.googleapis.com")).isTrue();
assertThat(isDefaultHost("datastore.googleapis.com:8443")).isFalse();
assertThat(isDefaultHost("custom-datastore.example.com:443")).isFalse();
assertThat(isDefaultHost("http://localhost:8081")).isFalse();
}

@Test
public void testIsLocalHost() {
assertThat(isLocalHost(null)).isFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import com.google.api.gax.grpc.GrpcTransportChannel;
import com.google.api.gax.rpc.ClientContext;
import com.google.auth.Credentials;
import com.google.cloud.TransportOptions;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.v1.stub.DatastoreStubSettings;
import com.google.cloud.grpc.GrpcTransportOptions;
import com.google.cloud.http.HttpTransportOptions;
import java.net.URI;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -124,4 +127,32 @@ public void testCustomHostOverridesDefaultEndpointWithUniverseDomain() throws Ex
rpc.close();
}
}

@Test
public void testToBuilder_httpToGrpcResolvesUniverseDomain() throws Exception {
String customUniverseDomain = "custom.example.com";
DatastoreOptions httpDefault =
DatastoreOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setCredentials(new FakeUniverseCredentials(customUniverseDomain))
.setTransportOptions(HttpTransportOptions.newBuilder().build())
.build();

DatastoreOptions switchedToGrpc =
httpDefault.toBuilder()
.setTransportOptions((TransportOptions) GrpcTransportOptions.newBuilder().build())
.setUniverseDomain(customUniverseDomain)
.build();

GrpcDatastoreRpc rpc = new GrpcDatastoreRpc(switchedToGrpc);
try {
ClientContext clientContext = rpc.getClientContext();
assertEquals(customUniverseDomain, clientContext.getUniverseDomain());
GrpcTransportChannel transportChannel =
(GrpcTransportChannel) clientContext.getTransportChannel();
assertEquals("datastore.custom.example.com:443", transportChannel.getChannel().authority());
} finally {
rpc.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.cloud.NoCredentials;
import com.google.cloud.datastore.DatastoreException;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.DatastoreUtils;
import com.google.cloud.http.HttpTransportOptions;
import com.google.datastore.v1.LookupRequest;
import com.google.rpc.Code;
Expand Down Expand Up @@ -194,4 +195,41 @@ public void testValidateUniverseDomain_apiKeyCredentialsSkipsValidation() {
HttpDatastoreRpc rpc = new HttpDatastoreRpc(options, null);
rpc.validateUniverseDomain();
}

@Test
public void testToBuilder_grpcToHttpResolvesUniverseDomain() {
DatastoreOptions grpcDefault =
DatastoreOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setCredentials(new FakeUniverseCredentials(CUSTOM_UNIVERSE_DOMAIN))
.build();

DatastoreOptions switchedToHttp =
grpcDefault.toBuilder()
.setTransportOptions(HttpTransportOptions.newBuilder().build())
.setUniverseDomain(CUSTOM_UNIVERSE_DOMAIN)
.build();

HttpRequestInitializer initializer = request -> {};
com.google.datastore.v1.client.DatastoreOptions clientOptions =
HttpDatastoreRpc.createClientOptionsBuilder(
switchedToHttp, initializer, new MockHttpTransport(), CUSTOM_UNIVERSE_DOMAIN)
.build();
assertEquals("datastore." + CUSTOM_UNIVERSE_DOMAIN, clientOptions.getHost());
}

@Test
public void testCreateClientOptionsBuilder_grpcDefaultEndpointResolvesUniverseDomain() {
DatastoreOptions options =
httpOptionsBuilder()
.setHost(DatastoreUtils.DEFAULT_GRPC_DATASTORE_ENDPOINT)
.setUniverseDomain(CUSTOM_UNIVERSE_DOMAIN)
.build();
HttpRequestInitializer initializer = request -> {};
com.google.datastore.v1.client.DatastoreOptions clientOptions =
HttpDatastoreRpc.createClientOptionsBuilder(
options, initializer, new MockHttpTransport(), CUSTOM_UNIVERSE_DOMAIN)
.build();
assertEquals("datastore." + CUSTOM_UNIVERSE_DOMAIN, clientOptions.getHost());
}
}
Loading