Skip to content
Open
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 @@ -82,6 +82,10 @@ public class DatastoreOptions extends ServiceOptions<Datastore, DatastoreOptions
@Deprecated public static final int MAX_CHANNEL_COUNT = 10;

private transient TransportChannelProvider channelProvider = null;
// Tracks whether channelProvider was explicitly supplied by the caller vs. synthesized in the
// constructor, so toBuilder() can safely rebuild auto-generated providers when settings change.
// Marked transient to match channelProvider so deserialization resets both together.
private final transient boolean hasCustomChannelProvider;

private final String namespace;
private final String databaseId;
Expand Down Expand Up @@ -135,6 +139,7 @@ public static class Builder extends ServiceOptions.Builder<Datastore, DatastoreO
private String databaseId;
private ImmutableList<String> requestTags = ImmutableList.of();
private TransportChannelProvider channelProvider = null;
private boolean hasCustomChannelProvider = false;
private String host;

@Nonnull
Expand All @@ -152,6 +157,7 @@ private Builder(DatastoreOptions options) {
this.requestTags = options.requestTags;
this.openTelemetryOptions = options.openTelemetryOptions;
this.channelProvider = options.channelProvider;
this.hasCustomChannelProvider = options.hasCustomChannelProvider;
this.host = options.getHost();
this.transportOptions = options.getTransportOptions();
}
Expand All @@ -172,7 +178,14 @@ public Builder setTransportOptions(@Nonnull TransportOptions transportOptions) {
throw new IllegalArgumentException(
"Only http and grpc transport are allowed for " + API_SHORT_NAME + ".");
}
boolean transportChanged =
!this.transportOptions.getClass().equals(transportOptions.getClass());
this.transportOptions = transportOptions;
if (transportChanged && !this.hasCustomChannelProvider) {
// Clear any auto-generated gRPC channel provider when switching transports so HTTP does not
// inherit a stale provider and gRPC builds a fresh one.
this.channelProvider = null;
}
Comment thread
jinseopkim0 marked this conversation as resolved.
Resolved
return super.setTransportOptions(transportOptions);
}

Expand All @@ -192,9 +205,28 @@ public Builder setTransportOptions(GrpcTransportOptions transportOptions) {
@Override
public Builder setHost(String host) {
this.host = host;
if (!this.hasCustomChannelProvider) {
// Clear auto-generated channel provider so the constructor rebuilds it with the new host
// (or defers the endpoint to GAX when reset to null/default).
this.channelProvider = null;
}
return super.setHost(host);
}
Comment thread
jinseopkim0 marked this conversation as resolved.
Resolved

/**
* Sets the universe domain and resets any auto-generated channel provider so endpoint
* resolution reflects the updated configuration.
*/
@Override
public Builder setUniverseDomain(String universeDomain) {
if (!this.hasCustomChannelProvider) {
// Rebuild auto-generated channel provider so stale endpoints are not reused across
// toBuilder() clones.
this.channelProvider = null;
}
return super.setUniverseDomain(universeDomain);
}
Comment thread
jinseopkim0 marked this conversation as resolved.
Resolved

/**
* Sets the {@link TransportChannelProvider} to use with this Datastore client.
*
Expand All @@ -209,6 +241,9 @@ public Builder setHost(String host) {
@BetaApi
public Builder setChannelProvider(TransportChannelProvider channelProvider) {
this.channelProvider = channelProvider;
// Setting a non-null provider marks it as caller-owned so later host/universe changes do not
// discard it; passing null reverts to auto-generated provider behavior.
this.hasCustomChannelProvider = channelProvider != null;
return this;
}

Expand Down Expand Up @@ -273,6 +308,11 @@ public Builder setRequestTags(List<String> requestTags) {
@Nonnull
public Builder setOpenTelemetryOptions(
@Nonnull DatastoreOpenTelemetryOptions openTelemetryOptions) {
if (!this.hasCustomChannelProvider) {
// Rebuild auto-generated channel provider so the updated telemetry channel configurator is
// attached.
this.channelProvider = null;
}
this.openTelemetryOptions = openTelemetryOptions;
return this;
}
Comment thread
jinseopkim0 marked this conversation as resolved.
Resolved
Expand All @@ -281,6 +321,8 @@ public Builder setOpenTelemetryOptions(
private DatastoreOptions(Builder builder) {
super(DatastoreFactory.class, DatastoreRpcFactory.class, builder, new DatastoreDefaults());

this.hasCustomChannelProvider = builder.hasCustomChannelProvider;

this.openTelemetryOptions =
builder.openTelemetryOptions != null
? builder.openTelemetryOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,78 @@ public void testGrpcDefaultChannelProviderDefersEndpointToGax() {
assertFalse(customChannelProvider.needsEndpoint());
assertEquals(customHost, customChannelProvider.getEndpoint());
}

@Test
public void testToBuilder_customGrpcHostRebuildsSynthesizedChannelProvider() {
DatastoreOptions initial =
DatastoreOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setCredentials(NoCredentials.getInstance())
.setHost("old-datastore.example.com:443")
.build();
assertEquals(
"old-datastore.example.com:443",
((InstantiatingGrpcChannelProvider) initial.getTransportChannelProvider()).getEndpoint());

DatastoreOptions updatedHost =
initial.toBuilder().setHost("new-datastore.example.com:443").build();
assertEquals(
"new-datastore.example.com:443",
((InstantiatingGrpcChannelProvider) updatedHost.getTransportChannelProvider())
.getEndpoint());

DatastoreOptions clearedToNull =
initial.toBuilder().setHost(null).setUniverseDomain("custom.example.com").build();
InstantiatingGrpcChannelProvider clearedNullProvider =
(InstantiatingGrpcChannelProvider) clearedToNull.getTransportChannelProvider();
assertTrue(clearedNullProvider.needsEndpoint());
assertThat(clearedNullProvider.getEndpoint()).isNull();

DatastoreOptions clearedToDefault =
initial.toBuilder()
.setHost(DatastoreSettings.getDefaultEndpoint())
.setUniverseDomain("custom.example.com")
.build();
InstantiatingGrpcChannelProvider clearedDefaultProvider =
(InstantiatingGrpcChannelProvider) clearedToDefault.getTransportChannelProvider();
assertTrue(clearedDefaultProvider.needsEndpoint());
assertThat(clearedDefaultProvider.getEndpoint()).isNull();
}

@Test
public void testToBuilder_explicitChannelProviderPreservedAndClearable() {
TransportChannel transportChannel = EasyMock.createMock(TransportChannel.class);
EasyMock.expect(transportChannel.getTransportName())
.andReturn(GrpcTransportChannel.getGrpcTransportName())
.anyTimes();
EasyMock.replay(transportChannel);
TransportChannelProvider explicitProvider =
FixedTransportChannelProvider.create(transportChannel);

DatastoreOptions withExplicit =
DatastoreOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setCredentials(NoCredentials.getInstance())
.setChannelProvider(explicitProvider)
.build();

DatastoreOptions clonedWithHost =
withExplicit.toBuilder()
.setHost("new-datastore.example.com:443")
.setUniverseDomain("custom.example.com")
.build();
assertEquals(explicitProvider, clonedWithHost.getTransportChannelProvider());

DatastoreOptions clearedProvider =
withExplicit.toBuilder()
.setChannelProvider(null)
.setHost("new-datastore.example.com:443")
.build();
assertThat(clearedProvider.getTransportChannelProvider())
.isInstanceOf(InstantiatingGrpcChannelProvider.class);
assertEquals(
"new-datastore.example.com:443",
((InstantiatingGrpcChannelProvider) clearedProvider.getTransportChannelProvider())
.getEndpoint());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,60 @@ public void testCustomHostOverridesDefaultEndpointWithUniverseDomain() throws Ex
rpc.close();
}
}

@Test
public void testToBuilder_customHostClearedResolvesUniverseDomain() throws Exception {
DatastoreOptions initial =
DatastoreOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setCredentials(new FakeUniverseCredentials(CUSTOM_UNIVERSE_DOMAIN))
.setHost("10.10.10.10:443")
.build();

DatastoreOptions updated =
initial.toBuilder().setHost(null).setUniverseDomain(CUSTOM_UNIVERSE_DOMAIN).build();

GrpcDatastoreRpc rpc = new GrpcDatastoreRpc(updated);
try {
ClientContext clientContext = rpc.getClientContext();
assertEquals(CUSTOM_UNIVERSE_DOMAIN, clientContext.getUniverseDomain());
GrpcTransportChannel transportChannel =
(GrpcTransportChannel) clientContext.getTransportChannel();
assertEquals("datastore.s3nsapis.fr:443", transportChannel.getChannel().authority());

DatastoreStubSettings stubSettings =
GrpcDatastoreRpc.createDatastoreStubSettings(updated, clientContext);
assertEquals("datastore.s3nsapis.fr:443", stubSettings.getEndpoint());
} finally {
rpc.close();
}
}

@Test
public void testToBuilder_customHostUpdatedRoutesToNewHost() throws Exception {
DatastoreOptions initial =
DatastoreOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setCredentials(new FakeUniverseCredentials(CUSTOM_UNIVERSE_DOMAIN))
.setUniverseDomain(CUSTOM_UNIVERSE_DOMAIN)
.setHost("10.10.10.10:443")
.build();

String newHost = "10.10.10.11:8443";
DatastoreOptions updated = initial.toBuilder().setHost(newHost).build();

GrpcDatastoreRpc rpc = new GrpcDatastoreRpc(updated);
try {
ClientContext clientContext = rpc.getClientContext();
GrpcTransportChannel transportChannel =
(GrpcTransportChannel) clientContext.getTransportChannel();
assertEquals(newHost, transportChannel.getChannel().authority());

DatastoreStubSettings stubSettings =
GrpcDatastoreRpc.createDatastoreStubSettings(updated, clientContext);
assertEquals(newHost, stubSettings.getEndpoint());
} finally {
rpc.close();
}
}
}
Loading