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,9 @@ public class DatastoreOptions extends ServiceOptions<Datastore, DatastoreOptions
@Deprecated public static final int MAX_CHANNEL_COUNT = 10;

private transient TransportChannelProvider channelProvider = null;
// Tracks only caller-supplied channelProvider so toBuilder() does not carry over an
// auto-generated provider whose baked-in endpoint or telemetry configurator may become stale.
private transient TransportChannelProvider customChannelProvider = null;

private final String namespace;
private final String databaseId;
Expand Down Expand Up @@ -151,7 +154,7 @@ private Builder(DatastoreOptions options) {
this.databaseId = options.databaseId;
this.requestTags = options.requestTags;
this.openTelemetryOptions = options.openTelemetryOptions;
this.channelProvider = options.channelProvider;
this.channelProvider = options.customChannelProvider;
this.host = options.getHost();
this.transportOptions = options.getTransportOptions();
}
Expand Down Expand Up @@ -333,6 +336,7 @@ private DatastoreOptions(Builder builder) {
"Only GRPC channels are allowed for " + API_SHORT_NAME + ".");
}
this.channelProvider = builder.channelProvider;
this.customChannelProvider = builder.channelProvider;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,78 @@ public void testToBuilder_customHostPreservedAcrossTransportSwitch() {
.build();
assertThat(switchedToHttp.getHost()).isEqualTo(customHost);
}

@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(DatastoreUtils.DEFAULT_GRPC_DATASTORE_ENDPOINT)
.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 @@ -155,4 +155,62 @@ public void testToBuilder_httpToGrpcResolvesUniverseDomain() throws Exception {
rpc.close();
}
}

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

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

GrpcDatastoreRpc rpc = new GrpcDatastoreRpc(updated);
try {
ClientContext clientContext = rpc.getClientContext();
assertEquals(customUniverseDomain, clientContext.getUniverseDomain());
GrpcTransportChannel transportChannel =
(GrpcTransportChannel) clientContext.getTransportChannel();
assertEquals("datastore.custom.example.com:443", transportChannel.getChannel().authority());

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

@Test
public void testToBuilder_customHostUpdatedRoutesToNewHost() throws Exception {
String customUniverseDomain = "custom.example.com";
DatastoreOptions initial =
DatastoreOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setCredentials(new FakeUniverseCredentials(customUniverseDomain))
.setUniverseDomain(customUniverseDomain)
.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