Skip to content

Commit 4054bc6

Browse files
authored
feat: add an option to enable DirectPath xDS (#1643)
1 parent 47851e8 commit 4054bc6

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java

+30-4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
108108
@Nullable private final Credentials credentials;
109109
@Nullable private final ChannelPrimer channelPrimer;
110110
@Nullable private final Boolean attemptDirectPath;
111+
@Nullable private final Boolean attemptDirectPathXds;
111112
@Nullable private final Boolean allowNonDefaultServiceAccount;
112113
@VisibleForTesting final ImmutableMap<String, ?> directPathServiceConfig;
113114
@Nullable private final MtlsProvider mtlsProvider;
@@ -133,6 +134,7 @@ private InstantiatingGrpcChannelProvider(Builder builder) {
133134
this.credentials = builder.credentials;
134135
this.channelPrimer = builder.channelPrimer;
135136
this.attemptDirectPath = builder.attemptDirectPath;
137+
this.attemptDirectPathXds = builder.attemptDirectPathXds;
136138
this.allowNonDefaultServiceAccount = builder.allowNonDefaultServiceAccount;
137139
this.directPathServiceConfig =
138140
builder.directPathServiceConfig == null
@@ -249,6 +251,21 @@ private boolean isDirectPathEnabled() {
249251
return false;
250252
}
251253

254+
@VisibleForTesting
255+
boolean isDirectPathXdsEnabled() {
256+
// Method 1: Enable DirectPath xDS by option.
257+
if (Boolean.TRUE.equals(attemptDirectPathXds)) {
258+
return true;
259+
}
260+
// Method 2: Enable DirectPath xDS by env.
261+
String directPathXdsEnv = envProvider.getenv(DIRECT_PATH_ENV_ENABLE_XDS);
262+
boolean isDirectPathXdsEnv = Boolean.parseBoolean(directPathXdsEnv);
263+
if (isDirectPathXdsEnv) {
264+
return true;
265+
}
266+
return false;
267+
}
268+
252269
private boolean isNonDefaultServiceAccountAllowed() {
253270
if (allowNonDefaultServiceAccount != null && allowNonDefaultServiceAccount) {
254271
return true;
@@ -304,13 +321,13 @@ private ManagedChannel createSingleChannel() throws IOException {
304321
ManagedChannelBuilder<?> builder;
305322

306323
// Check DirectPath traffic.
307-
boolean isDirectPathXdsEnabled = false;
324+
boolean useDirectPathXds = false;
308325
if (isDirectPathEnabled() && isNonDefaultServiceAccountAllowed() && isOnComputeEngine()) {
309326
CallCredentials callCreds = MoreCallCredentials.from(credentials);
310327
ChannelCredentials channelCreds =
311328
GoogleDefaultChannelCredentials.newBuilder().callCredentials(callCreds).build();
312-
isDirectPathXdsEnabled = Boolean.parseBoolean(envProvider.getenv(DIRECT_PATH_ENV_ENABLE_XDS));
313-
if (isDirectPathXdsEnabled) {
329+
useDirectPathXds = isDirectPathXdsEnabled();
330+
if (useDirectPathXds) {
314331
// google-c2p: CloudToProd(C2P) Directpath. This scheme is defined in
315332
// io.grpc.googleapis.GoogleCloudToProdNameResolverProvider.
316333
// This resolver target must not have a port number.
@@ -337,7 +354,7 @@ private ManagedChannel createSingleChannel() throws IOException {
337354
}
338355
}
339356
// google-c2p resolver requires service config lookup
340-
if (!isDirectPathXdsEnabled) {
357+
if (!useDirectPathXds) {
341358
// See https://github.com/googleapis/gapic-generator/issues/2816
342359
builder.disableServiceConfigLookUp();
343360
}
@@ -435,6 +452,7 @@ public static final class Builder {
435452
@Nullable private ChannelPrimer channelPrimer;
436453
private ChannelPoolSettings channelPoolSettings;
437454
@Nullable private Boolean attemptDirectPath;
455+
@Nullable private Boolean attemptDirectPathXds;
438456
@Nullable private Boolean allowNonDefaultServiceAccount;
439457
@Nullable private ImmutableMap<String, ?> directPathServiceConfig;
440458

@@ -461,6 +479,7 @@ private Builder(InstantiatingGrpcChannelProvider provider) {
461479
this.channelPrimer = provider.channelPrimer;
462480
this.channelPoolSettings = provider.channelPoolSettings;
463481
this.attemptDirectPath = provider.attemptDirectPath;
482+
this.attemptDirectPathXds = provider.attemptDirectPathXds;
464483
this.allowNonDefaultServiceAccount = provider.allowNonDefaultServiceAccount;
465484
this.directPathServiceConfig = provider.directPathServiceConfig;
466485
this.mtlsProvider = provider.mtlsProvider;
@@ -669,6 +688,13 @@ public Builder setAllowNonDefaultServiceAccount(boolean allowNonDefaultServiceAc
669688
return this;
670689
}
671690

691+
/** Use DirectPath xDS. Only valid if DirectPath is attempted. */
692+
@InternalApi("For internal use by google-cloud-java clients only")
693+
public Builder setAttemptDirectPathXds() {
694+
this.attemptDirectPathXds = true;
695+
return this;
696+
}
697+
672698
/**
673699
* Sets a service config for direct path. If direct path is not enabled, the provided service
674700
* config will be ignored.

gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProviderTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,25 @@ public void testWithGCECredentials() throws IOException {
273273
provider.getTransportChannel().shutdownNow();
274274
}
275275

276+
@Test
277+
public void testDirectPathXdsDisableByDefault() throws IOException {
278+
InstantiatingGrpcChannelProvider provider =
279+
InstantiatingGrpcChannelProvider.newBuilder().setAttemptDirectPath(true).build();
280+
281+
assertThat(provider.isDirectPathXdsEnabled()).isFalse();
282+
}
283+
284+
@Test
285+
public void testDirectPathXdsEnabled() throws IOException {
286+
InstantiatingGrpcChannelProvider provider =
287+
InstantiatingGrpcChannelProvider.newBuilder()
288+
.setAttemptDirectPath(true)
289+
.setAttemptDirectPathXds()
290+
.build();
291+
292+
assertThat(provider.isDirectPathXdsEnabled()).isTrue();
293+
}
294+
276295
@Test
277296
public void testWithNonGCECredentials() throws IOException {
278297
ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1);

0 commit comments

Comments
 (0)