Improve logging in DnsServerAddressStreamProviders (#10848)

Motivation:

`DnsServerAddressStreamProviders` tries to load `MacOSDnsServerAddressStreamProvider`
on macOS. However, it doesn't warn users when `MacOSDnsServerAddressStreamProvider`
is not awailable, which may cause incorrect results for DNS resolutions.

Modifications:

- Log at warn level if `MacOSDnsServerAddressStreamProvider` is not found on macOS;
- Log at debug level when `MacOSDnsServerAddressStreamProvider` is loaded and available;

Result:

macOS users are notified when `MacOSDnsServerAddressStreamProvider` is not available.
This commit is contained in:
Idel Pivnitskiy 2020-12-10 05:19:39 -08:00 committed by GitHub
parent 0537170ab5
commit b4479353e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,7 +21,6 @@ import io.netty.util.internal.logging.InternalLoggerFactory;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -35,6 +34,8 @@ public final class DnsServerAddressStreamProviders {
private static final InternalLogger LOGGER = private static final InternalLogger LOGGER =
InternalLoggerFactory.getInstance(DnsServerAddressStreamProviders.class); InternalLoggerFactory.getInstance(DnsServerAddressStreamProviders.class);
private static final Constructor<? extends DnsServerAddressStreamProvider> STREAM_PROVIDER_CONSTRUCTOR; private static final Constructor<? extends DnsServerAddressStreamProvider> STREAM_PROVIDER_CONSTRUCTOR;
private static final String MACOS_PROVIDER_CLASS_NAME =
"io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider";
static { static {
Constructor<? extends DnsServerAddressStreamProvider> constructor = null; Constructor<? extends DnsServerAddressStreamProvider> constructor = null;
@ -47,7 +48,7 @@ public final class DnsServerAddressStreamProviders {
public Object run() { public Object run() {
try { try {
return Class.forName( return Class.forName(
"io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider", MACOS_PROVIDER_CLASS_NAME,
true, true,
DnsServerAddressStreamProviders.class.getClassLoader()); DnsServerAddressStreamProviders.class.getClassLoader());
} catch (Throwable cause) { } catch (Throwable cause) {
@ -59,16 +60,16 @@ public final class DnsServerAddressStreamProviders {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Class<? extends DnsServerAddressStreamProvider> providerClass = Class<? extends DnsServerAddressStreamProvider> providerClass =
(Class<? extends DnsServerAddressStreamProvider>) maybeProvider; (Class<? extends DnsServerAddressStreamProvider>) maybeProvider;
Method method = providerClass.getMethod("ensureAvailability");
method.invoke(null);
constructor = providerClass.getConstructor(); constructor = providerClass.getConstructor();
constructor.newInstance(); constructor.newInstance(); // ctor ensures availability
} else if (!(maybeProvider instanceof ClassNotFoundException)) { LOGGER.debug("{}: available", MACOS_PROVIDER_CLASS_NAME);
} else {
throw (Throwable) maybeProvider; throw (Throwable) maybeProvider;
} }
} catch (Throwable cause) { } catch (Throwable cause) {
LOGGER.debug( LOGGER.warn(
"Unable to use MacOSDnsServerAddressStreamProvider, fallback to system defaults", cause); "Unable to load {}, fallback to system defaults. {}", MACOS_PROVIDER_CLASS_NAME,
"This may result in incorrect DNS resolutions on MacOS.", cause);
constructor = null; constructor = null;
} }
} }