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

grpc / grpc-java / #19585

10 Dec 2024 08:53PM UTC coverage: 88.578% (-0.06%) from 88.636%
#19585

push

github

web-flow
core: Simplify DnsNameResolver by using ObjectPool

ObjectPool is our standard solution for dealing with the
sometimes-shutdown resources. This was implemented by a contributor not
familiar with regular tools.

There are wider changes that can be made here, but I chose to just do a
smaller change because this class is used by GrpclbNameResolver.

33471 of 37787 relevant lines covered (88.58%)

0.89 hits per line

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

91.83
/../core/src/main/java/io/grpc/internal/DnsNameResolver.java
1
/*
2
 * Copyright 2015 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.internal;
18

19
import static com.google.common.base.Preconditions.checkNotNull;
20

21
import com.google.common.annotations.VisibleForTesting;
22
import com.google.common.base.MoreObjects;
23
import com.google.common.base.Objects;
24
import com.google.common.base.Preconditions;
25
import com.google.common.base.Stopwatch;
26
import com.google.common.base.Throwables;
27
import com.google.common.base.Verify;
28
import com.google.common.base.VerifyException;
29
import io.grpc.Attributes;
30
import io.grpc.EquivalentAddressGroup;
31
import io.grpc.NameResolver;
32
import io.grpc.ProxiedSocketAddress;
33
import io.grpc.ProxyDetector;
34
import io.grpc.Status;
35
import io.grpc.StatusOr;
36
import io.grpc.SynchronizationContext;
37
import io.grpc.internal.SharedResourceHolder.Resource;
38
import java.io.IOException;
39
import java.lang.reflect.Constructor;
40
import java.net.InetAddress;
41
import java.net.InetSocketAddress;
42
import java.net.URI;
43
import java.net.UnknownHostException;
44
import java.util.ArrayList;
45
import java.util.Arrays;
46
import java.util.Collections;
47
import java.util.HashSet;
48
import java.util.List;
49
import java.util.Map;
50
import java.util.Random;
51
import java.util.Set;
52
import java.util.concurrent.Executor;
53
import java.util.concurrent.TimeUnit;
54
import java.util.concurrent.atomic.AtomicReference;
55
import java.util.logging.Level;
56
import java.util.logging.Logger;
57
import javax.annotation.Nullable;
58

59
/**
60
 * A DNS-based {@link NameResolver}.
61
 *
62
 * <p>Each {@code A} or {@code AAAA} record emits an {@link EquivalentAddressGroup} in the list
63
 * passed to {@link NameResolver.Listener2#onResult2(ResolutionResult)}.
64
 *
65
 * @see DnsNameResolverProvider
66
 */
67
public class DnsNameResolver extends NameResolver {
68

69
  private static final Logger logger = Logger.getLogger(DnsNameResolver.class.getName());
1✔
70

71
  private static final String SERVICE_CONFIG_CHOICE_CLIENT_LANGUAGE_KEY = "clientLanguage";
72
  private static final String SERVICE_CONFIG_CHOICE_PERCENTAGE_KEY = "percentage";
73
  private static final String SERVICE_CONFIG_CHOICE_CLIENT_HOSTNAME_KEY = "clientHostname";
74
  private static final String SERVICE_CONFIG_CHOICE_SERVICE_CONFIG_KEY = "serviceConfig";
75

76
  // From https://github.com/grpc/proposal/blob/master/A2-service-configs-in-dns.md
77
  static final String SERVICE_CONFIG_PREFIX = "grpc_config=";
78
  private static final Set<String> SERVICE_CONFIG_CHOICE_KEYS =
1✔
79
      Collections.unmodifiableSet(
1✔
80
          new HashSet<>(
81
              Arrays.asList(
1✔
82
                  SERVICE_CONFIG_CHOICE_CLIENT_LANGUAGE_KEY,
83
                  SERVICE_CONFIG_CHOICE_PERCENTAGE_KEY,
84
                  SERVICE_CONFIG_CHOICE_CLIENT_HOSTNAME_KEY,
85
                  SERVICE_CONFIG_CHOICE_SERVICE_CONFIG_KEY)));
86

87
  // From https://github.com/grpc/proposal/blob/master/A2-service-configs-in-dns.md
88
  private static final String SERVICE_CONFIG_NAME_PREFIX = "_grpc_config.";
89

90
  private static final String JNDI_PROPERTY =
1✔
91
      System.getProperty("io.grpc.internal.DnsNameResolverProvider.enable_jndi", "true");
1✔
92
  private static final String JNDI_LOCALHOST_PROPERTY =
1✔
93
      System.getProperty("io.grpc.internal.DnsNameResolverProvider.enable_jndi_localhost", "false");
1✔
94
  private static final String JNDI_TXT_PROPERTY =
1✔
95
      System.getProperty("io.grpc.internal.DnsNameResolverProvider.enable_service_config", "false");
1✔
96

97
  /**
98
   * Java networking system properties name for caching DNS result.
99
   *
100
   * <p>Default value is -1 (cache forever) if security manager is installed. If security manager is
101
   * not installed, the ttl value is {@code null} which falls back to {@link
102
   * #DEFAULT_NETWORK_CACHE_TTL_SECONDS gRPC default value}.
103
   *
104
   * <p>For android, gRPC doesn't attempt to cache; this property value will be ignored.
105
   */
106
  @VisibleForTesting
107
  static final String NETWORKADDRESS_CACHE_TTL_PROPERTY = "networkaddress.cache.ttl";
108
  /** Default DNS cache duration if network cache ttl value is not specified ({@code null}). */
109
  @VisibleForTesting
110
  static final long DEFAULT_NETWORK_CACHE_TTL_SECONDS = 30;
111

112
  @VisibleForTesting
113
  static boolean enableJndi = Boolean.parseBoolean(JNDI_PROPERTY);
1✔
114
  @VisibleForTesting
115
  static boolean enableJndiLocalhost = Boolean.parseBoolean(JNDI_LOCALHOST_PROPERTY);
1✔
116
  @VisibleForTesting
117
  protected static boolean enableTxt = Boolean.parseBoolean(JNDI_TXT_PROPERTY);
1✔
118

119
  private static final ResourceResolverFactory resourceResolverFactory =
1✔
120
      getResourceResolverFactory(DnsNameResolver.class.getClassLoader());
1✔
121

122
  @VisibleForTesting
123
  final ProxyDetector proxyDetector;
124

125
  /** Access through {@link #getLocalHostname}. */
126
  private static String localHostname;
127

128
  private final Random random = new Random();
1✔
129

130
  protected volatile AddressResolver addressResolver = JdkAddressResolver.INSTANCE;
1✔
131
  private final AtomicReference<ResourceResolver> resourceResolver = new AtomicReference<>();
1✔
132

133
  private final String authority;
134
  private final String host;
135
  private final int port;
136

137
  private final ObjectPool<Executor> executorPool;
138
  private final long cacheTtlNanos;
139
  private final SynchronizationContext syncContext;
140
  private final ServiceConfigParser serviceConfigParser;
141

142
  // Following fields must be accessed from syncContext
143
  private final Stopwatch stopwatch;
144
  protected boolean resolved;
145
  private boolean shutdown;
146
  private Executor executor;
147

148
  private boolean resolving;
149

150
  // The field must be accessed from syncContext, although the methods on an Listener2 can be called
151
  // from any thread.
152
  private NameResolver.Listener2 listener;
153

154
  protected DnsNameResolver(
155
      @Nullable String nsAuthority,
156
      String name,
157
      Args args,
158
      Resource<Executor> executorResource,
159
      Stopwatch stopwatch,
160
      boolean isAndroid) {
1✔
161
    checkNotNull(args, "args");
1✔
162
    // TODO: if a DNS server is provided as nsAuthority, use it.
163
    // https://www.captechconsulting.com/blogs/accessing-the-dusty-corners-of-dns-with-java
164

165
    // Must prepend a "//" to the name when constructing a URI, otherwise it will be treated as an
166
    // opaque URI, thus the authority and host of the resulted URI would be null.
167
    URI nameUri = URI.create("//" + checkNotNull(name, "name"));
1✔
168
    Preconditions.checkArgument(nameUri.getHost() != null, "Invalid DNS name: %s", name);
1✔
169
    authority = Preconditions.checkNotNull(nameUri.getAuthority(),
1✔
170
        "nameUri (%s) doesn't have an authority", nameUri);
171
    host = nameUri.getHost();
1✔
172
    if (nameUri.getPort() == -1) {
1✔
173
      port = args.getDefaultPort();
1✔
174
    } else {
175
      port = nameUri.getPort();
1✔
176
    }
177
    this.proxyDetector = checkNotNull(args.getProxyDetector(), "proxyDetector");
1✔
178
    Executor offloadExecutor = args.getOffloadExecutor();
1✔
179
    if (offloadExecutor != null) {
1✔
180
      this.executorPool = new FixedObjectPool<>(offloadExecutor);
1✔
181
    } else {
182
      this.executorPool = SharedResourcePool.forResource(executorResource);
1✔
183
    }
184
    this.cacheTtlNanos = getNetworkAddressCacheTtlNanos(isAndroid);
1✔
185
    this.stopwatch = checkNotNull(stopwatch, "stopwatch");
1✔
186
    this.syncContext = checkNotNull(args.getSynchronizationContext(), "syncContext");
1✔
187
    this.serviceConfigParser = checkNotNull(args.getServiceConfigParser(), "serviceConfigParser");
1✔
188
  }
1✔
189

190
  @Override
191
  public String getServiceAuthority() {
192
    return authority;
1✔
193
  }
194

195
  @VisibleForTesting
196
  protected String getHost() {
197
    return host;
1✔
198
  }
199

200
  @Override
201
  public void start(Listener2 listener) {
202
    Preconditions.checkState(this.listener == null, "already started");
1✔
203
    executor = executorPool.getObject();
1✔
204
    this.listener = checkNotNull(listener, "listener");
1✔
205
    resolve();
1✔
206
  }
1✔
207

208
  @Override
209
  public void refresh() {
210
    Preconditions.checkState(listener != null, "not started");
1✔
211
    resolve();
1✔
212
  }
1✔
213

214
  private List<EquivalentAddressGroup> resolveAddresses() {
215
    List<? extends InetAddress> addresses;
216
    Exception addressesException = null;
1✔
217
    try {
218
      addresses = addressResolver.resolveAddress(host);
1✔
219
    } catch (Exception e) {
1✔
220
      addressesException = e;
1✔
221
      Throwables.throwIfUnchecked(e);
1✔
222
      throw new RuntimeException(e);
1✔
223
    } finally {
224
      if (addressesException != null) {
1✔
225
        logger.log(Level.FINE, "Address resolution failure", addressesException);
1✔
226
      }
227
    }
228
    // Each address forms an EAG
229
    List<EquivalentAddressGroup> servers = new ArrayList<>(addresses.size());
1✔
230
    for (InetAddress inetAddr : addresses) {
1✔
231
      servers.add(new EquivalentAddressGroup(new InetSocketAddress(inetAddr, port)));
1✔
232
    }
1✔
233
    return Collections.unmodifiableList(servers);
1✔
234
  }
235

236
  @Nullable
237
  private ConfigOrError resolveServiceConfig() {
238
    List<String> txtRecords = Collections.emptyList();
1✔
239
    ResourceResolver resourceResolver = getResourceResolver();
1✔
240
    if (resourceResolver != null) {
1✔
241
      try {
242
        txtRecords = resourceResolver.resolveTxt(SERVICE_CONFIG_NAME_PREFIX + host);
1✔
243
      } catch (Exception e) {
1✔
244
        logger.log(Level.FINE, "ServiceConfig resolution failure", e);
1✔
245
      }
1✔
246
    }
247
    if (!txtRecords.isEmpty()) {
1✔
248
      ConfigOrError rawServiceConfig = parseServiceConfig(txtRecords, random, getLocalHostname());
1✔
249
      if (rawServiceConfig != null) {
1✔
250
        if (rawServiceConfig.getError() != null) {
1✔
251
          return ConfigOrError.fromError(rawServiceConfig.getError());
1✔
252
        }
253

254
        @SuppressWarnings("unchecked")
255
        Map<String, ?> verifiedRawServiceConfig = (Map<String, ?>) rawServiceConfig.getConfig();
1✔
256
        return serviceConfigParser.parseServiceConfig(verifiedRawServiceConfig);
1✔
257
      }
258
    } else {
×
259
      logger.log(Level.FINE, "No TXT records found for {0}", new Object[]{host});
1✔
260
    }
261
    return null;
1✔
262
  }
263

264
  @Nullable
265
  private EquivalentAddressGroup detectProxy() throws IOException {
266
    InetSocketAddress destination =
1✔
267
        InetSocketAddress.createUnresolved(host, port);
1✔
268
    ProxiedSocketAddress proxiedAddr = proxyDetector.proxyFor(destination);
1✔
269
    if (proxiedAddr != null) {
1✔
270
      return new EquivalentAddressGroup(proxiedAddr);
1✔
271
    }
272
    return null;
1✔
273
  }
274

275
  /**
276
   * Main logic of name resolution.
277
   */
278
  protected InternalResolutionResult doResolve(boolean forceTxt) {
279
    InternalResolutionResult result = new InternalResolutionResult();
1✔
280
    try {
281
      result.addresses = resolveAddresses();
1✔
282
    } catch (Exception e) {
1✔
283
      if (!forceTxt) {
1✔
284
        result.error =
1✔
285
            Status.UNAVAILABLE.withDescription("Unable to resolve host " + host).withCause(e);
1✔
286
        return result;
1✔
287
      }
288
    }
1✔
289
    if (enableTxt) {
1✔
290
      result.config = resolveServiceConfig();
1✔
291
    }
292
    return result;
1✔
293
  }
294

295
  private final class Resolve implements Runnable {
296
    private final Listener2 savedListener;
297

298
    Resolve(Listener2 savedListener) {
1✔
299
      this.savedListener = checkNotNull(savedListener, "savedListener");
1✔
300
    }
1✔
301

302
    @Override
303
    public void run() {
304
      if (logger.isLoggable(Level.FINER)) {
1✔
305
        logger.finer("Attempting DNS resolution of " + host);
×
306
      }
307
      InternalResolutionResult result = null;
1✔
308
      try {
309
        EquivalentAddressGroup proxiedAddr = detectProxy();
1✔
310
        ResolutionResult.Builder resolutionResultBuilder = ResolutionResult.newBuilder();
1✔
311
        if (proxiedAddr != null) {
1✔
312
          if (logger.isLoggable(Level.FINER)) {
1✔
313
            logger.finer("Using proxy address " + proxiedAddr);
×
314
          }
315
          resolutionResultBuilder.setAddressesOrError(
1✔
316
              StatusOr.fromValue(Collections.singletonList(proxiedAddr)));
1✔
317
        } else {
318
          result = doResolve(false);
1✔
319
          if (result.error != null) {
1✔
320
            InternalResolutionResult finalResult = result;
1✔
321
            syncContext.execute(() ->
1✔
322
                savedListener.onResult2(ResolutionResult.newBuilder()
1✔
323
                    .setAddressesOrError(StatusOr.fromStatus(finalResult.error))
1✔
324
                    .build()));
1✔
325
            return;
1✔
326
          }
327
          if (result.addresses != null) {
1✔
328
            resolutionResultBuilder.setAddressesOrError(StatusOr.fromValue(result.addresses));
1✔
329
          }
330
          if (result.config != null) {
1✔
331
            resolutionResultBuilder.setServiceConfig(result.config);
1✔
332
          }
333
          if (result.attributes != null) {
1✔
334
            resolutionResultBuilder.setAttributes(result.attributes);
1✔
335
          }
336
        }
337
        syncContext.execute(() -> {
1✔
338
          savedListener.onResult2(resolutionResultBuilder.build());
1✔
339
        });
1✔
340
      } catch (IOException e) {
1✔
341
        syncContext.execute(() ->
1✔
342
            savedListener.onResult2(ResolutionResult.newBuilder()
1✔
343
                .setAddressesOrError(
1✔
344
                    StatusOr.fromStatus(
1✔
345
                        Status.UNAVAILABLE.withDescription(
1✔
346
                            "Unable to resolve host " + host).withCause(e))).build()));
1✔
347
      } finally {
348
        final boolean succeed = result != null && result.error == null;
1✔
349
        syncContext.execute(new Runnable() {
1✔
350
          @Override
351
          public void run() {
352
            if (succeed) {
1✔
353
              resolved = true;
1✔
354
              if (cacheTtlNanos > 0) {
1✔
355
                stopwatch.reset().start();
1✔
356
              }
357
            }
358
            resolving = false;
1✔
359
          }
1✔
360
        });
361
      }
362
    }
1✔
363
  }
364

365
  @Nullable
366
  static ConfigOrError parseServiceConfig(
367
      List<String> rawTxtRecords, Random random, String localHostname) {
368
    List<Map<String, ?>> possibleServiceConfigChoices;
369
    try {
370
      possibleServiceConfigChoices = parseTxtResults(rawTxtRecords);
1✔
371
    } catch (IOException | RuntimeException e) {
1✔
372
      return ConfigOrError.fromError(
1✔
373
          Status.UNKNOWN.withDescription("failed to parse TXT records").withCause(e));
1✔
374
    }
1✔
375
    Map<String, ?> possibleServiceConfig = null;
1✔
376
    for (Map<String, ?> possibleServiceConfigChoice : possibleServiceConfigChoices) {
1✔
377
      try {
378
        possibleServiceConfig =
1✔
379
            maybeChooseServiceConfig(possibleServiceConfigChoice, random, localHostname);
1✔
380
      } catch (RuntimeException e) {
1✔
381
        return ConfigOrError.fromError(
1✔
382
            Status.UNKNOWN.withDescription("failed to pick service config choice").withCause(e));
1✔
383
      }
1✔
384
      if (possibleServiceConfig != null) {
1✔
385
        break;
1✔
386
      }
387
    }
×
388
    if (possibleServiceConfig == null) {
1✔
389
      return null;
1✔
390
    }
391
    return ConfigOrError.fromConfig(possibleServiceConfig);
1✔
392
  }
393

394
  private void resolve() {
395
    if (resolving || shutdown || !cacheRefreshRequired()) {
1✔
396
      return;
1✔
397
    }
398
    resolving = true;
1✔
399
    executor.execute(new Resolve(listener));
1✔
400
  }
1✔
401

402
  private boolean cacheRefreshRequired() {
403
    return !resolved
1✔
404
        || cacheTtlNanos == 0
405
        || (cacheTtlNanos > 0 && stopwatch.elapsed(TimeUnit.NANOSECONDS) > cacheTtlNanos);
1✔
406
  }
407

408
  @Override
409
  public void shutdown() {
410
    if (shutdown) {
1✔
411
      return;
×
412
    }
413
    shutdown = true;
1✔
414
    if (executor != null) {
1✔
415
      executor = executorPool.returnObject(executor);
1✔
416
    }
417
  }
1✔
418

419
  final int getPort() {
420
    return port;
1✔
421
  }
422

423
  /**
424
   * Parse TXT service config records as JSON.
425
   *
426
   * @throws IOException if one of the txt records contains improperly formatted JSON.
427
   */
428
  @VisibleForTesting
429
  static List<Map<String, ?>> parseTxtResults(List<String> txtRecords) throws IOException {
430
    List<Map<String, ?>> possibleServiceConfigChoices = new ArrayList<>();
1✔
431
    for (String txtRecord : txtRecords) {
1✔
432
      if (!txtRecord.startsWith(SERVICE_CONFIG_PREFIX)) {
1✔
433
        logger.log(Level.FINE, "Ignoring non service config {0}", new Object[]{txtRecord});
1✔
434
        continue;
1✔
435
      }
436
      Object rawChoices = JsonParser.parse(txtRecord.substring(SERVICE_CONFIG_PREFIX.length()));
1✔
437
      if (!(rawChoices instanceof List)) {
1✔
438
        throw new ClassCastException("wrong type " + rawChoices);
1✔
439
      }
440
      List<?> listChoices = (List<?>) rawChoices;
1✔
441
      possibleServiceConfigChoices.addAll(JsonUtil.checkObjectList(listChoices));
1✔
442
    }
1✔
443
    return possibleServiceConfigChoices;
1✔
444
  }
445

446
  @Nullable
447
  private static final Double getPercentageFromChoice(Map<String, ?> serviceConfigChoice) {
448
    return JsonUtil.getNumberAsDouble(serviceConfigChoice, SERVICE_CONFIG_CHOICE_PERCENTAGE_KEY);
1✔
449
  }
450

451
  @Nullable
452
  private static final List<String> getClientLanguagesFromChoice(
453
      Map<String, ?> serviceConfigChoice) {
454
    return JsonUtil.getListOfStrings(
1✔
455
        serviceConfigChoice, SERVICE_CONFIG_CHOICE_CLIENT_LANGUAGE_KEY);
456
  }
457

458
  @Nullable
459
  private static final List<String> getHostnamesFromChoice(Map<String, ?> serviceConfigChoice) {
460
    return JsonUtil.getListOfStrings(
1✔
461
        serviceConfigChoice, SERVICE_CONFIG_CHOICE_CLIENT_HOSTNAME_KEY);
462
  }
463

464
  /**
465
   * Returns value of network address cache ttl property if not Android environment. For android,
466
   * DnsNameResolver does not cache the dns lookup result.
467
   */
468
  private static long getNetworkAddressCacheTtlNanos(boolean isAndroid) {
469
    if (isAndroid) {
1✔
470
      // on Android, ignore dns cache.
471
      return 0;
1✔
472
    }
473

474
    String cacheTtlPropertyValue = System.getProperty(NETWORKADDRESS_CACHE_TTL_PROPERTY);
1✔
475
    long cacheTtl = DEFAULT_NETWORK_CACHE_TTL_SECONDS;
1✔
476
    if (cacheTtlPropertyValue != null) {
1✔
477
      try {
478
        cacheTtl = Long.parseLong(cacheTtlPropertyValue);
1✔
479
      } catch (NumberFormatException e) {
1✔
480
        logger.log(
1✔
481
            Level.WARNING,
482
            "Property({0}) valid is not valid number format({1}), fall back to default({2})",
483
            new Object[] {NETWORKADDRESS_CACHE_TTL_PROPERTY, cacheTtlPropertyValue, cacheTtl});
1✔
484
      }
1✔
485
    }
486
    return cacheTtl > 0 ? TimeUnit.SECONDS.toNanos(cacheTtl) : cacheTtl;
1✔
487
  }
488

489
  /**
490
   * Determines if a given Service Config choice applies, and if so, returns it.
491
   *
492
   * @see <a href="https://github.com/grpc/proposal/blob/master/A2-service-configs-in-dns.md">
493
   *   Service Config in DNS</a>
494
   * @param choice The service config choice.
495
   * @return The service config object or {@code null} if this choice does not apply.
496
   */
497
  @Nullable
498
  @VisibleForTesting
499
  static Map<String, ?> maybeChooseServiceConfig(
500
      Map<String, ?> choice, Random random, String hostname) {
501
    for (Map.Entry<String, ?> entry : choice.entrySet()) {
1✔
502
      Verify.verify(SERVICE_CONFIG_CHOICE_KEYS.contains(entry.getKey()), "Bad key: %s", entry);
1✔
503
    }
1✔
504

505
    List<String> clientLanguages = getClientLanguagesFromChoice(choice);
1✔
506
    if (clientLanguages != null && !clientLanguages.isEmpty()) {
1✔
507
      boolean javaPresent = false;
1✔
508
      for (String lang : clientLanguages) {
1✔
509
        if ("java".equalsIgnoreCase(lang)) {
1✔
510
          javaPresent = true;
1✔
511
          break;
1✔
512
        }
513
      }
1✔
514
      if (!javaPresent) {
1✔
515
        return null;
1✔
516
      }
517
    }
518
    Double percentage = getPercentageFromChoice(choice);
1✔
519
    if (percentage != null) {
1✔
520
      int pct = percentage.intValue();
1✔
521
      Verify.verify(pct >= 0 && pct <= 100, "Bad percentage: %s", percentage);
1✔
522
      if (random.nextInt(100) >= pct) {
1✔
523
        return null;
1✔
524
      }
525
    }
526
    List<String> clientHostnames = getHostnamesFromChoice(choice);
1✔
527
    if (clientHostnames != null && !clientHostnames.isEmpty()) {
1✔
528
      boolean hostnamePresent = false;
1✔
529
      for (String clientHostname : clientHostnames) {
1✔
530
        if (clientHostname.equals(hostname)) {
1✔
531
          hostnamePresent = true;
1✔
532
          break;
1✔
533
        }
534
      }
1✔
535
      if (!hostnamePresent) {
1✔
536
        return null;
1✔
537
      }
538
    }
539
    Map<String, ?> sc =
1✔
540
        JsonUtil.getObject(choice, SERVICE_CONFIG_CHOICE_SERVICE_CONFIG_KEY);
1✔
541
    if (sc == null) {
1✔
542
      throw new VerifyException(String.format(
×
543
          "key '%s' missing in '%s'", choice, SERVICE_CONFIG_CHOICE_SERVICE_CONFIG_KEY));
544
    }
545
    return sc;
1✔
546
  }
547

548
  /**
549
   * Used as a DNS-based name resolver's internal representation of resolution result.
550
   */
551
  protected static final class InternalResolutionResult {
552
    private Status error;
553
    private List<EquivalentAddressGroup> addresses;
554
    private ConfigOrError config;
555
    public Attributes attributes;
556

557
    private InternalResolutionResult() {}
558
  }
559

560
  /**
561
   * Describes a parsed SRV record.
562
   */
563
  @VisibleForTesting
564
  public static final class SrvRecord {
565
    public final String host;
566
    public final int port;
567

568
    public SrvRecord(String host, int port) {
1✔
569
      this.host = host;
1✔
570
      this.port = port;
1✔
571
    }
1✔
572

573
    @Override
574
    public int hashCode() {
575
      return Objects.hashCode(host, port);
×
576
    }
577

578
    @Override
579
    public boolean equals(Object obj) {
580
      if (this == obj) {
1✔
581
        return true;
×
582
      }
583
      if (obj == null || getClass() != obj.getClass()) {
1✔
584
        return false;
×
585
      }
586
      SrvRecord that = (SrvRecord) obj;
1✔
587
      return port == that.port && host.equals(that.host);
1✔
588
    }
589

590
    @Override
591
    public String toString() {
592
      return
×
593
          MoreObjects.toStringHelper(this)
×
594
              .add("host", host)
×
595
              .add("port", port)
×
596
              .toString();
×
597
    }
598
  }
599

600
  @VisibleForTesting
601
  protected void setAddressResolver(AddressResolver addressResolver) {
602
    this.addressResolver = addressResolver;
1✔
603
  }
1✔
604

605
  @VisibleForTesting
606
  protected void setResourceResolver(ResourceResolver resourceResolver) {
607
    this.resourceResolver.set(resourceResolver);
1✔
608
  }
1✔
609

610
  /**
611
   * {@link ResourceResolverFactory} is a factory for making resource resolvers.  It supports
612
   * optionally checking if the factory is available.
613
   */
614
  interface ResourceResolverFactory {
615

616
    /**
617
     * Creates a new resource resolver.  The return value is {@code null} iff
618
     * {@link #unavailabilityCause()} is not null;
619
     */
620
    @Nullable ResourceResolver newResourceResolver();
621

622
    /**
623
     * Returns the reason why the resource resolver cannot be created.  The return value is
624
     * {@code null} if {@link #newResourceResolver()} is suitable for use.
625
     */
626
    @Nullable Throwable unavailabilityCause();
627
  }
628

629
  /**
630
   * AddressResolver resolves a hostname into a list of addresses.
631
   */
632
  @VisibleForTesting
633
  public interface AddressResolver {
634
    List<InetAddress> resolveAddress(String host) throws Exception;
635
  }
636

637
  private enum JdkAddressResolver implements AddressResolver {
1✔
638
    INSTANCE;
1✔
639

640
    @Override
641
    public List<InetAddress> resolveAddress(String host) throws UnknownHostException {
642
      return Collections.unmodifiableList(Arrays.asList(InetAddress.getAllByName(host)));
1✔
643
    }
644
  }
645

646
  /**
647
   * {@link ResourceResolver} is a Dns ResourceRecord resolver.
648
   */
649
  @VisibleForTesting
650
  public interface ResourceResolver {
651
    List<String> resolveTxt(String host) throws Exception;
652

653
    List<SrvRecord> resolveSrv(String host) throws Exception;
654
  }
655

656
  @Nullable
657
  protected ResourceResolver getResourceResolver() {
658
    if (!shouldUseJndi(enableJndi, enableJndiLocalhost, host)) {
1✔
659
      return null;
1✔
660
    }
661
    ResourceResolver rr;
662
    if ((rr = resourceResolver.get()) == null) {
1✔
663
      if (resourceResolverFactory != null) {
1✔
664
        assert resourceResolverFactory.unavailabilityCause() == null;
1✔
665
        rr = resourceResolverFactory.newResourceResolver();
1✔
666
      }
667
    }
668
    return rr;
1✔
669
  }
670

671
  @Nullable
672
  @VisibleForTesting
673
  static ResourceResolverFactory getResourceResolverFactory(ClassLoader loader) {
674
    Class<? extends ResourceResolverFactory> jndiClazz;
675
    try {
676
      jndiClazz =
1✔
677
          Class.forName("io.grpc.internal.JndiResourceResolverFactory", true, loader)
1✔
678
              .asSubclass(ResourceResolverFactory.class);
1✔
679
    } catch (ClassNotFoundException e) {
1✔
680
      logger.log(Level.FINE, "Unable to find JndiResourceResolverFactory, skipping.", e);
1✔
681
      return null;
1✔
682
    } catch (ClassCastException e) {
1✔
683
      // This can happen if JndiResourceResolverFactory was removed by something like Proguard
684
      // combined with a broken ClassLoader that prefers classes from the child over the parent
685
      // while also not properly filtering dependencies in the parent that should be hidden. If the
686
      // class loader prefers loading from the parent then ResourceresolverFactory would have also
687
      // been loaded from the parent. If the class loader filtered deps, then
688
      // JndiResourceResolverFactory wouldn't have been found.
689
      logger.log(Level.FINE, "Unable to cast JndiResourceResolverFactory, skipping.", e);
1✔
690
      return null;
1✔
691
    }
1✔
692
    Constructor<? extends ResourceResolverFactory> jndiCtor;
693
    try {
694
      jndiCtor = jndiClazz.getConstructor();
1✔
695
    } catch (Exception e) {
×
696
      logger.log(Level.FINE, "Can't find JndiResourceResolverFactory ctor, skipping.", e);
×
697
      return null;
×
698
    }
1✔
699
    ResourceResolverFactory rrf;
700
    try {
701
      rrf = jndiCtor.newInstance();
1✔
702
    } catch (Exception e) {
×
703
      logger.log(Level.FINE, "Can't construct JndiResourceResolverFactory, skipping.", e);
×
704
      return null;
×
705
    }
1✔
706
    if (rrf.unavailabilityCause() != null) {
1✔
707
      logger.log(
×
708
          Level.FINE,
709
          "JndiResourceResolverFactory not available, skipping.",
710
          rrf.unavailabilityCause());
×
711
      return null;
×
712
    }
713
    return rrf;
1✔
714
  }
715

716
  private static String getLocalHostname() {
717
    if (localHostname == null) {
1✔
718
      try {
719
        localHostname = InetAddress.getLocalHost().getHostName();
1✔
720
      } catch (UnknownHostException e) {
×
721
        throw new RuntimeException(e);
×
722
      }
1✔
723
    }
724
    return localHostname;
1✔
725
  }
726

727
  @VisibleForTesting
728
  protected static boolean shouldUseJndi(
729
      boolean jndiEnabled, boolean jndiLocalhostEnabled, String target) {
730
    if (!jndiEnabled) {
1✔
731
      return false;
1✔
732
    }
733
    if ("localhost".equalsIgnoreCase(target)) {
1✔
734
      return jndiLocalhostEnabled;
1✔
735
    }
736
    // Check if this name looks like IPv6
737
    if (target.contains(":")) {
1✔
738
      return false;
1✔
739
    }
740
    // Check if this might be IPv4.  Such addresses have no alphabetic characters.  This also
741
    // checks the target is empty.
742
    boolean alldigits = true;
1✔
743
    for (int i = 0; i < target.length(); i++) {
1✔
744
      char c = target.charAt(i);
1✔
745
      if (c != '.') {
1✔
746
        alldigits &= (c >= '0' && c <= '9');
1✔
747
      }
748
    }
749
    return !alldigits;
1✔
750
  }
751
}
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