-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
XdsServerWrapper.java
809 lines (756 loc) · 30.2 KB
/
XdsServerWrapper.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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/*
* Copyright 2021 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 static com.google.common.base.Preconditions.checkState;
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 com.google.common.util.concurrent.SettableFuture;
import io.grpc.Attributes;
import io.grpc.InternalServerInterceptors;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.ServerCall;
import io.grpc.ServerCall.Listener;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import io.grpc.ServerServiceDefinition;
import io.grpc.Status;
import io.grpc.StatusException;
import io.grpc.SynchronizationContext;
import io.grpc.SynchronizationContext.ScheduledHandle;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.ObjectPool;
import io.grpc.internal.SharedResourceHolder;
import io.grpc.xds.EnvoyServerProtoData.FilterChain;
import io.grpc.xds.Filter.FilterConfig;
import io.grpc.xds.Filter.NamedFilterConfig;
import io.grpc.xds.Filter.ServerInterceptorBuilder;
import io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler.FilterChainSelector;
import io.grpc.xds.ThreadSafeRandom.ThreadSafeRandomImpl;
import io.grpc.xds.VirtualHost.Route;
import io.grpc.xds.XdsClient.LdsResourceWatcher;
import io.grpc.xds.XdsClient.LdsUpdate;
import io.grpc.xds.XdsClient.RdsResourceWatcher;
import io.grpc.xds.XdsClient.RdsUpdate;
import io.grpc.xds.XdsNameResolverProvider.XdsClientPoolFactory;
import io.grpc.xds.XdsServerBuilder.XdsServingStatusListener;
import io.grpc.xds.internal.sds.SslContextProviderSupplier;
import java.io.IOException;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
final class XdsServerWrapper extends Server {
private static final Logger logger = Logger.getLogger(XdsServerWrapper.class.getName());
private final SynchronizationContext syncContext = new SynchronizationContext(
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
logger.log(Level.SEVERE, "Exception!" + e);
// TODO(chengyuanzhang): implement cleanup.
}
});
public static final Attributes.Key<AtomicReference<ServerRoutingConfig>>
ATTR_SERVER_ROUTING_CONFIG =
Attributes.Key.create("io.grpc.xds.ServerWrapper.serverRoutingConfig");
@VisibleForTesting
static final long RETRY_DELAY_NANOS = TimeUnit.MINUTES.toNanos(1);
private final String listenerAddress;
private final ServerBuilder<?> delegateBuilder;
private boolean sharedTimeService;
private final ScheduledExecutorService timeService;
private final FilterRegistry filterRegistry;
private final ThreadSafeRandom random = ThreadSafeRandomImpl.instance;
private final XdsClientPoolFactory xdsClientPoolFactory;
private final XdsServingStatusListener listener;
private final FilterChainSelectorManager filterChainSelectorManager;
private final AtomicBoolean started = new AtomicBoolean(false);
private final AtomicBoolean shutdown = new AtomicBoolean(false);
private boolean isServing;
private final CountDownLatch internalTerminationLatch = new CountDownLatch(1);
private final SettableFuture<Exception> initialStartFuture = SettableFuture.create();
private boolean initialStarted;
private ScheduledHandle restartTimer;
private ObjectPool<XdsClient> xdsClientPool;
private XdsClient xdsClient;
private DiscoveryState discoveryState;
private volatile Server delegate;
XdsServerWrapper(
String listenerAddress,
ServerBuilder<?> delegateBuilder,
XdsServingStatusListener listener,
FilterChainSelectorManager filterChainSelectorManager,
XdsClientPoolFactory xdsClientPoolFactory,
FilterRegistry filterRegistry) {
this(listenerAddress, delegateBuilder, listener, filterChainSelectorManager,
xdsClientPoolFactory, filterRegistry, SharedResourceHolder.get(GrpcUtil.TIMER_SERVICE));
sharedTimeService = true;
}
@VisibleForTesting
XdsServerWrapper(
String listenerAddress,
ServerBuilder<?> delegateBuilder,
XdsServingStatusListener listener,
FilterChainSelectorManager filterChainSelectorManager,
XdsClientPoolFactory xdsClientPoolFactory,
FilterRegistry filterRegistry,
ScheduledExecutorService timeService) {
this.listenerAddress = checkNotNull(listenerAddress, "listenerAddress");
this.delegateBuilder = checkNotNull(delegateBuilder, "delegateBuilder");
this.delegateBuilder.intercept(new ConfigApplyingInterceptor());
this.listener = checkNotNull(listener, "listener");
this.filterChainSelectorManager
= checkNotNull(filterChainSelectorManager, "filterChainSelectorManager");
this.xdsClientPoolFactory = checkNotNull(xdsClientPoolFactory, "xdsClientPoolFactory");
this.timeService = checkNotNull(timeService, "timeService");
this.filterRegistry = checkNotNull(filterRegistry,"filterRegistry");
this.delegate = delegateBuilder.build();
}
@Override
public Server start() throws IOException {
checkState(started.compareAndSet(false, true), "Already started");
syncContext.execute(new Runnable() {
@Override
public void run() {
internalStart();
}
});
Exception exception;
try {
exception = initialStartFuture.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
if (exception != null) {
throw (exception instanceof IOException) ? (IOException) exception :
new IOException(exception);
}
return this;
}
private void internalStart() {
try {
xdsClientPool = xdsClientPoolFactory.getOrCreate();
} catch (Exception e) {
StatusException statusException = Status.UNAVAILABLE.withDescription(
"Failed to initialize xDS").withCause(e).asException();
listener.onNotServing(statusException);
initialStartFuture.set(statusException);
return;
}
xdsClient = xdsClientPool.getObject();
boolean useProtocolV3 = xdsClient.getBootstrapInfo().getServers().get(0).isUseProtocolV3();
String listenerTemplate = xdsClient.getBootstrapInfo().getServerListenerResourceNameTemplate();
if (!useProtocolV3 || listenerTemplate == null) {
StatusException statusException =
Status.UNAVAILABLE.withDescription(
"Can only support xDS v3 with listener resource name template").asException();
listener.onNotServing(statusException);
initialStartFuture.set(statusException);
xdsClient = xdsClientPool.returnObject(xdsClient);
return;
}
discoveryState = new DiscoveryState(listenerTemplate.replaceAll("%s", listenerAddress));
}
@Override
public Server shutdown() {
if (!shutdown.compareAndSet(false, true)) {
return this;
}
syncContext.execute(new Runnable() {
@Override
public void run() {
if (!delegate.isShutdown()) {
delegate.shutdown();
}
internalShutdown();
}
});
return this;
}
@Override
public Server shutdownNow() {
if (!shutdown.compareAndSet(false, true)) {
return this;
}
syncContext.execute(new Runnable() {
@Override
public void run() {
if (!delegate.isShutdown()) {
delegate.shutdownNow();
}
internalShutdown();
}
});
return this;
}
// Must run in SynchronizationContext
private void internalShutdown() {
logger.log(Level.FINER, "Shutting down XdsServerWrapper");
if (discoveryState != null) {
discoveryState.shutdown();
}
if (xdsClient != null) {
xdsClient = xdsClientPool.returnObject(xdsClient);
}
if (restartTimer != null) {
restartTimer.cancel();
}
if (sharedTimeService) {
SharedResourceHolder.release(GrpcUtil.TIMER_SERVICE, timeService);
}
isServing = false;
internalTerminationLatch.countDown();
}
@Override
public boolean isShutdown() {
return shutdown.get();
}
@Override
public boolean isTerminated() {
return internalTerminationLatch.getCount() == 0 && delegate.isTerminated();
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
long startTime = System.nanoTime();
if (!internalTerminationLatch.await(timeout, unit)) {
return false;
}
long remainingTime = unit.toNanos(timeout) - (System.nanoTime() - startTime);
return delegate.awaitTermination(remainingTime, TimeUnit.NANOSECONDS);
}
@Override
public void awaitTermination() throws InterruptedException {
internalTerminationLatch.await();
delegate.awaitTermination();
}
@Override
public int getPort() {
return delegate.getPort();
}
@Override
public List<? extends SocketAddress> getListenSockets() {
return delegate.getListenSockets();
}
@Override
public List<ServerServiceDefinition> getServices() {
return delegate.getServices();
}
@Override
public List<ServerServiceDefinition> getImmutableServices() {
return delegate.getImmutableServices();
}
@Override
public List<ServerServiceDefinition> getMutableServices() {
return delegate.getMutableServices();
}
// Must run in SynchronizationContext
private void startDelegateServer() {
if (restartTimer != null && restartTimer.isPending()) {
return;
}
if (isServing) {
return;
}
if (delegate.isShutdown()) {
delegate = delegateBuilder.build();
}
try {
delegate.start();
listener.onServing();
isServing = true;
if (!initialStarted) {
initialStarted = true;
initialStartFuture.set(null);
}
} catch (IOException e) {
logger.log(Level.FINE, "Fail to start delegate server: {0}", e);
if (!initialStarted) {
initialStarted = true;
initialStartFuture.set(e);
}
restartTimer = syncContext.schedule(
new RestartTask(), RETRY_DELAY_NANOS, TimeUnit.NANOSECONDS, timeService);
}
}
private final class RestartTask implements Runnable {
@Override
public void run() {
startDelegateServer();
}
}
private final class DiscoveryState implements LdsResourceWatcher {
private final String resourceName;
// RDS resource name is the key.
private final Map<String, RouteDiscoveryState> routeDiscoveryStates = new HashMap<>();
// Track pending RDS resources using rds name.
private final Set<String> pendingRds = new HashSet<>();
// Most recently discovered filter chains.
private List<FilterChain> filterChains = new ArrayList<>();
// Most recently discovered default filter chain.
@Nullable
private FilterChain defaultFilterChain;
private boolean stopped;
private final Map<FilterChain, AtomicReference<ServerRoutingConfig>> savedRdsRoutingConfigRef
= new HashMap<>();
private final ServerInterceptor noopInterceptor = new ServerInterceptor() {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
Metadata headers, ServerCallHandler<ReqT, RespT> next) {
return next.startCall(call, headers);
}
};
private DiscoveryState(String resourceName) {
this.resourceName = checkNotNull(resourceName, "resourceName");
xdsClient.watchLdsResource(resourceName, this);
}
@Override
public void onChanged(final LdsUpdate update) {
syncContext.execute(new Runnable() {
@Override
public void run() {
if (stopped) {
return;
}
checkNotNull(update.listener(), "update");
if (!pendingRds.isEmpty()) {
// filter chain state has not yet been applied to filterChainSelectorManager and there
// are two sets of sslContextProviderSuppliers, so we release the old ones.
releaseSuppliersInFlight();
pendingRds.clear();
}
filterChains = update.listener().getFilterChains();
defaultFilterChain = update.listener().getDefaultFilterChain();
List<FilterChain> allFilterChains = filterChains;
if (defaultFilterChain != null) {
allFilterChains = new ArrayList<>(filterChains);
allFilterChains.add(defaultFilterChain);
}
Set<String> allRds = new HashSet<>();
for (FilterChain filterChain : allFilterChains) {
HttpConnectionManager hcm = filterChain.getHttpConnectionManager();
if (hcm.virtualHosts() == null) {
RouteDiscoveryState rdsState = routeDiscoveryStates.get(hcm.rdsName());
if (rdsState == null) {
rdsState = new RouteDiscoveryState(hcm.rdsName());
routeDiscoveryStates.put(hcm.rdsName(), rdsState);
xdsClient.watchRdsResource(hcm.rdsName(), rdsState);
}
if (rdsState.isPending) {
pendingRds.add(hcm.rdsName());
}
allRds.add(hcm.rdsName());
}
}
for (Map.Entry<String, RouteDiscoveryState> entry: routeDiscoveryStates.entrySet()) {
if (!allRds.contains(entry.getKey())) {
xdsClient.cancelRdsResourceWatch(entry.getKey(), entry.getValue());
}
}
routeDiscoveryStates.keySet().retainAll(allRds);
if (pendingRds.isEmpty()) {
updateSelector();
}
}
});
}
@Override
public void onResourceDoesNotExist(final String resourceName) {
syncContext.execute(new Runnable() {
@Override
public void run() {
if (stopped) {
return;
}
StatusException statusException = Status.UNAVAILABLE.withDescription(
"Listener " + resourceName + " unavailable").asException();
handleConfigNotFound(statusException);
}
});
}
@Override
public void onError(final Status error) {
syncContext.execute(new Runnable() {
@Override
public void run() {
if (stopped) {
return;
}
boolean isPermanentError = isPermanentError(error);
logger.log(Level.FINE, "{0} error from XdsClient: {1}",
new Object[]{isPermanentError ? "Permanent" : "Transient", error});
if (isPermanentError) {
handleConfigNotFound(error.asException());
} else if (!isServing) {
listener.onNotServing(error.asException());
}
}
});
}
private void shutdown() {
stopped = true;
cleanUpRouteDiscoveryStates();
logger.log(Level.FINE, "Stop watching LDS resource {0}", resourceName);
xdsClient.cancelLdsResourceWatch(resourceName, this);
List<SslContextProviderSupplier> toRelease = getSuppliersInUse();
filterChainSelectorManager.updateSelector(FilterChainSelector.NO_FILTER_CHAIN);
for (SslContextProviderSupplier s: toRelease) {
s.close();
}
releaseSuppliersInFlight();
}
private void updateSelector() {
Map<FilterChain, AtomicReference<ServerRoutingConfig>> filterChainRouting = new HashMap<>();
savedRdsRoutingConfigRef.clear();
for (FilterChain filterChain: filterChains) {
filterChainRouting.put(filterChain, generateRoutingConfig(filterChain));
}
FilterChainSelector selector = new FilterChainSelector(
Collections.unmodifiableMap(filterChainRouting),
defaultFilterChain == null ? null : defaultFilterChain.getSslContextProviderSupplier(),
defaultFilterChain == null ? new AtomicReference<ServerRoutingConfig>() :
generateRoutingConfig(defaultFilterChain));
List<SslContextProviderSupplier> toRelease = getSuppliersInUse();
filterChainSelectorManager.updateSelector(selector);
for (SslContextProviderSupplier e: toRelease) {
e.close();
}
startDelegateServer();
}
private AtomicReference<ServerRoutingConfig> generateRoutingConfig(FilterChain filterChain) {
HttpConnectionManager hcm = filterChain.getHttpConnectionManager();
if (hcm.virtualHosts() != null) {
ImmutableMap<Route, ServerInterceptor> interceptors = generatePerRouteInterceptors(
hcm.httpFilterConfigs(), hcm.virtualHosts());
return new AtomicReference<>(ServerRoutingConfig.create(hcm.virtualHosts(),interceptors));
} else {
RouteDiscoveryState rds = routeDiscoveryStates.get(hcm.rdsName());
checkNotNull(rds, "rds");
AtomicReference<ServerRoutingConfig> serverRoutingConfigRef = new AtomicReference<>();
if (rds.savedVirtualHosts != null) {
ImmutableMap<Route, ServerInterceptor> interceptors = generatePerRouteInterceptors(
hcm.httpFilterConfigs(), rds.savedVirtualHosts);
ServerRoutingConfig serverRoutingConfig =
ServerRoutingConfig.create(rds.savedVirtualHosts, interceptors);
serverRoutingConfigRef.set(serverRoutingConfig);
} else {
serverRoutingConfigRef.set(ServerRoutingConfig.FAILING_ROUTING_CONFIG);
}
savedRdsRoutingConfigRef.put(filterChain, serverRoutingConfigRef);
return serverRoutingConfigRef;
}
}
private ImmutableMap<Route, ServerInterceptor> generatePerRouteInterceptors(
List<NamedFilterConfig> namedFilterConfigs, List<VirtualHost> virtualHosts) {
ImmutableMap.Builder<Route, ServerInterceptor> perRouteInterceptors =
new ImmutableMap.Builder<>();
for (VirtualHost virtualHost : virtualHosts) {
for (Route route : virtualHost.routes()) {
List<ServerInterceptor> filterInterceptors = new ArrayList<>();
Map<String, FilterConfig> selectedOverrideConfigs =
new HashMap<>(virtualHost.filterConfigOverrides());
selectedOverrideConfigs.putAll(route.filterConfigOverrides());
for (NamedFilterConfig namedFilterConfig : namedFilterConfigs) {
FilterConfig filterConfig = namedFilterConfig.filterConfig;
Filter filter = filterRegistry.get(filterConfig.typeUrl());
if (filter instanceof ServerInterceptorBuilder) {
ServerInterceptor interceptor =
((ServerInterceptorBuilder) filter).buildServerInterceptor(
filterConfig, selectedOverrideConfigs.get(namedFilterConfig.name));
if (interceptor != null) {
filterInterceptors.add(interceptor);
}
} else {
logger.log(Level.WARNING, "HttpFilterConfig(type URL: "
+ filterConfig.typeUrl() + ") is not supported on server-side. "
+ "Probably a bug at ClientXdsClient verification.");
}
}
ServerInterceptor interceptor = combineInterceptors(filterInterceptors);
perRouteInterceptors.put(route, interceptor);
}
}
return perRouteInterceptors.build();
}
private ServerInterceptor combineInterceptors(final List<ServerInterceptor> interceptors) {
if (interceptors.isEmpty()) {
return noopInterceptor;
}
if (interceptors.size() == 1) {
return interceptors.get(0);
}
return new ServerInterceptor() {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
Metadata headers, ServerCallHandler<ReqT, RespT> next) {
// intercept forward
for (int i = interceptors.size() - 1; i >= 0; i--) {
next = InternalServerInterceptors.interceptCallHandlerCreate(
interceptors.get(i), next);
}
return next.startCall(call, headers);
}
};
}
private void handleConfigNotFound(StatusException exception) {
cleanUpRouteDiscoveryStates();
List<SslContextProviderSupplier> toRelease = getSuppliersInUse();
filterChainSelectorManager.updateSelector(FilterChainSelector.NO_FILTER_CHAIN);
for (SslContextProviderSupplier s: toRelease) {
s.close();
}
if (!initialStarted) {
initialStarted = true;
initialStartFuture.set(exception);
}
if (restartTimer != null) {
restartTimer.cancel();
}
if (!delegate.isShutdown()) {
delegate.shutdown(); // let in-progress calls finish
}
isServing = false;
listener.onNotServing(exception);
}
private void cleanUpRouteDiscoveryStates() {
for (RouteDiscoveryState rdsState : routeDiscoveryStates.values()) {
String rdsName = rdsState.resourceName;
logger.log(Level.FINE, "Stop watching RDS resource {0}", rdsName);
xdsClient.cancelRdsResourceWatch(rdsName, rdsState);
}
routeDiscoveryStates.clear();
savedRdsRoutingConfigRef.clear();
}
private List<SslContextProviderSupplier> getSuppliersInUse() {
List<SslContextProviderSupplier> toRelease = new ArrayList<>();
FilterChainSelector selector = filterChainSelectorManager.getSelectorToUpdateSelector();
if (selector != null) {
for (FilterChain f: selector.getRoutingConfigs().keySet()) {
if (f.getSslContextProviderSupplier() != null) {
toRelease.add(f.getSslContextProviderSupplier());
}
}
SslContextProviderSupplier defaultSupplier =
selector.getDefaultSslContextProviderSupplier();
if (defaultSupplier != null) {
toRelease.add(defaultSupplier);
}
}
return toRelease;
}
private void releaseSuppliersInFlight() {
SslContextProviderSupplier supplier;
for (FilterChain filterChain : filterChains) {
supplier = filterChain.getSslContextProviderSupplier();
if (supplier != null) {
supplier.close();
}
}
if (defaultFilterChain != null
&& (supplier = defaultFilterChain.getSslContextProviderSupplier()) != null) {
supplier.close();
}
}
private final class RouteDiscoveryState implements RdsResourceWatcher {
private final String resourceName;
private ImmutableList<VirtualHost> savedVirtualHosts;
private boolean isPending = true;
private RouteDiscoveryState(String resourceName) {
this.resourceName = checkNotNull(resourceName, "resourceName");
}
@Override
public void onChanged(final RdsUpdate update) {
syncContext.execute(new Runnable() {
@Override
public void run() {
if (!routeDiscoveryStates.containsKey(resourceName)) {
return;
}
if (savedVirtualHosts == null && !isPending) {
logger.log(Level.WARNING, "Received valid Rds {0} configuration.", resourceName);
}
savedVirtualHosts = ImmutableList.copyOf(update.virtualHosts);
updateRdsRoutingConfig();
maybeUpdateSelector();
}
});
}
@Override
public void onResourceDoesNotExist(final String resourceName) {
syncContext.execute(new Runnable() {
@Override
public void run() {
if (!routeDiscoveryStates.containsKey(resourceName)) {
return;
}
logger.log(Level.WARNING, "Rds {0} unavailable", resourceName);
savedVirtualHosts = null;
updateRdsRoutingConfig();
maybeUpdateSelector();
}
});
}
@Override
public void onError(final Status error) {
syncContext.execute(new Runnable() {
@Override
public void run() {
if (!routeDiscoveryStates.containsKey(resourceName)) {
return;
}
logger.log(Level.WARNING, "Error loading RDS resource {0} from XdsClient: {1}.",
new Object[]{resourceName, error});
maybeUpdateSelector();
}
});
}
private void updateRdsRoutingConfig() {
for (FilterChain filterChain : savedRdsRoutingConfigRef.keySet()) {
if (resourceName.equals(filterChain.getHttpConnectionManager().rdsName())) {
ServerRoutingConfig updatedRoutingConfig;
if (savedVirtualHosts == null) {
updatedRoutingConfig = ServerRoutingConfig.FAILING_ROUTING_CONFIG;
} else {
ImmutableMap<Route, ServerInterceptor> updatedInterceptors =
generatePerRouteInterceptors(
filterChain.getHttpConnectionManager().httpFilterConfigs(),
savedVirtualHosts);
updatedRoutingConfig = ServerRoutingConfig.create(savedVirtualHosts,
updatedInterceptors);
}
savedRdsRoutingConfigRef.get(filterChain).set(updatedRoutingConfig);
}
}
}
// Update the selector to use the most recently updated configs only after all rds have been
// discovered for the first time. Later changes on rds will be applied through virtual host
// list atomic ref.
private void maybeUpdateSelector() {
isPending = false;
boolean isLastPending = pendingRds.remove(resourceName) && pendingRds.isEmpty();
if (isLastPending) {
updateSelector();
}
}
}
private boolean isPermanentError(Status error) {
return EnumSet.of(
Status.Code.INTERNAL,
Status.Code.INVALID_ARGUMENT,
Status.Code.FAILED_PRECONDITION,
Status.Code.PERMISSION_DENIED,
Status.Code.UNAUTHENTICATED)
.contains(error.getCode());
}
}
@VisibleForTesting
final class ConfigApplyingInterceptor implements ServerInterceptor {
private final ServerInterceptor noopInterceptor = new ServerInterceptor() {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
Metadata headers, ServerCallHandler<ReqT, RespT> next) {
return next.startCall(call, headers);
}
};
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
Metadata headers, ServerCallHandler<ReqT, RespT> next) {
AtomicReference<ServerRoutingConfig> routingConfigRef =
call.getAttributes().get(ATTR_SERVER_ROUTING_CONFIG);
ServerRoutingConfig routingConfig = routingConfigRef == null ? null :
routingConfigRef.get();
if (routingConfig == null || routingConfig == ServerRoutingConfig.FAILING_ROUTING_CONFIG) {
String errorMsg = "Missing or broken xDS routing config: RDS config unavailable.";
call.close(Status.UNAVAILABLE.withDescription(errorMsg), new Metadata());
return new Listener<ReqT>() {};
}
List<VirtualHost> virtualHosts = routingConfig.virtualHosts();
VirtualHost virtualHost = RoutingUtils.findVirtualHostForHostName(
virtualHosts, call.getAuthority());
if (virtualHost == null) {
call.close(
Status.UNAVAILABLE.withDescription("Could not find xDS virtual host matching RPC"),
new Metadata());
return new Listener<ReqT>() {};
}
Route selectedRoute = null;
MethodDescriptor<ReqT, RespT> method = call.getMethodDescriptor();
for (Route route : virtualHost.routes()) {
if (RoutingUtils.matchRoute(
route.routeMatch(), "/" + method.getFullMethodName(), headers, random)) {
selectedRoute = route;
break;
}
}
if (selectedRoute == null) {
call.close(Status.UNAVAILABLE.withDescription("Could not find xDS route matching RPC"),
new Metadata());
return new ServerCall.Listener<ReqT>() {};
}
if (selectedRoute.routeAction() != null) {
call.close(Status.UNAVAILABLE.withDescription("Invalid xDS route action for matching "
+ "route: only Route.non_forwarding_action should be allowed."), new Metadata());
return new ServerCall.Listener<ReqT>() {};
}
ServerInterceptor routeInterceptor = noopInterceptor;
Map<Route, ServerInterceptor> perRouteInterceptors = routingConfig.interceptors();
if (perRouteInterceptors != null && perRouteInterceptors.get(selectedRoute) != null) {
routeInterceptor = perRouteInterceptors.get(selectedRoute);
}
return routeInterceptor.interceptCall(call, headers, next);
}
}
/**
* The HttpConnectionManager level configuration.
*/
@AutoValue
abstract static class ServerRoutingConfig {
@VisibleForTesting
static final ServerRoutingConfig FAILING_ROUTING_CONFIG = ServerRoutingConfig.create(
ImmutableList.<VirtualHost>of(), ImmutableMap.<Route, ServerInterceptor>of());
abstract ImmutableList<VirtualHost> virtualHosts();
// Prebuilt per route server interceptors from http filter configs.
abstract ImmutableMap<Route, ServerInterceptor> interceptors();
/**
* Server routing configuration.
* */
public static ServerRoutingConfig create(
ImmutableList<VirtualHost> virtualHosts,
ImmutableMap<Route, ServerInterceptor> interceptors) {
checkNotNull(virtualHosts, "virtualHosts");
checkNotNull(interceptors, "interceptors");
return new AutoValue_XdsServerWrapper_ServerRoutingConfig(virtualHosts, interceptors);
}
}
}