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

grpc / grpc-java / #19911

17 Jul 2025 06:05AM UTC coverage: 88.601% (-0.004%) from 88.605%
#19911

push

github

web-flow
Revert "xds: add "resource_timer_is_transient_failure" server feature (#12063)" (#12228)

34681 of 39143 relevant lines covered (88.6%)

0.89 hits per line

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

94.04
/../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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

423

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

522
    return controlPlaneClient;
1✔
523
  }
524

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

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

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

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

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

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

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

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

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

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

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

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

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

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

658

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

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

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

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

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

727
    void removeWatcher(ResourceWatcher<T> watcher) {
728
      checkArgument(watchers.containsKey(watcher), "watcher %s not registered", watcher);
1✔
729
      watchers.remove(watcher);
1✔
730
    }
1✔
731

732
    void restartTimer() {
733
      if (data != null || absent) {  // resource already resolved
1✔
734
        return;
×
735
      }
736
      ControlPlaneClient activeCpc = getActiveCpc(authority);
1✔
737
      if (activeCpc == null || !activeCpc.isReady()) {
1✔
738
        // When client becomes ready, it triggers a restartTimer for all relevant subscribers.
739
        return;
1✔
740
      }
741

742
      class ResourceNotFound implements Runnable {
1✔
743
        @Override
744
        public void run() {
745
          logger.log(XdsLogLevel.INFO, "{0} resource {1} initial fetch timeout",
1✔
746
              type, resource);
1✔
747
          respTimer = null;
1✔
748
          onAbsent(null, activeCpc.getServerInfo());
1✔
749
        }
1✔
750

751
        @Override
752
        public String toString() {
753
          return type + this.getClass().getSimpleName();
1✔
754
        }
755
      }
756

757
      // Initial fetch scheduled or rescheduled, transition metadata state to REQUESTED.
758
      metadata = ResourceMetadata.newResourceMetadataRequested();
1✔
759

760
      if (respTimer != null) {
1✔
761
        respTimer.cancel();
×
762
      }
763
      respTimer = syncContext.schedule(
1✔
764
          new ResourceNotFound(), INITIAL_RESOURCE_FETCH_TIMEOUT_SEC, TimeUnit.SECONDS,
765
          timeService);
1✔
766
    }
1✔
767

768
    void stopTimer() {
769
      if (respTimer != null && respTimer.isPending()) {
1✔
770
        respTimer.cancel();
1✔
771
        respTimer = null;
1✔
772
      }
773
    }
1✔
774

775
    void cancelResourceWatch() {
776
      if (isWatched()) {
1✔
777
        throw new IllegalStateException("Can't cancel resource watch with active watchers present");
×
778
      }
779
      stopTimer();
1✔
780
      String message = "Unsubscribing {0} resource {1} from server {2}";
1✔
781
      XdsLogLevel logLevel = XdsLogLevel.INFO;
1✔
782
      if (resourceDeletionIgnored) {
1✔
783
        message += " for which we previously ignored a deletion";
×
784
        logLevel = XdsLogLevel.FORCE_INFO;
×
785
      }
786
      logger.log(logLevel, message, type, resource, getTarget());
1✔
787
    }
1✔
788

789
    boolean isWatched() {
790
      return !watchers.isEmpty();
1✔
791
    }
792

793
    boolean hasResult() {
794
      return data != null || absent;
1✔
795
    }
796

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

828
    private String getTarget() {
829
      ControlPlaneClient activeCpc = getActiveCpc(authority);
1✔
830
      return (activeCpc != null)
1✔
831
             ? activeCpc.getServerInfo().target()
1✔
832
             : "unknown";
1✔
833
    }
834

835
    void onAbsent(@Nullable ProcessingTracker processingTracker, ServerInfo serverInfo) {
836
      if (respTimer != null && respTimer.isPending()) {  // too early to conclude absence
1✔
837
        return;
1✔
838
      }
839

840
      // Ignore deletion of State of the World resources when this feature is on,
841
      // and the resource is reusable.
842
      boolean ignoreResourceDeletionEnabled = serverInfo.ignoreResourceDeletion();
1✔
843
      if (ignoreResourceDeletionEnabled && type.isFullStateOfTheWorld() && data != null) {
1✔
844
        if (!resourceDeletionIgnored) {
1✔
845
          logger.log(XdsLogLevel.FORCE_WARNING,
1✔
846
              "xds server {0}: ignoring deletion for resource type {1} name {2}}",
847
              serverInfo.target(), type, resource);
1✔
848
          resourceDeletionIgnored = true;
1✔
849
        }
850
        return;
1✔
851
      }
852

853
      logger.log(XdsLogLevel.INFO, "Conclude {0} resource {1} not exist", type, resource);
1✔
854
      if (!absent) {
1✔
855
        data = null;
1✔
856
        absent = true;
1✔
857
        metadata = ResourceMetadata.newResourceMetadataDoesNotExist();
1✔
858
        for (ResourceWatcher<T> watcher : watchers.keySet()) {
1✔
859
          if (processingTracker != null) {
1✔
860
            processingTracker.startTask();
1✔
861
          }
862
          watchers.get(watcher).execute(() -> {
1✔
863
            try {
864
              watcher.onResourceDoesNotExist(resource);
1✔
865
            } finally {
866
              if (processingTracker != null) {
1✔
867
                processingTracker.onComplete();
1✔
868
              }
869
            }
870
          });
1✔
871
        }
1✔
872
      }
873
    }
1✔
874

875
    void onError(Status error, @Nullable ProcessingTracker tracker) {
876
      if (respTimer != null && respTimer.isPending()) {
1✔
877
        respTimer.cancel();
1✔
878
        respTimer = null;
1✔
879
      }
880

881
      // Include node ID in xds failures to allow cross-referencing with control plane logs
882
      // when debugging.
883
      String description = error.getDescription() == null ? "" : error.getDescription() + " ";
1✔
884
      Status errorAugmented = Status.fromCode(error.getCode())
1✔
885
          .withDescription(description + "nodeID: " + bootstrapInfo.node().getId())
1✔
886
          .withCause(error.getCause());
1✔
887

888
      for (ResourceWatcher<T> watcher : watchers.keySet()) {
1✔
889
        if (tracker != null) {
1✔
890
          tracker.startTask();
1✔
891
        }
892
        watchers.get(watcher).execute(() -> {
1✔
893
          try {
894
            watcher.onError(errorAugmented);
1✔
895
          } finally {
896
            if (tracker != null) {
1✔
897
              tracker.onComplete();
1✔
898
            }
899
          }
900
        });
1✔
901
      }
1✔
902
    }
1✔
903

904
    void onRejected(String rejectedVersion, long rejectedTime, String rejectedDetails) {
905
      metadata = ResourceMetadata
1✔
906
          .newResourceMetadataNacked(metadata, rejectedVersion, rejectedTime, rejectedDetails,
1✔
907
              data != null);
908
    }
1✔
909

910
    private void notifyWatcher(ResourceWatcher<T> watcher, T update) {
911
      watcher.onChanged(update);
1✔
912
    }
1✔
913
  }
914

915
  private class ResponseHandler implements XdsResponseHandler {
916
    final ServerInfo serverInfo;
917

918
    ResponseHandler(ServerInfo serverInfo) {
1✔
919
      this.serverInfo = serverInfo;
1✔
920
    }
1✔
921

922
    @Override
923
    public void handleResourceResponse(
924
        XdsResourceType<?> xdsResourceType, ServerInfo serverInfo, String versionInfo,
925
        List<Any> resources, String nonce, boolean isFirstResponse,
926
        ProcessingTracker processingTracker) {
927
      checkNotNull(xdsResourceType, "xdsResourceType");
1✔
928
      syncContext.throwIfNotInThisSynchronizationContext();
1✔
929
      Set<String> toParseResourceNames =
930
          xdsResourceType.shouldRetrieveResourceKeysForArgs()
1✔
931
          ? getResourceKeys(xdsResourceType)
1✔
932
          : null;
1✔
933
      XdsResourceType.Args args = new XdsResourceType.Args(serverInfo, versionInfo, nonce,
1✔
934
          bootstrapInfo, securityConfig, toParseResourceNames);
1✔
935
      handleResourceUpdate(args, resources, xdsResourceType, isFirstResponse, processingTracker);
1✔
936
    }
1✔
937

938
    @Override
939
    public void handleStreamClosed(Status status, boolean shouldTryFallback) {
940
      syncContext.throwIfNotInThisSynchronizationContext();
1✔
941

942
      ControlPlaneClient cpcClosed = serverCpClientMap.get(serverInfo);
1✔
943
      if (cpcClosed == null) {
1✔
944
        logger.log(XdsLogLevel.DEBUG,
×
945
            "Couldn't find closing CPC for {0}, so skipping cleanup and reporting", serverInfo);
946
        return;
×
947
      }
948

949
      cleanUpResourceTimers(cpcClosed);
1✔
950

951
      if (status.isOk()) {
1✔
952
        return; // Not considered an error
1✔
953
      }
954

955
      metricReporter.reportServerFailure(1L, serverInfo.target());
1✔
956

957
      Collection<String> authoritiesForClosedCpc = getActiveAuthorities(cpcClosed);
1✔
958
      for (Map<String, ResourceSubscriber<? extends ResourceUpdate>> subscriberMap :
959
          resourceSubscribers.values()) {
1✔
960
        for (ResourceSubscriber<? extends ResourceUpdate> subscriber : subscriberMap.values()) {
1✔
961
          if (subscriber.hasResult() || !authoritiesForClosedCpc.contains(subscriber.authority)) {
1✔
962
            continue;
1✔
963
          }
964

965
          // try to fallback to lower priority control plane client
966
          if (shouldTryFallback && manageControlPlaneClient(subscriber).didFallback) {
1✔
967
            authoritiesForClosedCpc.remove(subscriber.authority);
1✔
968
            if (authoritiesForClosedCpc.isEmpty()) {
1✔
969
              return; // optimization: no need to continue once all authorities have done fallback
1✔
970
            }
971
            continue; // since we did fallback, don't consider it an error
972
          }
973

974
          subscriber.onError(status, null);
1✔
975
        }
1✔
976
      }
1✔
977
    }
1✔
978

979
  }
980

981
  private static class CpcWithFallbackState {
982
    ControlPlaneClient cpc;
983
    boolean didFallback;
984

985
    private CpcWithFallbackState(ControlPlaneClient cpc, boolean didFallback) {
1✔
986
      this.cpc = cpc;
1✔
987
      this.didFallback = didFallback;
1✔
988
    }
1✔
989
  }
990

991
  private Collection<String> getActiveAuthorities(ControlPlaneClient cpc) {
992
    List<String> asList = activatedCpClients.entrySet().stream()
1✔
993
        .filter(entry -> !entry.getValue().isEmpty()
1✔
994
            && cpc == entry.getValue().get(entry.getValue().size() - 1))
1✔
995
        .map(Map.Entry::getKey)
1✔
996
        .collect(Collectors.toList());
1✔
997

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

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