Skip to content

Commit

Permalink
Add cache for CNAME mappings resolved during lookup of DNS entries. (n…
Browse files Browse the repository at this point in the history
…etty#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.
  • Loading branch information
normanmaurer authored Sep 27, 2018
1 parent 70efd25 commit 5650db5
Show file tree
Hide file tree
Showing 8 changed files with 557 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ private static List<String> getSearchDomainsHack() throws Exception {
*/
private final DnsCache resolveCache;
private final AuthoritativeDnsServerCache authoritativeDnsServerCache;
private final DnsCnameCache cnameCache;

private final FastThreadLocal<DnsServerAddressStream> nameServerAddrStream =
new FastThreadLocal<DnsServerAddressStream>() {
Expand Down Expand Up @@ -225,9 +226,7 @@ protected DnsServerAddressStream initialValue() {
* @param ndots the ndots value
* @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>.
* @deprecated Use {@link DnsNameResolver(EventLoop, ChannelFactory, DnsCache, AuthoritativeDnsServerCache,
* DnsQueryLifecycleObserverFactory, long, ResolvedAddressTypes, boolean, int, boolean, int, boolean,
* HostsFileEntriesResolver, DnsServerAddressStreamProvider, String[], int, boolean)}
* @deprecated Use {@link DnsNameResolverBuilder}.
*/
@Deprecated
public DnsNameResolver(
Expand Down Expand Up @@ -279,7 +278,9 @@ public DnsNameResolver(
* @param ndots the ndots value
* @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>.
* @deprecated Use {@link DnsNameResolverBuilder}.
*/
@Deprecated
public DnsNameResolver(
EventLoop eventLoop,
ChannelFactory<? extends DatagramChannel> channelFactory,
Expand All @@ -298,6 +299,31 @@ public DnsNameResolver(
String[] searchDomains,
int ndots,
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);
this.queryTimeoutMillis = checkPositive(queryTimeoutMillis, "queryTimeoutMillis");
this.resolvedAddressTypes = resolvedAddressTypes != null ? resolvedAddressTypes : DEFAULT_RESOLVE_ADDRESS_TYPES;
Expand All @@ -309,6 +335,7 @@ public DnsNameResolver(
this.dnsServerAddressStreamProvider =
checkNotNull(dnsServerAddressStreamProvider, "dnsServerAddressStreamProvider");
this.resolveCache = checkNotNull(resolveCache, "resolveCache");
this.cnameCache = checkNotNull(cnameCache, "cnameCache");
this.dnsQueryLifecycleObserverFactory = traceEnabled ?
dnsQueryLifecycleObserverFactory instanceof NoopDnsQueryLifecycleObserverFactory ?
new TraceDnsQueryLifeCycleObserverFactory() :
Expand Down Expand Up @@ -382,6 +409,7 @@ protected void initChannel(DatagramChannel ch) throws Exception {
@Override
public void operationComplete(ChannelFuture future) {
resolveCache.clear();
cnameCache.clear();
authoritativeDnsServerCache.clear();
}
});
Expand Down Expand Up @@ -433,6 +461,13 @@ public DnsCache resolveCache() {
return resolveCache;
}

/**
* Returns the {@link DnsCnameCache}.
*/
DnsCnameCache cnameCache() {
return cnameCache;
}

/**
* Returns the cache used for authoritative DNS servers for a domain.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public final class DnsNameResolverBuilder {
private EventLoop eventLoop;
private ChannelFactory<? extends DatagramChannel> channelFactory;
private DnsCache resolveCache;
private DnsCnameCache cnameCache;
private AuthoritativeDnsServerCache authoritativeDnsServerCache;
private Integer minTtl;
private Integer maxTtl;
Expand Down Expand Up @@ -123,6 +124,17 @@ public DnsNameResolverBuilder resolveCache(DnsCache resolveCache) {
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.
* @param lifecycleObserverFactory the factory used to generate objects which can observe individual DNS queries.
Expand Down Expand Up @@ -376,6 +388,11 @@ private AuthoritativeDnsServerCache newAuthoritativeDnsServerCache() {
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.
* See <a href="https://tools.ietf.org/html/rfc3492">rfc3492</a>.
Expand Down Expand Up @@ -407,12 +424,14 @@ public DnsNameResolver build() {
}

DnsCache resolveCache = this.resolveCache != null ? this.resolveCache : newCache();
DnsCnameCache cnameCache = this.cnameCache != null ? this.cnameCache : newCnameCache();
AuthoritativeDnsServerCache authoritativeDnsServerCache = this.authoritativeDnsServerCache != null ?
this.authoritativeDnsServerCache : newAuthoritativeDnsServerCache();
return new DnsNameResolver(
eventLoop,
channelFactory,
resolveCache,
cnameCache,
authoritativeDnsServerCache,
dnsQueryLifecycleObserverFactory,
queryTimeoutMillis,
Expand Down Expand Up @@ -449,6 +468,9 @@ public DnsNameResolverBuilder copy() {
copiedBuilder.resolveCache(resolveCache);
}

if (cnameCache != null) {
copiedBuilder.cnameCache(cnameCache);
}
if (maxTtl != null && minTtl != null) {
copiedBuilder.ttl(minTtl, maxTtl);
}
Expand Down
Loading

0 comments on commit 5650db5

Please sign in to comment.