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

feat: expose property in GrpcTransportChannel if it uses direct path. #3170

Merged
merged 10 commits into from
Sep 13, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public GrpcCallContext getEmptyCallContext() {
/** The channel in use. */
abstract ManagedChannel getManagedChannel();

abstract boolean getUseDirectPath();

public boolean useDirectPath() {
return getUseDirectPath();
}

public Channel getChannel() {
return getManagedChannel();
}
Expand Down Expand Up @@ -100,17 +106,23 @@ public void close() {
}

public static Builder newBuilder() {
return new AutoValue_GrpcTransportChannel.Builder();
return new AutoValue_GrpcTransportChannel.Builder().setUseDirectPath(false);
}

public static GrpcTransportChannel create(ManagedChannel channel) {
return newBuilder().setManagedChannel(channel).build();
}

public static GrpcTransportChannel create(ManagedChannel channel, boolean canUseDirectPath) {
return newBuilder().setManagedChannel(channel).setUseDirectPath(canUseDirectPath).build();
}

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setManagedChannel(ManagedChannel value);

abstract Builder setUseDirectPath(boolean value);

public abstract GrpcTransportChannel build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ public TransportChannel getTransportChannel() throws IOException {
private TransportChannel createChannel() throws IOException {
return GrpcTransportChannel.create(
ChannelPool.create(
channelPoolSettings, InstantiatingGrpcChannelProvider.this::createSingleChannel));
channelPoolSettings, InstantiatingGrpcChannelProvider.this::createSingleChannel),
this.canUseDirectPath());
}

private boolean isDirectPathEnabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ void testLogDirectPathMisconfigNotOnGCE() throws Exception {
}

@Test
public void canUseDirectPath_happyPath() {
public void canUseDirectPath_happyPath() throws IOException {
System.setProperty("os.name", "Linux");
EnvironmentProvider envProvider = Mockito.mock(EnvironmentProvider.class);
Mockito.when(
Expand All @@ -718,14 +718,20 @@ public void canUseDirectPath_happyPath() {
.setAttemptDirectPath(true)
.setCredentials(computeEngineCredentials)
.setEndpoint(DEFAULT_ENDPOINT)
.setEnvProvider(envProvider);
.setEnvProvider(envProvider)
.setHeaderProvider(Mockito.mock(HeaderProvider.class));
InstantiatingGrpcChannelProvider provider =
new InstantiatingGrpcChannelProvider(builder, GCE_PRODUCTION_NAME_AFTER_2016);
Truth.assertThat(provider.canUseDirectPath()).isTrue();

// verify this info is passed correctly to transport channel
TransportChannel transportChannel = provider.getTransportChannel();
Truth.assertThat(((GrpcTransportChannel) transportChannel).useDirectPath()).isTrue();
transportChannel.shutdownNow();
}

@Test
public void canUseDirectPath_directPathEnvVarDisabled() {
public void canUseDirectPath_directPathEnvVarDisabled() throws IOException {
System.setProperty("os.name", "Linux");
EnvironmentProvider envProvider = Mockito.mock(EnvironmentProvider.class);
Mockito.when(
Expand All @@ -737,10 +743,16 @@ public void canUseDirectPath_directPathEnvVarDisabled() {
.setAttemptDirectPath(true)
.setCredentials(computeEngineCredentials)
.setEndpoint(DEFAULT_ENDPOINT)
.setEnvProvider(envProvider);
.setEnvProvider(envProvider)
.setHeaderProvider(Mockito.mock(HeaderProvider.class));
InstantiatingGrpcChannelProvider provider =
new InstantiatingGrpcChannelProvider(builder, GCE_PRODUCTION_NAME_AFTER_2016);
Truth.assertThat(provider.canUseDirectPath()).isFalse();

// verify this info is passed correctly to transport channel
TransportChannel transportChannel = provider.getTransportChannel();
Truth.assertThat(((GrpcTransportChannel) transportChannel).useDirectPath()).isFalse();
transportChannel.shutdownNow();
}

@Test
Expand Down
Loading