View Javadoc
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.resolver.dns;
17  
18  import io.netty.channel.EventLoop;
19  import io.netty.handler.codec.dns.DnsRecord;
20  import io.netty.util.concurrent.ScheduledFuture;
21  import io.netty.util.internal.PlatformDependent;
22  import io.netty.util.internal.UnstableApi;
23  
24  import java.net.InetAddress;
25  import java.util.ArrayList;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.concurrent.ConcurrentMap;
30  import java.util.concurrent.TimeUnit;
31  
32  import static io.netty.util.internal.ObjectUtil.checkNotNull;
33  import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
34  
35  /**
36   * Default implementation of {@link DnsCache}, backed by a {@link ConcurrentMap}.
37   * If any additional {@link DnsRecord} is used, no caching takes place.
38   */
39  @UnstableApi
40  public class DefaultDnsCache implements DnsCache {
41  
42      private final ConcurrentMap<String, List<DefaultDnsCacheEntry>> resolveCache =
43                                                              PlatformDependent.newConcurrentHashMap();
44      private final int minTtl;
45      private final int maxTtl;
46      private final int negativeTtl;
47  
48      /**
49       * Create a cache that respects the TTL returned by the DNS server
50       * and doesn't cache negative responses.
51       */
52      public DefaultDnsCache() {
53          this(0, Integer.MAX_VALUE, 0);
54      }
55  
56      /**
57       * Create a cache.
58       * @param minTtl the minimum TTL
59       * @param maxTtl the maximum TTL
60       * @param negativeTtl the TTL for failed queries
61       */
62      public DefaultDnsCache(int minTtl, int maxTtl, int negativeTtl) {
63          this.minTtl = checkPositiveOrZero(minTtl, "minTtl");
64          this.maxTtl = checkPositiveOrZero(maxTtl, "maxTtl");
65          if (minTtl > maxTtl) {
66              throw new IllegalArgumentException(
67                      "minTtl: " + minTtl + ", maxTtl: " + maxTtl + " (expected: 0 <= minTtl <= maxTtl)");
68          }
69          this.negativeTtl = checkPositiveOrZero(negativeTtl, "negativeTtl");
70      }
71  
72      /**
73       * Returns the minimum TTL of the cached DNS resource records (in seconds).
74       *
75       * @see #maxTtl()
76       */
77      public int minTtl() {
78          return minTtl;
79      }
80  
81      /**
82       * Returns the maximum TTL of the cached DNS resource records (in seconds).
83       *
84       * @see #minTtl()
85       */
86      public int maxTtl() {
87          return maxTtl;
88      }
89  
90      /**
91       * Returns the TTL of the cache for the failed DNS queries (in seconds). The default value is {@code 0}, which
92       * disables the cache for negative results.
93       */
94      public int negativeTtl() {
95          return negativeTtl;
96      }
97  
98      @Override
99      public void clear() {
100         for (Iterator<Map.Entry<String, List<DefaultDnsCacheEntry>>> i = resolveCache.entrySet().iterator();
101              i.hasNext();) {
102             final Map.Entry<String, List<DefaultDnsCacheEntry>> e = i.next();
103             i.remove();
104             cancelExpiration(e.getValue());
105         }
106     }
107 
108     @Override
109     public boolean clear(String hostname) {
110         checkNotNull(hostname, "hostname");
111         boolean removed = false;
112         for (Iterator<Map.Entry<String, List<DefaultDnsCacheEntry>>> i = resolveCache.entrySet().iterator();
113              i.hasNext();) {
114             final Map.Entry<String, List<DefaultDnsCacheEntry>> e = i.next();
115             if (e.getKey().equals(hostname)) {
116                 i.remove();
117                 cancelExpiration(e.getValue());
118                 removed = true;
119             }
120         }
121         return removed;
122     }
123 
124     private static boolean emptyAdditionals(DnsRecord[] additionals) {
125         return additionals == null || additionals.length == 0;
126     }
127 
128     @Override
129     public List<? extends DnsCacheEntry> get(String hostname, DnsRecord[] additionals) {
130         checkNotNull(hostname, "hostname");
131         if (!emptyAdditionals(additionals)) {
132             return null;
133         }
134         return resolveCache.get(hostname);
135     }
136 
137     private List<DefaultDnsCacheEntry> cachedEntries(String hostname) {
138         List<DefaultDnsCacheEntry> oldEntries = resolveCache.get(hostname);
139         final List<DefaultDnsCacheEntry> entries;
140         if (oldEntries == null) {
141             List<DefaultDnsCacheEntry> newEntries = new ArrayList<DefaultDnsCacheEntry>(8);
142             oldEntries = resolveCache.putIfAbsent(hostname, newEntries);
143             entries = oldEntries != null? oldEntries : newEntries;
144         } else {
145             entries = oldEntries;
146         }
147         return entries;
148     }
149 
150     @Override
151     public DnsCacheEntry cache(String hostname, DnsRecord[] additionals,
152                                InetAddress address, long originalTtl, EventLoop loop) {
153         checkNotNull(hostname, "hostname");
154         checkNotNull(address, "address");
155         checkNotNull(loop, "loop");
156         final DefaultDnsCacheEntry e = new DefaultDnsCacheEntry(hostname, address);
157         if (maxTtl == 0 || !emptyAdditionals(additionals)) {
158             return e;
159         }
160         final int ttl = Math.max(minTtl, (int) Math.min(maxTtl, originalTtl));
161         final List<DefaultDnsCacheEntry> entries = cachedEntries(hostname);
162 
163         synchronized (entries) {
164             if (!entries.isEmpty()) {
165                 final DefaultDnsCacheEntry firstEntry = entries.get(0);
166                 if (firstEntry.cause() != null) {
167                     assert entries.size() == 1;
168                     firstEntry.cancelExpiration();
169                     entries.clear();
170                 }
171             }
172             entries.add(e);
173         }
174 
175         scheduleCacheExpiration(entries, e, ttl, loop);
176         return e;
177     }
178 
179     @Override
180     public DnsCacheEntry cache(String hostname, DnsRecord[] additionals, Throwable cause, EventLoop loop) {
181         checkNotNull(hostname, "hostname");
182         checkNotNull(cause, "cause");
183         checkNotNull(loop, "loop");
184 
185         final DefaultDnsCacheEntry e = new DefaultDnsCacheEntry(hostname, cause);
186         if (negativeTtl == 0 || !emptyAdditionals(additionals)) {
187             return e;
188         }
189         final List<DefaultDnsCacheEntry> entries = cachedEntries(hostname);
190 
191         synchronized (entries) {
192             final int numEntries = entries.size();
193             for (int i = 0; i < numEntries; i ++) {
194                 entries.get(i).cancelExpiration();
195             }
196             entries.clear();
197             entries.add(e);
198         }
199 
200         scheduleCacheExpiration(entries, e, negativeTtl, loop);
201         return e;
202     }
203 
204     private static void cancelExpiration(List<DefaultDnsCacheEntry> entries) {
205         final int numEntries = entries.size();
206         for (int i = 0; i < numEntries; i++) {
207             entries.get(i).cancelExpiration();
208         }
209     }
210 
211     private void scheduleCacheExpiration(final List<DefaultDnsCacheEntry> entries,
212                                          final DefaultDnsCacheEntry e,
213                                          int ttl,
214                                          EventLoop loop) {
215         e.scheduleExpiration(loop, new Runnable() {
216                     @Override
217                     public void run() {
218                         synchronized (entries) {
219                             entries.remove(e);
220                             if (entries.isEmpty()) {
221                                 resolveCache.remove(e.hostname());
222                             }
223                         }
224                     }
225                 }, ttl, TimeUnit.SECONDS);
226     }
227 
228     @Override
229     public String toString() {
230         return new StringBuilder()
231                 .append("DefaultDnsCache(minTtl=")
232                 .append(minTtl).append(", maxTtl=")
233                 .append(maxTtl).append(", negativeTtl=")
234                 .append(negativeTtl).append(", cached resolved hostname=")
235                 .append(resolveCache.size()).append(")")
236                 .toString();
237     }
238 
239     private static final class DefaultDnsCacheEntry implements DnsCacheEntry {
240         private final String hostname;
241         private final InetAddress address;
242         private final Throwable cause;
243         private volatile ScheduledFuture<?> expirationFuture;
244 
245         DefaultDnsCacheEntry(String hostname, InetAddress address) {
246             this.hostname = checkNotNull(hostname, "hostname");
247             this.address = checkNotNull(address, "address");
248             cause = null;
249         }
250 
251         DefaultDnsCacheEntry(String hostname, Throwable cause) {
252             this.hostname = checkNotNull(hostname, "hostname");
253             this.cause = checkNotNull(cause, "cause");
254             address = null;
255         }
256 
257         @Override
258         public InetAddress address() {
259             return address;
260         }
261 
262         @Override
263         public Throwable cause() {
264             return cause;
265         }
266 
267         String hostname() {
268             return hostname;
269         }
270 
271         void scheduleExpiration(EventLoop loop, Runnable task, long delay, TimeUnit unit) {
272             assert expirationFuture == null : "expiration task scheduled already";
273             expirationFuture = loop.schedule(task, delay, unit);
274         }
275 
276         void cancelExpiration() {
277             ScheduledFuture<?> expirationFuture = this.expirationFuture;
278             if (expirationFuture != null) {
279                 expirationFuture.cancel(false);
280             }
281         }
282 
283         @Override
284         public String toString() {
285             if (cause != null) {
286                 return hostname + '/' + cause;
287             } else {
288                 return address.toString();
289             }
290         }
291     }
292 }