1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.resolver.dns;
17
18 import io.netty.channel.EventLoop;
19 import io.netty.util.internal.PlatformDependent;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.concurrent.ConcurrentMap;
28 import java.util.concurrent.Delayed;
29 import java.util.concurrent.ScheduledFuture;
30 import java.util.concurrent.TimeUnit;
31 import java.util.concurrent.atomic.AtomicReference;
32 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
33
34 import static java.util.Collections.singletonList;
35
36
37
38
39
40
41 abstract class Cache<E> {
42 private static final AtomicReferenceFieldUpdater<Cache.Entries, ScheduledFuture> FUTURE_UPDATER =
43 AtomicReferenceFieldUpdater.newUpdater(Cache.Entries.class, ScheduledFuture.class, "expirationFuture");
44
45 private static final ScheduledFuture<?> CANCELLED = new ScheduledFuture<Object>() {
46
47 @Override
48 public boolean cancel(boolean mayInterruptIfRunning) {
49 return false;
50 }
51
52 @Override
53 public long getDelay(TimeUnit unit) {
54
55
56 return Long.MIN_VALUE;
57 }
58
59 @Override
60 public int compareTo(Delayed o) {
61 throw new UnsupportedOperationException();
62 }
63
64 @Override
65 public boolean isCancelled() {
66 return true;
67 }
68
69 @Override
70 public boolean isDone() {
71 return true;
72 }
73
74 @Override
75 public Object get() {
76 throw new UnsupportedOperationException();
77 }
78
79 @Override
80 public Object get(long timeout, TimeUnit unit) {
81 throw new UnsupportedOperationException();
82 }
83 };
84
85
86
87 static final int MAX_SUPPORTED_TTL_SECS = (int) TimeUnit.DAYS.toSeconds(365 * 2);
88
89 private final ConcurrentMap<String, Entries> resolveCache = PlatformDependent.newConcurrentHashMap();
90
91
92
93
94 final void clear() {
95 while (!resolveCache.isEmpty()) {
96 for (Iterator<Entry<String, Entries>> i = resolveCache.entrySet().iterator(); i.hasNext();) {
97 Map.Entry<String, Entries> e = i.next();
98 i.remove();
99
100 e.getValue().clearAndCancel();
101 }
102 }
103 }
104
105
106
107
108 final boolean clear(String hostname) {
109 Entries entries = resolveCache.remove(hostname);
110 return entries != null && entries.clearAndCancel();
111 }
112
113
114
115
116 final List<? extends E> get(String hostname) {
117 Entries entries = resolveCache.get(hostname);
118 return entries == null ? null : entries.get();
119 }
120
121
122
123
124 final void cache(String hostname, E value, int ttl, EventLoop loop) {
125 Entries entries = resolveCache.get(hostname);
126 if (entries == null) {
127 entries = new Entries(hostname);
128 Entries oldEntries = resolveCache.putIfAbsent(hostname, entries);
129 if (oldEntries != null) {
130 entries = oldEntries;
131 }
132 }
133 entries.add(value, ttl, loop);
134 }
135
136
137
138
139 final int size() {
140 return resolveCache.size();
141 }
142
143
144
145
146 protected abstract boolean shouldReplaceAll(E entry);
147
148
149
150
151 protected void sortEntries(
152 @SuppressWarnings("unused") String hostname, @SuppressWarnings("unused") List<E> entries) {
153
154 }
155
156
157
158
159 protected abstract boolean equals(E entry, E otherEntry);
160
161
162 private final class Entries extends AtomicReference<List<E>> implements Runnable {
163
164 private final String hostname;
165
166 volatile ScheduledFuture<?> expirationFuture;
167
168 Entries(String hostname) {
169 super(Collections.<E>emptyList());
170 this.hostname = hostname;
171 }
172
173 void add(E e, int ttl, EventLoop loop) {
174 if (!shouldReplaceAll(e)) {
175 for (;;) {
176 List<E> entries = get();
177 if (!entries.isEmpty()) {
178 final E firstEntry = entries.get(0);
179 if (shouldReplaceAll(firstEntry)) {
180 assert entries.size() == 1;
181
182 if (compareAndSet(entries, singletonList(e))) {
183 scheduleCacheExpirationIfNeeded(ttl, loop);
184 return;
185 } else {
186
187 continue;
188 }
189 }
190
191
192 List<E> newEntries = new ArrayList<E>(entries.size() + 1);
193 int i = 0;
194 E replacedEntry = null;
195 do {
196 E entry = entries.get(i);
197
198
199
200 if (!Cache.this.equals(e, entry)) {
201 newEntries.add(entry);
202 } else {
203 replacedEntry = entry;
204 newEntries.add(e);
205
206 ++i;
207 for (; i < entries.size(); ++i) {
208 newEntries.add(entries.get(i));
209 }
210 break;
211 }
212 } while (++i < entries.size());
213 if (replacedEntry == null) {
214 newEntries.add(e);
215 }
216 sortEntries(hostname, newEntries);
217
218 if (compareAndSet(entries, Collections.unmodifiableList(newEntries))) {
219 scheduleCacheExpirationIfNeeded(ttl, loop);
220 return;
221 }
222 } else if (compareAndSet(entries, singletonList(e))) {
223 scheduleCacheExpirationIfNeeded(ttl, loop);
224 return;
225 }
226 }
227 } else {
228 set(singletonList(e));
229 scheduleCacheExpirationIfNeeded(ttl, loop);
230 }
231 }
232
233 private void scheduleCacheExpirationIfNeeded(int ttl, EventLoop loop) {
234 for (;;) {
235
236
237
238 ScheduledFuture<?> oldFuture = FUTURE_UPDATER.get(this);
239 if (oldFuture == null || oldFuture.getDelay(TimeUnit.SECONDS) > ttl) {
240 ScheduledFuture<?> newFuture = loop.schedule(this, ttl, TimeUnit.SECONDS);
241
242
243
244
245
246
247 if (FUTURE_UPDATER.compareAndSet(this, oldFuture, newFuture)) {
248 if (oldFuture != null) {
249 oldFuture.cancel(true);
250 }
251 break;
252 } else {
253
254 newFuture.cancel(true);
255 }
256 } else {
257 break;
258 }
259 }
260 }
261
262 boolean clearAndCancel() {
263 List<E> entries = getAndSet(Collections.<E>emptyList());
264 if (entries.isEmpty()) {
265 return false;
266 }
267
268 ScheduledFuture<?> expirationFuture = FUTURE_UPDATER.getAndSet(this, CANCELLED);
269 if (expirationFuture != null) {
270 expirationFuture.cancel(false);
271 }
272
273 return true;
274 }
275
276 @Override
277 public void run() {
278
279
280
281
282
283
284
285
286
287 resolveCache.remove(hostname, this);
288
289 clearAndCancel();
290 }
291 }
292 }