forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
XdsNameResolver.java
307 lines (285 loc) · 11.8 KB
/
XdsNameResolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright 2019 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 static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Stopwatch;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.gson.Gson;
import io.envoyproxy.envoy.api.v2.core.Node;
import io.grpc.Attributes;
import io.grpc.EquivalentAddressGroup;
import io.grpc.InternalLogId;
import io.grpc.NameResolver;
import io.grpc.Status;
import io.grpc.Status.Code;
import io.grpc.SynchronizationContext;
import io.grpc.internal.BackoffPolicy;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.ObjectPool;
import io.grpc.xds.Bootstrapper.BootstrapInfo;
import io.grpc.xds.Bootstrapper.ServerInfo;
import io.grpc.xds.EnvoyProtoData.ClusterWeight;
import io.grpc.xds.EnvoyProtoData.Route;
import io.grpc.xds.EnvoyProtoData.RouteAction;
import io.grpc.xds.XdsClient.ConfigUpdate;
import io.grpc.xds.XdsClient.ConfigWatcher;
import io.grpc.xds.XdsClient.RefCountedXdsClientObjectPool;
import io.grpc.xds.XdsClient.XdsChannelFactory;
import io.grpc.xds.XdsClient.XdsClientFactory;
import io.grpc.xds.XdsLogger.XdsLogLevel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import javax.annotation.Nullable;
/**
* A {@link NameResolver} for resolving gRPC target names with "xds-experimental" scheme.
*
* <p>Resolving a gRPC target involves contacting the control plane management server via xDS
* protocol to retrieve service information and produce a service config to the caller.
*
* @see XdsNameResolverProvider
*/
final class XdsNameResolver extends NameResolver {
private final XdsLogger logger;
private final String authority;
private final XdsChannelFactory channelFactory;
private final SynchronizationContext syncContext;
private final ScheduledExecutorService timeService;
private final ServiceConfigParser serviceConfigParser;
private final BackoffPolicy.Provider backoffPolicyProvider;
private final Supplier<Stopwatch> stopwatchSupplier;
private final Bootstrapper bootstrapper;
@Nullable
private ObjectPool<XdsClient> xdsClientPool;
@Nullable
private XdsClient xdsClient;
XdsNameResolver(
String name,
Args args,
BackoffPolicy.Provider backoffPolicyProvider,
Supplier<Stopwatch> stopwatchSupplier,
XdsChannelFactory channelFactory,
Bootstrapper bootstrapper) {
authority = GrpcUtil.checkAuthority(checkNotNull(name, "name"));
this.channelFactory = checkNotNull(channelFactory, "channelFactory");
this.syncContext = checkNotNull(args.getSynchronizationContext(), "syncContext");
this.timeService = checkNotNull(args.getScheduledExecutorService(), "timeService");
this.serviceConfigParser = checkNotNull(args.getServiceConfigParser(), "serviceConfigParser");
this.backoffPolicyProvider = checkNotNull(backoffPolicyProvider, "backoffPolicyProvider");
this.stopwatchSupplier = checkNotNull(stopwatchSupplier, "stopwatchSupplier");
this.bootstrapper = checkNotNull(bootstrapper, "bootstrapper");
logger = XdsLogger.withLogId(InternalLogId.allocate("xds-resolver", name));
logger.log(XdsLogLevel.INFO, "Created resolver for {0}", name);
}
@Override
public String getServiceAuthority() {
return authority;
}
@Override
public void start(Listener2 listener) {
BootstrapInfo bootstrapInfo;
try {
bootstrapInfo = bootstrapper.readBootstrap();
} catch (Exception e) {
listener.onError(Status.UNAVAILABLE.withDescription("Failed to bootstrap").withCause(e));
return;
}
final List<ServerInfo> serverList = bootstrapInfo.getServers();
final Node node = bootstrapInfo.getNode();
if (serverList.isEmpty()) {
listener.onError(
Status.UNAVAILABLE.withDescription("No management server provided by bootstrap"));
return;
}
XdsClientFactory xdsClientFactory = new XdsClientFactory() {
@Override
XdsClient createXdsClient() {
return
new XdsClientImpl(
authority,
serverList,
channelFactory,
node,
syncContext,
timeService,
backoffPolicyProvider,
stopwatchSupplier);
}
};
xdsClientPool = new RefCountedXdsClientObjectPool(xdsClientFactory);
xdsClient = xdsClientPool.getObject();
xdsClient.watchConfigData(authority, new ConfigWatcherImpl(listener));
}
private class ConfigWatcherImpl implements ConfigWatcher {
final Listener2 listener;
ConfigWatcherImpl(Listener2 listener) {
this.listener = listener;
}
@Override
public void onConfigChanged(ConfigUpdate update) {
Map<String, ?> rawLbConfig;
if (update.getRoutes().size() > 1) {
logger.log(
XdsLogLevel.INFO,
"Received config update with {0} routes from xDS client {1}",
update.getRoutes().size(),
xdsClient);
rawLbConfig = generateXdsRoutingRawConfig(update.getRoutes());
} else {
Route defaultRoute = Iterables.getOnlyElement(update.getRoutes());
String clusterName = defaultRoute.getRouteAction().getCluster();
if (!clusterName.isEmpty()) {
logger.log(
XdsLogLevel.INFO,
"Received config update from xDS client {0}: cluster_name={1}",
xdsClient,
clusterName);
rawLbConfig = generateCdsRawConfig(clusterName);
} else {
logger.log(
XdsLogLevel.INFO,
"Received config update with one weighted cluster route from xDS client {0}",
xdsClient);
List<ClusterWeight> clusterWeights = defaultRoute.getRouteAction().getWeightedCluster();
rawLbConfig = generateWeightedTargetRawConfig(clusterWeights);
}
}
Map<String, ?> serviceConfig =
ImmutableMap.of("loadBalancingConfig", ImmutableList.of(rawLbConfig));
if (logger.isLoggable(XdsLogLevel.INFO)) {
logger.log(
XdsLogLevel.INFO,
"Generated service config:\n{0}",
new Gson().toJson(serviceConfig));
}
Attributes attrs =
Attributes.newBuilder()
.set(XdsAttributes.XDS_CLIENT_POOL, xdsClientPool)
.build();
ConfigOrError parsedServiceConfig = serviceConfigParser.parseServiceConfig(serviceConfig);
ResolutionResult result =
ResolutionResult.newBuilder()
.setAddresses(ImmutableList.<EquivalentAddressGroup>of())
.setAttributes(attrs)
.setServiceConfig(parsedServiceConfig)
.build();
listener.onResult(result);
}
@Override
public void onError(Status error) {
// In order to distinguish between IO error and resource not found, which trigger
// different handling, return an empty resolution result to channel for resource not
// found.
// TODO(chengyuanzhang): Returning an empty resolution result based on status code is
// a temporary solution. More design discussion needs to be done.
if (error.getCode().equals(Code.NOT_FOUND)) {
logger.log(
XdsLogLevel.WARNING,
"Received error from xDS client {0}: {1}", xdsClient, error.getDescription());
listener.onResult(ResolutionResult.newBuilder().build());
return;
}
listener.onError(Status.UNAVAILABLE.withDescription(error.getDescription()));
}
}
private static ImmutableMap<String, ?> generateXdsRoutingRawConfig(List<Route> routesUpdate) {
List<Object> routes = new ArrayList<>(routesUpdate.size());
Map<String, Object> actions = new LinkedHashMap<>();
Map<RouteAction, String> exitingActions = new HashMap<>();
for (Route route : routesUpdate) {
String service = "";
String method = "";
if (!route.getRouteMatch().isDefaultMatcher()) {
String prefix = route.getRouteMatch().getPrefix();
String path = route.getRouteMatch().getPath();
if (!prefix.isEmpty()) {
service = prefix.substring(1, prefix.length() - 1);
} else if (!path.isEmpty()) {
int splitIndex = path.lastIndexOf('/');
service = path.substring(1, splitIndex);
method = path.substring(splitIndex + 1);
}
}
Map<String, String> methodName = ImmutableMap.of("service", service, "method", method);
String actionName;
RouteAction routeAction = route.getRouteAction();
Map<String, ?> actionPolicy;
if (exitingActions.containsKey(routeAction)) {
actionName = exitingActions.get(routeAction);
} else {
if (!routeAction.getCluster().isEmpty()) {
actionName = "cds:" + routeAction.getCluster();
actionPolicy = generateCdsRawConfig(routeAction.getCluster());
} else {
StringBuilder sb = new StringBuilder("weighted:");
List<ClusterWeight> clusterWeights = routeAction.getWeightedCluster();
for (ClusterWeight clusterWeight : clusterWeights) {
sb.append(clusterWeight.getName()).append('_');
}
sb.append(routeAction.hashCode());
actionName = sb.toString();
if (actions.containsKey(actionName)) {
// Just in case of hash collision, append exitingActions.size() to make actionName
// unique. However, in case of collision, when new ConfigUpdate is received, actions
// and actionNames might be associated differently from the previous update, but it
// is just suboptimal and won't cause a problem.
actionName = actionName + "_" + exitingActions.size();
}
actionPolicy = generateWeightedTargetRawConfig(clusterWeights);
}
exitingActions.put(routeAction, actionName);
List<?> childPolicies = ImmutableList.of(actionPolicy);
actions.put(actionName, ImmutableMap.of("childPolicy", childPolicies));
}
routes.add(ImmutableMap.of("methodName", methodName, "action", actionName));
}
return ImmutableMap.of(
XdsLbPolicies.XDS_ROUTING_POLICY_NAME,
ImmutableMap.of("route", routes, "action", actions));
}
private static ImmutableMap<String, ?> generateWeightedTargetRawConfig(
List<ClusterWeight> clusterWeights) {
Map<String, Object> targets = new LinkedHashMap<>();
for (ClusterWeight clusterWeight : clusterWeights) {
Map<String, ?> childPolicy = generateCdsRawConfig(clusterWeight.getName());
Map<String, ?> weightedConfig = ImmutableMap.of(
"weight",
(double) clusterWeight.getWeight(),
"childPolicy",
ImmutableList.of(childPolicy));
targets.put(clusterWeight.getName(), weightedConfig);
}
return ImmutableMap.of(
XdsLbPolicies.WEIGHTED_TARGET_POLICY_NAME,
ImmutableMap.of("targets", targets));
}
private static ImmutableMap<String, ?> generateCdsRawConfig(String clusterName) {
return ImmutableMap.of(XdsLbPolicies.CDS_POLICY_NAME, ImmutableMap.of("cluster", clusterName));
}
@Override
public void shutdown() {
logger.log(XdsLogLevel.INFO, "Shutdown");
if (xdsClient != null) {
xdsClient = xdsClientPool.returnObject(xdsClient);
}
}
}