Skip to content

Commit 9b65e38

Browse files
committed
direct-path-interconnect
1 parent 72b24f6 commit 9b65e38

2 files changed

Lines changed: 33 additions & 25 deletions

File tree

googleapis/src/main/java/io/grpc/googleapis/GoogleCloudToProdNameResolver.java

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import java.io.Reader;
4949
import java.net.HttpURLConnection;
5050
import java.net.URI;
51-
import java.net.URISyntaxException;
5251
import java.net.URL;
5352
import java.nio.charset.StandardCharsets;
5453
import java.util.List;
@@ -96,6 +95,7 @@ private static BootstrapInfo getBootstrapInfo(boolean isForcedXds)
9695
}
9796
BootstrapInfo newInfo = InternalGrpcBootstrapperImpl.parseBootstrap(
9897
generateBootstrap("", true, true));
98+
// Avoid setting global when testing
9999
if (httpConnectionProvider == HttpConnectionFactory.INSTANCE) {
100100
forceXdsBootstrapInfo = newInfo;
101101
}
@@ -108,6 +108,7 @@ private static BootstrapInfo getBootstrapInfo(boolean isForcedXds)
108108
}
109109
BootstrapInfo newInfo = InternalGrpcBootstrapperImpl.parseBootstrap(
110110
generateBootstrap());
111+
// Avoid setting global when testing
111112
if (httpConnectionProvider == HttpConnectionFactory.INSTANCE) {
112113
bootstrapInfo = newInfo;
113114
}
@@ -143,12 +144,13 @@ private static BootstrapInfo getBootstrapInfo(boolean isForcedXds)
143144
GoogleCloudToProdNameResolver(URI targetUri, Args args, Resource<Executor> executorResource,
144145
NameResolver.Factory nameResolverFactory) {
145146
this.executorResource = checkNotNull(executorResource, "executorResource");
146-
String query = targetUri.getRawQuery();
147+
String targetPath = checkNotNull(checkNotNull(targetUri, "targetUri").getPath(), "targetPath");
148+
Uri grpcUri = Uri.create(targetUri.toString());
149+
String query = grpcUri.getRawQuery();
147150
this.forceXds = checkForceXds(query);
148151
this.schemeOverride = (forceXds || isOnGcp) ? "xds" : "dns";
149152
String newQuery = stripForceXds(query);
150153

151-
String targetPath = checkNotNull(checkNotNull(targetUri, "targetUri").getPath(), "targetPath");
152154
Preconditions.checkArgument(
153155
targetPath.startsWith("/"),
154156
"the path component (%s) of the target (%s) must start with '/'",
@@ -157,28 +159,16 @@ private static BootstrapInfo getBootstrapInfo(boolean isForcedXds)
157159
authority = GrpcUtil.checkAuthority(targetPath.substring(1));
158160
syncContext = checkNotNull(args, "args").getSynchronizationContext();
159161

160-
String rawAuthority = schemeOverride.equals("xds")
161-
? C2P_AUTHORITY
162-
: targetUri.getRawAuthority();
163-
String rawPath = targetUri.getRawPath();
164-
String rawFragment = targetUri.getRawFragment();
165-
try {
166-
StringBuilder uriStr = new StringBuilder();
167-
uriStr.append(schemeOverride).append(":");
168-
if (rawAuthority != null) {
169-
uriStr.append("//").append(rawAuthority);
170-
}
171-
uriStr.append(rawPath);
172-
if (newQuery != null) {
173-
uriStr.append("?").append(newQuery);
174-
}
175-
if (rawFragment != null) {
176-
uriStr.append("#").append(rawFragment);
177-
}
178-
targetUri = new URI(uriStr.toString());
179-
} catch (URISyntaxException e) {
180-
throw new IllegalArgumentException("Invalid URI", e);
162+
Uri.Builder modifiedTargetBuilder = grpcUri.toBuilder().setScheme(schemeOverride);
163+
if (newQuery != null) {
164+
modifiedTargetBuilder.setRawQuery(newQuery);
165+
} else {
166+
modifiedTargetBuilder.setQuery(null);
181167
}
168+
if (schemeOverride.equals("xds")) {
169+
modifiedTargetBuilder.setRawAuthority(C2P_AUTHORITY);
170+
}
171+
targetUri = URI.create(modifiedTargetBuilder.build().toString());
182172

183173
if (schemeOverride.equals("xds")) {
184174
args = args.toBuilder()
@@ -440,9 +430,13 @@ private static boolean checkForceXds(String query) {
440430
return false;
441431
}
442432
for (String part : Splitter.on('&').split(query)) {
443-
if (part.equals("force-xds") || part.startsWith("force-xds=")) {
433+
if (part.equals("force-xds")) {
444434
return true;
445435
}
436+
if (part.startsWith("force-xds=")) {
437+
String value = part.substring("force-xds=".length());
438+
return !value.equalsIgnoreCase("false") && !value.equals("0");
439+
}
446440
}
447441
return false;
448442
}

googleapis/src/test/java/io/grpc/googleapis/GoogleCloudToProdNameResolverTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,20 @@ public void notOnGcpButForceXds_KeyValueTrue_DelegateToXds() {
238238
}
239239
}
240240

241+
@Test
242+
public void notOnGcpButForceXds_KeyValueFalse_DelegateToDns() {
243+
GoogleCloudToProdNameResolver.isOnGcp = false;
244+
String target = TARGET_URI + "?force-xds=false";
245+
resolver = enableRfc3986UrisParam
246+
? new GoogleCloudToProdNameResolver(
247+
Uri.create(target), args, fakeExecutorResource, nsRegistry.asFactory())
248+
: new GoogleCloudToProdNameResolver(
249+
URI.create(target), args, fakeExecutorResource, nsRegistry.asFactory());
250+
resolver.start(mockListener);
251+
fakeExecutor.runDueTasks();
252+
assertThat(delegatedResolver.keySet()).containsExactly("dns");
253+
}
254+
241255
@Test
242256
public void notOnGcpButForceXds_KeyValueOne_DelegateToXds() {
243257
GoogleCloudToProdNameResolver.isOnGcp = false;

0 commit comments

Comments
 (0)