forked from grpc/grpc-java
/
XdsClient.java
626 lines (532 loc) · 18.1 KB
/
XdsClient.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
* 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.checkState;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.grpc.ManagedChannel;
import io.grpc.Status;
import io.grpc.internal.ObjectPool;
import io.grpc.xds.EnvoyProtoData.DropOverload;
import io.grpc.xds.EnvoyProtoData.Locality;
import io.grpc.xds.EnvoyProtoData.LocalityLbEndpoints;
import io.grpc.xds.EnvoyProtoData.Route;
import io.grpc.xds.EnvoyServerProtoData.Listener;
import io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext;
import io.grpc.xds.LoadStatsManager.LoadStatsStore;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nullable;
/**
* An {@link XdsClient} instance encapsulates all of the logic for communicating with the xDS
* server. It may create multiple RPC streams (or a single ADS stream) for a series of xDS
* protocols (e.g., LDS, RDS, VHDS, CDS and EDS) over a single channel. Watch-based interfaces
* are provided for each set of data needed by gRPC.
*/
abstract class XdsClient {
/**
* Data class containing the results of performing a series of resource discovery RPCs via
* LDS/RDS/VHDS protocols. The results may include configurations for path/host rewriting,
* traffic mirroring, retry or hedging, default timeouts and load balancing policy that will
* be used to generate a service config.
*/
static final class ConfigUpdate {
private final List<Route> routes;
private ConfigUpdate(List<Route> routes) {
this.routes = routes;
}
List<Route> getRoutes() {
return routes;
}
@Override
public String toString() {
return
MoreObjects
.toStringHelper(this)
.add("routes", routes)
.toString();
}
static Builder newBuilder() {
return new Builder();
}
static final class Builder {
private final List<Route> routes = new ArrayList<>();
// Use ConfigUpdate.newBuilder().
private Builder() {
}
Builder addRoutes(Collection<Route> route) {
routes.addAll(route);
return this;
}
ConfigUpdate build() {
checkState(!routes.isEmpty(), "routes is empty");
return new ConfigUpdate(Collections.unmodifiableList(routes));
}
}
}
/**
* Data class containing the results of performing a resource discovery RPC via CDS protocol.
* The results include configurations for a single upstream cluster, such as endpoint discovery
* type, load balancing policy, connection timeout and etc.
*/
static final class ClusterUpdate {
private final String clusterName;
@Nullable
private final String edsServiceName;
private final String lbPolicy;
@Nullable
private final String lrsServerName;
private final UpstreamTlsContext upstreamTlsContext;
private ClusterUpdate(
String clusterName,
@Nullable String edsServiceName,
String lbPolicy,
@Nullable String lrsServerName,
@Nullable UpstreamTlsContext upstreamTlsContext) {
this.clusterName = clusterName;
this.edsServiceName = edsServiceName;
this.lbPolicy = lbPolicy;
this.lrsServerName = lrsServerName;
this.upstreamTlsContext = upstreamTlsContext;
}
String getClusterName() {
return clusterName;
}
/**
* Returns the resource name for EDS requests.
*/
@Nullable
String getEdsServiceName() {
return edsServiceName;
}
/**
* Returns the policy of balancing loads to endpoints. Only "round_robin" is supported
* as of now.
*/
String getLbPolicy() {
return lbPolicy;
}
/**
* Returns the server name to send client load reports to if LRS is enabled. {@code null} if
* load reporting is disabled for this cluster.
*/
@Nullable
String getLrsServerName() {
return lrsServerName;
}
/** Returns the {@link UpstreamTlsContext} for this cluster if present, else null. */
@Nullable
UpstreamTlsContext getUpstreamTlsContext() {
return upstreamTlsContext;
}
@Override
public String toString() {
return
MoreObjects
.toStringHelper(this)
.add("clusterName", clusterName)
.add("edsServiceName", edsServiceName)
.add("lbPolicy", lbPolicy)
.add("lrsServerName", lrsServerName)
.add("upstreamTlsContext", upstreamTlsContext)
.toString();
}
@Override
public int hashCode() {
return Objects.hash(
clusterName, edsServiceName, lbPolicy, lrsServerName, upstreamTlsContext);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ClusterUpdate that = (ClusterUpdate) o;
return Objects.equals(clusterName, that.clusterName)
&& Objects.equals(edsServiceName, that.edsServiceName)
&& Objects.equals(lbPolicy, that.lbPolicy)
&& Objects.equals(lrsServerName, that.lrsServerName)
&& Objects.equals(upstreamTlsContext, that.upstreamTlsContext);
}
static Builder newBuilder() {
return new Builder();
}
static final class Builder {
private String clusterName;
@Nullable
private String edsServiceName;
private String lbPolicy;
@Nullable
private String lrsServerName;
@Nullable
private UpstreamTlsContext upstreamTlsContext;
// Use ClusterUpdate.newBuilder().
private Builder() {
}
Builder setClusterName(String clusterName) {
this.clusterName = clusterName;
return this;
}
Builder setEdsServiceName(String edsServiceName) {
this.edsServiceName = edsServiceName;
return this;
}
Builder setLbPolicy(String lbPolicy) {
this.lbPolicy = lbPolicy;
return this;
}
Builder setLrsServerName(String lrsServerName) {
this.lrsServerName = lrsServerName;
return this;
}
Builder setUpstreamTlsContext(UpstreamTlsContext upstreamTlsContext) {
this.upstreamTlsContext = upstreamTlsContext;
return this;
}
ClusterUpdate build() {
checkState(clusterName != null, "clusterName is not set");
checkState(lbPolicy != null, "lbPolicy is not set");
return
new ClusterUpdate(
clusterName, edsServiceName, lbPolicy, lrsServerName, upstreamTlsContext);
}
}
}
/**
* Data class containing the results of performing a resource discovery RPC via EDS protocol.
* The results include endpoint addresses running the requested service, as well as
* configurations for traffic control such as drop overloads, inter-cluster load balancing
* policy and etc.
*/
static final class EndpointUpdate {
private final String clusterName;
private final Map<Locality, LocalityLbEndpoints> localityLbEndpointsMap;
private final List<DropOverload> dropPolicies;
private EndpointUpdate(
String clusterName,
Map<Locality, LocalityLbEndpoints> localityLbEndpoints,
List<DropOverload> dropPolicies) {
this.clusterName = clusterName;
this.localityLbEndpointsMap = localityLbEndpoints;
this.dropPolicies = dropPolicies;
}
static Builder newBuilder() {
return new Builder();
}
String getClusterName() {
return clusterName;
}
/**
* Returns a map of localities with endpoints load balancing information in each locality.
*/
Map<Locality, LocalityLbEndpoints> getLocalityLbEndpointsMap() {
return Collections.unmodifiableMap(localityLbEndpointsMap);
}
/**
* Returns a list of drop policies to be applied to outgoing requests.
*/
List<DropOverload> getDropPolicies() {
return Collections.unmodifiableList(dropPolicies);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
EndpointUpdate that = (EndpointUpdate) o;
return Objects.equals(clusterName, that.clusterName)
&& Objects.equals(localityLbEndpointsMap, that.localityLbEndpointsMap)
&& Objects.equals(dropPolicies, that.dropPolicies);
}
@Override
public int hashCode() {
return Objects.hash(clusterName, localityLbEndpointsMap, dropPolicies);
}
@Override
public String toString() {
return
MoreObjects
.toStringHelper(this)
.add("clusterName", clusterName)
.add("localityLbEndpointsMap", localityLbEndpointsMap)
.add("dropPolicies", dropPolicies)
.toString();
}
static final class Builder {
private String clusterName;
private Map<Locality, LocalityLbEndpoints> localityLbEndpointsMap = new LinkedHashMap<>();
private List<DropOverload> dropPolicies = new ArrayList<>();
// Use EndpointUpdate.newBuilder().
private Builder() {
}
Builder setClusterName(String clusterName) {
this.clusterName = clusterName;
return this;
}
Builder addLocalityLbEndpoints(Locality locality, LocalityLbEndpoints info) {
localityLbEndpointsMap.put(locality, info);
return this;
}
Builder addDropPolicy(DropOverload policy) {
dropPolicies.add(policy);
return this;
}
EndpointUpdate build() {
checkState(clusterName != null, "clusterName is not set");
return
new EndpointUpdate(
clusterName,
ImmutableMap.copyOf(localityLbEndpointsMap),
ImmutableList.copyOf(dropPolicies));
}
}
}
/**
* Updates via resource discovery RPCs using LDS. Includes {@link Listener} object containing
* config for security, RBAC or other server side features such as rate limit.
*/
static final class ListenerUpdate {
// TODO(sanjaypujare): flatten structure by moving Listener class members here.
private final Listener listener;
private ListenerUpdate(Listener listener) {
this.listener = listener;
}
public Listener getListener() {
return listener;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("listener", listener)
.toString();
}
static Builder newBuilder() {
return new Builder();
}
static final class Builder {
private Listener listener;
// Use ListenerUpdate.newBuilder().
private Builder() {
}
Builder setListener(Listener listener) {
this.listener = listener;
return this;
}
ListenerUpdate build() {
checkState(listener != null, "listener is not set");
return new ListenerUpdate(listener);
}
}
}
/**
* Watcher interface for a single requested xDS resource.
*/
private interface ResourceWatcher {
/**
* Called when the resource discovery RPC encounters some transient error.
*/
void onError(Status error);
/**
* Called when the requested resource is not available.
*
* @param resourceName name of the resource requested in discovery request.
*/
void onResourceDoesNotExist(String resourceName);
}
/**
* Config watcher interface. To be implemented by the xDS resolver.
*/
interface ConfigWatcher extends ResourceWatcher {
/**
* Called when receiving an update on virtual host configurations.
*/
void onConfigChanged(ConfigUpdate update);
}
/**
* Cluster watcher interface.
*/
interface ClusterWatcher extends ResourceWatcher {
void onClusterChanged(ClusterUpdate update);
}
/**
* Endpoint watcher interface.
*/
interface EndpointWatcher extends ResourceWatcher {
void onEndpointChanged(EndpointUpdate update);
}
/**
* Listener watcher interface. To be used by {@link io.grpc.xds.internal.sds.XdsServerBuilder}.
*/
interface ListenerWatcher extends ResourceWatcher {
/**
* Called when receiving an update on Listener configuration.
*/
void onListenerChanged(ListenerUpdate update);
}
/**
* Shutdown this {@link XdsClient} and release resources.
*/
abstract void shutdown();
/**
* Registers a watcher to receive {@link ConfigUpdate} for service with the given target
* authority.
*
* <p>Unlike watchers for cluster data and endpoint data, at most one ConfigWatcher can be
* registered. Once it is registered, it cannot be unregistered.
*
* @param targetAuthority authority of the "xds:" URI for the server name that the gRPC client
* targets for.
* @param watcher the {@link ConfigWatcher} to receive {@link ConfigUpdate}.
*/
void watchConfigData(String targetAuthority, ConfigWatcher watcher) {
}
/**
* Registers a data watcher for the given cluster.
*/
void watchClusterData(String clusterName, ClusterWatcher watcher) {
}
/**
* Unregisters the given cluster watcher, which was registered to receive updates for the
* given cluster.
*/
void cancelClusterDataWatch(String clusterName, ClusterWatcher watcher) {
}
/**
* Registers a data watcher for endpoints in the given cluster.
*/
void watchEndpointData(String clusterName, EndpointWatcher watcher) {
}
/**
* Unregisters the given endpoints watcher, which was registered to receive updates for
* endpoints information in the given cluster.
*/
void cancelEndpointDataWatch(String clusterName, EndpointWatcher watcher) {
}
/**
* Registers a watcher for a Listener with the given port.
*/
void watchListenerData(int port, ListenerWatcher watcher) {
}
/**
* Starts client side load reporting via LRS. All clusters report load through one LRS stream,
* only the first call of this method effectively starts the LRS stream.
*/
void reportClientStats() {
}
/**
* Stops client side load reporting via LRS. All clusters report load through one LRS stream,
* only the last call of this method effectively stops the LRS stream.
*/
void cancelClientStatsReport() {
}
/**
* Starts recording client load stats for the given cluster:cluster_service. Caller should use
* the returned {@link LoadStatsStore} to record and aggregate stats for load sent to the given
* cluster:cluster_service. Recorded stats may be reported to a load reporting server if enabled.
*/
LoadStatsStore addClientStats(String clusterName, @Nullable String clusterServiceName) {
throw new UnsupportedOperationException();
}
/**
* Stops recording client load stats for the given cluster:cluster_service. The load reporting
* server will no longer receive stats for the given cluster:cluster_service after this call.
*/
void removeClientStats(String clusterName, @Nullable String clusterServiceName) {
throw new UnsupportedOperationException();
}
// TODO(chengyuanzhang): eliminate this factory
abstract static class XdsClientFactory {
abstract XdsClient createXdsClient();
}
/**
* An {@link ObjectPool} holding reference and ref-count of an {@link XdsClient} instance.
* Initially the instance is null and the ref-count is zero. {@link #getObject()} will create a
* new XdsClient instance if the ref-count is zero when calling the method. {@code #getObject()}
* increments the ref-count and {@link #returnObject(Object)} decrements it. Anytime when the
* ref-count gets back to zero, the XdsClient instance will be shutdown and de-referenced.
*/
static final class RefCountedXdsClientObjectPool implements ObjectPool<XdsClient> {
private final XdsClientFactory xdsClientFactory;
@VisibleForTesting
@Nullable
XdsClient xdsClient;
private int refCount;
RefCountedXdsClientObjectPool(XdsClientFactory xdsClientFactory) {
this.xdsClientFactory = Preconditions.checkNotNull(xdsClientFactory, "xdsClientFactory");
}
/**
* See {@link RefCountedXdsClientObjectPool}.
*/
@Override
public synchronized XdsClient getObject() {
if (xdsClient == null) {
checkState(
refCount == 0,
"Bug: refCount should be zero while xdsClient is null");
xdsClient = xdsClientFactory.createXdsClient();
}
refCount++;
return xdsClient;
}
/**
* See {@link RefCountedXdsClientObjectPool}.
*/
@Override
public synchronized XdsClient returnObject(Object object) {
checkState(
object == xdsClient,
"Bug: the returned object '%s' does not match current XdsClient '%s'",
object,
xdsClient);
refCount--;
checkState(refCount >= 0, "Bug: refCount of XdsClient less than 0");
if (refCount == 0) {
xdsClient.shutdown();
xdsClient = null;
}
return null;
}
}
static final class XdsChannel {
private final ManagedChannel managedChannel;
private final boolean useProtocolV3;
@VisibleForTesting
XdsChannel(ManagedChannel managedChannel, boolean useProtocolV3) {
this.managedChannel = managedChannel;
this.useProtocolV3 = useProtocolV3;
}
ManagedChannel getManagedChannel() {
return managedChannel;
}
boolean isUseProtocolV3() {
return useProtocolV3;
}
}
}