Skip to content

Commit 7a26115

Browse files
fix: improve warnings for Direct Path xDS set via env (#3019)
Fixes #2427 ### Context If `GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS` is set in the environment aiming to enable Direct Path xDS in another client, we want to just warn the user that _if this is intended_, it can be a misconfiguration and that the gRPCLB Direct Path setting should be enabled as well. ### Approach As said in the context, we will _warn_ the user that it _could_ be a misconfiguration _if_ the env var setting was meant for the client in question. Other warn cases remain the same. ### Coverage Due to the fact that part of the method in question is tested via a special env var test (not detected by SonarCloud), it will show as uncovered. See this screenshot for coverage of all tests combined ![image](https://github.com/googleapis/sdk-platform-java/assets/22083784/af24e721-6495-4030-8804-c013b14679d7) --------- Co-authored-by: Lawrence Qiu <[email protected]>
1 parent 4bbb8ac commit 7a26115

File tree

4 files changed

+105
-32
lines changed

4 files changed

+105
-32
lines changed

.github/workflows/ci.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
# Set the Env Var for this step only
3131
env:
3232
GOOGLE_CLOUD_UNIVERSE_DOMAIN: random.com
33+
GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS: true
3334
- run: bazelisk version
3435
- name: Install Maven modules
3536
run: |
@@ -80,6 +81,7 @@ jobs:
8081
# Set the Env Var for this step only
8182
env:
8283
GOOGLE_CLOUD_UNIVERSE_DOMAIN: random.com
84+
GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS: true
8385
- run: bazelisk version
8486
- name: Install Maven modules
8587
run: |
@@ -130,6 +132,7 @@ jobs:
130132
# Set the Env Var for this step only
131133
env:
132134
GOOGLE_CLOUD_UNIVERSE_DOMAIN: random.com
135+
GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS: true
133136

134137
build-java8-gapic-generator-java:
135138
name: "build(8) for gapic-generator-java"

gax-java/gax-grpc/pom.xml

+24
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,30 @@
128128
</execution>
129129
</executions>
130130
</plugin>
131+
<plugin>
132+
<groupId>org.apache.maven.plugins</groupId>
133+
<artifactId>maven-surefire-plugin</artifactId>
134+
<configuration>
135+
<!-- These tests require an Env Var to be set. Use -PenvVarTest to ONLY run these tests -->
136+
<test>!InstantiatingGrpcChannelProviderTest#testLogDirectPathMisconfig_AttemptDirectPathNotSetAndAttemptDirectPathXdsSetViaEnv_warns</test>
137+
</configuration>
138+
</plugin>
131139
</plugins>
132140
</build>
141+
<profiles>
142+
<profile>
143+
<id>envVarTest</id>
144+
<build>
145+
<plugins>
146+
<plugin>
147+
<groupId>org.apache.maven.plugins</groupId>
148+
<artifactId>maven-surefire-plugin</artifactId>
149+
<configuration>
150+
<test>InstantiatingGrpcChannelProviderTest#testLogDirectPathMisconfig_AttemptDirectPathNotSetAndAttemptDirectPathXdsSetViaEnv_warns</test>
151+
</configuration>
152+
</plugin>
153+
</plugins>
154+
</build>
155+
</profile>
156+
</profiles>
133157
</project>

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

+43-19
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
9292
static final Logger LOG = Logger.getLogger(InstantiatingGrpcChannelProvider.class.getName());
9393

9494
static final String DIRECT_PATH_ENV_DISABLE_DIRECT_PATH = "GOOGLE_CLOUD_DISABLE_DIRECT_PATH";
95-
private static final String DIRECT_PATH_ENV_ENABLE_XDS = "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS";
95+
96+
@VisibleForTesting
97+
static final String DIRECT_PATH_ENV_ENABLE_XDS = "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS";
98+
9699
static final long DIRECT_PATH_KEEP_ALIVE_TIME_SECONDS = 3600;
97100
static final long DIRECT_PATH_KEEP_ALIVE_TIMEOUT_SECONDS = 20;
98101
static final String GCE_PRODUCTION_NAME_PRIOR_2016 = "Google";
@@ -273,42 +276,63 @@ private boolean isDirectPathEnabled() {
273276
return false;
274277
}
275278

279+
private boolean isDirectPathXdsEnabledViaBuilderOption() {
280+
return Boolean.TRUE.equals(attemptDirectPathXds);
281+
}
282+
283+
private boolean isDirectPathXdsEnabledViaEnv() {
284+
String directPathXdsEnv = envProvider.getenv(DIRECT_PATH_ENV_ENABLE_XDS);
285+
return Boolean.parseBoolean(directPathXdsEnv);
286+
}
287+
288+
/**
289+
* This method tells if Direct Path xDS was enabled. There are two ways of enabling it: via
290+
* environment variable (by setting GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS=true) or when building
291+
* this channel provider (via the {@link Builder#setAttemptDirectPathXds()} method).
292+
*
293+
* @return true if Direct Path xDS was either enabled via env var or via builder option
294+
*/
276295
@InternalApi
277296
public boolean isDirectPathXdsEnabled() {
278-
// Method 1: Enable DirectPath xDS by option.
279-
if (Boolean.TRUE.equals(attemptDirectPathXds)) {
280-
return true;
281-
}
282-
// Method 2: Enable DirectPath xDS by env.
283-
String directPathXdsEnv = envProvider.getenv(DIRECT_PATH_ENV_ENABLE_XDS);
284-
boolean isDirectPathXdsEnv = Boolean.parseBoolean(directPathXdsEnv);
285-
if (isDirectPathXdsEnv) {
286-
return true;
287-
}
288-
return false;
297+
return isDirectPathXdsEnabledViaEnv() || isDirectPathXdsEnabledViaBuilderOption();
289298
}
290299

291300
// This method should be called once per client initialization, hence can not be called in the
292301
// builder or createSingleChannel, only in getTransportChannel which creates the first channel
293302
// for a client.
294303
private void logDirectPathMisconfig() {
295304
if (isDirectPathXdsEnabled()) {
296-
// Case 1: does not enable DirectPath
297305
if (!isDirectPathEnabled()) {
298-
LOG.log(
299-
Level.WARNING,
300-
"DirectPath is misconfigured. Please set the attemptDirectPath option along with the"
301-
+ " attemptDirectPathXds option.");
306+
// This misconfiguration occurs when Direct Path xDS is enabled, but Direct Path is not
307+
// Direct Path xDS can be enabled two ways: via environment variable or via builder.
308+
// Case 1: Direct Path is only enabled via xDS env var. We will _warn_ the user that this is
309+
// a misconfiguration if they intended to set the env var.
310+
if (isDirectPathXdsEnabledViaEnv()) {
311+
LOG.log(
312+
Level.WARNING,
313+
"Env var "
314+
+ DIRECT_PATH_ENV_ENABLE_XDS
315+
+ " was found and set to TRUE, but DirectPath was not enabled for this client. If this is intended for "
316+
+ "this client, please note that this is a misconfiguration and set the attemptDirectPath option as well.");
317+
}
318+
// Case 2: Direct Path xDS was enabled via Builder. Direct Path Traffic Director must be set
319+
// (enabled with `setAttemptDirectPath(true)`) along with xDS.
320+
// Here we warn the user about this.
321+
else if (isDirectPathXdsEnabledViaBuilderOption()) {
322+
LOG.log(
323+
Level.WARNING,
324+
"DirectPath is misconfigured. The DirectPath XDS option was set, but the attemptDirectPath option was not. Please set both the attemptDirectPath and attemptDirectPathXds options.");
325+
}
302326
} else {
303-
// Case 2: credential is not correctly set
327+
// Case 3: credential is not correctly set
304328
if (!isCredentialDirectPathCompatible()) {
305329
LOG.log(
306330
Level.WARNING,
307331
"DirectPath is misconfigured. Please make sure the credential is an instance of "
308332
+ ComputeEngineCredentials.class.getName()
309333
+ " .");
310334
}
311-
// Case 3: not running on GCE
335+
// Case 4: not running on GCE
312336
if (!isOnComputeEngine()) {
313337
LOG.log(
314338
Level.WARNING,

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

+35-13
Original file line numberDiff line numberDiff line change
@@ -594,28 +594,50 @@ protected Object getMtlsObjectFromTransportChannel(MtlsProvider provider)
594594
return channelProvider.createMtlsChannelCredentials();
595595
}
596596

597+
private InstantiatingGrpcChannelProvider.Builder
598+
createChannelProviderBuilderForDirectPathLogTests() {
599+
return InstantiatingGrpcChannelProvider.newBuilder()
600+
.setHeaderProvider(Mockito.mock(HeaderProvider.class))
601+
.setExecutor(Mockito.mock(Executor.class))
602+
.setEndpoint("localhost:8080");
603+
}
604+
605+
private void createAndCloseTransportChannel(InstantiatingGrpcChannelProvider provider)
606+
throws Exception {
607+
TransportChannel transportChannel = provider.getTransportChannel();
608+
transportChannel.close();
609+
transportChannel.awaitTermination(10, TimeUnit.SECONDS);
610+
}
611+
597612
@Test
598-
void testLogDirectPathMisconfigAttrempDirectPathNotSet() throws Exception {
613+
void
614+
testLogDirectPathMisconfig_AttemptDirectPathNotSetAndAttemptDirectPathXdsSetViaBuilder_warns()
615+
throws Exception {
599616
FakeLogHandler logHandler = new FakeLogHandler();
600617
InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler);
601618
InstantiatingGrpcChannelProvider provider =
602-
InstantiatingGrpcChannelProvider.newBuilder()
603-
.setAttemptDirectPathXds()
604-
.setHeaderProvider(Mockito.mock(HeaderProvider.class))
605-
.setExecutor(Mockito.mock(Executor.class))
606-
.setEndpoint("localhost:8080")
607-
.build();
619+
createChannelProviderBuilderForDirectPathLogTests().setAttemptDirectPathXds().build();
620+
createAndCloseTransportChannel(provider);
621+
assertThat(logHandler.getAllMessages())
622+
.contains(
623+
"DirectPath is misconfigured. The DirectPath XDS option was set, but the attemptDirectPath option was not. Please set both the attemptDirectPath and attemptDirectPathXds options.");
624+
InstantiatingGrpcChannelProvider.LOG.removeHandler(logHandler);
625+
}
608626

609-
TransportChannel transportChannel = provider.getTransportChannel();
627+
@Test
628+
void testLogDirectPathMisconfig_AttemptDirectPathNotSetAndAttemptDirectPathXdsSetViaEnv_warns()
629+
throws Exception {
630+
FakeLogHandler logHandler = new FakeLogHandler();
631+
InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler);
610632

633+
InstantiatingGrpcChannelProvider provider =
634+
createChannelProviderBuilderForDirectPathLogTests().build();
635+
createAndCloseTransportChannel(provider);
611636
assertThat(logHandler.getAllMessages())
612637
.contains(
613-
"DirectPath is misconfigured. Please set the attemptDirectPath option along with the"
614-
+ " attemptDirectPathXds option.");
638+
"Env var GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS was found and set to TRUE, but DirectPath was not enabled for this client. If this is intended for "
639+
+ "this client, please note that this is a misconfiguration and set the attemptDirectPath option as well.");
615640
InstantiatingGrpcChannelProvider.LOG.removeHandler(logHandler);
616-
617-
transportChannel.close();
618-
transportChannel.awaitTermination(10, TimeUnit.SECONDS);
619641
}
620642

621643
@Test

0 commit comments

Comments
 (0)