Skip to content

Commit

Permalink
googleapis: Move GoogleCloudToProdNameResolver from xds
Browse files Browse the repository at this point in the history
GoogleCloudToProdNameResolver has a hard dependency on alts whereas xds
only has a weak dependency on alts that can be solved by a
ChannelCredentialsRegistry. So split out the code to a separate
artifact.
  • Loading branch information
ejona86 committed Mar 3, 2022
1 parent abd1642 commit ecc8cc3
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 34 deletions.
36 changes: 36 additions & 0 deletions googleapis/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
plugins {
id "java-library"
id "maven-publish"

id "ru.vyarus.animalsniffer"
}

description = 'gRPC: googleapis'

dependencies {
api project(':grpc-api')
implementation project(':grpc-alts'),
project(':grpc-core'),
project(':grpc-xds'),
libraries.guava
testImplementation project(':grpc-core').sourceSets.test.output

signature "org.codehaus.mojo.signature:java17:1.0@signature"
}

publishing {
publications {
maven(MavenPublication) {
pom {
withXml {
// Since internal APIs are used, pin the version.
asNode().dependencies.'*'.findAll() { dep ->
dep.artifactId.text() in ['grpc-alts', 'grpc-xds']
}.each() { core ->
core.version*.value = "[" + core.version.text() + "]"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.grpc.xds;
package io.grpc.googleapis;

import static com.google.common.base.Preconditions.checkNotNull;

Expand All @@ -32,14 +32,14 @@
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.SharedResourceHolder;
import io.grpc.internal.SharedResourceHolder.Resource;
import io.grpc.xds.XdsNameResolverProvider.XdsClientPoolFactory;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Executor;

Expand Down Expand Up @@ -70,7 +70,7 @@ final class GoogleCloudToProdNameResolver extends NameResolver {
private final String authority;
private final SynchronizationContext syncContext;
private final Resource<Executor> executorResource;
private final XdsClientPoolFactory xdsClientPoolFactory;
private final BootstrapSetter bootstrapSetter;
private final NameResolver delegate;
private final Random rand;
private final boolean usingExecutorResource;
Expand All @@ -84,17 +84,16 @@ final class GoogleCloudToProdNameResolver extends NameResolver {
private boolean shutdown;

GoogleCloudToProdNameResolver(URI targetUri, Args args, Resource<Executor> executorResource,
XdsClientPoolFactory xdsClientPoolFactory) {
this(targetUri, args, executorResource, new Random(), xdsClientPoolFactory,
BootstrapSetter bootstrapSetter) {
this(targetUri, args, executorResource, new Random(), bootstrapSetter,
NameResolverRegistry.getDefaultRegistry().asFactory());
}

@VisibleForTesting
GoogleCloudToProdNameResolver(URI targetUri, Args args, Resource<Executor> executorResource,
Random rand, XdsClientPoolFactory xdsClientPoolFactory,
NameResolver.Factory nameResolverFactory) {
Random rand, BootstrapSetter bootstrapSetter, NameResolver.Factory nameResolverFactory) {
this.executorResource = checkNotNull(executorResource, "executorResource");
this.xdsClientPoolFactory = checkNotNull(xdsClientPoolFactory, "xdsClientPoolFactory");
this.bootstrapSetter = checkNotNull(bootstrapSetter, "bootstrapSetter");
this.rand = checkNotNull(rand, "rand");
String targetPath = checkNotNull(checkNotNull(targetUri, "targetUri").getPath(), "targetPath");
Preconditions.checkArgument(
Expand Down Expand Up @@ -159,7 +158,7 @@ public void run() {
@Override
public void run() {
if (!shutdown && finalRawBootstrap != null) {
xdsClientPoolFactory.setBootstrapOverride(finalRawBootstrap);
bootstrapSetter.setBootstrap(finalRawBootstrap);
delegate.start(listener);
succeeded = true;
}
Expand Down Expand Up @@ -284,4 +283,8 @@ public HttpURLConnection createConnection(String url) throws IOException {
interface HttpConnectionProvider {
HttpURLConnection createConnection(String url) throws IOException;
}

public interface BootstrapSetter {
void setBootstrap(Map<String, ?> bootstrap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
* limitations under the License.
*/

package io.grpc.xds;
package io.grpc.googleapis;

import io.grpc.Internal;
import io.grpc.NameResolver;
import io.grpc.NameResolver.Args;
import io.grpc.NameResolverProvider;
import io.grpc.internal.GrpcUtil;
import io.grpc.xds.InternalSharedXdsClientPoolProvider;
import java.net.URI;
import java.util.Map;

/**
* A provider for {@link GoogleCloudToProdNameResolver}.
Expand All @@ -36,7 +38,7 @@ public NameResolver newNameResolver(URI targetUri, Args args) {
if (SCHEME.equals(targetUri.getScheme())) {
return new GoogleCloudToProdNameResolver(
targetUri, args, GrpcUtil.SHARED_CHANNEL_EXECUTOR,
SharedXdsClientPoolProvider.getDefaultProvider());
new SharedXdsClientPoolProviderBootstrapSetter());
}
return null;
}
Expand All @@ -55,4 +57,12 @@ protected boolean isAvailable() {
protected int priority() {
return 4;
}

private static final class SharedXdsClientPoolProviderBootstrapSetter
implements GoogleCloudToProdNameResolver.BootstrapSetter {
@Override
public void setBootstrap(Map<String, ?> bootstrap) {
InternalSharedXdsClientPoolProvider.setDefaultProviderBootstrapOverride(bootstrap);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.grpc.googleapis.GoogleCloudToProdNameResolverProvider
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.grpc.xds;
package io.grpc.googleapis;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.grpc.xds;
package io.grpc.googleapis;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
Expand All @@ -33,12 +33,10 @@
import io.grpc.Status;
import io.grpc.Status.Code;
import io.grpc.SynchronizationContext;
import io.grpc.googleapis.GoogleCloudToProdNameResolver.HttpConnectionProvider;
import io.grpc.internal.FakeClock;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.ObjectPool;
import io.grpc.internal.SharedResourceHolder.Resource;
import io.grpc.xds.GoogleCloudToProdNameResolver.HttpConnectionProvider;
import io.grpc.xds.XdsNameResolverProvider.XdsClientPoolFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
Expand All @@ -50,7 +48,6 @@
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -88,7 +85,7 @@ public void uncaughtException(Thread t, Throwable e) {
.setChannelLogger(mock(ChannelLogger.class))
.build();
private final FakeClock fakeExecutor = new FakeClock();
private final FakeXdsClientPoolFactory fakeXdsClientPoolFactory = new FakeXdsClientPoolFactory();
private final FakeBootstrapSetter fakeBootstrapSetter = new FakeBootstrapSetter();
private final Resource<Executor> fakeExecutorResource = new Resource<Executor>() {
@Override
public Executor create() {
Expand Down Expand Up @@ -144,7 +141,7 @@ public HttpURLConnection createConnection(String url) throws IOException {
}
};
resolver = new GoogleCloudToProdNameResolver(
TARGET_URI, args, fakeExecutorResource, random, fakeXdsClientPoolFactory,
TARGET_URI, args, fakeExecutorResource, random, fakeBootstrapSetter,
nsRegistry.asFactory());
resolver.setHttpConnectionProvider(httpConnections);
}
Expand Down Expand Up @@ -178,7 +175,7 @@ public void onGcpAndNoProvidedBootstrapDelegateToXds() {
fakeExecutor.runDueTasks();
assertThat(delegatedResolver.keySet()).containsExactly("xds");
verify(Iterables.getOnlyElement(delegatedResolver.values())).start(mockListener);
Map<String, ?> bootstrap = fakeXdsClientPoolFactory.bootstrapRef.get();
Map<String, ?> bootstrap = fakeBootstrapSetter.bootstrapRef.get();
Map<String, ?> node = (Map<String, ?>) bootstrap.get("node");
assertThat(node).containsExactly(
"id", "C2P-991614323",
Expand Down Expand Up @@ -246,23 +243,13 @@ public String getDefaultScheme() {
}
}

private static final class FakeXdsClientPoolFactory implements XdsClientPoolFactory {
private static final class FakeBootstrapSetter
implements GoogleCloudToProdNameResolver.BootstrapSetter {
private final AtomicReference<Map<String, ?>> bootstrapRef = new AtomicReference<>();

@Override
public void setBootstrapOverride(Map<String, ?> bootstrap) {
public void setBootstrap(Map<String, ?> bootstrap) {
bootstrapRef.set(bootstrap);
}

@Override
@Nullable
public ObjectPool<XdsClient> get() {
throw new UnsupportedOperationException("Should not be called");
}

@Override
public ObjectPool<XdsClient> getOrCreate() {
throw new UnsupportedOperationException("Should not be called");
}
}
}
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ include ":grpc-protobuf"
include ":grpc-protobuf-lite"
include ":grpc-netty"
include ":grpc-netty-shaded"
include ":grpc-googleapis"
include ":grpc-grpclb"
include ":grpc-testing"
include ":grpc-testing-proto"
Expand All @@ -63,6 +64,7 @@ project(':grpc-protobuf').projectDir = "$rootDir/protobuf" as File
project(':grpc-protobuf-lite').projectDir = "$rootDir/protobuf-lite" as File
project(':grpc-netty').projectDir = "$rootDir/netty" as File
project(':grpc-netty-shaded').projectDir = "$rootDir/netty/shaded" as File
project(':grpc-googleapis').projectDir = "$rootDir/googleapis" as File
project(':grpc-grpclb').projectDir = "$rootDir/grpclb" as File
project(':grpc-testing').projectDir = "$rootDir/testing" as File
project(':grpc-testing-proto').projectDir = "$rootDir/testing-proto" as File
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2022 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.xds;

import io.grpc.Internal;
import java.util.Map;

/**
* Accessor for global factory for managing XdsClient instance.
*/
@Internal
public final class InternalSharedXdsClientPoolProvider {
// Prevent instantiation
private InternalSharedXdsClientPoolProvider() {}

public static void setDefaultProviderBootstrapOverride(Map<String, ?> bootstrap) {
SharedXdsClientPoolProvider.getDefaultProvider().setBootstrapOverride(bootstrap);
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
io.grpc.xds.XdsNameResolverProvider
io.grpc.xds.GoogleCloudToProdNameResolverProvider

0 comments on commit ecc8cc3

Please sign in to comment.