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

grpc / grpc-java / #19950

19 Aug 2025 04:11PM UTC coverage: 88.555% (-0.005%) from 88.56%
#19950

push

github

web-flow
xds: xdsClient caches transient error for new watchers (#12262)

34695 of 39179 relevant lines covered (88.56%)

0.89 hits per line

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

94.21
/../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.XdsResourceType.ParsedResource;
22
import static io.grpc.xds.client.XdsResourceType.ValidatedResourceUpdate;
23

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

424

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

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

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

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

443
  // cpcForThisStream is null when doing shutdown
444
  private void cleanUpResourceTimers(ControlPlaneClient cpcForThisStream) {
445
    Collection<String> authoritiesForCpc = getActiveAuthorities(cpcForThisStream);
1✔
446
    String target = cpcForThisStream == null ? "null" : cpcForThisStream.getServerInfo().target();
1✔
447
    logger.log(XdsLogLevel.DEBUG, "Cleaning up resource timers for CPC {0}, authorities {1}",
1✔
448
        target, authoritiesForCpc);
449

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

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

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

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

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

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

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

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

514
    serverCpClientMap.put(serverInfo, controlPlaneClient);
1✔
515

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

523
    return controlPlaneClient;
1✔
524
  }
525

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

532
  @Nullable
533
  private ImmutableList<ServerInfo> getServerInfos(String authority) {
534
    if (authority != null) {
1✔
535
      AuthorityInfo authorityInfo = bootstrapInfo.authorities().get(authority);
1✔
536
      if (authorityInfo == null || authorityInfo.xdsServers().isEmpty()) {
1✔
537
        return null;
1✔
538
      }
539
      return authorityInfo.xdsServers();
1✔
540
    } else {
541
      return bootstrapInfo.servers();
1✔
542
    }
543
  }
544

545
  @SuppressWarnings("unchecked")
546
  private <T extends ResourceUpdate> void handleResourceUpdate(
547
      XdsResourceType.Args args, List<Any> resources, XdsResourceType<T> xdsResourceType,
548
      boolean isFirstResponse, ProcessingTracker processingTracker) {
549
    ControlPlaneClient controlPlaneClient = serverCpClientMap.get(args.serverInfo);
1✔
550

551
    if (isFirstResponse) {
1✔
552
      shutdownLowerPriorityCpcs(controlPlaneClient);
1✔
553
    }
554

555
    ValidatedResourceUpdate<T> result = xdsResourceType.parse(args, resources);
1✔
556
    logger.log(XdsLogger.XdsLogLevel.INFO,
1✔
557
        "Received {0} Response version {1} nonce {2}. Parsed resources: {3}",
558
        xdsResourceType.typeName(), args.versionInfo, args.nonce, result.unpackedResources);
1✔
559
    Map<String, ParsedResource<T>> parsedResources = result.parsedResources;
1✔
560
    Set<String> invalidResources = result.invalidResources;
1✔
561
    metricReporter.reportResourceUpdates(Long.valueOf(parsedResources.size()),
1✔
562
        Long.valueOf(invalidResources.size()),
1✔
563
        args.getServerInfo().target(), xdsResourceType.typeUrl());
1✔
564

565
    List<String> errors = result.errors;
1✔
566
    String errorDetail = null;
1✔
567
    if (errors.isEmpty()) {
1✔
568
      checkArgument(invalidResources.isEmpty(), "found invalid resources but missing errors");
1✔
569
      controlPlaneClient.ackResponse(xdsResourceType, args.versionInfo, args.nonce);
1✔
570
    } else {
571
      errorDetail = Joiner.on('\n').join(errors);
1✔
572
      logger.log(XdsLogLevel.WARNING,
1✔
573
          "Failed processing {0} Response version {1} nonce {2}. Errors:\n{3}",
574
          xdsResourceType.typeName(), args.versionInfo, args.nonce, errorDetail);
1✔
575
      controlPlaneClient.nackResponse(xdsResourceType, args.nonce, errorDetail);
1✔
576
    }
577

578
    long updateTime = timeProvider.currentTimeNanos();
1✔
579
    Map<String, ResourceSubscriber<? extends ResourceUpdate>> subscribedResources =
1✔
580
        resourceSubscribers.getOrDefault(xdsResourceType, Collections.emptyMap());
1✔
581
    for (Map.Entry<String, ResourceSubscriber<?>> entry : subscribedResources.entrySet()) {
1✔
582
      String resourceName = entry.getKey();
1✔
583
      ResourceSubscriber<T> subscriber = (ResourceSubscriber<T>) entry.getValue();
1✔
584
      if (parsedResources.containsKey(resourceName)) {
1✔
585
        // Happy path: the resource updated successfully. Notify the watchers of the update.
586
        subscriber.onData(parsedResources.get(resourceName), args.versionInfo, updateTime,
1✔
587
            processingTracker);
588
        continue;
1✔
589
      }
590

591
      if (invalidResources.contains(resourceName)) {
1✔
592
        // The resource update is invalid. Capture the error without notifying the watchers.
593
        subscriber.onRejected(args.versionInfo, updateTime, errorDetail);
1✔
594
      }
595

596
      if (invalidResources.contains(resourceName)) {
1✔
597
        // The resource is missing. Reuse the cached resource if possible.
598
        if (subscriber.data == null) {
1✔
599
          // No cached data. Notify the watchers of an invalid update.
600
          subscriber.onError(Status.UNAVAILABLE.withDescription(errorDetail), processingTracker);
1✔
601
        }
602
        continue;
603
      }
604

605
      // Nothing else to do for incremental ADS resources.
606
      if (!xdsResourceType.isFullStateOfTheWorld()) {
1✔
607
        continue;
1✔
608
      }
609

610
      // For State of the World services, notify watchers when their watched resource is missing
611
      // from the ADS update. Note that we can only do this if the resource update is coming from
612
      // the same xDS server that the ResourceSubscriber is subscribed to.
613
      if (getActiveCpc(subscriber.authority) == controlPlaneClient) {
1✔
614
        subscriber.onAbsent(processingTracker, args.serverInfo);
1✔
615
      }
616
    }
1✔
617
  }
1✔
618

619
  @Override
620
  public Future<Void> reportServerConnections(ServerConnectionCallback callback) {
621
    SettableFuture<Void> future = SettableFuture.create();
1✔
622
    syncContext.execute(() -> {
1✔
623
      serverCpClientMap.forEach((serverInfo, controlPlaneClient) ->
1✔
624
          callback.reportServerConnectionGauge(
1✔
625
              !controlPlaneClient.isInError(), serverInfo.target()));
1✔
626
      future.set(null);
1✔
627
    });
1✔
628
    return future;
1✔
629
  }
630

631
  private void shutdownLowerPriorityCpcs(ControlPlaneClient activatedCpc) {
632
    // For each authority, remove any control plane clients, with lower priority than the activated
633
    // one, from activatedCpClients storing them all in cpcsToShutdown.
634
    Set<ControlPlaneClient> cpcsToShutdown = new HashSet<>();
1✔
635
    for ( List<ControlPlaneClient> cpcsForAuth : activatedCpClients.values()) {
1✔
636
      if (cpcsForAuth == null) {
1✔
637
        continue;
×
638
      }
639
      int index = cpcsForAuth.indexOf(activatedCpc);
1✔
640
      if (index > -1) {
1✔
641
        cpcsToShutdown.addAll(cpcsForAuth.subList(index + 1, cpcsForAuth.size()));
1✔
642
        cpcsForAuth.subList(index + 1, cpcsForAuth.size()).clear(); // remove lower priority cpcs
1✔
643
      }
644
    }
1✔
645

646
    // Shutdown any lower priority control plane clients identified above that aren't still being
647
    // used by another authority.  If they are still being used let the XDS server know that we
648
    // no longer are interested in subscriptions for authorities we are no longer responsible for.
649
    for (ControlPlaneClient cpc : cpcsToShutdown) {
1✔
650
      if (activatedCpClients.values().stream().noneMatch(list -> list.contains(cpc))) {
1✔
651
        cpc.shutdown();
1✔
652
        serverCpClientMap.remove(cpc.getServerInfo());
1✔
653
      } else {
654
        cpc.sendDiscoveryRequests();
×
655
      }
656
    }
1✔
657
  }
1✔
658

659

660
  /** Tracks a single subscribed resource. */
661
  private final class ResourceSubscriber<T extends ResourceUpdate> {
662
    @Nullable
663
    private final String authority;
664
    private final XdsResourceType<T> type;
665
    private final String resource;
666
    private final Map<ResourceWatcher<T>, Executor> watchers = new HashMap<>();
1✔
667
    @Nullable
668
    private T data;
669
    private boolean absent;
670
    // Tracks whether the deletion has been ignored per bootstrap server feature.
671
    // See https://github.com/grpc/proposal/blob/master/A53-xds-ignore-resource-deletion.md
672
    private boolean resourceDeletionIgnored;
673
    @Nullable
674
    private ScheduledHandle respTimer;
675
    @Nullable
676
    private ResourceMetadata metadata;
677
    @Nullable
678
    private String errorDescription;
679
    @Nullable
680
    private Status lastError;
681

682
    ResourceSubscriber(XdsResourceType<T> type, String resource) {
1✔
683
      syncContext.throwIfNotInThisSynchronizationContext();
1✔
684
      this.type = type;
1✔
685
      this.resource = resource;
1✔
686
      this.authority = getAuthorityFromResourceName(resource);
1✔
687
      if (getServerInfos(authority) == null) {
1✔
688
        this.errorDescription = "Wrong configuration: xds server does not exist for resource "
1✔
689
            + resource;
690
        return;
1✔
691
      }
692

693
      // Initialize metadata in UNKNOWN state to cover the case when resource subscriber,
694
      // is created but not yet requested because the client is in backoff.
695
      this.metadata = ResourceMetadata.newResourceMetadataUnknown();
1✔
696
    }
1✔
697

698
    @Override
699
    public String toString() {
700
      return "ResourceSubscriber{"
×
701
          + "resource='" + resource + '\''
702
          + ", authority='" + authority + '\''
703
          + ", type=" + type
704
          + ", watchers=" + watchers.size()
×
705
          + ", data=" + data
706
          + ", absent=" + absent
707
          + ", resourceDeletionIgnored=" + resourceDeletionIgnored
708
          + ", errorDescription='" + errorDescription + '\''
709
          + '}';
710
    }
711

712
    void addWatcher(ResourceWatcher<T> watcher, Executor watcherExecutor) {
713
      checkArgument(!watchers.containsKey(watcher), "watcher %s already registered", watcher);
1✔
714
      watchers.put(watcher, watcherExecutor);
1✔
715
      T savedData = data;
1✔
716
      boolean savedAbsent = absent;
1✔
717
      Status savedError = lastError;
1✔
718
      watcherExecutor.execute(() -> {
1✔
719
        if (errorDescription != null) {
1✔
720
          watcher.onError(Status.INVALID_ARGUMENT.withDescription(errorDescription));
1✔
721
          return;
1✔
722
        }
723
        if (savedError != null) {
1✔
724
          watcher.onError(savedError);
1✔
725
          return;
1✔
726
        }
727
        if (savedData != null) {
1✔
728
          notifyWatcher(watcher, savedData);
1✔
729
        } else if (savedAbsent) {
1✔
730
          watcher.onResourceDoesNotExist(resource);
1✔
731
        }
732
      });
1✔
733
    }
1✔
734

735
    void removeWatcher(ResourceWatcher<T> watcher) {
736
      checkArgument(watchers.containsKey(watcher), "watcher %s not registered", watcher);
1✔
737
      watchers.remove(watcher);
1✔
738
    }
1✔
739

740
    void restartTimer() {
741
      if (data != null || absent) {  // resource already resolved
1✔
742
        return;
×
743
      }
744
      ControlPlaneClient activeCpc = getActiveCpc(authority);
1✔
745
      if (activeCpc == null || !activeCpc.isReady()) {
1✔
746
        // When client becomes ready, it triggers a restartTimer for all relevant subscribers.
747
        return;
1✔
748
      }
749
      ServerInfo serverInfo = activeCpc.getServerInfo();
1✔
750
      int timeoutSec = serverInfo.resourceTimerIsTransientError()
1✔
751
          ? EXTENDED_RESOURCE_FETCH_TIMEOUT_SEC : INITIAL_RESOURCE_FETCH_TIMEOUT_SEC;
1✔
752

753
      class ResourceNotFound implements Runnable {
1✔
754
        @Override
755
        public void run() {
756
          logger.log(XdsLogLevel.INFO, "{0} resource {1} initial fetch timeout",
1✔
757
              type, resource);
1✔
758
          respTimer = null;
1✔
759
          onAbsent(null, activeCpc.getServerInfo());
1✔
760
        }
1✔
761

762
        @Override
763
        public String toString() {
764
          return type + this.getClass().getSimpleName();
1✔
765
        }
766
      }
767

768
      // Initial fetch scheduled or rescheduled, transition metadata state to REQUESTED.
769
      metadata = ResourceMetadata.newResourceMetadataRequested();
1✔
770

771
      if (respTimer != null) {
1✔
772
        respTimer.cancel();
×
773
      }
774
      respTimer = syncContext.schedule(
1✔
775
          new ResourceNotFound(), timeoutSec, TimeUnit.SECONDS, timeService);
1✔
776
    }
1✔
777

778
    void stopTimer() {
779
      if (respTimer != null && respTimer.isPending()) {
1✔
780
        respTimer.cancel();
1✔
781
        respTimer = null;
1✔
782
      }
783
    }
1✔
784

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

799
    boolean isWatched() {
800
      return !watchers.isEmpty();
1✔
801
    }
802

803
    boolean hasResult() {
804
      return data != null || absent;
1✔
805
    }
806

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

839
    private String getTarget() {
840
      ControlPlaneClient activeCpc = getActiveCpc(authority);
1✔
841
      return (activeCpc != null)
1✔
842
             ? activeCpc.getServerInfo().target()
1✔
843
             : "unknown";
1✔
844
    }
845

846
    void onAbsent(@Nullable ProcessingTracker processingTracker, ServerInfo serverInfo) {
847
      if (respTimer != null && respTimer.isPending()) {  // too early to conclude absence
1✔
848
        return;
1✔
849
      }
850

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

864
      logger.log(XdsLogLevel.INFO, "Conclude {0} resource {1} not exist", type, resource);
1✔
865
      if (!absent) {
1✔
866
        data = null;
1✔
867
        absent = true;
1✔
868
        lastError = null;
1✔
869
        metadata = serverInfo.resourceTimerIsTransientError()
1✔
870
            ? ResourceMetadata.newResourceMetadataTimeout()
1✔
871
            : ResourceMetadata.newResourceMetadataDoesNotExist();
1✔
872
        for (ResourceWatcher<T> watcher : watchers.keySet()) {
1✔
873
          if (processingTracker != null) {
1✔
874
            processingTracker.startTask();
1✔
875
          }
876
          watchers.get(watcher).execute(() -> {
1✔
877
            try {
878
              if (serverInfo.resourceTimerIsTransientError()) {
1✔
879
                watcher.onError(Status.UNAVAILABLE.withDescription(
1✔
880
                    "Timed out waiting for resource " + resource + " from xDS server"));
881
              } else {
882
                watcher.onResourceDoesNotExist(resource);
1✔
883
              }
884
            } finally {
885
              if (processingTracker != null) {
1✔
886
                processingTracker.onComplete();
1✔
887
              }
888
            }
889
          });
1✔
890
        }
1✔
891
      }
892
    }
1✔
893

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

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

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

924
    void onRejected(String rejectedVersion, long rejectedTime, String rejectedDetails) {
925
      metadata = ResourceMetadata
1✔
926
          .newResourceMetadataNacked(metadata, rejectedVersion, rejectedTime, rejectedDetails,
1✔
927
              data != null);
928
    }
1✔
929

930
    private void notifyWatcher(ResourceWatcher<T> watcher, T update) {
931
      watcher.onChanged(update);
1✔
932
    }
1✔
933
  }
934

935
  private class ResponseHandler implements XdsResponseHandler {
936
    final ServerInfo serverInfo;
937

938
    ResponseHandler(ServerInfo serverInfo) {
1✔
939
      this.serverInfo = serverInfo;
1✔
940
    }
1✔
941

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

958
    @Override
959
    public void handleStreamClosed(Status status, boolean shouldTryFallback) {
960
      syncContext.throwIfNotInThisSynchronizationContext();
1✔
961

962
      ControlPlaneClient cpcClosed = serverCpClientMap.get(serverInfo);
1✔
963
      if (cpcClosed == null) {
1✔
964
        logger.log(XdsLogLevel.DEBUG,
×
965
            "Couldn't find closing CPC for {0}, so skipping cleanup and reporting", serverInfo);
966
        return;
×
967
      }
968

969
      cleanUpResourceTimers(cpcClosed);
1✔
970

971
      if (status.isOk()) {
1✔
972
        return; // Not considered an error
1✔
973
      }
974

975
      metricReporter.reportServerFailure(1L, serverInfo.target());
1✔
976

977
      Collection<String> authoritiesForClosedCpc = getActiveAuthorities(cpcClosed);
1✔
978
      for (Map<String, ResourceSubscriber<? extends ResourceUpdate>> subscriberMap :
979
          resourceSubscribers.values()) {
1✔
980
        for (ResourceSubscriber<? extends ResourceUpdate> subscriber : subscriberMap.values()) {
1✔
981
          if (subscriber.hasResult() || !authoritiesForClosedCpc.contains(subscriber.authority)) {
1✔
982
            continue;
1✔
983
          }
984

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

994
          subscriber.onError(status, null);
1✔
995
        }
1✔
996
      }
1✔
997
    }
1✔
998

999
  }
1000

1001
  private static class CpcWithFallbackState {
1002
    ControlPlaneClient cpc;
1003
    boolean didFallback;
1004

1005
    private CpcWithFallbackState(ControlPlaneClient cpc, boolean didFallback) {
1✔
1006
      this.cpc = cpc;
1✔
1007
      this.didFallback = didFallback;
1✔
1008
    }
1✔
1009
  }
1010

1011
  private Collection<String> getActiveAuthorities(ControlPlaneClient cpc) {
1012
    List<String> asList = activatedCpClients.entrySet().stream()
1✔
1013
        .filter(entry -> !entry.getValue().isEmpty()
1✔
1014
            && cpc == entry.getValue().get(entry.getValue().size() - 1))
1✔
1015
        .map(Map.Entry::getKey)
1✔
1016
        .collect(Collectors.toList());
1✔
1017

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

1022
}
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