DnsNameResolver should log in trace level if notification of the promise fails (#9022)

Motivation:

During investigating some other bug I noticed that we log with warn level if we fail to notify the promise due the fact that it is already full-filled. This is not correct and missleading as there is nothing wrong with it in general. A promise may already been fullfilled because we did multiple queries and one of these was successful.

Modifications:

- Change log level to trace
- Add unit test which before did log with warn level but now does with trace level.

Result:

Less missleading noise in the log.
This commit is contained in:
Norman Maurer 2019-04-10 07:13:53 +02:00
parent fdb4b0e7af
commit dbd2282abe
3 changed files with 68 additions and 5 deletions

View File

@ -858,13 +858,19 @@ public class DnsNameResolver extends InetNameResolver {
static <T> void trySuccess(Promise<T> promise, T result) {
if (!promise.trySuccess(result)) {
logger.warn("Failed to notify success ({}) to a promise: {}", result, promise);
// There is nothing really wrong with not be able to notify the promise as we may have raced here because
// of multiple queries that have been executed. Log it with trace level anyway just in case the user
// wants to better understand what happened.
logger.trace("Failed to notify success ({}) to a promise: {}", result, promise);
}
}
private static void tryFailure(Promise<?> promise, Throwable cause) {
if (!promise.tryFailure(cause)) {
logger.warn("Failed to notify failure to a promise: {}", promise, cause);
// There is nothing really wrong with not be able to notify the promise as we may have raced here because
// of multiple queries that have been executed. Log it with trace level anyway just in case the user
// wants to better understand what happened.
logger.trace("Failed to notify failure to a promise: {}", promise, cause);
}
}

View File

@ -2550,4 +2550,31 @@ public class DnsNameResolverTest {
}
}
}
@Test
public void testDropAAAA() throws IOException {
String host = "somehost.netty.io";
TestDnsServer dnsServer2 = new TestDnsServer(Collections.singleton(host));
dnsServer2.start(true);
DnsNameResolver resolver = null;
try {
DnsNameResolverBuilder builder = newResolver()
.recursionDesired(false)
.queryTimeoutMillis(500)
.resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED)
.maxQueriesPerResolve(16)
.nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress()));
resolver = builder.build();
List<InetAddress> addressList = resolver.resolveAll(host).syncUninterruptibly().getNow();
assertEquals(1, addressList.size());
assertEquals(host, addressList.get(0).getHostName());
} finally {
dnsServer2.stop();
if (resolver != null) {
resolver.close();
}
}
}
}

View File

@ -17,6 +17,7 @@ package io.netty.resolver.dns;
import io.netty.util.NetUtil;
import org.apache.directory.server.dns.DnsServer;
import org.apache.directory.server.dns.io.decoder.DnsMessageDecoder;
import org.apache.directory.server.dns.io.encoder.DnsMessageEncoder;
import org.apache.directory.server.dns.io.encoder.ResourceRecordEncoder;
import org.apache.directory.server.dns.messages.DnsMessage;
@ -27,7 +28,6 @@ import org.apache.directory.server.dns.messages.ResourceRecord;
import org.apache.directory.server.dns.messages.ResourceRecordImpl;
import org.apache.directory.server.dns.messages.ResourceRecordModifier;
import org.apache.directory.server.dns.protocol.DnsProtocolHandler;
import org.apache.directory.server.dns.protocol.DnsUdpDecoder;
import org.apache.directory.server.dns.protocol.DnsUdpEncoder;
import org.apache.directory.server.dns.store.DnsAttribute;
import org.apache.directory.server.dns.store.RecordStore;
@ -37,6 +37,8 @@ import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderAdapter;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.apache.mina.transport.socket.DatagramAcceptor;
@ -83,6 +85,13 @@ class TestDnsServer extends DnsServer {
@Override
public void start() throws IOException {
start(false);
}
/**
* Start the {@link TestDnsServer} but drop all {@code AAAA} queries and not send any response to these at all.
*/
public void start(final boolean dropAAAAQueries) throws IOException {
InetSocketAddress address = new InetSocketAddress(NetUtil.LOCALHOST4, 0);
UdpTransport transport = new UdpTransport(address.getHostName(), address.getPort());
setTransports(transport);
@ -94,7 +103,8 @@ class TestDnsServer extends DnsServer {
public void sessionCreated(IoSession session) {
// USe our own codec to support AAAA testing
session.getFilterChain()
.addFirst("codec", new ProtocolCodecFilter(new TestDnsProtocolUdpCodecFactory()));
.addFirst("codec", new ProtocolCodecFilter(
new TestDnsProtocolUdpCodecFactory(dropAAAAQueries)));
}
});
@ -142,6 +152,11 @@ class TestDnsServer extends DnsServer {
private final class TestDnsProtocolUdpCodecFactory implements ProtocolCodecFactory {
private final DnsMessageEncoder encoder = new DnsMessageEncoder();
private final TestAAAARecordEncoder recordEncoder = new TestAAAARecordEncoder();
private final boolean dropAAAArecords;
TestDnsProtocolUdpCodecFactory(boolean dropAAAArecords) {
this.dropAAAArecords = dropAAAArecords;
}
@Override
public ProtocolEncoder getEncoder(IoSession session) {
@ -175,7 +190,22 @@ class TestDnsServer extends DnsServer {
@Override
public ProtocolDecoder getDecoder(IoSession session) {
return new DnsUdpDecoder();
return new ProtocolDecoderAdapter() {
private DnsMessageDecoder decoder = new DnsMessageDecoder();
@Override
public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws IOException {
DnsMessage message = decoder.decode(in);
if (dropAAAArecords) {
for (QuestionRecord record: message.getQuestionRecords()) {
if (record.getRecordType() == RecordType.AAAA) {
return;
}
}
}
out.write(message);
}
};
}
private final class TestAAAARecordEncoder extends ResourceRecordEncoder {