Use allocation free algorithm to detect CNAME cache loops (#10291)
Motivation: We did use a HashSet to detect CNAME cache loops which needs allocations. We can use an algorithm that doesnt need any allocations Modifications: Use algorithm that doesnt need allocations Result: Less allocations on the slow path
This commit is contained in:
parent
1fe92e3077
commit
bdb5e20d99
@ -47,7 +47,6 @@ import java.util.AbstractList;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@ -267,8 +266,9 @@ abstract class DnsResolveContext<T> {
|
||||
|
||||
// Resolve the final name from the CNAME cache until there is nothing to follow anymore. This also
|
||||
// guards against loops in the cache but early return once a loop is detected.
|
||||
private String cnameResolveFromCache(String name) {
|
||||
DnsCnameCache cnameCache = cnameCache();
|
||||
//
|
||||
// Visible for testing only
|
||||
static String cnameResolveFromCache(DnsCnameCache cnameCache, String name) {
|
||||
String first = cnameCache.get(hostnameWithDot(name));
|
||||
if (first == null) {
|
||||
// Nothing in the cache at all
|
||||
@ -289,28 +289,30 @@ abstract class DnsResolveContext<T> {
|
||||
return cnameResolveFromCacheLoop(cnameCache, first, second);
|
||||
}
|
||||
|
||||
private String cnameResolveFromCacheLoop(DnsCnameCache cnameCache, String first, String mapping) {
|
||||
// Detect loops using a HashSet. We use this as last resort implementation to reduce allocations in the most
|
||||
// common cases.
|
||||
Set<String> cnames = new HashSet<>(4);
|
||||
cnames.add(first);
|
||||
cnames.add(mapping);
|
||||
static String cnameResolveFromCacheLoop(DnsCnameCache cnameCache, String first, String mapping) {
|
||||
// Detect loops by advance only every other iteration.
|
||||
// See https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_Tortoise_and_Hare
|
||||
boolean advance = false;
|
||||
|
||||
String name = mapping;
|
||||
// Resolve from cnameCache() until there is no more cname entry cached.
|
||||
while ((mapping = cnameCache.get(hostnameWithDot(name))) != null) {
|
||||
if (!cnames.add(mapping)) {
|
||||
if (first.equals(mapping)) {
|
||||
// Follow CNAME from cache would loop. Lets break here.
|
||||
break;
|
||||
}
|
||||
name = mapping;
|
||||
if (advance) {
|
||||
first = cnameCache.get(first);
|
||||
}
|
||||
advance = !advance;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
private void internalResolve(String name, Promise<List<T>> promise) {
|
||||
// Resolve from cnameCache() until there is no more cname entry cached.
|
||||
name = cnameResolveFromCache(name);
|
||||
name = cnameResolveFromCache(cnameCache(), name);
|
||||
|
||||
try {
|
||||
DnsServerAddressStream nameServerAddressStream = getNameServers(name);
|
||||
@ -975,7 +977,7 @@ abstract class DnsResolveContext<T> {
|
||||
|
||||
private void followCname(DnsQuestion question, String cname, DnsQueryLifecycleObserver queryLifecycleObserver,
|
||||
Promise<List<T>> promise) {
|
||||
cname = cnameResolveFromCache(cname);
|
||||
cname = cnameResolveFromCache(cnameCache(), cname);
|
||||
DnsServerAddressStream stream = getNameServers(cname);
|
||||
|
||||
final DnsQuestion cnameQuestion;
|
||||
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2020 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.embedded.EmbeddedChannel;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DnsResolveContextTest {
|
||||
|
||||
private static final String HOSTNAME = "netty.io.";
|
||||
|
||||
@Test
|
||||
public void testCnameLoop() {
|
||||
for (int i = 1; i < 128; i++) {
|
||||
DnsResolveContext.cnameResolveFromCache(buildCache(i), HOSTNAME);
|
||||
}
|
||||
}
|
||||
|
||||
private static DnsCnameCache buildCache(int chainLength) {
|
||||
EmbeddedChannel channel = new EmbeddedChannel();
|
||||
DnsCnameCache cache = new DefaultDnsCnameCache();
|
||||
if (chainLength == 1) {
|
||||
cache.cache(HOSTNAME, HOSTNAME, Long.MAX_VALUE, channel.eventLoop());
|
||||
} else {
|
||||
String lastName = HOSTNAME;
|
||||
for (int i = 1; i < chainLength; i++) {
|
||||
String nextName = i + "." + lastName;
|
||||
cache.cache(lastName, nextName, Long.MAX_VALUE, channel.eventLoop());
|
||||
lastName = nextName;
|
||||
}
|
||||
cache.cache(lastName, HOSTNAME, Long.MAX_VALUE, channel.eventLoop());
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user