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

grpc / grpc-java / #19786

22 Apr 2025 09:04AM UTC coverage: 88.62% (+0.03%) from 88.591%
#19786

push

github

web-flow
xds: add the missing xds.authority metric (#12018)

This completes the [XDS client metrics](https://github.com/grpc/proposal/blob/master/A78-grpc-metrics-wrr-pf-xds.md#xdsclient) by adding the remaining grpc.xds.authority metric.

34771 of 39236 relevant lines covered (88.62%)

0.89 hits per line

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

93.4
/../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}",
×
366
              type.typeName(), resourceName);
×
367
          return;
×
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
      // Nothing else to do for incremental ADS resources.
596
      if (!xdsResourceType.isFullStateOfTheWorld()) {
1✔
597
        continue;
1✔
598
      }
599

600
      // Handle State of the World ADS: invalid resources.
601
      if (invalidResources.contains(resourceName)) {
1✔
602
        // The resource is missing. Reuse the cached resource if possible.
603
        if (subscriber.data == null) {
1✔
604
          // No cached data. Notify the watchers of an invalid update.
605
          subscriber.onError(Status.UNAVAILABLE.withDescription(errorDetail), processingTracker);
1✔
606
        }
607
        continue;
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

950
      cleanUpResourceTimers(cpcClosed);
1✔
951

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

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

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

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

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

980
  }
981

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

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

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

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

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