Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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 @@ -27,6 +27,7 @@
import io.grpc.MetricRecorder;
import io.grpc.NameResolver;
import io.grpc.NameResolverRegistry;
import io.grpc.QueryParams;
import io.grpc.Status;
import io.grpc.SynchronizationContext;
import io.grpc.Uri;
Expand All @@ -47,7 +48,6 @@
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
Expand Down Expand Up @@ -81,18 +81,26 @@ final class GoogleCloudToProdNameResolver extends NameResolver {
private static HttpConnectionProvider httpConnectionProvider = HttpConnectionFactory.INSTANCE;
private static int c2pId = new Random().nextInt();

private static synchronized BootstrapInfo getBootstrapInfo()
private static synchronized BootstrapInfo getBootstrapInfo(boolean isForcedXds)
throws XdsInitializationException, IOException {
if (bootstrapInfo != null) {
return bootstrapInfo;
}
BootstrapInfo bootstrapInfoTmp =
InternalGrpcBootstrapperImpl.parseBootstrap(generateBootstrap());
BootstrapInfo newInfo;
if (isForcedXds) {
newInfo = InternalGrpcBootstrapperImpl.parseBootstrap(
generateBootstrap("", true));
} else {
newInfo = InternalGrpcBootstrapperImpl.parseBootstrap(
generateBootstrap(
queryZoneMetadata(METADATA_URL_ZONE),
queryIpv6SupportMetadata(METADATA_URL_SUPPORT_IPV6)));
}
// Avoid setting global when testing
if (httpConnectionProvider == HttpConnectionFactory.INSTANCE) {
bootstrapInfo = bootstrapInfoTmp;
bootstrapInfo = newInfo;
}
return bootstrapInfoTmp;
return newInfo;
}

private final String authority;
Expand All @@ -102,7 +110,8 @@ private static synchronized BootstrapInfo getBootstrapInfo()
private final MetricRecorder metricRecorder;
private final NameResolver delegate;
private final boolean usingExecutorResource;
private final String schemeOverride = !isOnGcp ? "dns" : "xds";
private final boolean forceXds;
private final String schemeOverride;
private XdsClientResult xdsClientPool;
private XdsClient xdsClient;
private Executor executor;
Expand All @@ -122,16 +131,32 @@ private static synchronized BootstrapInfo getBootstrapInfo()
NameResolver.Factory nameResolverFactory) {
this.executorResource = checkNotNull(executorResource, "executorResource");
String targetPath = checkNotNull(checkNotNull(targetUri, "targetUri").getPath(), "targetPath");
Uri grpcUri = Uri.create(targetUri.toString());
String query = grpcUri.getRawQuery();
this.forceXds = checkForceXds(query);
this.schemeOverride = (forceXds || isOnGcp) ? "xds" : "dns";
String newQuery = stripForceXds(query);

Preconditions.checkArgument(
targetPath.startsWith("/"),
"the path component (%s) of the target (%s) must start with '/'",
targetPath,
targetUri);
authority = GrpcUtil.checkAuthority(targetPath.substring(1));
syncContext = checkNotNull(args, "args").getSynchronizationContext();
targetUri = overrideUriScheme(targetUri, schemeOverride);

Uri.Builder modifiedTargetBuilder = grpcUri.toBuilder().setScheme(schemeOverride);
if (newQuery != null) {
modifiedTargetBuilder.setRawQuery(newQuery);
} else {
modifiedTargetBuilder.setRawQuery(null);
}
if (schemeOverride.equals("xds")) {
modifiedTargetBuilder.setRawAuthority(C2P_AUTHORITY);
}
targetUri = URI.create(modifiedTargetBuilder.build().toString());

if (schemeOverride.equals("xds")) {
targetUri = overrideUriAuthority(targetUri, C2P_AUTHORITY);
args = args.toBuilder()
.setArg(XdsNameResolverProvider.XDS_CLIENT_SUPPLIER, () -> xdsClient)
.build();
Expand All @@ -155,6 +180,11 @@ private static synchronized BootstrapInfo getBootstrapInfo()
Resource<Executor> executorResource,
NameResolver.Factory nameResolverFactory) {
this.executorResource = checkNotNull(executorResource, "executorResource");
String query = targetUri.getRawQuery();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Consider working with the query component as an instance of QueryParams rather than a string. That way you can parse it just once here, then read/modify it in both checkForceXds() and stripForceXds() without parsing it again.

this.forceXds = checkForceXds(query);
this.schemeOverride = (forceXds || isOnGcp) ? "xds" : "dns";
String newQuery = stripForceXds(query);

Preconditions.checkArgument(
targetUri.isPathAbsolute(),
"the path component of the target (%s) must start with '/'",
Expand All @@ -167,6 +197,12 @@ private static synchronized BootstrapInfo getBootstrapInfo()
authority = GrpcUtil.checkAuthority(pathSegments.get(0));
syncContext = checkNotNull(args, "args").getSynchronizationContext();
Uri.Builder modifiedTargetBuilder = targetUri.toBuilder().setScheme(schemeOverride);
if (newQuery != null) {
modifiedTargetBuilder.setRawQuery(newQuery);
} else {
modifiedTargetBuilder.setRawQuery(null);
}

if (schemeOverride.equals("xds")) {
modifiedTargetBuilder.setRawAuthority(C2P_AUTHORITY);
args =
Expand Down Expand Up @@ -226,7 +262,7 @@ class Resolve implements Runnable {
public void run() {
BootstrapInfo bootstrapInfo = null;
try {
bootstrapInfo = getBootstrapInfo();
bootstrapInfo = getBootstrapInfo(forceXds);
} catch (IOException e) {
listener.onError(
Status.INTERNAL.withDescription("Unable to get metadata").withCause(e));
Expand Down Expand Up @@ -259,16 +295,11 @@ public void run() {
executor.execute(new Resolve());
}

@VisibleForTesting
static ImmutableMap<String, ?> generateBootstrap() throws IOException {
return generateBootstrap(
queryZoneMetadata(METADATA_URL_ZONE),
queryIpv6SupportMetadata(METADATA_URL_SUPPORT_IPV6));
}

private static ImmutableMap<String, ?> generateBootstrap(String zone, boolean supportIpv6) {
private static ImmutableMap<String, ?> generateBootstrap(
String zone, boolean supportIpv6) {
ImmutableMap.Builder<String, Object> nodeBuilder = ImmutableMap.builder();
nodeBuilder.put("id", "C2P-" + (c2pId & Integer.MAX_VALUE));
String nodeIdPrefix = isOnGcp ? "C2P-" : "C2P-non-gcp-";
nodeBuilder.put("id", nodeIdPrefix + (c2pId & Integer.MAX_VALUE));
if (!zone.isEmpty()) {
nodeBuilder.put("locality", ImmutableMap.of("zone", zone));
}
Expand Down Expand Up @@ -373,24 +404,26 @@ static void setC2pId(int c2pId) {
GoogleCloudToProdNameResolver.c2pId = c2pId;
}

private static URI overrideUriScheme(URI uri, String scheme) {
URI res;
try {
res = new URI(scheme, uri.getAuthority(), uri.getPath(), uri.getQuery(), uri.getFragment());
} catch (URISyntaxException ex) {
throw new IllegalArgumentException("Invalid scheme: " + scheme, ex);
private static boolean checkForceXds(String query) {
if (query == null) {
return false;
}
return res;
QueryParams params = QueryParams.fromRawQuery(query);
for (QueryParams.Entry entry : params.asList()) {
if ("force-xds".equals(entry.getKey())) {
return true;
}
}
return false;
}

private static URI overrideUriAuthority(URI uri, String authority) {
URI res;
try {
res = new URI(uri.getScheme(), authority, uri.getPath(), uri.getQuery(), uri.getFragment());
} catch (URISyntaxException ex) {
throw new IllegalArgumentException("Invalid authority: " + authority, ex);
private static String stripForceXds(String query) {
if (query == null) {
return null;
}
return res;
QueryParams params = QueryParams.fromRawQuery(query);
params.asList().removeIf(entry -> "force-xds".equals(entry.getKey()));
return params.toRawQuery();
}

private enum HttpConnectionFactory implements HttpConnectionProvider {
Expand Down
Loading
Loading