Skip to content

Commit 9f644a0

Browse files
authored
xds: migrate Bootstrapper data classes to use AutoValue (#8594)
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.
1 parent 8e5c188 commit 9f644a0

20 files changed

+188
-200
lines changed

xds/src/main/java/io/grpc/xds/AbstractXdsClient.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ protected final boolean isInBackoff() {
303303
// Must be synchronized.
304304
private void startRpcStream() {
305305
checkState(adsStream == null, "Previous adsStream has not been cleared yet");
306-
if (bootstrapInfo.getServers().get(0).isUseProtocolV3()) {
306+
if (bootstrapInfo.servers().get(0).useProtocolV3()) {
307307
adsStream = new AdsStreamV3();
308308
} else {
309309
adsStream = new AdsStreamV2();
@@ -619,7 +619,7 @@ void sendDiscoveryRequest(ResourceType type, String versionInfo, Collection<Stri
619619
io.envoyproxy.envoy.api.v2.DiscoveryRequest.Builder builder =
620620
io.envoyproxy.envoy.api.v2.DiscoveryRequest.newBuilder()
621621
.setVersionInfo(versionInfo)
622-
.setNode(bootstrapInfo.getNode().toEnvoyProtoNodeV2())
622+
.setNode(bootstrapInfo.node().toEnvoyProtoNodeV2())
623623
.addAllResourceNames(resources)
624624
.setTypeUrl(type.typeUrlV2())
625625
.setResponseNonce(nonce);
@@ -696,7 +696,7 @@ void sendDiscoveryRequest(ResourceType type, String versionInfo, Collection<Stri
696696
DiscoveryRequest.Builder builder =
697697
DiscoveryRequest.newBuilder()
698698
.setVersionInfo(versionInfo)
699-
.setNode(bootstrapInfo.getNode().toEnvoyProtoNode())
699+
.setNode(bootstrapInfo.node().toEnvoyProtoNode())
700700
.addAllResourceNames(resources)
701701
.setTypeUrl(type.typeUrl())
702702
.setResponseNonce(nonce);

xds/src/main/java/io/grpc/xds/Bootstrapper.java

+46-70
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
package io.grpc.xds;
1818

19-
import static com.google.common.base.Preconditions.checkNotNull;
20-
19+
import com.google.auto.value.AutoValue;
2120
import com.google.common.annotations.VisibleForTesting;
21+
import com.google.common.collect.ImmutableList;
22+
import com.google.common.collect.ImmutableMap;
2223
import io.grpc.ChannelCredentials;
2324
import io.grpc.Internal;
2425
import io.grpc.xds.EnvoyProtoData.Node;
25-
import java.util.Collections;
2626
import java.util.List;
2727
import java.util.Map;
2828
import javax.annotation.Nullable;
@@ -49,101 +49,77 @@ BootstrapInfo bootstrap(Map<String, ?> rawData) throws XdsInitializationExceptio
4949
* Data class containing xDS server information, such as server URI and channel credentials
5050
* to be used for communication.
5151
*/
52+
@AutoValue
5253
@Internal
53-
static class ServerInfo {
54-
private final String target;
55-
private final ChannelCredentials channelCredentials;
56-
private final boolean useProtocolV3;
54+
abstract static class ServerInfo {
55+
abstract String target();
5756

58-
@VisibleForTesting
59-
ServerInfo(String target, ChannelCredentials channelCredentials, boolean useProtocolV3) {
60-
this.target = checkNotNull(target, "target");
61-
this.channelCredentials = checkNotNull(channelCredentials, "channelCredentials");
62-
this.useProtocolV3 = useProtocolV3;
63-
}
57+
abstract ChannelCredentials channelCredentials();
6458

65-
String getTarget() {
66-
return target;
67-
}
59+
abstract boolean useProtocolV3();
6860

69-
ChannelCredentials getChannelCredentials() {
70-
return channelCredentials;
71-
}
72-
73-
boolean isUseProtocolV3() {
74-
return useProtocolV3;
61+
@VisibleForTesting
62+
static ServerInfo create(
63+
String target, ChannelCredentials channelCredentials, boolean useProtocolV3) {
64+
return new AutoValue_Bootstrapper_ServerInfo(target, channelCredentials, useProtocolV3);
7565
}
7666
}
7767

7868
/**
7969
* Data class containing Certificate provider information: the plugin-name and an opaque
8070
* Map that represents the config for that plugin.
8171
*/
72+
@AutoValue
8273
@Internal
83-
public static class CertificateProviderInfo {
84-
private final String pluginName;
85-
private final Map<String, ?> config;
74+
public abstract static class CertificateProviderInfo {
75+
public abstract String pluginName();
8676

87-
@VisibleForTesting
88-
public CertificateProviderInfo(String pluginName, Map<String, ?> config) {
89-
this.pluginName = checkNotNull(pluginName, "pluginName");
90-
this.config = checkNotNull(config, "config");
91-
}
92-
93-
public String getPluginName() {
94-
return pluginName;
95-
}
77+
public abstract ImmutableMap<String, ?> config();
9678

97-
public Map<String, ?> getConfig() {
98-
return config;
79+
@VisibleForTesting
80+
public static CertificateProviderInfo create(String pluginName, Map<String, ?> config) {
81+
return new AutoValue_Bootstrapper_CertificateProviderInfo(
82+
pluginName, ImmutableMap.copyOf(config));
9983
}
10084
}
10185

10286
/**
10387
* Data class containing the results of reading bootstrap.
10488
*/
89+
@AutoValue
10590
@Internal
106-
public static class BootstrapInfo {
107-
private List<ServerInfo> servers;
108-
private final Node node;
109-
@Nullable private final Map<String, CertificateProviderInfo> certProviders;
110-
@Nullable private final String serverListenerResourceNameTemplate;
91+
public abstract static class BootstrapInfo {
92+
/** Returns the list of xDS servers to be connected to. */
93+
abstract ImmutableList<ServerInfo> servers();
94+
95+
/** Returns the node identifier to be included in xDS requests. */
96+
public abstract Node node();
97+
98+
/** Returns the cert-providers config map. */
99+
@Nullable
100+
public abstract ImmutableMap<String, CertificateProviderInfo> certProviders();
101+
102+
@Nullable
103+
public abstract String serverListenerResourceNameTemplate();
111104

112105
@VisibleForTesting
113-
BootstrapInfo(
114-
List<ServerInfo> servers,
115-
Node node,
116-
Map<String, CertificateProviderInfo> certProviders,
117-
String serverListenerResourceNameTemplate) {
118-
this.servers = servers;
119-
this.node = node;
120-
this.certProviders = certProviders;
121-
this.serverListenerResourceNameTemplate = serverListenerResourceNameTemplate;
106+
static Builder builder() {
107+
return new AutoValue_Bootstrapper_BootstrapInfo.Builder();
122108
}
123109

124-
/**
125-
* Returns the list of xDS servers to be connected to.
126-
*/
127-
List<ServerInfo> getServers() {
128-
return Collections.unmodifiableList(servers);
129-
}
110+
@AutoValue.Builder
111+
@VisibleForTesting
112+
abstract static class Builder {
113+
abstract Builder servers(List<ServerInfo> servers);
130114

131-
/**
132-
* Returns the node identifier to be included in xDS requests.
133-
*/
134-
public Node getNode() {
135-
return node;
136-
}
115+
abstract Builder node(Node node);
137116

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

144-
@Nullable
145-
public String getServerListenerResourceNameTemplate() {
146-
return serverListenerResourceNameTemplate;
119+
abstract Builder serverListenerResourceNameTemplate(
120+
@Nullable String serverListenerResourceNameTemplate);
121+
122+
abstract BootstrapInfo build();
147123
}
148124
}
149125
}

xds/src/main/java/io/grpc/xds/BootstrapperImpl.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ BootstrapInfo bootstrap(Map<String, ?> rawData) throws XdsInitializationExceptio
154154
logger.log(XdsLogLevel.INFO, "Server features: {0}", serverFeatures);
155155
useProtocolV3 = serverFeatures.contains(XDS_V3_SERVER_FEATURE);
156156
}
157-
servers.add(new ServerInfo(serverUri, channelCredentials, useProtocolV3));
157+
servers.add(ServerInfo.create(serverUri, channelCredentials, useProtocolV3));
158158
}
159159

160160
Node.Builder nodeBuilder = Node.newBuilder();
@@ -211,13 +211,18 @@ BootstrapInfo bootstrap(Map<String, ?> rawData) throws XdsInitializationExceptio
211211
checkForNull(JsonUtil.getString(valueMap, "plugin_name"), "plugin_name");
212212
Map<String, ?> config = checkForNull(JsonUtil.getObject(valueMap, "config"), "config");
213213
CertificateProviderInfo certificateProviderInfo =
214-
new CertificateProviderInfo(pluginName, config);
214+
CertificateProviderInfo.create(pluginName, config);
215215
certProviders.put(name, certificateProviderInfo);
216216
}
217217
}
218218
String grpcServerResourceId =
219219
JsonUtil.getString(rawData, "server_listener_resource_name_template");
220-
return new BootstrapInfo(servers, nodeBuilder.build(), certProviders, grpcServerResourceId);
220+
return BootstrapInfo.builder()
221+
.servers(servers)
222+
.node(nodeBuilder.build())
223+
.certProviders(certProviders)
224+
.serverListenerResourceNameTemplate(grpcServerResourceId)
225+
.build();
221226
}
222227

223228
@VisibleForTesting

xds/src/main/java/io/grpc/xds/ClientXdsClient.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ final class ClientXdsClient extends AbstractXdsClient {
186186
this.timeProvider = timeProvider;
187187
this.tlsContextManager = checkNotNull(tlsContextManager, "tlsContextManager");
188188
lrsClient = new LoadReportClient(loadStatsManager, channel, context,
189-
bootstrapInfo.getServers().get(0).isUseProtocolV3(), bootstrapInfo.getNode(),
189+
bootstrapInfo.servers().get(0).useProtocolV3(), bootstrapInfo.node(),
190190
getSyncContext(), timeService, backoffPolicyProvider, stopwatchSupplier);
191191
}
192192

@@ -263,8 +263,8 @@ private LdsUpdate processServerSideListener(
263263
Listener proto, Set<String> rdsResources, boolean parseHttpFilter)
264264
throws ResourceInvalidException {
265265
Set<String> certProviderInstances = null;
266-
if (getBootstrapInfo() != null && getBootstrapInfo().getCertProviders() != null) {
267-
certProviderInstances = getBootstrapInfo().getCertProviders().keySet();
266+
if (getBootstrapInfo() != null && getBootstrapInfo().certProviders() != null) {
267+
certProviderInstances = getBootstrapInfo().certProviders().keySet();
268268
}
269269
return LdsUpdate.forTcpListener(parseServerSideListener(
270270
proto, rdsResources, tlsContextManager, filterRegistry, certProviderInstances,
@@ -1404,8 +1404,8 @@ protected void handleCdsResponse(String versionInfo, List<Any> resources, String
14041404
CdsUpdate cdsUpdate;
14051405
try {
14061406
Set<String> certProviderInstances = null;
1407-
if (getBootstrapInfo() != null && getBootstrapInfo().getCertProviders() != null) {
1408-
certProviderInstances = getBootstrapInfo().getCertProviders().keySet();
1407+
if (getBootstrapInfo() != null && getBootstrapInfo().certProviders() != null) {
1408+
certProviderInstances = getBootstrapInfo().certProviders().keySet();
14091409
}
14101410
cdsUpdate = parseCluster(cluster, retainedEdsResources, certProviderInstances);
14111411
} catch (ResourceInvalidException e) {

xds/src/main/java/io/grpc/xds/CsdsService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static ClientConfig getClientConfigForXdsClient(XdsClient xdsClient) {
162162
xdsClient.getSubscribedResourcesMetadata(ResourceType.EDS));
163163

164164
return ClientConfig.newBuilder()
165-
.setNode(xdsClient.getBootstrapInfo().getNode().toEnvoyProtoNode())
165+
.setNode(xdsClient.getBootstrapInfo().node().toEnvoyProtoNode())
166166
.addXdsConfig(PerXdsConfig.newBuilder().setListenerConfig(ldsConfig))
167167
.addXdsConfig(PerXdsConfig.newBuilder().setRouteConfig(rdsConfig))
168168
.addXdsConfig(PerXdsConfig.newBuilder().setClusterConfig(cdsConfig))

xds/src/main/java/io/grpc/xds/SharedXdsClientPoolProvider.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public ObjectPool<XdsClient> getOrCreate() throws XdsInitializationException {
9090
} else {
9191
bootstrapInfo = bootstrapper.bootstrap();
9292
}
93-
if (bootstrapInfo.getServers().isEmpty()) {
93+
if (bootstrapInfo.servers().isEmpty()) {
9494
throw new XdsInitializationException("No xDS server provided");
9595
}
9696
ref = xdsClientPool = new RefCountedXdsClientObjectPool(bootstrapInfo);
@@ -128,9 +128,9 @@ static class RefCountedXdsClientObjectPool implements ObjectPool<XdsClient> {
128128
public XdsClient getObject() {
129129
synchronized (lock) {
130130
if (refCount == 0) {
131-
ServerInfo serverInfo = bootstrapInfo.getServers().get(0); // use first server
132-
String target = serverInfo.getTarget();
133-
ChannelCredentials channelCredentials = serverInfo.getChannelCredentials();
131+
ServerInfo serverInfo = bootstrapInfo.servers().get(0); // use first server
132+
String target = serverInfo.target();
133+
ChannelCredentials channelCredentials = serverInfo.channelCredentials();
134134
channel = Grpc.newChannelBuilder(target, channelCredentials)
135135
.keepAliveTime(5, TimeUnit.MINUTES)
136136
.build();

xds/src/main/java/io/grpc/xds/XdsServerWrapper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ private void internalStart() {
181181
return;
182182
}
183183
xdsClient = xdsClientPool.getObject();
184-
boolean useProtocolV3 = xdsClient.getBootstrapInfo().getServers().get(0).isUseProtocolV3();
185-
String listenerTemplate = xdsClient.getBootstrapInfo().getServerListenerResourceNameTemplate();
184+
boolean useProtocolV3 = xdsClient.getBootstrapInfo().servers().get(0).useProtocolV3();
185+
String listenerTemplate = xdsClient.getBootstrapInfo().serverListenerResourceNameTemplate();
186186
if (!useProtocolV3 || listenerTemplate == null) {
187187
StatusException statusException =
188188
Status.UNAVAILABLE.withDescription(

xds/src/main/java/io/grpc/xds/internal/certprovider/CertProviderSslContextProvider.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ protected CertProviderSslContextProvider(
6161
certHandle = certProviderInstanceConfig == null ? null
6262
: certificateProviderStore.createOrGetProvider(
6363
certInstance.getCertificateName(),
64-
certProviderInstanceConfig.getPluginName(),
65-
certProviderInstanceConfig.getConfig(),
64+
certProviderInstanceConfig.pluginName(),
65+
certProviderInstanceConfig.config(),
6666
this,
6767
true);
6868
} else {
@@ -76,8 +76,8 @@ protected CertProviderSslContextProvider(
7676
rootCertHandle = certProviderInstanceConfig == null ? null
7777
: certificateProviderStore.createOrGetProvider(
7878
rootCertInstance.getCertificateName(),
79-
certProviderInstanceConfig.getPluginName(),
80-
certProviderInstanceConfig.getConfig(),
79+
certProviderInstanceConfig.pluginName(),
80+
certProviderInstanceConfig.config(),
8181
this,
8282
true);
8383
} else {

xds/src/main/java/io/grpc/xds/internal/sds/ClientSslContextProviderFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public SslContextProvider create(UpstreamTlsContext upstreamTlsContext) {
5252
upstreamTlsContext.getCommonTlsContext())) {
5353
return certProviderClientSslContextProviderFactory.getProvider(
5454
upstreamTlsContext,
55-
bootstrapInfo.getNode().toEnvoyProtoNode(),
56-
bootstrapInfo.getCertProviders());
55+
bootstrapInfo.node().toEnvoyProtoNode(),
56+
bootstrapInfo.certProviders());
5757
}
5858
throw new UnsupportedOperationException("Unsupported configurations in UpstreamTlsContext!");
5959
}

xds/src/main/java/io/grpc/xds/internal/sds/ServerSslContextProviderFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public SslContextProvider create(
5353
downstreamTlsContext.getCommonTlsContext())) {
5454
return certProviderServerSslContextProviderFactory.getProvider(
5555
downstreamTlsContext,
56-
bootstrapInfo.getNode().toEnvoyProtoNode(),
57-
bootstrapInfo.getCertProviders());
56+
bootstrapInfo.node().toEnvoyProtoNode(),
57+
bootstrapInfo.certProviders());
5858
}
5959
throw new UnsupportedOperationException("Unsupported configurations in DownstreamTlsContext!");
6060
}

0 commit comments

Comments
 (0)