Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve warnings for Direct Path xDS set via env #3019

Merged
merged 15 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
# Set the Env Var for this step only
env:
GOOGLE_CLOUD_UNIVERSE_DOMAIN: random.com
GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS: true
- run: bazelisk version
- name: Install Maven modules
run: |
Expand Down Expand Up @@ -80,6 +81,7 @@ jobs:
# Set the Env Var for this step only
env:
GOOGLE_CLOUD_UNIVERSE_DOMAIN: random.com
GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS: true
- run: bazelisk version
- name: Install Maven modules
run: |
Expand Down Expand Up @@ -130,6 +132,7 @@ jobs:
# Set the Env Var for this step only
env:
GOOGLE_CLOUD_UNIVERSE_DOMAIN: random.com
GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS: true

build-java8-gapic-generator-java:
name: "build(8) for gapic-generator-java"
Expand Down
24 changes: 24 additions & 0 deletions gax-java/gax-grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- These tests require an Env Var to be set. Use -PenvVarTest to ONLY run these tests -->
<test>!InstantiatingGrpcChannelProviderTest#testLogDirectPathMisconfig_AttemptDirectPathNotSetAndAttemptDirectPathXdsSetViaEnv_warns</test>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>envVarTest</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<test>InstantiatingGrpcChannelProviderTest#testLogDirectPathMisconfig_AttemptDirectPathNotSetAndAttemptDirectPathXdsSetViaEnv_warns</test>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
static final Logger LOG = Logger.getLogger(InstantiatingGrpcChannelProvider.class.getName());

static final String DIRECT_PATH_ENV_DISABLE_DIRECT_PATH = "GOOGLE_CLOUD_DISABLE_DIRECT_PATH";
private static final String DIRECT_PATH_ENV_ENABLE_XDS = "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS";

@VisibleForTesting
static final String DIRECT_PATH_ENV_ENABLE_XDS = "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS";

static final long DIRECT_PATH_KEEP_ALIVE_TIME_SECONDS = 3600;
static final long DIRECT_PATH_KEEP_ALIVE_TIMEOUT_SECONDS = 20;
static final String GCE_PRODUCTION_NAME_PRIOR_2016 = "Google";
Expand Down Expand Up @@ -273,32 +276,36 @@ private boolean isDirectPathEnabled() {
return false;
}

private boolean isDirectPathXdsEnabledViaBuilderOption() {
return Boolean.TRUE.equals(attemptDirectPathXds);
}

private boolean isDirectPathXdsEnabledViaEnv() {
String directPathXdsEnv = envProvider.getenv(DIRECT_PATH_ENV_ENABLE_XDS);
return Boolean.parseBoolean(directPathXdsEnv);
}

@InternalApi
public boolean isDirectPathXdsEnabled() {
// Method 1: Enable DirectPath xDS by option.
if (Boolean.TRUE.equals(attemptDirectPathXds)) {
if (isDirectPathXdsEnabledViaBuilderOption()) {
diegomarquezp marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
// Method 2: Enable DirectPath xDS by env.
String directPathXdsEnv = envProvider.getenv(DIRECT_PATH_ENV_ENABLE_XDS);
boolean isDirectPathXdsEnv = Boolean.parseBoolean(directPathXdsEnv);
if (isDirectPathXdsEnv) {
return true;
}
return false;
return isDirectPathXdsEnabledViaEnv();
}

// This method should be called once per client initialization, hence can not be called in the
// builder or createSingleChannel, only in getTransportChannel which creates the first channel
// for a client.
private void logDirectPathMisconfig() {
if (isDirectPathXdsEnabled()) {
// Case 1: does not enable DirectPath
if (!isDirectPathEnabled()) {
LOG.log(
Level.WARNING,
"DirectPath is misconfigured. Please set the attemptDirectPath option along with the"
+ " attemptDirectPathXds option.");
final String onlyOneDirectPathEnabledMessage =
diegomarquezp marked this conversation as resolved.
Show resolved Hide resolved
"DirectPath is misconfigured. Please set both the "
+ "attemptDirectPath and the attemptDirectPathXds options.";
if (isDirectPathEnabled()) {
// Case 1: does not enable DirectPath xDS
diegomarquezp marked this conversation as resolved.
Show resolved Hide resolved
if (!isDirectPathXdsEnabled()) {
LOG.log(Level.WARNING, onlyOneDirectPathEnabledMessage);
} else {
// Case 2: credential is not correctly set
if (!isCredentialDirectPathCompatible()) {
Expand All @@ -315,6 +322,15 @@ private void logDirectPathMisconfig() {
"DirectPath is misconfigured. DirectPath is only available in a GCE environment.");
}
}
} else if (isDirectPathXdsEnabledViaBuilderOption()) {
LOG.log(Level.WARNING, onlyOneDirectPathEnabledMessage);
} else if (isDirectPathXdsEnabledViaEnv()) {
LOG.log(
Level.WARNING,
"Env var "
+ DIRECT_PATH_ENV_ENABLE_XDS
+ " was found. If this is intended for "
+ "this client, please note that this is a misconfiguration and set the attemptDirectPath option as well.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,28 +594,65 @@ protected Object getMtlsObjectFromTransportChannel(MtlsProvider provider)
return channelProvider.createMtlsChannelCredentials();
}

private InstantiatingGrpcChannelProvider.Builder
createChannelProviderBuilderForDirectPathLogTests() {
return InstantiatingGrpcChannelProvider.newBuilder()
.setHeaderProvider(Mockito.mock(HeaderProvider.class))
.setExecutor(Mockito.mock(Executor.class))
.setEndpoint("localhost:8080");
}

private void createAndCloseTransportChannel(InstantiatingGrpcChannelProvider provider)
throws Exception {
TransportChannel transportChannel = provider.getTransportChannel();
transportChannel.close();
transportChannel.awaitTermination(10, TimeUnit.SECONDS);
}

@Test
void testLogDirectPathMisconfigAttrempDirectPathNotSet() throws Exception {
void
testLogDirectPathMisconfig_AttemptDirectPathNotSetAndAttemptDirectPathXdsSetViaBuilder_warns()
throws Exception {
FakeLogHandler logHandler = new FakeLogHandler();
InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler);
InstantiatingGrpcChannelProvider provider =
InstantiatingGrpcChannelProvider.newBuilder()
.setAttemptDirectPathXds()
.setHeaderProvider(Mockito.mock(HeaderProvider.class))
.setExecutor(Mockito.mock(Executor.class))
.setEndpoint("localhost:8080")
.build();
createChannelProviderBuilderForDirectPathLogTests().setAttemptDirectPathXds().build();
createAndCloseTransportChannel(provider);
assertThat(logHandler.getAllMessages())
.contains(
"DirectPath is misconfigured. Please set both the attemptDirectPath and the attemptDirectPathXds options.");
InstantiatingGrpcChannelProvider.LOG.removeHandler(logHandler);
}

TransportChannel transportChannel = provider.getTransportChannel();
@Test
void testLogDirectPathMisconfig_AttemptDirectPathNotSetAndAttemptDirectPathXdsSetViaEnv_warns()
throws Exception {
FakeLogHandler logHandler = new FakeLogHandler();
InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler);

InstantiatingGrpcChannelProvider provider =
createChannelProviderBuilderForDirectPathLogTests().build();
createAndCloseTransportChannel(provider);
assertThat(logHandler.getAllMessages())
.contains(
"DirectPath is misconfigured. Please set the attemptDirectPath option along with the"
+ " attemptDirectPathXds option.");
"Env var GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS was found. If this is intended for this client, please note "
+ "that this is a misconfiguration and set the attemptDirectPath option as well.");
InstantiatingGrpcChannelProvider.LOG.removeHandler(logHandler);
}

transportChannel.close();
transportChannel.awaitTermination(10, TimeUnit.SECONDS);
@Test
void testLogDirectPathMisconfig_AttemptDirectPathSetAndAttemptDirectPathXdsNotSet_warns()
throws Exception {
FakeLogHandler logHandler = new FakeLogHandler();
InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler);
InstantiatingGrpcChannelProvider provider =
createChannelProviderBuilderForDirectPathLogTests().setAttemptDirectPath(true).build();
createAndCloseTransportChannel(provider);
assertThat(logHandler.getAllMessages())
.contains(
"DirectPath is misconfigured. Please set both the attemptDirectPath and the "
+ "attemptDirectPathXds options.");
InstantiatingGrpcChannelProvider.LOG.removeHandler(logHandler);
}

@Test
Expand Down
Loading