Add cache for CNAME mappings resolved during lookup of DNS entries. (#8314)
* Add cache for CNAME mappings resolved during lookup of DNS entries. Motivation: If the CNAMEd hostname is backed by load balancing component, typically the final A or AAAA DNS records have small TTL. However, the CNAME record itself is setup with longer TTL. For example: * x.netty.io could be CNAMEd to y.netty.io with TTL of 5 min * A / AAAA records for y.netty.io has a TTL of 0.5 min In current Netty implementation, original hostname is saved in resolved cached with the TTL of final A / AAAA records. When that cache entry expires, Netty recursive resolver sends at least two queries — 1st one to be resolved as CNAME record and the 2nd one to resolve the hostname in CNAME record. If CNAME record was cached, only the 2nd query would be needed most of the time. 1st query would be needed less frequently. Modifications: Add a new CnameCache that will be used to cache CNAMEs and so may reduce queries. Result: Less queries needed when CNAME is used.
This commit is contained in:
parent
70efd25801
commit
5650db5826
@ -0,0 +1,99 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 The Netty Project
|
||||||
|
*
|
||||||
|
* The Netty Project licenses this file to you under the Apache License,
|
||||||
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at:
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package io.netty.resolver.dns;
|
||||||
|
|
||||||
|
import io.netty.channel.EventLoop;
|
||||||
|
import io.netty.util.AsciiString;
|
||||||
|
import io.netty.util.internal.UnstableApi;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static io.netty.util.internal.ObjectUtil.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default implementation of a {@link DnsCnameCache}.
|
||||||
|
*/
|
||||||
|
@UnstableApi
|
||||||
|
public final class DefaultDnsCnameCache implements DnsCnameCache {
|
||||||
|
private final int minTtl;
|
||||||
|
private final int maxTtl;
|
||||||
|
|
||||||
|
private final Cache<String> cache = new Cache<String>() {
|
||||||
|
@Override
|
||||||
|
protected boolean shouldReplaceAll(String entry) {
|
||||||
|
// Only one 1:1 mapping is supported as specified in the RFC.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean equals(String entry, String otherEntry) {
|
||||||
|
return AsciiString.contentEqualsIgnoreCase(entry, otherEntry);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a cache that respects the TTL returned by the DNS server.
|
||||||
|
*/
|
||||||
|
public DefaultDnsCnameCache() {
|
||||||
|
this(0, Cache.MAX_SUPPORTED_TTL_SECS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a cache.
|
||||||
|
*
|
||||||
|
* @param minTtl the minimum TTL
|
||||||
|
* @param maxTtl the maximum TTL
|
||||||
|
*/
|
||||||
|
public DefaultDnsCnameCache(int minTtl, int maxTtl) {
|
||||||
|
this.minTtl = Math.min(Cache.MAX_SUPPORTED_TTL_SECS, checkPositiveOrZero(minTtl, "minTtl"));
|
||||||
|
this.maxTtl = Math.min(Cache.MAX_SUPPORTED_TTL_SECS, checkPositive(maxTtl, "maxTtl"));
|
||||||
|
if (minTtl > maxTtl) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"minTtl: " + minTtl + ", maxTtl: " + maxTtl + " (expected: 0 <= minTtl <= maxTtl)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public String get(String hostname) {
|
||||||
|
checkNotNull(hostname, "hostname");
|
||||||
|
List<? extends String> cached = cache.get(hostname);
|
||||||
|
if (cached == null || cached.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// We can never have more then one record.
|
||||||
|
return cached.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cache(String hostname, String cname, long originalTtl, EventLoop loop) {
|
||||||
|
checkNotNull(hostname, "hostname");
|
||||||
|
checkNotNull(cname, "cname");
|
||||||
|
checkNotNull(loop, "loop");
|
||||||
|
cache.cache(hostname, cname, Math.max(minTtl, (int) Math.min(maxTtl, originalTtl)), loop);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
cache.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean clear(String hostname) {
|
||||||
|
checkNotNull(hostname, "hostname");
|
||||||
|
return cache.clear(hostname);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 The Netty Project
|
||||||
|
*
|
||||||
|
* The Netty Project licenses this file to you under the Apache License,
|
||||||
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at:
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package io.netty.resolver.dns;
|
||||||
|
|
||||||
|
import io.netty.channel.EventLoop;
|
||||||
|
import io.netty.util.internal.UnstableApi;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A cache for {@code CNAME}s.
|
||||||
|
*/
|
||||||
|
@UnstableApi
|
||||||
|
public interface DnsCnameCache {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the cached cname for the given hostname.
|
||||||
|
*
|
||||||
|
* @param hostname the hostname
|
||||||
|
* @return the cached entries or an {@code null} if none.
|
||||||
|
*/
|
||||||
|
String get(String hostname);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Caches a cname entry that should be used for the given hostname.
|
||||||
|
*
|
||||||
|
* @param hostname the hostname
|
||||||
|
* @param cname the cname mapping.
|
||||||
|
* @param originalTtl the TTL as returned by the DNS server
|
||||||
|
* @param loop the {@link EventLoop} used to register the TTL timeout
|
||||||
|
*/
|
||||||
|
void cache(String hostname, String cname, long originalTtl, EventLoop loop);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears all cached nameservers.
|
||||||
|
*
|
||||||
|
* @see #clear(String)
|
||||||
|
*/
|
||||||
|
void clear();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears the cached nameservers for the specified hostname.
|
||||||
|
*
|
||||||
|
* @return {@code true} if and only if there was an entry for the specified host name in the cache and
|
||||||
|
* it has been removed by this method
|
||||||
|
*/
|
||||||
|
boolean clear(String hostname);
|
||||||
|
}
|
@ -174,6 +174,7 @@ public class DnsNameResolver extends InetNameResolver {
|
|||||||
*/
|
*/
|
||||||
private final DnsCache resolveCache;
|
private final DnsCache resolveCache;
|
||||||
private final AuthoritativeDnsServerCache authoritativeDnsServerCache;
|
private final AuthoritativeDnsServerCache authoritativeDnsServerCache;
|
||||||
|
private final DnsCnameCache cnameCache;
|
||||||
|
|
||||||
private final FastThreadLocal<DnsServerAddressStream> nameServerAddrStream =
|
private final FastThreadLocal<DnsServerAddressStream> nameServerAddrStream =
|
||||||
new FastThreadLocal<DnsServerAddressStream>() {
|
new FastThreadLocal<DnsServerAddressStream>() {
|
||||||
@ -225,9 +226,7 @@ public class DnsNameResolver extends InetNameResolver {
|
|||||||
* @param ndots the ndots value
|
* @param ndots the ndots value
|
||||||
* @param decodeIdn {@code true} if domain / host names should be decoded to unicode when received.
|
* @param decodeIdn {@code true} if domain / host names should be decoded to unicode when received.
|
||||||
* See <a href="https://tools.ietf.org/html/rfc3492">rfc3492</a>.
|
* See <a href="https://tools.ietf.org/html/rfc3492">rfc3492</a>.
|
||||||
* @deprecated Use {@link DnsNameResolver(EventLoop, ChannelFactory, DnsCache, AuthoritativeDnsServerCache,
|
* @deprecated Use {@link DnsNameResolverBuilder}.
|
||||||
* DnsQueryLifecycleObserverFactory, long, ResolvedAddressTypes, boolean, int, boolean, int, boolean,
|
|
||||||
* HostsFileEntriesResolver, DnsServerAddressStreamProvider, String[], int, boolean)}
|
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public DnsNameResolver(
|
public DnsNameResolver(
|
||||||
@ -279,7 +278,9 @@ public class DnsNameResolver extends InetNameResolver {
|
|||||||
* @param ndots the ndots value
|
* @param ndots the ndots value
|
||||||
* @param decodeIdn {@code true} if domain / host names should be decoded to unicode when received.
|
* @param decodeIdn {@code true} if domain / host names should be decoded to unicode when received.
|
||||||
* See <a href="https://tools.ietf.org/html/rfc3492">rfc3492</a>.
|
* See <a href="https://tools.ietf.org/html/rfc3492">rfc3492</a>.
|
||||||
|
* @deprecated Use {@link DnsNameResolverBuilder}.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public DnsNameResolver(
|
public DnsNameResolver(
|
||||||
EventLoop eventLoop,
|
EventLoop eventLoop,
|
||||||
ChannelFactory<? extends DatagramChannel> channelFactory,
|
ChannelFactory<? extends DatagramChannel> channelFactory,
|
||||||
@ -298,6 +299,31 @@ public class DnsNameResolver extends InetNameResolver {
|
|||||||
String[] searchDomains,
|
String[] searchDomains,
|
||||||
int ndots,
|
int ndots,
|
||||||
boolean decodeIdn) {
|
boolean decodeIdn) {
|
||||||
|
this(eventLoop, channelFactory, resolveCache, NoopDnsCnameCache.INSTANCE, authoritativeDnsServerCache,
|
||||||
|
dnsQueryLifecycleObserverFactory, queryTimeoutMillis, resolvedAddressTypes, recursionDesired,
|
||||||
|
maxQueriesPerResolve, traceEnabled, maxPayloadSize, optResourceEnabled, hostsFileEntriesResolver,
|
||||||
|
dnsServerAddressStreamProvider, searchDomains, ndots, decodeIdn);
|
||||||
|
}
|
||||||
|
|
||||||
|
DnsNameResolver(
|
||||||
|
EventLoop eventLoop,
|
||||||
|
ChannelFactory<? extends DatagramChannel> channelFactory,
|
||||||
|
final DnsCache resolveCache,
|
||||||
|
final DnsCnameCache cnameCache,
|
||||||
|
final AuthoritativeDnsServerCache authoritativeDnsServerCache,
|
||||||
|
DnsQueryLifecycleObserverFactory dnsQueryLifecycleObserverFactory,
|
||||||
|
long queryTimeoutMillis,
|
||||||
|
ResolvedAddressTypes resolvedAddressTypes,
|
||||||
|
boolean recursionDesired,
|
||||||
|
int maxQueriesPerResolve,
|
||||||
|
boolean traceEnabled,
|
||||||
|
int maxPayloadSize,
|
||||||
|
boolean optResourceEnabled,
|
||||||
|
HostsFileEntriesResolver hostsFileEntriesResolver,
|
||||||
|
DnsServerAddressStreamProvider dnsServerAddressStreamProvider,
|
||||||
|
String[] searchDomains,
|
||||||
|
int ndots,
|
||||||
|
boolean decodeIdn) {
|
||||||
super(eventLoop);
|
super(eventLoop);
|
||||||
this.queryTimeoutMillis = checkPositive(queryTimeoutMillis, "queryTimeoutMillis");
|
this.queryTimeoutMillis = checkPositive(queryTimeoutMillis, "queryTimeoutMillis");
|
||||||
this.resolvedAddressTypes = resolvedAddressTypes != null ? resolvedAddressTypes : DEFAULT_RESOLVE_ADDRESS_TYPES;
|
this.resolvedAddressTypes = resolvedAddressTypes != null ? resolvedAddressTypes : DEFAULT_RESOLVE_ADDRESS_TYPES;
|
||||||
@ -309,6 +335,7 @@ public class DnsNameResolver extends InetNameResolver {
|
|||||||
this.dnsServerAddressStreamProvider =
|
this.dnsServerAddressStreamProvider =
|
||||||
checkNotNull(dnsServerAddressStreamProvider, "dnsServerAddressStreamProvider");
|
checkNotNull(dnsServerAddressStreamProvider, "dnsServerAddressStreamProvider");
|
||||||
this.resolveCache = checkNotNull(resolveCache, "resolveCache");
|
this.resolveCache = checkNotNull(resolveCache, "resolveCache");
|
||||||
|
this.cnameCache = checkNotNull(cnameCache, "cnameCache");
|
||||||
this.dnsQueryLifecycleObserverFactory = traceEnabled ?
|
this.dnsQueryLifecycleObserverFactory = traceEnabled ?
|
||||||
dnsQueryLifecycleObserverFactory instanceof NoopDnsQueryLifecycleObserverFactory ?
|
dnsQueryLifecycleObserverFactory instanceof NoopDnsQueryLifecycleObserverFactory ?
|
||||||
new TraceDnsQueryLifeCycleObserverFactory() :
|
new TraceDnsQueryLifeCycleObserverFactory() :
|
||||||
@ -382,6 +409,7 @@ public class DnsNameResolver extends InetNameResolver {
|
|||||||
@Override
|
@Override
|
||||||
public void operationComplete(ChannelFuture future) {
|
public void operationComplete(ChannelFuture future) {
|
||||||
resolveCache.clear();
|
resolveCache.clear();
|
||||||
|
cnameCache.clear();
|
||||||
authoritativeDnsServerCache.clear();
|
authoritativeDnsServerCache.clear();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -433,6 +461,13 @@ public class DnsNameResolver extends InetNameResolver {
|
|||||||
return resolveCache;
|
return resolveCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the {@link DnsCnameCache}.
|
||||||
|
*/
|
||||||
|
DnsCnameCache cnameCache() {
|
||||||
|
return cnameCache;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the cache used for authoritative DNS servers for a domain.
|
* Returns the cache used for authoritative DNS servers for a domain.
|
||||||
*/
|
*/
|
||||||
|
@ -40,6 +40,7 @@ public final class DnsNameResolverBuilder {
|
|||||||
private EventLoop eventLoop;
|
private EventLoop eventLoop;
|
||||||
private ChannelFactory<? extends DatagramChannel> channelFactory;
|
private ChannelFactory<? extends DatagramChannel> channelFactory;
|
||||||
private DnsCache resolveCache;
|
private DnsCache resolveCache;
|
||||||
|
private DnsCnameCache cnameCache;
|
||||||
private AuthoritativeDnsServerCache authoritativeDnsServerCache;
|
private AuthoritativeDnsServerCache authoritativeDnsServerCache;
|
||||||
private Integer minTtl;
|
private Integer minTtl;
|
||||||
private Integer maxTtl;
|
private Integer maxTtl;
|
||||||
@ -123,6 +124,17 @@ public final class DnsNameResolverBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the cache for {@code CNAME} mappings.
|
||||||
|
*
|
||||||
|
* @param cnameCache the cache used to cache {@code CNAME} mappings for a domain.
|
||||||
|
* @return {@code this}
|
||||||
|
*/
|
||||||
|
public DnsNameResolverBuilder cnameCache(DnsCnameCache cnameCache) {
|
||||||
|
this.cnameCache = cnameCache;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the factory used to generate objects which can observe individual DNS queries.
|
* Set the factory used to generate objects which can observe individual DNS queries.
|
||||||
* @param lifecycleObserverFactory the factory used to generate objects which can observe individual DNS queries.
|
* @param lifecycleObserverFactory the factory used to generate objects which can observe individual DNS queries.
|
||||||
@ -376,6 +388,11 @@ public final class DnsNameResolverBuilder {
|
|||||||
new NameServerComparator(DnsNameResolver.preferredAddressType(resolvedAddressTypes).addressType()));
|
new NameServerComparator(DnsNameResolver.preferredAddressType(resolvedAddressTypes).addressType()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private DnsCnameCache newCnameCache() {
|
||||||
|
return new DefaultDnsCnameCache(
|
||||||
|
intValue(minTtl, 0), intValue(maxTtl, Integer.MAX_VALUE));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set if domain / host names should be decoded to unicode when received.
|
* Set if domain / host names should be decoded to unicode when received.
|
||||||
* See <a href="https://tools.ietf.org/html/rfc3492">rfc3492</a>.
|
* See <a href="https://tools.ietf.org/html/rfc3492">rfc3492</a>.
|
||||||
@ -407,12 +424,14 @@ public final class DnsNameResolverBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DnsCache resolveCache = this.resolveCache != null ? this.resolveCache : newCache();
|
DnsCache resolveCache = this.resolveCache != null ? this.resolveCache : newCache();
|
||||||
|
DnsCnameCache cnameCache = this.cnameCache != null ? this.cnameCache : newCnameCache();
|
||||||
AuthoritativeDnsServerCache authoritativeDnsServerCache = this.authoritativeDnsServerCache != null ?
|
AuthoritativeDnsServerCache authoritativeDnsServerCache = this.authoritativeDnsServerCache != null ?
|
||||||
this.authoritativeDnsServerCache : newAuthoritativeDnsServerCache();
|
this.authoritativeDnsServerCache : newAuthoritativeDnsServerCache();
|
||||||
return new DnsNameResolver(
|
return new DnsNameResolver(
|
||||||
eventLoop,
|
eventLoop,
|
||||||
channelFactory,
|
channelFactory,
|
||||||
resolveCache,
|
resolveCache,
|
||||||
|
cnameCache,
|
||||||
authoritativeDnsServerCache,
|
authoritativeDnsServerCache,
|
||||||
dnsQueryLifecycleObserverFactory,
|
dnsQueryLifecycleObserverFactory,
|
||||||
queryTimeoutMillis,
|
queryTimeoutMillis,
|
||||||
@ -449,6 +468,9 @@ public final class DnsNameResolverBuilder {
|
|||||||
copiedBuilder.resolveCache(resolveCache);
|
copiedBuilder.resolveCache(resolveCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (cnameCache != null) {
|
||||||
|
copiedBuilder.cnameCache(cnameCache);
|
||||||
|
}
|
||||||
if (maxTtl != null && minTtl != null) {
|
if (maxTtl != null && minTtl != null) {
|
||||||
copiedBuilder.ttl(minTtl, maxTtl);
|
copiedBuilder.ttl(minTtl, maxTtl);
|
||||||
}
|
}
|
||||||
|
@ -131,6 +131,13 @@ abstract class DnsResolveContext<T> {
|
|||||||
return parent.resolveCache();
|
return parent.resolveCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link DnsCnameCache} that is used for resolving.
|
||||||
|
*/
|
||||||
|
DnsCnameCache cnameCache() {
|
||||||
|
return parent.cnameCache();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link AuthoritativeDnsServerCache} to use while resolving.
|
* The {@link AuthoritativeDnsServerCache} to use while resolving.
|
||||||
*/
|
*/
|
||||||
@ -237,7 +244,23 @@ abstract class DnsResolveContext<T> {
|
|||||||
nextContext.internalResolve(hostname, nextPromise);
|
nextContext.internalResolve(hostname, nextPromise);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String hostnameWithDot(String name) {
|
||||||
|
if (StringUtil.endsWith(name, '.')) {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
return name + '.';
|
||||||
|
}
|
||||||
|
|
||||||
private void internalResolve(String name, Promise<List<T>> promise) {
|
private void internalResolve(String name, Promise<List<T>> promise) {
|
||||||
|
for (;;) {
|
||||||
|
// Resolve from cnameCache() until there is no more cname entry cached.
|
||||||
|
String mapping = cnameCache().get(hostnameWithDot(name));
|
||||||
|
if (mapping == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
name = mapping;
|
||||||
|
}
|
||||||
|
|
||||||
DnsServerAddressStream nameServerAddressStream = getNameServers(name);
|
DnsServerAddressStream nameServerAddressStream = getNameServers(name);
|
||||||
|
|
||||||
final int end = expectedTypes.length - 1;
|
final int end = expectedTypes.length - 1;
|
||||||
@ -448,7 +471,8 @@ abstract class DnsResolveContext<T> {
|
|||||||
final DnsRecordType type = question.type();
|
final DnsRecordType type = question.type();
|
||||||
|
|
||||||
if (type == DnsRecordType.CNAME) {
|
if (type == DnsRecordType.CNAME) {
|
||||||
onResponseCNAME(question, buildAliasMap(envelope.content()), queryLifecycleObserver, promise);
|
onResponseCNAME(question, buildAliasMap(envelope.content(), cnameCache(), parent.executor()),
|
||||||
|
queryLifecycleObserver, promise);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -600,7 +624,7 @@ abstract class DnsResolveContext<T> {
|
|||||||
|
|
||||||
// We often get a bunch of CNAMES as well when we asked for A/AAAA.
|
// We often get a bunch of CNAMES as well when we asked for A/AAAA.
|
||||||
final DnsResponse response = envelope.content();
|
final DnsResponse response = envelope.content();
|
||||||
final Map<String, String> cnames = buildAliasMap(response);
|
final Map<String, String> cnames = buildAliasMap(response, cnameCache(), parent.executor());
|
||||||
final int answerCount = response.count(DnsSection.ANSWER);
|
final int answerCount = response.count(DnsSection.ANSWER);
|
||||||
|
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
@ -695,7 +719,7 @@ abstract class DnsResolveContext<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<String, String> buildAliasMap(DnsResponse response) {
|
private static Map<String, String> buildAliasMap(DnsResponse response, DnsCnameCache cache, EventLoop loop) {
|
||||||
final int answerCount = response.count(DnsSection.ANSWER);
|
final int answerCount = response.count(DnsSection.ANSWER);
|
||||||
Map<String, String> cnames = null;
|
Map<String, String> cnames = null;
|
||||||
for (int i = 0; i < answerCount; i ++) {
|
for (int i = 0; i < answerCount; i ++) {
|
||||||
@ -719,7 +743,12 @@ abstract class DnsResolveContext<T> {
|
|||||||
cnames = new HashMap<String, String>(min(8, answerCount));
|
cnames = new HashMap<String, String>(min(8, answerCount));
|
||||||
}
|
}
|
||||||
|
|
||||||
cnames.put(r.name().toLowerCase(Locale.US), domainName.toLowerCase(Locale.US));
|
String name = r.name().toLowerCase(Locale.US);
|
||||||
|
String mapping = domainName.toLowerCase(Locale.US);
|
||||||
|
|
||||||
|
// Cache the CNAME as well.
|
||||||
|
cache.cache(hostnameWithDot(name), hostnameWithDot(mapping), r.timeToLive(), loop);
|
||||||
|
cnames.put(name, mapping);
|
||||||
}
|
}
|
||||||
|
|
||||||
return cnames != null? cnames : Collections.<String, String>emptyMap();
|
return cnames != null? cnames : Collections.<String, String>emptyMap();
|
||||||
@ -843,6 +872,15 @@ abstract class DnsResolveContext<T> {
|
|||||||
|
|
||||||
private void followCname(DnsQuestion question, String cname, DnsQueryLifecycleObserver queryLifecycleObserver,
|
private void followCname(DnsQuestion question, String cname, DnsQueryLifecycleObserver queryLifecycleObserver,
|
||||||
Promise<List<T>> promise) {
|
Promise<List<T>> promise) {
|
||||||
|
for (;;) {
|
||||||
|
// Resolve from cnameCache() until there is no more cname entry cached.
|
||||||
|
String mapping = cnameCache().get(hostnameWithDot(cname));
|
||||||
|
if (mapping == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
cname = mapping;
|
||||||
|
}
|
||||||
|
|
||||||
DnsServerAddressStream stream = getNameServers(cname);
|
DnsServerAddressStream stream = getNameServers(cname);
|
||||||
|
|
||||||
final DnsQuestion cnameQuestion;
|
final DnsQuestion cnameQuestion;
|
||||||
@ -865,7 +903,7 @@ abstract class DnsResolveContext<T> {
|
|||||||
// Assume a single failure means that queries will succeed. If the hostname is invalid for one type
|
// Assume a single failure means that queries will succeed. If the hostname is invalid for one type
|
||||||
// there is no case where it is known to be valid for another type.
|
// there is no case where it is known to be valid for another type.
|
||||||
promise.tryFailure(new IllegalArgumentException("Unable to create DNS Question for: [" + hostname + ", " +
|
promise.tryFailure(new IllegalArgumentException("Unable to create DNS Question for: [" + hostname + ", " +
|
||||||
type + "]", cause));
|
type + ']', cause));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
query(dnsServerAddressStream, 0, question, promise, null);
|
query(dnsServerAddressStream, 0, question, promise, null);
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 The Netty Project
|
||||||
|
*
|
||||||
|
* The Netty Project licenses this file to you under the Apache License,
|
||||||
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at:
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package io.netty.resolver.dns;
|
||||||
|
|
||||||
|
import io.netty.channel.EventLoop;
|
||||||
|
import io.netty.util.internal.UnstableApi;
|
||||||
|
|
||||||
|
@UnstableApi
|
||||||
|
public final class NoopDnsCnameCache implements DnsCnameCache {
|
||||||
|
|
||||||
|
public static final NoopDnsCnameCache INSTANCE = new NoopDnsCnameCache();
|
||||||
|
|
||||||
|
private NoopDnsCnameCache() { }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String get(String hostname) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cache(String hostname, String cname, long originalTtl, EventLoop loop) {
|
||||||
|
// NOOP
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
// NOOP
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean clear(String hostname) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,132 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018 The Netty Project
|
||||||
|
*
|
||||||
|
* The Netty Project licenses this file to you under the Apache License,
|
||||||
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at:
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
package io.netty.resolver.dns;
|
||||||
|
|
||||||
|
import io.netty.channel.DefaultEventLoopGroup;
|
||||||
|
import io.netty.channel.EventLoop;
|
||||||
|
import io.netty.channel.EventLoopGroup;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class DefaultDnsCnameCacheTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExpire() throws Throwable {
|
||||||
|
EventLoopGroup group = new DefaultEventLoopGroup(1);
|
||||||
|
|
||||||
|
try {
|
||||||
|
EventLoop loop = group.next();
|
||||||
|
final DefaultDnsCnameCache cache = new DefaultDnsCnameCache();
|
||||||
|
cache.cache("netty.io", "mapping.netty.io", 1, loop);
|
||||||
|
|
||||||
|
Throwable error = loop.schedule(new Callable<Throwable>() {
|
||||||
|
@Override
|
||||||
|
public Throwable call() {
|
||||||
|
try {
|
||||||
|
assertNull(cache.get("netty.io"));
|
||||||
|
return null;
|
||||||
|
} catch (Throwable cause) {
|
||||||
|
return cause;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 1, TimeUnit.SECONDS).get();
|
||||||
|
if (error != null) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
group.shutdownGracefully();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExpireWithDifferentTTLs() {
|
||||||
|
testExpireWithTTL0(1);
|
||||||
|
testExpireWithTTL0(1000);
|
||||||
|
testExpireWithTTL0(1000000);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void testExpireWithTTL0(int days) {
|
||||||
|
EventLoopGroup group = new DefaultEventLoopGroup(1);
|
||||||
|
|
||||||
|
try {
|
||||||
|
EventLoop loop = group.next();
|
||||||
|
final DefaultDnsCnameCache cache = new DefaultDnsCnameCache();
|
||||||
|
cache.cache("netty.io", "mapping.netty.io", TimeUnit.DAYS.toSeconds(days), loop);
|
||||||
|
assertEquals("mapping.netty.io", cache.get("netty.io"));
|
||||||
|
} finally {
|
||||||
|
group.shutdownGracefully();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMultipleCnamesForSameHostname() throws Exception {
|
||||||
|
EventLoopGroup group = new DefaultEventLoopGroup(1);
|
||||||
|
|
||||||
|
try {
|
||||||
|
EventLoop loop = group.next();
|
||||||
|
final DefaultDnsCnameCache cache = new DefaultDnsCnameCache();
|
||||||
|
cache.cache("netty.io", "mapping1.netty.io", 10, loop);
|
||||||
|
cache.cache("netty.io", "mapping2.netty.io", 10000, loop);
|
||||||
|
|
||||||
|
assertEquals("mapping2.netty.io", cache.get("netty.io"));
|
||||||
|
} finally {
|
||||||
|
group.shutdownGracefully();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddSameCnameForSameHostname() throws Exception {
|
||||||
|
EventLoopGroup group = new DefaultEventLoopGroup(1);
|
||||||
|
|
||||||
|
try {
|
||||||
|
EventLoop loop = group.next();
|
||||||
|
final DefaultDnsCnameCache cache = new DefaultDnsCnameCache();
|
||||||
|
cache.cache("netty.io", "mapping.netty.io", 10, loop);
|
||||||
|
cache.cache("netty.io", "mapping.netty.io", 10000, loop);
|
||||||
|
|
||||||
|
assertEquals("mapping.netty.io", cache.get("netty.io"));
|
||||||
|
} finally {
|
||||||
|
group.shutdownGracefully();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testClear() throws Exception {
|
||||||
|
EventLoopGroup group = new DefaultEventLoopGroup(1);
|
||||||
|
|
||||||
|
try {
|
||||||
|
EventLoop loop = group.next();
|
||||||
|
final DefaultDnsCnameCache cache = new DefaultDnsCnameCache();
|
||||||
|
cache.cache("x.netty.io", "mapping.netty.io", 100000, loop);
|
||||||
|
cache.cache("y.netty.io", "mapping.netty.io", 100000, loop);
|
||||||
|
|
||||||
|
assertEquals("mapping.netty.io", cache.get("x.netty.io"));
|
||||||
|
assertEquals("mapping.netty.io", cache.get("y.netty.io"));
|
||||||
|
|
||||||
|
assertTrue(cache.clear("x.netty.io"));
|
||||||
|
assertNull(cache.get("x.netty.io"));
|
||||||
|
assertEquals("mapping.netty.io", cache.get("y.netty.io"));
|
||||||
|
cache.clear();
|
||||||
|
assertNull(cache.get("y.netty.io"));
|
||||||
|
} finally {
|
||||||
|
group.shutdownGracefully();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -82,11 +82,13 @@ import java.util.Map;
|
|||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
import static io.netty.handler.codec.dns.DnsRecordType.A;
|
import static io.netty.handler.codec.dns.DnsRecordType.A;
|
||||||
@ -2286,4 +2288,119 @@ public class DnsNameResolverTest {
|
|||||||
assertSame(exception, e);
|
assertSame(exception, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCNameCached() throws Exception {
|
||||||
|
final Map<String, String> cache = new ConcurrentHashMap<String, String>();
|
||||||
|
final AtomicInteger cnameQueries = new AtomicInteger();
|
||||||
|
final AtomicInteger aQueries = new AtomicInteger();
|
||||||
|
|
||||||
|
TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<ResourceRecord> getRecords(QuestionRecord question) {
|
||||||
|
if ("cname.netty.io".equals(question.getDomainName())) {
|
||||||
|
aQueries.incrementAndGet();
|
||||||
|
|
||||||
|
return Collections.<ResourceRecord>singleton(new TestDnsServer.TestResourceRecord(
|
||||||
|
question.getDomainName(), RecordType.A,
|
||||||
|
Collections.<String, Object>singletonMap(
|
||||||
|
DnsAttribute.IP_ADDRESS.toLowerCase(), "10.0.0.99")));
|
||||||
|
}
|
||||||
|
if ("x.netty.io".equals(question.getDomainName())) {
|
||||||
|
cnameQueries.incrementAndGet();
|
||||||
|
|
||||||
|
return Collections.<ResourceRecord>singleton(new TestDnsServer.TestResourceRecord(
|
||||||
|
question.getDomainName(), RecordType.CNAME,
|
||||||
|
Collections.<String, Object>singletonMap(
|
||||||
|
DnsAttribute.DOMAIN_NAME.toLowerCase(), "cname.netty.io")));
|
||||||
|
}
|
||||||
|
if ("y.netty.io".equals(question.getDomainName())) {
|
||||||
|
cnameQueries.incrementAndGet();
|
||||||
|
|
||||||
|
return Collections.<ResourceRecord>singleton(new TestDnsServer.TestResourceRecord(
|
||||||
|
question.getDomainName(), RecordType.CNAME,
|
||||||
|
Collections.<String, Object>singletonMap(
|
||||||
|
DnsAttribute.DOMAIN_NAME.toLowerCase(), "x.netty.io")));
|
||||||
|
}
|
||||||
|
return Collections.emptySet();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
dnsServer2.start();
|
||||||
|
DnsNameResolver resolver = null;
|
||||||
|
try {
|
||||||
|
DnsNameResolverBuilder builder = newResolver()
|
||||||
|
.recursionDesired(true)
|
||||||
|
.resolvedAddressTypes(ResolvedAddressTypes.IPV4_ONLY)
|
||||||
|
.maxQueriesPerResolve(16)
|
||||||
|
.nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress()))
|
||||||
|
.resolveCache(NoopDnsCache.INSTANCE)
|
||||||
|
.cnameCache(new DnsCnameCache() {
|
||||||
|
@Override
|
||||||
|
public String get(String hostname) {
|
||||||
|
assertTrue(hostname, hostname.endsWith("."));
|
||||||
|
return cache.get(hostname);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cache(String hostname, String cname, long originalTtl, EventLoop loop) {
|
||||||
|
assertTrue(hostname, hostname.endsWith("."));
|
||||||
|
cache.put(hostname, cname);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
// NOOP
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean clear(String hostname) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
resolver = builder.build();
|
||||||
|
List<InetAddress> resolvedAddresses =
|
||||||
|
resolver.resolveAll("x.netty.io").syncUninterruptibly().getNow();
|
||||||
|
assertEquals(1, resolvedAddresses.size());
|
||||||
|
assertTrue(resolvedAddresses.contains(InetAddress.getByAddress(new byte[] { 10, 0, 0, 99 })));
|
||||||
|
|
||||||
|
assertEquals("cname.netty.io.", cache.get("x.netty.io."));
|
||||||
|
assertEquals(1, cnameQueries.get());
|
||||||
|
assertEquals(1, aQueries.get());
|
||||||
|
|
||||||
|
resolvedAddresses =
|
||||||
|
resolver.resolveAll("x.netty.io").syncUninterruptibly().getNow();
|
||||||
|
assertEquals(1, resolvedAddresses.size());
|
||||||
|
assertTrue(resolvedAddresses.contains(InetAddress.getByAddress(new byte[] { 10, 0, 0, 99 })));
|
||||||
|
|
||||||
|
// Should not have queried for the CNAME again.
|
||||||
|
assertEquals(1, cnameQueries.get());
|
||||||
|
assertEquals(2, aQueries.get());
|
||||||
|
|
||||||
|
resolvedAddresses =
|
||||||
|
resolver.resolveAll("y.netty.io").syncUninterruptibly().getNow();
|
||||||
|
assertEquals(1, resolvedAddresses.size());
|
||||||
|
assertTrue(resolvedAddresses.contains(InetAddress.getByAddress(new byte[] { 10, 0, 0, 99 })));
|
||||||
|
|
||||||
|
assertEquals("x.netty.io.", cache.get("y.netty.io."));
|
||||||
|
|
||||||
|
// Will only query for one CNAME
|
||||||
|
assertEquals(2, cnameQueries.get());
|
||||||
|
assertEquals(3, aQueries.get());
|
||||||
|
|
||||||
|
resolvedAddresses =
|
||||||
|
resolver.resolveAll("y.netty.io").syncUninterruptibly().getNow();
|
||||||
|
assertEquals(1, resolvedAddresses.size());
|
||||||
|
assertTrue(resolvedAddresses.contains(InetAddress.getByAddress(new byte[] { 10, 0, 0, 99 })));
|
||||||
|
|
||||||
|
// Should not have queried for the CNAME again.
|
||||||
|
assertEquals(2, cnameQueries.get());
|
||||||
|
assertEquals(4, aQueries.get());
|
||||||
|
} finally {
|
||||||
|
dnsServer2.stop();
|
||||||
|
if (resolver != null) {
|
||||||
|
resolver.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user