Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -76,23 +76,33 @@ final class GoogleCloudToProdNameResolver extends NameResolver {
private static final String serverUriOverride =
System.getenv("GRPC_TEST_ONLY_GOOGLE_C2P_RESOLVER_TRAFFIC_DIRECTOR_URI");

@GuardedBy("GoogleCloudToProdNameResolver.class")
private static BootstrapInfo bootstrapInfo;
private static final Object BOOTSTRAP_LOCK = new Object();
Comment thread
ejona86 marked this conversation as resolved.
Outdated

@GuardedBy("BOOTSTRAP_LOCK")
static BootstrapInfo bootstrapInfo;
Comment thread
ejona86 marked this conversation as resolved.
Outdated
private static HttpConnectionProvider httpConnectionProvider = HttpConnectionFactory.INSTANCE;
private static int c2pId = new Random().nextInt();

private static synchronized BootstrapInfo getBootstrapInfo()
private static BootstrapInfo getBootstrapInfo(boolean isForcedXds)
Comment thread
ejona86 marked this conversation as resolved.
Outdated
throws XdsInitializationException, IOException {
if (bootstrapInfo != null) {
return bootstrapInfo;
}
BootstrapInfo bootstrapInfoTmp =
InternalGrpcBootstrapperImpl.parseBootstrap(generateBootstrap());
// Avoid setting global when testing
if (httpConnectionProvider == HttpConnectionFactory.INSTANCE) {
bootstrapInfo = bootstrapInfoTmp;
synchronized (BOOTSTRAP_LOCK) {
if (bootstrapInfo != null) {
return bootstrapInfo;
}
BootstrapInfo newInfo;
if (isForcedXds) {
newInfo = InternalGrpcBootstrapperImpl.parseBootstrap(
generateBootstrap("", true, true));
} else {
newInfo = InternalGrpcBootstrapperImpl.parseBootstrap(
generateBootstrap());
}
// Avoid setting global when testing
if (httpConnectionProvider == HttpConnectionFactory.INSTANCE) {
bootstrapInfo = newInfo;
}
return newInfo;
}
return bootstrapInfoTmp;
}

private final String authority;
Expand All @@ -102,7 +112,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 +133,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) {
Comment thread
shivaspeaks marked this conversation as resolved.
Outdated
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 +182,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 +199,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 +264,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 @@ -263,16 +301,18 @@ public void run() {
static ImmutableMap<String, ?> generateBootstrap() throws IOException {
return generateBootstrap(
queryZoneMetadata(METADATA_URL_ZONE),
queryIpv6SupportMetadata(METADATA_URL_SUPPORT_IPV6));
queryIpv6SupportMetadata(METADATA_URL_SUPPORT_IPV6), false);
}

private static ImmutableMap<String, ?> generateBootstrap(String zone, boolean supportIpv6) {
static ImmutableMap<String, ?> generateBootstrap(
String zone, boolean supportIpv6, boolean isForcedXds) {
ImmutableMap.Builder<String, Object> nodeBuilder = ImmutableMap.builder();
nodeBuilder.put("id", "C2P-" + (c2pId & Integer.MAX_VALUE));
if (!zone.isEmpty()) {
String nodeIdPrefix = isOnGcp ? "C2P-" : "C2P-non-gcp-";
nodeBuilder.put("id", nodeIdPrefix + (c2pId & Integer.MAX_VALUE));
if (!isForcedXds && !zone.isEmpty()) {
Comment thread
ejona86 marked this conversation as resolved.
Outdated
nodeBuilder.put("locality", ImmutableMap.of("zone", zone));
}
if (supportIpv6) {
if (isForcedXds || supportIpv6) {
nodeBuilder.put("metadata",
ImmutableMap.of("TRAFFICDIRECTOR_DIRECTPATH_C2P_IPV6_CAPABLE", true));
}
Expand Down Expand Up @@ -373,24 +413,34 @@ 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);
@VisibleForTesting
static void resetBootstrapInfo() {
synchronized (BOOTSTRAP_LOCK) {
bootstrapInfo = null;
}
return res;
}

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 boolean checkForceXds(String query) {
if (query == null) {
return false;
}
QueryParams params = QueryParams.fromRawQuery(query);
for (QueryParams.Entry entry : params.asList()) {
if ("force-xds".equals(entry.getKey())) {
return true;
}
}
return false;
}

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()));
params.asList().removeIf(entry -> entry.getKey().isEmpty() && !entry.hasValue());
Comment thread
ejona86 marked this conversation as resolved.
Outdated
return params.toRawQuery();
}

private enum HttpConnectionFactory implements HttpConnectionProvider {
Expand Down
Loading
Loading