Skip to content

Commit

Permalink
xds: migrate Bootstrapper data classes to use AutoValue (#8594)
Browse files Browse the repository at this point in the history
As many new fields will be added to `BootstrapInfo` for xds federation support, refactor `Bootstrapper.java` to use `AutoValue`. All the other files are just mechanical changes due to the refactoring.
  • Loading branch information
dapengzhang0 committed Oct 14, 2021
1 parent 8e5c188 commit 9f644a0
Show file tree
Hide file tree
Showing 20 changed files with 188 additions and 200 deletions.
6 changes: 3 additions & 3 deletions xds/src/main/java/io/grpc/xds/AbstractXdsClient.java
Expand Up @@ -303,7 +303,7 @@ protected final boolean isInBackoff() {
// Must be synchronized.
private void startRpcStream() {
checkState(adsStream == null, "Previous adsStream has not been cleared yet");
if (bootstrapInfo.getServers().get(0).isUseProtocolV3()) {
if (bootstrapInfo.servers().get(0).useProtocolV3()) {
adsStream = new AdsStreamV3();
} else {
adsStream = new AdsStreamV2();
Expand Down Expand Up @@ -619,7 +619,7 @@ void sendDiscoveryRequest(ResourceType type, String versionInfo, Collection<Stri
io.envoyproxy.envoy.api.v2.DiscoveryRequest.Builder builder =
io.envoyproxy.envoy.api.v2.DiscoveryRequest.newBuilder()
.setVersionInfo(versionInfo)
.setNode(bootstrapInfo.getNode().toEnvoyProtoNodeV2())
.setNode(bootstrapInfo.node().toEnvoyProtoNodeV2())
.addAllResourceNames(resources)
.setTypeUrl(type.typeUrlV2())
.setResponseNonce(nonce);
Expand Down Expand Up @@ -696,7 +696,7 @@ void sendDiscoveryRequest(ResourceType type, String versionInfo, Collection<Stri
DiscoveryRequest.Builder builder =
DiscoveryRequest.newBuilder()
.setVersionInfo(versionInfo)
.setNode(bootstrapInfo.getNode().toEnvoyProtoNode())
.setNode(bootstrapInfo.node().toEnvoyProtoNode())
.addAllResourceNames(resources)
.setTypeUrl(type.typeUrl())
.setResponseNonce(nonce);
Expand Down
116 changes: 46 additions & 70 deletions xds/src/main/java/io/grpc/xds/Bootstrapper.java
Expand Up @@ -16,13 +16,13 @@

package io.grpc.xds;

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

import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.grpc.ChannelCredentials;
import io.grpc.Internal;
import io.grpc.xds.EnvoyProtoData.Node;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
Expand All @@ -49,101 +49,77 @@ BootstrapInfo bootstrap(Map<String, ?> rawData) throws XdsInitializationExceptio
* Data class containing xDS server information, such as server URI and channel credentials
* to be used for communication.
*/
@AutoValue
@Internal
static class ServerInfo {
private final String target;
private final ChannelCredentials channelCredentials;
private final boolean useProtocolV3;
abstract static class ServerInfo {
abstract String target();

@VisibleForTesting
ServerInfo(String target, ChannelCredentials channelCredentials, boolean useProtocolV3) {
this.target = checkNotNull(target, "target");
this.channelCredentials = checkNotNull(channelCredentials, "channelCredentials");
this.useProtocolV3 = useProtocolV3;
}
abstract ChannelCredentials channelCredentials();

String getTarget() {
return target;
}
abstract boolean useProtocolV3();

ChannelCredentials getChannelCredentials() {
return channelCredentials;
}

boolean isUseProtocolV3() {
return useProtocolV3;
@VisibleForTesting
static ServerInfo create(
String target, ChannelCredentials channelCredentials, boolean useProtocolV3) {
return new AutoValue_Bootstrapper_ServerInfo(target, channelCredentials, useProtocolV3);
}
}

/**
* Data class containing Certificate provider information: the plugin-name and an opaque
* Map that represents the config for that plugin.
*/
@AutoValue
@Internal
public static class CertificateProviderInfo {
private final String pluginName;
private final Map<String, ?> config;
public abstract static class CertificateProviderInfo {
public abstract String pluginName();

@VisibleForTesting
public CertificateProviderInfo(String pluginName, Map<String, ?> config) {
this.pluginName = checkNotNull(pluginName, "pluginName");
this.config = checkNotNull(config, "config");
}

public String getPluginName() {
return pluginName;
}
public abstract ImmutableMap<String, ?> config();

public Map<String, ?> getConfig() {
return config;
@VisibleForTesting
public static CertificateProviderInfo create(String pluginName, Map<String, ?> config) {
return new AutoValue_Bootstrapper_CertificateProviderInfo(
pluginName, ImmutableMap.copyOf(config));
}
}

/**
* Data class containing the results of reading bootstrap.
*/
@AutoValue
@Internal
public static class BootstrapInfo {
private List<ServerInfo> servers;
private final Node node;
@Nullable private final Map<String, CertificateProviderInfo> certProviders;
@Nullable private final String serverListenerResourceNameTemplate;
public abstract static class BootstrapInfo {
/** Returns the list of xDS servers to be connected to. */
abstract ImmutableList<ServerInfo> servers();

/** Returns the node identifier to be included in xDS requests. */
public abstract Node node();

/** Returns the cert-providers config map. */
@Nullable
public abstract ImmutableMap<String, CertificateProviderInfo> certProviders();

@Nullable
public abstract String serverListenerResourceNameTemplate();

@VisibleForTesting
BootstrapInfo(
List<ServerInfo> servers,
Node node,
Map<String, CertificateProviderInfo> certProviders,
String serverListenerResourceNameTemplate) {
this.servers = servers;
this.node = node;
this.certProviders = certProviders;
this.serverListenerResourceNameTemplate = serverListenerResourceNameTemplate;
static Builder builder() {
return new AutoValue_Bootstrapper_BootstrapInfo.Builder();
}

/**
* Returns the list of xDS servers to be connected to.
*/
List<ServerInfo> getServers() {
return Collections.unmodifiableList(servers);
}
@AutoValue.Builder
@VisibleForTesting
abstract static class Builder {
abstract Builder servers(List<ServerInfo> servers);

/**
* Returns the node identifier to be included in xDS requests.
*/
public Node getNode() {
return node;
}
abstract Builder node(Node node);

/** Returns the cert-providers config map. */
@Nullable
public Map<String, CertificateProviderInfo> getCertProviders() {
return certProviders == null ? null : Collections.unmodifiableMap(certProviders);
}
abstract Builder certProviders(@Nullable Map<String, CertificateProviderInfo> certProviders);

@Nullable
public String getServerListenerResourceNameTemplate() {
return serverListenerResourceNameTemplate;
abstract Builder serverListenerResourceNameTemplate(
@Nullable String serverListenerResourceNameTemplate);

abstract BootstrapInfo build();
}
}
}
11 changes: 8 additions & 3 deletions xds/src/main/java/io/grpc/xds/BootstrapperImpl.java
Expand Up @@ -154,7 +154,7 @@ BootstrapInfo bootstrap(Map<String, ?> rawData) throws XdsInitializationExceptio
logger.log(XdsLogLevel.INFO, "Server features: {0}", serverFeatures);
useProtocolV3 = serverFeatures.contains(XDS_V3_SERVER_FEATURE);
}
servers.add(new ServerInfo(serverUri, channelCredentials, useProtocolV3));
servers.add(ServerInfo.create(serverUri, channelCredentials, useProtocolV3));
}

Node.Builder nodeBuilder = Node.newBuilder();
Expand Down Expand Up @@ -211,13 +211,18 @@ BootstrapInfo bootstrap(Map<String, ?> rawData) throws XdsInitializationExceptio
checkForNull(JsonUtil.getString(valueMap, "plugin_name"), "plugin_name");
Map<String, ?> config = checkForNull(JsonUtil.getObject(valueMap, "config"), "config");
CertificateProviderInfo certificateProviderInfo =
new CertificateProviderInfo(pluginName, config);
CertificateProviderInfo.create(pluginName, config);
certProviders.put(name, certificateProviderInfo);
}
}
String grpcServerResourceId =
JsonUtil.getString(rawData, "server_listener_resource_name_template");
return new BootstrapInfo(servers, nodeBuilder.build(), certProviders, grpcServerResourceId);
return BootstrapInfo.builder()
.servers(servers)
.node(nodeBuilder.build())
.certProviders(certProviders)
.serverListenerResourceNameTemplate(grpcServerResourceId)
.build();
}

@VisibleForTesting
Expand Down
10 changes: 5 additions & 5 deletions xds/src/main/java/io/grpc/xds/ClientXdsClient.java
Expand Up @@ -186,7 +186,7 @@ final class ClientXdsClient extends AbstractXdsClient {
this.timeProvider = timeProvider;
this.tlsContextManager = checkNotNull(tlsContextManager, "tlsContextManager");
lrsClient = new LoadReportClient(loadStatsManager, channel, context,
bootstrapInfo.getServers().get(0).isUseProtocolV3(), bootstrapInfo.getNode(),
bootstrapInfo.servers().get(0).useProtocolV3(), bootstrapInfo.node(),
getSyncContext(), timeService, backoffPolicyProvider, stopwatchSupplier);
}

Expand Down Expand Up @@ -263,8 +263,8 @@ private LdsUpdate processServerSideListener(
Listener proto, Set<String> rdsResources, boolean parseHttpFilter)
throws ResourceInvalidException {
Set<String> certProviderInstances = null;
if (getBootstrapInfo() != null && getBootstrapInfo().getCertProviders() != null) {
certProviderInstances = getBootstrapInfo().getCertProviders().keySet();
if (getBootstrapInfo() != null && getBootstrapInfo().certProviders() != null) {
certProviderInstances = getBootstrapInfo().certProviders().keySet();
}
return LdsUpdate.forTcpListener(parseServerSideListener(
proto, rdsResources, tlsContextManager, filterRegistry, certProviderInstances,
Expand Down Expand Up @@ -1404,8 +1404,8 @@ protected void handleCdsResponse(String versionInfo, List<Any> resources, String
CdsUpdate cdsUpdate;
try {
Set<String> certProviderInstances = null;
if (getBootstrapInfo() != null && getBootstrapInfo().getCertProviders() != null) {
certProviderInstances = getBootstrapInfo().getCertProviders().keySet();
if (getBootstrapInfo() != null && getBootstrapInfo().certProviders() != null) {
certProviderInstances = getBootstrapInfo().certProviders().keySet();
}
cdsUpdate = parseCluster(cluster, retainedEdsResources, certProviderInstances);
} catch (ResourceInvalidException e) {
Expand Down
2 changes: 1 addition & 1 deletion xds/src/main/java/io/grpc/xds/CsdsService.java
Expand Up @@ -162,7 +162,7 @@ static ClientConfig getClientConfigForXdsClient(XdsClient xdsClient) {
xdsClient.getSubscribedResourcesMetadata(ResourceType.EDS));

return ClientConfig.newBuilder()
.setNode(xdsClient.getBootstrapInfo().getNode().toEnvoyProtoNode())
.setNode(xdsClient.getBootstrapInfo().node().toEnvoyProtoNode())
.addXdsConfig(PerXdsConfig.newBuilder().setListenerConfig(ldsConfig))
.addXdsConfig(PerXdsConfig.newBuilder().setRouteConfig(rdsConfig))
.addXdsConfig(PerXdsConfig.newBuilder().setClusterConfig(cdsConfig))
Expand Down
Expand Up @@ -90,7 +90,7 @@ public ObjectPool<XdsClient> getOrCreate() throws XdsInitializationException {
} else {
bootstrapInfo = bootstrapper.bootstrap();
}
if (bootstrapInfo.getServers().isEmpty()) {
if (bootstrapInfo.servers().isEmpty()) {
throw new XdsInitializationException("No xDS server provided");
}
ref = xdsClientPool = new RefCountedXdsClientObjectPool(bootstrapInfo);
Expand Down Expand Up @@ -128,9 +128,9 @@ static class RefCountedXdsClientObjectPool implements ObjectPool<XdsClient> {
public XdsClient getObject() {
synchronized (lock) {
if (refCount == 0) {
ServerInfo serverInfo = bootstrapInfo.getServers().get(0); // use first server
String target = serverInfo.getTarget();
ChannelCredentials channelCredentials = serverInfo.getChannelCredentials();
ServerInfo serverInfo = bootstrapInfo.servers().get(0); // use first server
String target = serverInfo.target();
ChannelCredentials channelCredentials = serverInfo.channelCredentials();
channel = Grpc.newChannelBuilder(target, channelCredentials)
.keepAliveTime(5, TimeUnit.MINUTES)
.build();
Expand Down
4 changes: 2 additions & 2 deletions xds/src/main/java/io/grpc/xds/XdsServerWrapper.java
Expand Up @@ -181,8 +181,8 @@ private void internalStart() {
return;
}
xdsClient = xdsClientPool.getObject();
boolean useProtocolV3 = xdsClient.getBootstrapInfo().getServers().get(0).isUseProtocolV3();
String listenerTemplate = xdsClient.getBootstrapInfo().getServerListenerResourceNameTemplate();
boolean useProtocolV3 = xdsClient.getBootstrapInfo().servers().get(0).useProtocolV3();
String listenerTemplate = xdsClient.getBootstrapInfo().serverListenerResourceNameTemplate();
if (!useProtocolV3 || listenerTemplate == null) {
StatusException statusException =
Status.UNAVAILABLE.withDescription(
Expand Down
Expand Up @@ -61,8 +61,8 @@ protected CertProviderSslContextProvider(
certHandle = certProviderInstanceConfig == null ? null
: certificateProviderStore.createOrGetProvider(
certInstance.getCertificateName(),
certProviderInstanceConfig.getPluginName(),
certProviderInstanceConfig.getConfig(),
certProviderInstanceConfig.pluginName(),
certProviderInstanceConfig.config(),
this,
true);
} else {
Expand All @@ -76,8 +76,8 @@ protected CertProviderSslContextProvider(
rootCertHandle = certProviderInstanceConfig == null ? null
: certificateProviderStore.createOrGetProvider(
rootCertInstance.getCertificateName(),
certProviderInstanceConfig.getPluginName(),
certProviderInstanceConfig.getConfig(),
certProviderInstanceConfig.pluginName(),
certProviderInstanceConfig.config(),
this,
true);
} else {
Expand Down
Expand Up @@ -52,8 +52,8 @@ public SslContextProvider create(UpstreamTlsContext upstreamTlsContext) {
upstreamTlsContext.getCommonTlsContext())) {
return certProviderClientSslContextProviderFactory.getProvider(
upstreamTlsContext,
bootstrapInfo.getNode().toEnvoyProtoNode(),
bootstrapInfo.getCertProviders());
bootstrapInfo.node().toEnvoyProtoNode(),
bootstrapInfo.certProviders());
}
throw new UnsupportedOperationException("Unsupported configurations in UpstreamTlsContext!");
}
Expand Down
Expand Up @@ -53,8 +53,8 @@ public SslContextProvider create(
downstreamTlsContext.getCommonTlsContext())) {
return certProviderServerSslContextProviderFactory.getProvider(
downstreamTlsContext,
bootstrapInfo.getNode().toEnvoyProtoNode(),
bootstrapInfo.getCertProviders());
bootstrapInfo.node().toEnvoyProtoNode(),
bootstrapInfo.certProviders());
}
throw new UnsupportedOperationException("Unsupported configurations in DownstreamTlsContext!");
}
Expand Down

0 comments on commit 9f644a0

Please sign in to comment.