Skip to content

feat: Allow JDBC to configure directpath for connection #3927

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

Merged
merged 2 commits into from
Jun 27, 2025
Merged
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 @@ -1000,7 +1000,7 @@ public static class Builder
private String compressorName;
private String emulatorHost = System.getenv("SPANNER_EMULATOR_HOST");
private boolean leaderAwareRoutingEnabled = true;
private boolean attemptDirectPath = true;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this configuration wasn't used at all so far, I changed the default behaviour.

private boolean attemptDirectPath = false;
private DirectedReadOptions directedReadOptions;
private boolean useVirtualThreads = false;
private OpenTelemetry openTelemetry;
Expand Down Expand Up @@ -1604,6 +1604,12 @@ public Builder disableLeaderAwareRouting() {
return this;
}

@BetaApi
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since directpath is not public GA, I have marked as @BetaApi

public Builder enableDirectPath() {
this.attemptDirectPath = true;
return this;
}

@BetaApi
public Builder disableDirectPath() {
this.attemptDirectPath = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.spanner.connection;

import static com.google.cloud.spanner.connection.ConnectionProperties.ATTEMPT_DIRECT_PATH;
import static com.google.cloud.spanner.connection.ConnectionProperties.AUTOCOMMIT;
import static com.google.cloud.spanner.connection.ConnectionProperties.AUTO_CONFIG_EMULATOR;
import static com.google.cloud.spanner.connection.ConnectionProperties.AUTO_PARTITION_MODE;
Expand Down Expand Up @@ -1081,6 +1082,10 @@ boolean isExperimentalHost() {
return getInitialConnectionPropertyValue(IS_EXPERIMENTAL_HOST);
}

boolean isAttemptDirectPath() {
return getInitialConnectionPropertyValue(ATTEMPT_DIRECT_PATH);
}

String getClientCertificate() {
return getInitialConnectionPropertyValue(CLIENT_CERTIFICATE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,20 @@ public class ConnectionProperties {
BOOLEANS,
BooleanConverter.INSTANCE,
Context.STARTUP);
static final ConnectionProperty<Boolean> ATTEMPT_DIRECT_PATH =
create(
"attemptDirectPath",
"Configure the connection to try to connect to Spanner using "
+ "DirectPath (true/false). The client will try to connect to Spanner "
+ "using a direct Google network connection. DirectPath will work only "
+ "if the client is trying to establish a connection from a Google Cloud VM. "
+ "Otherwise it will automatically fallback to the standard network path. "
+ "NOTE: The default for this property is currently false, "
+ "but this could be changed in the future.",
false,
BOOLEANS,
BooleanConverter.INSTANCE,
Context.STARTUP);
static final ConnectionProperty<Boolean> USE_AUTO_SAVEPOINTS_FOR_EMULATOR =
create(
"useAutoSavepointsForEmulator",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ static class SpannerPoolKey {
private final String clientCertificate;
private final String clientCertificateKey;
private final boolean isExperimentalHost;
private final boolean attemptDirectPath;

@VisibleForTesting
static SpannerPoolKey of(ConnectionOptions options) {
Expand Down Expand Up @@ -198,6 +199,7 @@ private SpannerPoolKey(ConnectionOptions options) throws IOException {
this.clientCertificate = options.getClientCertificate();
this.clientCertificateKey = options.getClientCertificateKey();
this.isExperimentalHost = options.isExperimentalHost();
this.attemptDirectPath = options.isAttemptDirectPath();
}

@Override
Expand All @@ -223,7 +225,8 @@ public boolean equals(Object o) {
&& Objects.equals(this.enableEndToEndTracing, other.enableEndToEndTracing)
&& Objects.equals(this.clientCertificate, other.clientCertificate)
&& Objects.equals(this.clientCertificateKey, other.clientCertificateKey)
&& Objects.equals(this.isExperimentalHost, other.isExperimentalHost);
&& Objects.equals(this.isExperimentalHost, other.isExperimentalHost)
&& Objects.equals(this.attemptDirectPath, other.attemptDirectPath);
}

@Override
Expand All @@ -245,7 +248,8 @@ public int hashCode() {
this.enableEndToEndTracing,
this.clientCertificate,
this.clientCertificateKey,
this.isExperimentalHost);
this.isExperimentalHost,
this.attemptDirectPath);
}
}

Expand Down Expand Up @@ -412,6 +416,9 @@ Spanner createSpanner(SpannerPoolKey key, ConnectionOptions options) {
if (key.isExperimentalHost) {
builder.setExperimentalHost(key.host);
}
if (key.attemptDirectPath) {
builder.enableDirectPath();
}
if (options.getConfigurator() != null) {
options.getConfigurator().configure(builder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public GapicSpannerRpc(final SpannerOptions options) {
.withEncoding(compressorName))
.setHeaderProvider(headerProviderWithUserAgent)
.setAllowNonDefaultServiceAccount(true);
boolean isAttemptDirectPathXds = isEnableDirectPathXdsEnv();
boolean isAttemptDirectPathXds = isEnableDirectPathXdsEnv() || options.isAttemptDirectPath();
if (isAttemptDirectPathXds) {
defaultChannelProviderBuilder.setAttemptDirectPath(true);
// This will let the credentials try to fetch a hard-bound access token if the runtime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1311,4 +1311,17 @@ public void testExperimentalHost() {
.getSessionPoolOptions()
.getUseMultiplexedSessionPartitionedOps());
}

@Test
public void testAttemptDirectPath() {
ConnectionOptions.Builder builderWithoutDirectPathParam = ConnectionOptions.newBuilder();
builderWithoutDirectPathParam.setUri(
"spanner://localhost:15000/instances/default/databases/singers-db;usePlainText=true");
assertFalse(builderWithoutDirectPathParam.build().isAttemptDirectPath());

ConnectionOptions.Builder builderWithDirectPathParam = ConnectionOptions.newBuilder();
builderWithDirectPathParam.setUri(
"spanner://localhost:15000/projects/default/instances/default/databases/singers-db;usePlainText=true;attemptDirectPath=true");
assertTrue(builderWithDirectPathParam.build().isAttemptDirectPath());
}
}