• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #19583

09 Dec 2024 11:42PM UTC coverage: 88.636% (+0.05%) from 88.591%
#19583

push

github

web-flow
Xds fallback (#11254)

* XDS Client Fallback

33493 of 37787 relevant lines covered (88.64%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.68
/../xds/src/main/java/io/grpc/xds/client/XdsClientImpl.java
1
/*
2
 * Copyright 2020 The gRPC Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package io.grpc.xds.client;
18

19
import static com.google.common.base.Preconditions.checkArgument;
20
import static com.google.common.base.Preconditions.checkNotNull;
21
import static io.grpc.xds.client.Bootstrapper.XDSTP_SCHEME;
22
import static io.grpc.xds.client.XdsResourceType.ParsedResource;
23
import static io.grpc.xds.client.XdsResourceType.ValidatedResourceUpdate;
24

25
import com.google.common.annotations.VisibleForTesting;
26
import com.google.common.base.Joiner;
27
import com.google.common.base.Stopwatch;
28
import com.google.common.base.Supplier;
29
import com.google.common.collect.ImmutableList;
30
import com.google.common.collect.ImmutableMap;
31
import com.google.common.util.concurrent.ListenableFuture;
32
import com.google.common.util.concurrent.SettableFuture;
33
import com.google.protobuf.Any;
34
import io.grpc.Internal;
35
import io.grpc.InternalLogId;
36
import io.grpc.Status;
37
import io.grpc.SynchronizationContext;
38
import io.grpc.SynchronizationContext.ScheduledHandle;
39
import io.grpc.internal.BackoffPolicy;
40
import io.grpc.internal.TimeProvider;
41
import io.grpc.xds.client.Bootstrapper.AuthorityInfo;
42
import io.grpc.xds.client.Bootstrapper.ServerInfo;
43
import io.grpc.xds.client.XdsClient.ResourceStore;
44
import io.grpc.xds.client.XdsLogger.XdsLogLevel;
45
import java.io.IOException;
46
import java.net.URI;
47
import java.util.ArrayList;
48
import java.util.Collection;
49
import java.util.Collections;
50
import java.util.HashMap;
51
import java.util.HashSet;
52
import java.util.List;
53
import java.util.Map;
54
import java.util.Objects;
55
import java.util.Set;
56
import java.util.concurrent.Executor;
57
import java.util.concurrent.Future;
58
import java.util.concurrent.ScheduledExecutorService;
59
import java.util.concurrent.TimeUnit;
60
import java.util.stream.Collectors;
61
import javax.annotation.Nullable;
62

63
/**
64
 * XdsClient implementation.
65
 */
66
@Internal
67
public final class XdsClientImpl extends XdsClient implements ResourceStore {
68

69
  // Longest time to wait, since the subscription to some resource, for concluding its absence.
70
  @VisibleForTesting
71
  public static final int INITIAL_RESOURCE_FETCH_TIMEOUT_SEC = 15;
72

73
  private final SynchronizationContext syncContext = new SynchronizationContext(
1✔
74
      new Thread.UncaughtExceptionHandler() {
1✔
75
        @Override
76
        public void uncaughtException(Thread t, Throwable e) {
77
          logger.log(
×
78
              XdsLogLevel.ERROR,
79
              "Uncaught exception in XdsClient SynchronizationContext. Panic!",
80
              e);
81
          // TODO: better error handling.
82
          throw new AssertionError(e);
×
83
        }
84
      });
85

86
  private final Map<ServerInfo, LoadStatsManager2> loadStatsManagerMap = new HashMap<>();
1✔
87
  final Map<ServerInfo, LoadReportClient> serverLrsClientMap = new HashMap<>();
1✔
88
  /** Map of authority to its activated control plane client (affected by xds fallback).
89
   * The last entry in the list for each value is the "active" CPC for the matching key */
90
  private final Map<String, List<ControlPlaneClient>> activatedCpClients = new HashMap<>();
1✔
91
  private final Map<ServerInfo, ControlPlaneClient> serverCpClientMap = new HashMap<>();
1✔
92

93
  /** Maps resource type to the corresponding map of subscribers (keyed by resource name). */
94
  private final Map<XdsResourceType<? extends ResourceUpdate>,
1✔
95
      Map<String, ResourceSubscriber<? extends ResourceUpdate>>>
96
      resourceSubscribers = new HashMap<>();
97
  /** Maps typeUrl to the corresponding XdsResourceType. */
98
  private final Map<String, XdsResourceType<?>> subscribedResourceTypeUrls = new HashMap<>();
1✔
99

100
  private final XdsTransportFactory xdsTransportFactory;
101
  private final Bootstrapper.BootstrapInfo bootstrapInfo;
102
  private final ScheduledExecutorService timeService;
103
  private final BackoffPolicy.Provider backoffPolicyProvider;
104
  private final Supplier<Stopwatch> stopwatchSupplier;
105
  private final TimeProvider timeProvider;
106
  private final Object securityConfig;
107
  private final InternalLogId logId;
108
  private final XdsLogger logger;
109
  private volatile boolean isShutdown;
110
  private final MessagePrettyPrinter messagePrinter;
111
  private final XdsClientMetricReporter metricReporter;
112

113
  public XdsClientImpl(
114
      XdsTransportFactory xdsTransportFactory,
115
      Bootstrapper.BootstrapInfo bootstrapInfo,
116
      ScheduledExecutorService timeService,
117
      BackoffPolicy.Provider backoffPolicyProvider,
118
      Supplier<Stopwatch> stopwatchSupplier,
119
      TimeProvider timeProvider,
120
      MessagePrettyPrinter messagePrinter,
121
      Object securityConfig,
122
      XdsClientMetricReporter metricReporter) {
1✔
123
    this.xdsTransportFactory = xdsTransportFactory;
1✔
124
    this.bootstrapInfo = bootstrapInfo;
1✔
125
    this.timeService = timeService;
1✔
126
    this.backoffPolicyProvider = backoffPolicyProvider;
1✔
127
    this.stopwatchSupplier = stopwatchSupplier;
1✔
128
    this.timeProvider = timeProvider;
1✔
129
    this.messagePrinter = messagePrinter;
1✔
130
    this.securityConfig = securityConfig;
1✔
131
    this.metricReporter = metricReporter;
1✔
132
    logId = InternalLogId.allocate("xds-client", null);
1✔
133
    logger = XdsLogger.withLogId(logId);
1✔
134
    logger.log(XdsLogLevel.INFO, "Created");
1✔
135
  }
1✔
136

137
  @Override
138
  public void shutdown() {
139
    syncContext.execute(
1✔
140
        new Runnable() {
1✔
141
          @Override
142
          public void run() {
143
            if (isShutdown) {
1✔
144
              return;
×
145
            }
146
            isShutdown = true;
1✔
147
            for (ControlPlaneClient xdsChannel : serverCpClientMap.values()) {
1✔
148
              xdsChannel.shutdown();
1✔
149
            }
1✔
150
            for (final LoadReportClient lrsClient : serverLrsClientMap.values()) {
1✔
151
              lrsClient.stopLoadReporting();
1✔
152
            }
1✔
153
            cleanUpResourceTimers(null);
1✔
154
            activatedCpClients.clear();
1✔
155
          }
1✔
156
        });
157
  }
1✔
158

159
  @Override
160
  public boolean isShutDown() {
161
    return isShutdown;
1✔
162
  }
163

164
  @Override
165
  public Map<String, XdsResourceType<?>> getSubscribedResourceTypesWithTypeUrl() {
166
    return Collections.unmodifiableMap(subscribedResourceTypeUrls);
1✔
167
  }
168

169
  private ControlPlaneClient getActiveCpc(String authority) {
170
    List<ControlPlaneClient> controlPlaneClients = activatedCpClients.get(authority);
1✔
171
    if (controlPlaneClients == null || controlPlaneClients.isEmpty()) {
1✔
172
      return null;
1✔
173
    }
174

175
    return controlPlaneClients.get(controlPlaneClients.size() - 1);
1✔
176
  }
177

178
  @Nullable
179
  @Override
180
  public Collection<String> getSubscribedResources(
181
      ServerInfo serverInfo, XdsResourceType<? extends ResourceUpdate> type) {
182
    ControlPlaneClient targetCpc = serverCpClientMap.get(serverInfo);
1✔
183
    if (targetCpc == null) {
1✔
184
      return null;
×
185
    }
186

187
    // This should include all of the authorities that targetCpc or a fallback from it is serving
188
    List<String> authorities = activatedCpClients.entrySet().stream()
1✔
189
        .filter(entry -> entry.getValue().contains(targetCpc))
1✔
190
        .map(Map.Entry::getKey)
1✔
191
        .collect(Collectors.toList());
1✔
192

193
    Map<String, ResourceSubscriber<? extends ResourceUpdate>> resources =
1✔
194
        resourceSubscribers.getOrDefault(type, Collections.emptyMap());
1✔
195

196
    Collection<String> retVal = resources.entrySet().stream()
1✔
197
        .filter(entry -> authorities.contains(entry.getValue().authority))
1✔
198
        .map(Map.Entry::getKey)
1✔
199
        .collect(Collectors.toList());
1✔
200

201
    return retVal.isEmpty() ? null : retVal;
1✔
202
  }
203

204
  @Override
205
  public void startMissingResourceTimers(Collection<String> resourceNames,
206
                                         XdsResourceType<?> resourceType) {
207
    Map<String, ResourceSubscriber<? extends ResourceUpdate>> subscriberMap =
1✔
208
        resourceSubscribers.get(resourceType);
1✔
209

210
    for (String resourceName : resourceNames) {
1✔
211
      ResourceSubscriber<?> subscriber = subscriberMap.get(resourceName);
1✔
212
      if (subscriber.respTimer == null && !subscriber.hasResult()) {
1✔
213
        subscriber.restartTimer();
1✔
214
      }
215
    }
1✔
216
  }
1✔
217

218
  // As XdsClient APIs becomes resource agnostic, subscribed resource types are dynamic.
219
  // ResourceTypes that do not have subscribers does not show up in the snapshot keys.
220
  @Override
221
  public ListenableFuture<Map<XdsResourceType<?>, Map<String, ResourceMetadata>>>
222
      getSubscribedResourcesMetadataSnapshot() {
223
    final SettableFuture<Map<XdsResourceType<?>, Map<String, ResourceMetadata>>> future =
224
        SettableFuture.create();
1✔
225
    syncContext.execute(new Runnable() {
1✔
226
      @Override
227
      public void run() {
228
        // A map from a "resource type" to a map ("resource name": "resource metadata")
229
        ImmutableMap.Builder<XdsResourceType<?>, Map<String, ResourceMetadata>> metadataSnapshot =
230
            ImmutableMap.builder();
1✔
231
        for (XdsResourceType<?> resourceType : resourceSubscribers.keySet()) {
1✔
232
          ImmutableMap.Builder<String, ResourceMetadata> metadataMap = ImmutableMap.builder();
1✔
233
          for (Map.Entry<String, ResourceSubscriber<? extends ResourceUpdate>> resourceEntry
234
              : resourceSubscribers.get(resourceType).entrySet()) {
1✔
235
            metadataMap.put(resourceEntry.getKey(), resourceEntry.getValue().metadata);
1✔
236
          }
1✔
237
          metadataSnapshot.put(resourceType, metadataMap.buildOrThrow());
1✔
238
        }
1✔
239
        future.set(metadataSnapshot.buildOrThrow());
1✔
240
      }
1✔
241
    });
242
    return future;
1✔
243
  }
244

245
  @Override
246
  public Object getSecurityConfig() {
247
    return securityConfig;
×
248
  }
249

250
  @Override
251
  public <T extends ResourceUpdate> void watchXdsResource(XdsResourceType<T> type,
252
                                                          String resourceName,
253
                                                          ResourceWatcher<T> watcher,
254
                                                          Executor watcherExecutor) {
255
    syncContext.execute(new Runnable() {
1✔
256
      @Override
257
      @SuppressWarnings("unchecked")
258
      public void run() {
259
        if (!resourceSubscribers.containsKey(type)) {
1✔
260
          resourceSubscribers.put(type, new HashMap<>());
1✔
261
          subscribedResourceTypeUrls.put(type.typeUrl(), type);
1✔
262
        }
263
        ResourceSubscriber<T> subscriber =
1✔
264
            (ResourceSubscriber<T>) resourceSubscribers.get(type).get(resourceName);
1✔
265

266
        if (subscriber == null) {
1✔
267
          logger.log(XdsLogLevel.INFO, "Subscribe {0} resource {1}", type, resourceName);
1✔
268
          subscriber = new ResourceSubscriber<>(type, resourceName);
1✔
269
          resourceSubscribers.get(type).put(resourceName, subscriber);
1✔
270

271
          if (subscriber.errorDescription == null) {
1✔
272
            CpcWithFallbackState cpcToUse = manageControlPlaneClient(subscriber);
1✔
273
            if (cpcToUse.cpc != null) {
1✔
274
              cpcToUse.cpc.adjustResourceSubscription(type);
1✔
275
            }
276
          }
277
        }
278

279
        subscriber.addWatcher(watcher, watcherExecutor);
1✔
280
      }
1✔
281
    });
282
  }
1✔
283

284
  /**
285
   * Gets a ControlPlaneClient for the subscriber's authority, creating one if necessary.
286
   * If there already was an active CPC for this authority, and it is different from the one
287
   * identified, then do fallback to the identified one (cpcToUse).
288
   *
289
   * @return identified CPC or {@code null} (if there are no valid ServerInfos associated with the
290
   *     subscriber's authority or CPC's for all are in backoff), and whether did a fallback.
291
   */
292
  @VisibleForTesting
293
  private <T extends ResourceUpdate> CpcWithFallbackState manageControlPlaneClient(
294
      ResourceSubscriber<T> subscriber) {
295

296
    ControlPlaneClient cpcToUse;
297
    boolean didFallback = false;
1✔
298
    try {
299
      cpcToUse = getOrCreateControlPlaneClient(subscriber.authority);
1✔
300
    } catch (IllegalArgumentException e) {
×
301
      if (subscriber.errorDescription == null) {
×
302
        subscriber.errorDescription = "Bad configuration:  " + e.getMessage();
×
303
      }
304

305
      subscriber.onError(
×
306
          Status.INVALID_ARGUMENT.withDescription(subscriber.errorDescription), null);
×
307
      return new CpcWithFallbackState(null, false);
×
308
    } catch (IOException e) {
1✔
309
      logger.log(XdsLogLevel.DEBUG,
1✔
310
          "Could not create a control plane client for authority {0}: {1}",
311
          subscriber.authority, e.getMessage());
1✔
312
      return new CpcWithFallbackState(null, false);
1✔
313
    }
1✔
314

315
    ControlPlaneClient activeCpClient = getActiveCpc(subscriber.authority);
1✔
316
    if (cpcToUse != activeCpClient) {
1✔
317
      addCpcToAuthority(subscriber.authority, cpcToUse); // makes it active
1✔
318
      if (activeCpClient != null) {
1✔
319
        didFallback = cpcToUse != null && !cpcToUse.isInError();
1✔
320
        if (didFallback) {
1✔
321
          logger.log(XdsLogLevel.INFO, "Falling back to XDS server {0}",
1✔
322
              cpcToUse.getServerInfo().target());
1✔
323
        } else {
324
          logger.log(XdsLogLevel.WARNING, "No working fallback XDS Servers found from {0}",
×
325
              activeCpClient.getServerInfo().target());
×
326
        }
327
      }
328
    }
329

330
    return new CpcWithFallbackState(cpcToUse, didFallback);
1✔
331
  }
332

333
  private void addCpcToAuthority(String authority, ControlPlaneClient cpcToUse) {
334
    List<ControlPlaneClient> controlPlaneClients =
1✔
335
        activatedCpClients.computeIfAbsent(authority, k -> new ArrayList<>());
1✔
336

337
    if (controlPlaneClients.contains(cpcToUse)) {
1✔
338
      return;
×
339
    }
340

341
    // if there are any missing CPCs between the last one and cpcToUse, add them + add cpcToUse
342
    ImmutableList<ServerInfo> serverInfos = getServerInfos(authority);
1✔
343
    for (int i = controlPlaneClients.size(); i < serverInfos.size(); i++) {
1✔
344
      ServerInfo serverInfo = serverInfos.get(i);
1✔
345
      ControlPlaneClient cpc = serverCpClientMap.get(serverInfo);
1✔
346
      controlPlaneClients.add(cpc);
1✔
347
      logger.log(XdsLogLevel.DEBUG, "Adding control plane client {0} to authority {1}",
1✔
348
          cpc, authority);
349
      cpcToUse.sendDiscoveryRequests();
1✔
350
      if (cpc == cpcToUse) {
1✔
351
        break;
1✔
352
      }
353
    }
354
  }
1✔
355

356
  @Override
357
  public <T extends ResourceUpdate> void cancelXdsResourceWatch(XdsResourceType<T> type,
358
                                                                String resourceName,
359
                                                                ResourceWatcher<T> watcher) {
360
    syncContext.execute(new Runnable() {
1✔
361
      @Override
362
      @SuppressWarnings("unchecked")
363
      public void run() {
364
        ResourceSubscriber<T> subscriber =
1✔
365
            (ResourceSubscriber<T>) resourceSubscribers.get(type).get(resourceName);
1✔
366
        if (subscriber == null) {
1✔
367
          logger.log(XdsLogLevel.WARNING, "double cancel of resource watch for {0}:{1}",
×
368
              type.typeName(), resourceName);
×
369
          return;
×
370
        }
371
        subscriber.removeWatcher(watcher);
1✔
372
        if (!subscriber.isWatched()) {
1✔
373
          subscriber.cancelResourceWatch();
1✔
374
          resourceSubscribers.get(type).remove(resourceName);
1✔
375

376
          List<ControlPlaneClient> controlPlaneClients =
1✔
377
              activatedCpClients.get(subscriber.authority);
1✔
378
          if (controlPlaneClients != null) {
1✔
379
            controlPlaneClients.forEach((cpc) -> {
1✔
380
              cpc.adjustResourceSubscription(type);
1✔
381
            });
1✔
382
          }
383

384
          if (resourceSubscribers.get(type).isEmpty()) {
1✔
385
            resourceSubscribers.remove(type);
1✔
386
            subscribedResourceTypeUrls.remove(type.typeUrl());
1✔
387
          }
388
        }
389
      }
1✔
390
    });
391
  }
1✔
392

393
  @Override
394
  public LoadStatsManager2.ClusterDropStats addClusterDropStats(
395
      final ServerInfo serverInfo, String clusterName,
396
      @Nullable String edsServiceName) {
397
    LoadStatsManager2 loadStatsManager = loadStatsManagerMap.get(serverInfo);
1✔
398
    LoadStatsManager2.ClusterDropStats dropCounter =
1✔
399
        loadStatsManager.getClusterDropStats(clusterName, edsServiceName);
1✔
400
    syncContext.execute(new Runnable() {
1✔
401
      @Override
402
      public void run() {
403
        serverLrsClientMap.get(serverInfo).startLoadReporting();
1✔
404
      }
1✔
405
    });
406
    return dropCounter;
1✔
407
  }
408

409
  @Override
410
  public LoadStatsManager2.ClusterLocalityStats addClusterLocalityStats(
411
      final ServerInfo serverInfo, String clusterName, @Nullable String edsServiceName,
412
      Locality locality) {
413
    LoadStatsManager2 loadStatsManager = loadStatsManagerMap.get(serverInfo);
1✔
414
    LoadStatsManager2.ClusterLocalityStats loadCounter =
1✔
415
        loadStatsManager.getClusterLocalityStats(clusterName, edsServiceName, locality);
1✔
416
    syncContext.execute(new Runnable() {
1✔
417
      @Override
418
      public void run() {
419
        serverLrsClientMap.get(serverInfo).startLoadReporting();
1✔
420
      }
1✔
421
    });
422
    return loadCounter;
1✔
423
  }
424

425

426
  @Override
427
  public Bootstrapper.BootstrapInfo getBootstrapInfo() {
428
    return bootstrapInfo;
1✔
429
  }
430

431
  @Override
432
  public String toString() {
433
    return logId.toString();
×
434
  }
435

436
  private Set<String> getResourceKeys(XdsResourceType<?> xdsResourceType) {
437
    if (!resourceSubscribers.containsKey(xdsResourceType)) {
1✔
438
      return null;
×
439
    }
440

441
    return resourceSubscribers.get(xdsResourceType).keySet();
1✔
442
  }
443

444
  // cpcForThisStream is null when doing shutdown
445
  private void cleanUpResourceTimers(ControlPlaneClient cpcForThisStream) {
446
    Collection<String> authoritiesForCpc = getActiveAuthorities(cpcForThisStream);
1✔
447

448
    for (Map<String, ResourceSubscriber<?>> subscriberMap : resourceSubscribers.values()) {
1✔
449
      for (ResourceSubscriber<?> subscriber : subscriberMap.values()) {
1✔
450
        if (cpcForThisStream == null || authoritiesForCpc.contains(subscriber.authority)) {
1✔
451
          subscriber.stopTimer();
1✔
452
        }
453
      }
1✔
454
    }
1✔
455
  }
1✔
456

457
  private ControlPlaneClient getOrCreateControlPlaneClient(String authority) throws IOException {
458
    // Optimize for the common case of a working ads stream already exists for the authority
459
    ControlPlaneClient activeCpc = getActiveCpc(authority);
1✔
460
    if (activeCpc != null && !activeCpc.isInError()) {
1✔
461
      return activeCpc;
1✔
462
    }
463

464
    ImmutableList<ServerInfo> serverInfos = getServerInfos(authority);
1✔
465
    if (serverInfos == null) {
1✔
466
      throw new IllegalArgumentException("No xds servers found for authority " + authority);
×
467
    }
468

469
    for (ServerInfo serverInfo : serverInfos) {
1✔
470
      ControlPlaneClient cpc = getOrCreateControlPlaneClient(serverInfo);
1✔
471
      if (cpc.isInError()) {
1✔
472
        continue;
1✔
473
      }
474
      return cpc;
1✔
475
    }
476

477
    // Everything existed and is in backoff so throw
478
    throw new IOException("All xds transports for authority " + authority + " are in backoff");
1✔
479
  }
480

481
  private ControlPlaneClient getOrCreateControlPlaneClient(ServerInfo serverInfo) {
482
    syncContext.throwIfNotInThisSynchronizationContext();
1✔
483
    if (serverCpClientMap.containsKey(serverInfo)) {
1✔
484
      return serverCpClientMap.get(serverInfo);
1✔
485
    }
486

487
    logger.log(XdsLogLevel.DEBUG, "Creating control plane client for {0}", serverInfo.target());
1✔
488
    XdsTransportFactory.XdsTransport xdsTransport;
489
    try {
490
      xdsTransport = xdsTransportFactory.create(serverInfo);
1✔
491
    } catch (Exception e) {
1✔
492
      String msg = String.format("Failed to create xds transport for %s: %s",
1✔
493
          serverInfo.target(), e.getMessage());
1✔
494
      logger.log(XdsLogLevel.WARNING, msg);
1✔
495
      xdsTransport =
1✔
496
          new ControlPlaneClient.FailingXdsTransport(Status.UNAVAILABLE.withDescription(msg));
1✔
497
    }
1✔
498

499
    ControlPlaneClient controlPlaneClient = new ControlPlaneClient(
1✔
500
        xdsTransport,
501
        serverInfo,
502
        bootstrapInfo.node(),
1✔
503
        new ResponseHandler(serverInfo),
504
        this,
505
        timeService,
506
        syncContext,
507
        backoffPolicyProvider,
508
        stopwatchSupplier,
509
        messagePrinter
510
    );
511

512
    serverCpClientMap.put(serverInfo, controlPlaneClient);
1✔
513

514
    LoadStatsManager2 loadStatsManager = new LoadStatsManager2(stopwatchSupplier);
1✔
515
    loadStatsManagerMap.put(serverInfo, loadStatsManager);
1✔
516
    LoadReportClient lrsClient = new LoadReportClient(
1✔
517
        loadStatsManager, xdsTransport, bootstrapInfo.node(),
1✔
518
        syncContext, timeService, backoffPolicyProvider, stopwatchSupplier);
519
    serverLrsClientMap.put(serverInfo, lrsClient);
1✔
520

521
    return controlPlaneClient;
1✔
522
  }
523

524
  @VisibleForTesting
525
  @Override
526
  public Map<ServerInfo, LoadReportClient> getServerLrsClientMap() {
527
    return ImmutableMap.copyOf(serverLrsClientMap);
1✔
528
  }
529

530
  private String getAuthority(String resource) {
531
    String authority;
532
    if (resource.startsWith(XDSTP_SCHEME)) {
1✔
533
      URI uri = URI.create(resource);
1✔
534
      authority = uri.getAuthority();
1✔
535
      if (authority == null) {
1✔
536
        authority = "";
1✔
537
      }
538
    } else {
1✔
539
      authority = null;
1✔
540
    }
541

542
    return authority;
1✔
543
  }
544

545
  @Nullable
546
  private ImmutableList<ServerInfo> getServerInfos(String authority) {
547
    if (authority != null) {
1✔
548
      AuthorityInfo authorityInfo = bootstrapInfo.authorities().get(authority);
1✔
549
      if (authorityInfo == null || authorityInfo.xdsServers().isEmpty()) {
1✔
550
        return null;
1✔
551
      }
552
      return authorityInfo.xdsServers();
1✔
553
    } else {
554
      return bootstrapInfo.servers();
1✔
555
    }
556
  }
557

558
  @SuppressWarnings("unchecked")
559
  private <T extends ResourceUpdate> void handleResourceUpdate(
560
      XdsResourceType.Args args, List<Any> resources, XdsResourceType<T> xdsResourceType,
561
      boolean isFirstResponse, ProcessingTracker processingTracker) {
562
    ControlPlaneClient controlPlaneClient = serverCpClientMap.get(args.serverInfo);
1✔
563

564
    if (isFirstResponse) {
1✔
565
      shutdownLowerPriorityCpcs(controlPlaneClient);
1✔
566
    }
567

568
    ValidatedResourceUpdate<T> result = xdsResourceType.parse(args, resources);
1✔
569
    logger.log(XdsLogger.XdsLogLevel.INFO,
1✔
570
        "Received {0} Response version {1} nonce {2}. Parsed resources: {3}",
571
        xdsResourceType.typeName(), args.versionInfo, args.nonce, result.unpackedResources);
1✔
572
    Map<String, ParsedResource<T>> parsedResources = result.parsedResources;
1✔
573
    Set<String> invalidResources = result.invalidResources;
1✔
574
    metricReporter.reportResourceUpdates(Long.valueOf(parsedResources.size()),
1✔
575
        Long.valueOf(invalidResources.size()),
1✔
576
        args.getServerInfo().target(), xdsResourceType.typeUrl());
1✔
577

578
    List<String> errors = result.errors;
1✔
579
    String errorDetail = null;
1✔
580
    if (errors.isEmpty()) {
1✔
581
      checkArgument(invalidResources.isEmpty(), "found invalid resources but missing errors");
1✔
582
      controlPlaneClient.ackResponse(xdsResourceType, args.versionInfo,
1✔
583
          args.nonce);
584
    } else {
585
      errorDetail = Joiner.on('\n').join(errors);
1✔
586
      logger.log(XdsLogLevel.WARNING,
1✔
587
          "Failed processing {0} Response version {1} nonce {2}. Errors:\n{3}",
588
          xdsResourceType.typeName(), args.versionInfo, args.nonce, errorDetail);
1✔
589
      controlPlaneClient.nackResponse(xdsResourceType, args.nonce, errorDetail);
1✔
590
    }
591

592
    long updateTime = timeProvider.currentTimeNanos();
1✔
593
    Map<String, ResourceSubscriber<? extends ResourceUpdate>> subscribedResources =
1✔
594
        resourceSubscribers.getOrDefault(xdsResourceType, Collections.emptyMap());
1✔
595
    for (Map.Entry<String, ResourceSubscriber<?>> entry : subscribedResources.entrySet()) {
1✔
596
      String resourceName = entry.getKey();
1✔
597
      ResourceSubscriber<T> subscriber = (ResourceSubscriber<T>) entry.getValue();
1✔
598
      if (parsedResources.containsKey(resourceName)) {
1✔
599
        // Happy path: the resource updated successfully. Notify the watchers of the update.
600
        subscriber.onData(parsedResources.get(resourceName), args.versionInfo, updateTime,
1✔
601
            processingTracker);
602
        continue;
1✔
603
      }
604

605
      if (invalidResources.contains(resourceName)) {
1✔
606
        // The resource update is invalid. Capture the error without notifying the watchers.
607
        subscriber.onRejected(args.versionInfo, updateTime, errorDetail);
1✔
608
      }
609

610
      // Nothing else to do for incremental ADS resources.
611
      if (!xdsResourceType.isFullStateOfTheWorld()) {
1✔
612
        continue;
1✔
613
      }
614

615
      // Handle State of the World ADS: invalid resources.
616
      if (invalidResources.contains(resourceName)) {
1✔
617
        // The resource is missing. Reuse the cached resource if possible.
618
        if (subscriber.data == null) {
1✔
619
          // No cached data. Notify the watchers of an invalid update.
620
          subscriber.onError(Status.UNAVAILABLE.withDescription(errorDetail), processingTracker);
1✔
621
        }
622
        continue;
623
      }
624

625
      // For State of the World services, notify watchers when their watched resource is missing
626
      // from the ADS update. Note that we can only do this if the resource update is coming from
627
      // the same xDS server that the ResourceSubscriber is subscribed to.
628
      if (getActiveCpc(subscriber.authority) == controlPlaneClient) {
1✔
629
        subscriber.onAbsent(processingTracker, args.serverInfo);
1✔
630
      }
631
    }
1✔
632
  }
1✔
633

634
  @Override
635
  public Future<Void> reportServerConnections(ServerConnectionCallback callback) {
636
    SettableFuture<Void> future = SettableFuture.create();
1✔
637
    syncContext.execute(() -> {
1✔
638
      serverCpClientMap.forEach((serverInfo, controlPlaneClient) ->
1✔
639
          callback.reportServerConnectionGauge(
1✔
640
              !controlPlaneClient.isInError(), serverInfo.target()));
1✔
641
      future.set(null);
1✔
642
    });
1✔
643
    return future;
1✔
644
  }
645

646
  private void shutdownLowerPriorityCpcs(ControlPlaneClient activatedCpc) {
647
    // For each authority, remove any control plane clients, with lower priority than the activated
648
    // one, from activatedCpClients storing them all in cpcsToShutdown.
649
    Set<ControlPlaneClient> cpcsToShutdown = new HashSet<>();
1✔
650
    for ( List<ControlPlaneClient> cpcsForAuth : activatedCpClients.values()) {
1✔
651
      if (cpcsForAuth == null) {
1✔
652
        continue;
×
653
      }
654
      int index = cpcsForAuth.indexOf(activatedCpc);
1✔
655
      if (index > -1) {
1✔
656
        cpcsToShutdown.addAll(cpcsForAuth.subList(index + 1, cpcsForAuth.size()));
1✔
657
        cpcsForAuth.subList(index + 1, cpcsForAuth.size()).clear(); // remove lower priority cpcs
1✔
658
      }
659
    }
1✔
660

661
    // Shutdown any lower priority control plane clients identified above that aren't still being
662
    // used by another authority.  If they are still being used let the XDS server know that we
663
    // no longer are interested in subscriptions for authorities we are no longer responsible for.
664
    for (ControlPlaneClient cpc : cpcsToShutdown) {
1✔
665
      if (activatedCpClients.values().stream().noneMatch(list -> list.contains(cpc))) {
1✔
666
        cpc.shutdown();
1✔
667
        serverCpClientMap.remove(cpc.getServerInfo());
1✔
668
      } else {
669
        cpc.sendDiscoveryRequests();
×
670
      }
671
    }
1✔
672
  }
1✔
673

674

675
  /** Tracks a single subscribed resource. */
676
  private final class ResourceSubscriber<T extends ResourceUpdate> {
677
    @Nullable
678
    private final String authority;
679
    private final XdsResourceType<T> type;
680
    private final String resource;
681
    private final Map<ResourceWatcher<T>, Executor> watchers = new HashMap<>();
1✔
682
    @Nullable
683
    private T data;
684
    private boolean absent;
685
    // Tracks whether the deletion has been ignored per bootstrap server feature.
686
    // See https://github.com/grpc/proposal/blob/master/A53-xds-ignore-resource-deletion.md
687
    private boolean resourceDeletionIgnored;
688
    @Nullable
689
    private ScheduledHandle respTimer;
690
    @Nullable
691
    private ResourceMetadata metadata;
692
    @Nullable
693
    private String errorDescription;
694

695
    ResourceSubscriber(XdsResourceType<T> type, String resource) {
1✔
696
      syncContext.throwIfNotInThisSynchronizationContext();
1✔
697
      this.type = type;
1✔
698
      this.resource = resource;
1✔
699
      this.authority = getAuthority(resource);
1✔
700
      if (getServerInfos(authority) == null) {
1✔
701
        this.errorDescription = "Wrong configuration: xds server does not exist for resource "
1✔
702
            + resource;
703
        return;
1✔
704
      }
705

706
      // Initialize metadata in UNKNOWN state to cover the case when resource subscriber,
707
      // is created but not yet requested because the client is in backoff.
708
      this.metadata = ResourceMetadata.newResourceMetadataUnknown();
1✔
709
    }
1✔
710

711
    @Override
712
    public String toString() {
713
      return "ResourceSubscriber{"
×
714
          + "resource='" + resource + '\''
715
          + ", authority='" + authority + '\''
716
          + ", type=" + type
717
          + ", watchers=" + watchers.size()
×
718
          + ", data=" + data
719
          + ", absent=" + absent
720
          + ", resourceDeletionIgnored=" + resourceDeletionIgnored
721
          + ", errorDescription='" + errorDescription + '\''
722
          + '}';
723
    }
724

725
    void addWatcher(ResourceWatcher<T> watcher, Executor watcherExecutor) {
726
      checkArgument(!watchers.containsKey(watcher), "watcher %s already registered", watcher);
1✔
727
      watchers.put(watcher, watcherExecutor);
1✔
728
      T savedData = data;
1✔
729
      boolean savedAbsent = absent;
1✔
730
      watcherExecutor.execute(() -> {
1✔
731
        if (errorDescription != null) {
1✔
732
          watcher.onError(Status.INVALID_ARGUMENT.withDescription(errorDescription));
1✔
733
          return;
1✔
734
        }
735
        if (savedData != null) {
1✔
736
          notifyWatcher(watcher, savedData);
1✔
737
        } else if (savedAbsent) {
1✔
738
          watcher.onResourceDoesNotExist(resource);
1✔
739
        }
740
      });
1✔
741
    }
1✔
742

743
    void removeWatcher(ResourceWatcher<T> watcher) {
744
      checkArgument(watchers.containsKey(watcher), "watcher %s not registered", watcher);
1✔
745
      watchers.remove(watcher);
1✔
746
    }
1✔
747

748
    void restartTimer() {
749
      if (data != null || absent) {  // resource already resolved
1✔
750
        return;
×
751
      }
752
      ControlPlaneClient activeCpc = getActiveCpc(authority);
1✔
753
      if (activeCpc == null || !activeCpc.isReady()) {
1✔
754
        // When client becomes ready, it triggers a restartTimer for all relevant subscribers.
755
        return;
1✔
756
      }
757

758
      class ResourceNotFound implements Runnable {
1✔
759
        @Override
760
        public void run() {
761
          logger.log(XdsLogLevel.INFO, "{0} resource {1} initial fetch timeout",
1✔
762
              type, resource);
1✔
763
          respTimer = null;
1✔
764
          onAbsent(null, activeCpc.getServerInfo());
1✔
765
        }
1✔
766

767
        @Override
768
        public String toString() {
769
          return type + this.getClass().getSimpleName();
1✔
770
        }
771
      }
772

773
      // Initial fetch scheduled or rescheduled, transition metadata state to REQUESTED.
774
      metadata = ResourceMetadata.newResourceMetadataRequested();
1✔
775

776
      if (respTimer != null) {
1✔
777
        respTimer.cancel();
×
778
      }
779
      respTimer = syncContext.schedule(
1✔
780
          new ResourceNotFound(), INITIAL_RESOURCE_FETCH_TIMEOUT_SEC, TimeUnit.SECONDS,
781
          timeService);
1✔
782
    }
1✔
783

784
    void stopTimer() {
785
      if (respTimer != null && respTimer.isPending()) {
1✔
786
        respTimer.cancel();
1✔
787
        respTimer = null;
1✔
788
      }
789
    }
1✔
790

791
    void cancelResourceWatch() {
792
      if (isWatched()) {
1✔
793
        throw new IllegalStateException("Can't cancel resource watch with active watchers present");
×
794
      }
795
      stopTimer();
1✔
796
      String message = "Unsubscribing {0} resource {1} from server {2}";
1✔
797
      XdsLogLevel logLevel = XdsLogLevel.INFO;
1✔
798
      if (resourceDeletionIgnored) {
1✔
799
        message += " for which we previously ignored a deletion";
×
800
        logLevel = XdsLogLevel.FORCE_INFO;
×
801
      }
802
      logger.log(logLevel, message, type, resource, getTarget());
1✔
803
    }
1✔
804

805
    boolean isWatched() {
806
      return !watchers.isEmpty();
1✔
807
    }
808

809
    boolean hasResult() {
810
      return data != null || absent;
1✔
811
    }
812

813
    void onData(ParsedResource<T> parsedResource, String version, long updateTime,
814
                ProcessingTracker processingTracker) {
815
      if (respTimer != null && respTimer.isPending()) {
1✔
816
        respTimer.cancel();
1✔
817
        respTimer = null;
1✔
818
      }
819
      ResourceUpdate oldData = this.data;
1✔
820
      this.data = parsedResource.getResourceUpdate();
1✔
821
      this.metadata = ResourceMetadata
1✔
822
          .newResourceMetadataAcked(parsedResource.getRawResource(), version, updateTime);
1✔
823
      absent = false;
1✔
824
      if (resourceDeletionIgnored) {
1✔
825
        logger.log(XdsLogLevel.FORCE_INFO, "xds server {0}: server returned new version "
1✔
826
                + "of resource for which we previously ignored a deletion: type {1} name {2}",
827
            getTarget(), type, resource);
1✔
828
        resourceDeletionIgnored = false;
1✔
829
      }
830
      if (!Objects.equals(oldData, data)) {
1✔
831
        for (ResourceWatcher<T> watcher : watchers.keySet()) {
1✔
832
          processingTracker.startTask();
1✔
833
          watchers.get(watcher).execute(() -> {
1✔
834
            try {
835
              notifyWatcher(watcher, data);
1✔
836
            } finally {
837
              processingTracker.onComplete();
1✔
838
            }
839
          });
1✔
840
        }
1✔
841
      }
842
    }
1✔
843

844
    private String getTarget() {
845
      ControlPlaneClient activeCpc = getActiveCpc(authority);
1✔
846
      return (activeCpc != null)
1✔
847
             ? activeCpc.getServerInfo().target()
1✔
848
             : "unknown";
1✔
849
    }
850

851
    void onAbsent(@Nullable ProcessingTracker processingTracker, ServerInfo serverInfo) {
852
      if (respTimer != null && respTimer.isPending()) {  // too early to conclude absence
1✔
853
        return;
1✔
854
      }
855

856
      // Ignore deletion of State of the World resources when this feature is on,
857
      // and the resource is reusable.
858
      boolean ignoreResourceDeletionEnabled = serverInfo.ignoreResourceDeletion();
1✔
859
      if (ignoreResourceDeletionEnabled && type.isFullStateOfTheWorld() && data != null) {
1✔
860
        if (!resourceDeletionIgnored) {
1✔
861
          logger.log(XdsLogLevel.FORCE_WARNING,
1✔
862
              "xds server {0}: ignoring deletion for resource type {1} name {2}}",
863
              serverInfo.target(), type, resource);
1✔
864
          resourceDeletionIgnored = true;
1✔
865
        }
866
        return;
1✔
867
      }
868

869
      logger.log(XdsLogLevel.INFO, "Conclude {0} resource {1} not exist", type, resource);
1✔
870
      if (!absent) {
1✔
871
        data = null;
1✔
872
        absent = true;
1✔
873
        metadata = ResourceMetadata.newResourceMetadataDoesNotExist();
1✔
874
        for (ResourceWatcher<T> watcher : watchers.keySet()) {
1✔
875
          if (processingTracker != null) {
1✔
876
            processingTracker.startTask();
1✔
877
          }
878
          watchers.get(watcher).execute(() -> {
1✔
879
            try {
880
              watcher.onResourceDoesNotExist(resource);
1✔
881
            } finally {
882
              if (processingTracker != null) {
1✔
883
                processingTracker.onComplete();
1✔
884
              }
885
            }
886
          });
1✔
887
        }
1✔
888
      }
889
    }
1✔
890

891
    void onError(Status error, @Nullable ProcessingTracker tracker) {
892
      if (respTimer != null && respTimer.isPending()) {
1✔
893
        respTimer.cancel();
1✔
894
        respTimer = null;
1✔
895
      }
896

897
      // Include node ID in xds failures to allow cross-referencing with control plane logs
898
      // when debugging.
899
      String description = error.getDescription() == null ? "" : error.getDescription() + " ";
1✔
900
      Status errorAugmented = Status.fromCode(error.getCode())
1✔
901
          .withDescription(description + "nodeID: " + bootstrapInfo.node().getId())
1✔
902
          .withCause(error.getCause());
1✔
903

904
      for (ResourceWatcher<T> watcher : watchers.keySet()) {
1✔
905
        if (tracker != null) {
1✔
906
          tracker.startTask();
1✔
907
        }
908
        watchers.get(watcher).execute(() -> {
1✔
909
          try {
910
            watcher.onError(errorAugmented);
1✔
911
          } finally {
912
            if (tracker != null) {
1✔
913
              tracker.onComplete();
1✔
914
            }
915
          }
916
        });
1✔
917
      }
1✔
918
    }
1✔
919

920
    void onRejected(String rejectedVersion, long rejectedTime, String rejectedDetails) {
921
      metadata = ResourceMetadata
1✔
922
          .newResourceMetadataNacked(metadata, rejectedVersion, rejectedTime, rejectedDetails,
1✔
923
              data != null);
924
    }
1✔
925

926
    private void notifyWatcher(ResourceWatcher<T> watcher, T update) {
927
      watcher.onChanged(update);
1✔
928
    }
1✔
929
  }
930

931
  private class ResponseHandler implements XdsResponseHandler {
932
    final ServerInfo serverInfo;
933

934
    ResponseHandler(ServerInfo serverInfo) {
1✔
935
      this.serverInfo = serverInfo;
1✔
936
    }
1✔
937

938
    @Override
939
    public void handleResourceResponse(
940
        XdsResourceType<?> xdsResourceType, ServerInfo serverInfo, String versionInfo,
941
        List<Any> resources, String nonce, boolean isFirstResponse,
942
        ProcessingTracker processingTracker) {
943
      checkNotNull(xdsResourceType, "xdsResourceType");
1✔
944
      syncContext.throwIfNotInThisSynchronizationContext();
1✔
945
      Set<String> toParseResourceNames =
946
          xdsResourceType.shouldRetrieveResourceKeysForArgs()
1✔
947
          ? getResourceKeys(xdsResourceType)
1✔
948
          : null;
1✔
949
      XdsResourceType.Args args = new XdsResourceType.Args(serverInfo, versionInfo, nonce,
1✔
950
          bootstrapInfo, securityConfig, toParseResourceNames);
1✔
951
      handleResourceUpdate(args, resources, xdsResourceType, isFirstResponse, processingTracker);
1✔
952
    }
1✔
953

954
    @Override
955
    public void handleStreamClosed(Status status, boolean shouldTryFallback) {
956
      syncContext.throwIfNotInThisSynchronizationContext();
1✔
957

958
      ControlPlaneClient cpcClosed = serverCpClientMap.get(serverInfo);
1✔
959
      if (cpcClosed == null) {
1✔
960
        return;
×
961
      }
962

963
      cleanUpResourceTimers(cpcClosed);
1✔
964

965
      if (status.isOk()) {
1✔
966
        return; // Not considered an error
1✔
967
      }
968

969
      metricReporter.reportServerFailure(1L, serverInfo.target());
1✔
970

971
      Collection<String> authoritiesForClosedCpc = getActiveAuthorities(cpcClosed);
1✔
972
      for (Map<String, ResourceSubscriber<? extends ResourceUpdate>> subscriberMap :
973
          resourceSubscribers.values()) {
1✔
974
        for (ResourceSubscriber<? extends ResourceUpdate> subscriber : subscriberMap.values()) {
1✔
975
          if (subscriber.hasResult() || !authoritiesForClosedCpc.contains(subscriber.authority)) {
1✔
976
            continue;
1✔
977
          }
978

979
          // try to fallback to lower priority control plane client
980
          if (shouldTryFallback && manageControlPlaneClient(subscriber).didFallback) {
1✔
981
            authoritiesForClosedCpc.remove(subscriber.authority);
1✔
982
            if (authoritiesForClosedCpc.isEmpty()) {
1✔
983
              return; // optimization: no need to continue once all authorities have done fallback
1✔
984
            }
985
            continue; // since we did fallback, don't consider it an error
986
          }
987

988
          subscriber.onError(status, null);
1✔
989
        }
1✔
990
      }
1✔
991
    }
1✔
992

993
  }
994

995
  private static class CpcWithFallbackState {
996
    ControlPlaneClient cpc;
997
    boolean didFallback;
998

999
    private CpcWithFallbackState(ControlPlaneClient cpc, boolean didFallback) {
1✔
1000
      this.cpc = cpc;
1✔
1001
      this.didFallback = didFallback;
1✔
1002
    }
1✔
1003
  }
1004

1005
  private Collection<String> getActiveAuthorities(ControlPlaneClient cpc) {
1006
    List<String> asList = activatedCpClients.entrySet().stream()
1✔
1007
        .filter(entry -> !entry.getValue().isEmpty()
1✔
1008
            && cpc == entry.getValue().get(entry.getValue().size() - 1))
1✔
1009
        .map(Map.Entry::getKey)
1✔
1010
        .collect(Collectors.toList());
1✔
1011

1012
    // Since this is usually used for contains, use a set when the list is large
1013
    return (asList.size() < 100) ? asList : new HashSet<>(asList);
1✔
1014
  }
1015

1016
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc