Skip to content

Commit 43544ea

Browse files
authored
fix(datastore): reset transport-default host when switching transport via toBuilder (#15414)
b/588887355#comment11 Reset transport-defaulted host when switching between gRPC and HTTP via `DatastoreOptions.toBuilder().setTransportOptions(...)` and recognize both transport defaults in `DatastoreUtils.isDefaultHost()`.
1 parent 87f5735 commit 43544ea

8 files changed

Lines changed: 176 additions & 10 deletions

File tree

‎java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreOptions.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,11 @@ public Builder setChannelProvider(TransportChannelProvider channelProvider) {
226226
*/
227227
@Override
228228
public DatastoreOptions build() {
229-
if (this.host == null) {
230-
// Use whatever host value the user passes in, otherwise use the transport specific default
231-
// host values
229+
// Normalize null or transport-default hosts to the active transport's default so switching
230+
// transports via toBuilder() never carries over a stale default endpoint.
231+
if (this.host == null || DatastoreUtils.isDefaultHost(this.host)) {
232232
if (this.transportOptions instanceof GrpcTransportOptions) {
233-
this.setHost(DatastoreSettings.getDefaultEndpoint());
233+
this.setHost(DatastoreUtils.DEFAULT_GRPC_DATASTORE_ENDPOINT);
234234
} else if (this.transportOptions instanceof HttpTransportOptions) {
235235
this.setHost(DEFAULT_HOST);
236236
}
@@ -317,7 +317,7 @@ private DatastoreOptions(Builder builder) {
317317
.setChannelPoolSettings(datastoreChannelPoolSettings);
318318
// Only pin the endpoint when a custom host is configured; otherwise leave it unset
319319
// so GAX resolves datastore.<universeDomain>:443 automatically.
320-
if (getHost() != null && !DatastoreSettings.getDefaultEndpoint().equals(getHost())) {
320+
if (getHost() != null && !DatastoreUtils.isDefaultHost(getHost())) {
321321
channelProviderBuilder.setEndpoint(getHost());
322322
}
323323
if (traceUtil.getChannelConfigurator() != null) {

‎java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreUtils.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
public class DatastoreUtils {
3030

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

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

43+
/**
44+
* Returns {@code true} if {@code host} matches the default Datastore service host for either HTTP
45+
* ({@code https://datastore.googleapis.com}) or gRPC ({@code datastore.googleapis.com:443}).
46+
*
47+
* <p>Recognizing both transport defaults allows transport switches via {@code toBuilder()} and
48+
* custom universe-domain endpoint resolution to distinguish default hosts from explicit custom
49+
* host overrides.
50+
*/
51+
public static boolean isDefaultHost(String host) {
52+
if (Strings.isNullOrEmpty(host)) {
53+
return false;
54+
}
55+
// removeScheme() lowercases the input before stripping http:// or https://.
56+
String normalized = removeScheme(host);
57+
return DEFAULT_DATASTORE_HOSTNAME.equals(normalized)
58+
|| DEFAULT_GRPC_DATASTORE_ENDPOINT.equals(normalized);
59+
}
60+
4061
public static boolean isLocalHost(String host) {
4162
if (Strings.isNullOrEmpty(host)) {
4263
return false;

‎java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/spi/v1/GrpcDatastoreRpc.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import static com.google.cloud.datastore.DatastoreUtils.isDefaultHost;
1920
import static com.google.cloud.datastore.DatastoreUtils.isEmulator;
2021
import static com.google.cloud.datastore.DatastoreUtils.removeScheme;
2122
import static com.google.cloud.datastore.spi.v1.RpcUtils.retrySettingSetter;
@@ -205,8 +206,7 @@ ClientContext getClientContext(DatastoreOptions datastoreOptions) throws IOExcep
205206
// and validates credentials against it.
206207
settingsBuilder.setUniverseDomain(datastoreOptions.getUniverseDomain());
207208
// Preserve custom host overrides; default hosts are resolved by GAX from the universe domain.
208-
if (datastoreOptions.getHost() != null
209-
&& !DatastoreSettings.getDefaultEndpoint().equals(datastoreOptions.getHost())) {
209+
if (datastoreOptions.getHost() != null && !isDefaultHost(datastoreOptions.getHost())) {
210210
settingsBuilder.setEndpoint(datastoreOptions.getHost());
211211
}
212212
return ClientContext.create(settingsBuilder.build());

‎java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/spi/v1/HttpDatastoreRpc.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import static com.google.cloud.datastore.DatastoreUtils.isDefaultHost;
1920
import static com.google.cloud.datastore.DatastoreUtils.isLocalHost;
2021
import static com.google.cloud.datastore.DatastoreUtils.removeScheme;
2122

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

117118
if (isLocalHost(normalizedHost)) {
118119
clientBuilder = clientBuilder.localHost(removeScheme(normalizedHost));
119-
} else if (!removeScheme(normalizedHost)
120-
.equals(removeScheme(com.google.datastore.v1.client.DatastoreFactory.DEFAULT_HOST))
121-
&& !normalizedHost.isEmpty()) {
120+
} else if (!isDefaultHost(normalizedHost) && !normalizedHost.isEmpty()) {
121+
// Only treat non-default hosts as custom overrides; both HTTP and gRPC default hosts fall
122+
// through to universe-domain host resolution below.
122123
// DatastoreFactory.create(...) automatically builds the valid endpoint URL using the host,
123124
// so we only need to set the host without any suffix.
124125
clientBuilder = clientBuilder.host(removeScheme(normalizedHost));

‎java-datastore/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreOptionsTest.java‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.api.gax.rpc.TransportChannel;
3333
import com.google.api.gax.rpc.TransportChannelProvider;
3434
import com.google.cloud.NoCredentials;
35+
import com.google.cloud.TransportOptions;
3536
import com.google.cloud.datastore.spi.DatastoreRpcFactory;
3637
import com.google.cloud.datastore.spi.v1.DatastoreRpc;
3738
import com.google.cloud.datastore.v1.DatastoreSettings;
@@ -438,4 +439,63 @@ public void testGrpcDefaultChannelProviderDefersEndpointToGax() {
438439
assertFalse(customChannelProvider.needsEndpoint());
439440
assertEquals(customHost, customChannelProvider.getEndpoint());
440441
}
442+
443+
@Test
444+
public void testToBuilder_grpcToHttpResetsDefaultHost() {
445+
DatastoreOptions grpcDefault =
446+
DatastoreOptions.newBuilder()
447+
.setProjectId(PROJECT_ID)
448+
.setCredentials(NoCredentials.getInstance())
449+
.build();
450+
assertThat(grpcDefault.getHost()).isEqualTo(DatastoreUtils.DEFAULT_GRPC_DATASTORE_ENDPOINT);
451+
452+
DatastoreOptions switchedToHttp =
453+
grpcDefault.toBuilder()
454+
.setTransportOptions(HttpTransportOptions.newBuilder().build())
455+
.setUniverseDomain("custom.example.com")
456+
.build();
457+
assertThat(switchedToHttp.getTransportOptions()).isInstanceOf(HttpTransportOptions.class);
458+
assertThat(switchedToHttp.getHost()).isEqualTo(DatastoreFactory.DEFAULT_HOST);
459+
assertThat(switchedToHttp.getUniverseDomain()).isEqualTo("custom.example.com");
460+
}
461+
462+
@Test
463+
public void testToBuilder_httpToGrpcResetsDefaultHost() {
464+
DatastoreOptions httpDefault =
465+
DatastoreOptions.newBuilder()
466+
.setProjectId(PROJECT_ID)
467+
.setCredentials(NoCredentials.getInstance())
468+
.setTransportOptions(HttpTransportOptions.newBuilder().build())
469+
.build();
470+
assertThat(httpDefault.getHost()).isEqualTo(DatastoreFactory.DEFAULT_HOST);
471+
472+
DatastoreOptions switchedToGrpc =
473+
httpDefault.toBuilder()
474+
.setTransportOptions((TransportOptions) GrpcTransportOptions.newBuilder().build())
475+
.setUniverseDomain("custom.example.com")
476+
.build();
477+
assertThat(switchedToGrpc.getTransportOptions()).isInstanceOf(GrpcTransportOptions.class);
478+
assertThat(switchedToGrpc.getHost()).isEqualTo(DatastoreUtils.DEFAULT_GRPC_DATASTORE_ENDPOINT);
479+
InstantiatingGrpcChannelProvider channelProvider =
480+
(InstantiatingGrpcChannelProvider) switchedToGrpc.getTransportChannelProvider();
481+
assertTrue(channelProvider.needsEndpoint());
482+
assertThat(channelProvider.getEndpoint()).isNull();
483+
}
484+
485+
@Test
486+
public void testToBuilder_customHostPreservedAcrossTransportSwitch() {
487+
String customHost = "custom-datastore.example.com:443";
488+
DatastoreOptions grpcCustom =
489+
DatastoreOptions.newBuilder()
490+
.setProjectId(PROJECT_ID)
491+
.setCredentials(NoCredentials.getInstance())
492+
.setHost(customHost)
493+
.build();
494+
495+
DatastoreOptions switchedToHttp =
496+
grpcCustom.toBuilder()
497+
.setTransportOptions(HttpTransportOptions.newBuilder().build())
498+
.build();
499+
assertThat(switchedToHttp.getHost()).isEqualTo(customHost);
500+
}
441501
}

‎java-datastore/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreUtilsTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.datastore;
1818

19+
import static com.google.cloud.datastore.DatastoreUtils.isDefaultHost;
1920
import static com.google.cloud.datastore.DatastoreUtils.isEmulator;
2021
import static com.google.cloud.datastore.DatastoreUtils.isLocalHost;
2122
import static com.google.cloud.datastore.DatastoreUtils.removeScheme;
@@ -39,6 +40,20 @@ public void testIsEmulator() {
3940
assertThat(isEmulator(noCredsRemoteHost)).isTrue();
4041
}
4142

43+
@Test
44+
public void testIsDefaultHost() {
45+
assertThat(isDefaultHost(null)).isFalse();
46+
assertThat(isDefaultHost("")).isFalse();
47+
assertThat(isDefaultHost(DatastoreUtils.DEFAULT_GRPC_DATASTORE_ENDPOINT)).isTrue();
48+
assertThat(isDefaultHost(DatastoreFactory.DEFAULT_HOST)).isTrue();
49+
assertThat(isDefaultHost("https://datastore.googleapis.com:443")).isTrue();
50+
assertThat(isDefaultHost("DATASTORE.GOOGLEAPIS.COM:443")).isTrue();
51+
assertThat(isDefaultHost("datastore.googleapis.com")).isTrue();
52+
assertThat(isDefaultHost("datastore.googleapis.com:8443")).isFalse();
53+
assertThat(isDefaultHost("custom-datastore.example.com:443")).isFalse();
54+
assertThat(isDefaultHost("http://localhost:8081")).isFalse();
55+
}
56+
4257
@Test
4358
public void testIsLocalHost() {
4459
assertThat(isLocalHost(null)).isFalse();

‎java-datastore/google-cloud-datastore/src/test/java/com/google/cloud/datastore/spi/v1/GrpcDatastoreRpcTest.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
import com.google.api.gax.grpc.GrpcTransportChannel;
2222
import com.google.api.gax.rpc.ClientContext;
2323
import com.google.auth.Credentials;
24+
import com.google.cloud.TransportOptions;
2425
import com.google.cloud.datastore.DatastoreOptions;
2526
import com.google.cloud.datastore.v1.stub.DatastoreStubSettings;
27+
import com.google.cloud.grpc.GrpcTransportOptions;
28+
import com.google.cloud.http.HttpTransportOptions;
2629
import java.net.URI;
2730
import java.util.Collections;
2831
import java.util.List;
@@ -124,4 +127,32 @@ public void testCustomHostOverridesDefaultEndpointWithUniverseDomain() throws Ex
124127
rpc.close();
125128
}
126129
}
130+
131+
@Test
132+
public void testToBuilder_httpToGrpcResolvesUniverseDomain() throws Exception {
133+
String customUniverseDomain = "custom.example.com";
134+
DatastoreOptions httpDefault =
135+
DatastoreOptions.newBuilder()
136+
.setProjectId(PROJECT_ID)
137+
.setCredentials(new FakeUniverseCredentials(customUniverseDomain))
138+
.setTransportOptions(HttpTransportOptions.newBuilder().build())
139+
.build();
140+
141+
DatastoreOptions switchedToGrpc =
142+
httpDefault.toBuilder()
143+
.setTransportOptions((TransportOptions) GrpcTransportOptions.newBuilder().build())
144+
.setUniverseDomain(customUniverseDomain)
145+
.build();
146+
147+
GrpcDatastoreRpc rpc = new GrpcDatastoreRpc(switchedToGrpc);
148+
try {
149+
ClientContext clientContext = rpc.getClientContext();
150+
assertEquals(customUniverseDomain, clientContext.getUniverseDomain());
151+
GrpcTransportChannel transportChannel =
152+
(GrpcTransportChannel) clientContext.getTransportChannel();
153+
assertEquals("datastore.custom.example.com:443", transportChannel.getChannel().authority());
154+
} finally {
155+
rpc.close();
156+
}
157+
}
127158
}

‎java-datastore/google-cloud-datastore/src/test/java/com/google/cloud/datastore/spi/v1/HttpDatastoreRpcTest.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.cloud.NoCredentials;
2929
import com.google.cloud.datastore.DatastoreException;
3030
import com.google.cloud.datastore.DatastoreOptions;
31+
import com.google.cloud.datastore.DatastoreUtils;
3132
import com.google.cloud.http.HttpTransportOptions;
3233
import com.google.datastore.v1.LookupRequest;
3334
import com.google.rpc.Code;
@@ -194,4 +195,41 @@ public void testValidateUniverseDomain_apiKeyCredentialsSkipsValidation() {
194195
HttpDatastoreRpc rpc = new HttpDatastoreRpc(options, null);
195196
rpc.validateUniverseDomain();
196197
}
198+
199+
@Test
200+
public void testToBuilder_grpcToHttpResolvesUniverseDomain() {
201+
DatastoreOptions grpcDefault =
202+
DatastoreOptions.newBuilder()
203+
.setProjectId(PROJECT_ID)
204+
.setCredentials(new FakeUniverseCredentials(CUSTOM_UNIVERSE_DOMAIN))
205+
.build();
206+
207+
DatastoreOptions switchedToHttp =
208+
grpcDefault.toBuilder()
209+
.setTransportOptions(HttpTransportOptions.newBuilder().build())
210+
.setUniverseDomain(CUSTOM_UNIVERSE_DOMAIN)
211+
.build();
212+
213+
HttpRequestInitializer initializer = request -> {};
214+
com.google.datastore.v1.client.DatastoreOptions clientOptions =
215+
HttpDatastoreRpc.createClientOptionsBuilder(
216+
switchedToHttp, initializer, new MockHttpTransport(), CUSTOM_UNIVERSE_DOMAIN)
217+
.build();
218+
assertEquals("datastore." + CUSTOM_UNIVERSE_DOMAIN, clientOptions.getHost());
219+
}
220+
221+
@Test
222+
public void testCreateClientOptionsBuilder_grpcDefaultEndpointResolvesUniverseDomain() {
223+
DatastoreOptions options =
224+
httpOptionsBuilder()
225+
.setHost(DatastoreUtils.DEFAULT_GRPC_DATASTORE_ENDPOINT)
226+
.setUniverseDomain(CUSTOM_UNIVERSE_DOMAIN)
227+
.build();
228+
HttpRequestInitializer initializer = request -> {};
229+
com.google.datastore.v1.client.DatastoreOptions clientOptions =
230+
HttpDatastoreRpc.createClientOptionsBuilder(
231+
options, initializer, new MockHttpTransport(), CUSTOM_UNIVERSE_DOMAIN)
232+
.build();
233+
assertEquals("datastore." + CUSTOM_UNIVERSE_DOMAIN, clientOptions.getHost());
234+
}
197235
}

0 commit comments

Comments
 (0)