GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Port 4.1: Evict DNS cache entries when expiration tasks are cancelled… · netty/netty@b735c7a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • resolver-dns/src
    • main/java/io/netty/resolver/dns
      • Cache.java
    • test/java/io/netty/resolver/dns
      • CacheTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎resolver-dns/src/main/java/io/netty/resolver/dns/Cache.java‎

Copy file name to clipboardExpand all lines: resolver-dns/src/main/java/io/netty/resolver/dns/Cache.java
+21-2Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package io.netty.resolver.dns;
1717

1818
import io.netty.channel.EventLoop;
19+
import io.netty.util.concurrent.Future;
20+
import io.netty.util.concurrent.FutureListener;
1921
import io.netty.util.internal.PlatformDependent;
2022

2123
import java.util.ArrayList;
@@ -159,7 +161,7 @@ protected void sortEntries(
159161
protected abstract boolean equals(E entry, E otherEntry);
160162

161163
// Directly extend AtomicReference for intrinsics and also to keep memory overhead low.
162-
private final class Entries extends AtomicReference<List<E>> implements Runnable {
164+
private final class Entries extends AtomicReference<List<E>> implements Runnable, FutureListener<Object> {
163165

164166
private final String hostname;
165167
// Needs to be package-private to be able to access it via the AtomicReferenceFieldUpdater
@@ -237,14 +239,19 @@ private void scheduleCacheExpirationIfNeeded(int ttl, EventLoop loop) {
237239
// we can reconsider.
238240
ScheduledFuture<?> oldFuture = FUTURE_UPDATER.get(this);
239241
if (oldFuture == null || oldFuture.getDelay(TimeUnit.SECONDS) > ttl) {
240-
ScheduledFuture<?> newFuture = loop.schedule(this, ttl, TimeUnit.SECONDS);
242+
io.netty.util.concurrent.ScheduledFuture<?> newFuture = loop.schedule(this, ttl, TimeUnit.SECONDS);
241243
// It is possible that
242244
// 1. task will fire in between this line, or
243245
// 2. multiple timers may be set if there is concurrency
244246
// (1) Shouldn't be a problem because we will fail the CAS and then the next loop will see CANCELLED
245247
// so the ttl will not be less, and we will bail out of the loop.
246248
// (2) This is a trade-off to avoid concurrency resulting in contention on a synchronized block.
247249
if (FUTURE_UPDATER.compareAndSet(this, oldFuture, newFuture)) {
250+
if (!newFuture.isDone()) {
251+
newFuture.addListener(this);
252+
}
253+
// The loop may have terminated before the listener was registered.
254+
removeIfCancelled(newFuture);
248255
if (oldFuture != null) {
249256
oldFuture.cancel(true);
250257
}
@@ -259,6 +266,18 @@ private void scheduleCacheExpirationIfNeeded(int ttl, EventLoop loop) {
259266
}
260267
}
261268

269+
@Override
270+
public void operationComplete(Future<Object> future) {
271+
removeIfCancelled((ScheduledFuture<?>) future);
272+
}
273+
274+
private void removeIfCancelled(ScheduledFuture<?> future) {
275+
// Only evict if this is still the active timer, not one cancelled when shortening the TTL.
276+
if (future.isCancelled() && FUTURE_UPDATER.compareAndSet(this, future, CANCELLED)) {
277+
run();
278+
}
279+
}
280+
262281
boolean clearAndCancel() {
263282
List<E> entries = getAndSet(Collections.<E>emptyList());
264283
if (entries.isEmpty()) {
Collapse file

‎resolver-dns/src/test/java/io/netty/resolver/dns/CacheTest.java‎

Copy file name to clipboard
+120Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2026 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+
* https://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.DefaultEventLoop;
19+
import io.netty.util.concurrent.ScheduledFuture;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.ValueSource;
23+
24+
import java.util.concurrent.TimeUnit;
25+
26+
import static java.util.Arrays.asList;
27+
import static java.util.Collections.singletonList;
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
class CacheTest {
31+
32+
@ParameterizedTest
33+
@ValueSource(strings = { "address", "negative" })
34+
void testEvictWhenExpirationEventLoopTerminates(String value) {
35+
Cache<String> cache = newCache();
36+
DefaultEventLoop originalLoop = new DefaultEventLoop();
37+
DefaultEventLoop replacementLoop = new DefaultEventLoop();
38+
try {
39+
cache.cache("netty.io", value, 86400, originalLoop);
40+
assertThat(cache.get("netty.io")).isEqualTo(singletonList(value));
41+
42+
originalLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly();
43+
44+
// A cancelled expiration must not leave entries cached indefinitely, even without further lookups.
45+
assertThat(cache.size()).isZero();
46+
assertThat(cache.get("netty.io")).isNull();
47+
48+
cache.cache("netty.io", "fresh", 86400, replacementLoop);
49+
assertThat(cache.get("netty.io")).isEqualTo(singletonList("fresh"));
50+
} finally {
51+
originalLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly();
52+
replacementLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly();
53+
cache.clear();
54+
}
55+
}
56+
57+
@Test
58+
void testReplacingExpirationKeepsEntriesUntilNewEventLoopTerminates() {
59+
Cache<String> cache = newCache();
60+
DefaultEventLoop originalLoop = new DefaultEventLoop();
61+
DefaultEventLoop replacementLoop = new DefaultEventLoop();
62+
try {
63+
cache.cache("netty.io", "first", 86400, originalLoop);
64+
cache.cache("netty.io", "second", 3600, replacementLoop);
65+
originalLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly();
66+
67+
// Cancelling the superseded timer must not evict entries now owned by the shorter timer.
68+
assertThat(cache.get("netty.io")).isEqualTo(asList("first", "second"));
69+
70+
replacementLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly();
71+
assertThat(cache.size()).isZero();
72+
assertThat(cache.get("netty.io")).isNull();
73+
} finally {
74+
originalLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly();
75+
replacementLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly();
76+
cache.clear();
77+
}
78+
}
79+
80+
@Test
81+
void testExpirationCancelledBeforeScheduleReturns() {
82+
Cache<String> cache = newCache();
83+
DefaultEventLoop loop = new DefaultEventLoop() {
84+
@Override
85+
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
86+
ScheduledFuture<?> future = super.schedule(command, delay, unit);
87+
submit(new Runnable() {
88+
@Override
89+
public void run() {
90+
// NOOP
91+
}
92+
}).syncUninterruptibly();
93+
shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly();
94+
return future;
95+
}
96+
};
97+
try {
98+
cache.cache("netty.io", "address", 86400, loop);
99+
assertThat(cache.size()).isZero();
100+
assertThat(cache.get("netty.io")).isNull();
101+
} finally {
102+
loop.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly();
103+
cache.clear();
104+
}
105+
}
106+
107+
private static Cache<String> newCache() {
108+
return new Cache<String>() {
109+
@Override
110+
protected boolean shouldReplaceAll(String entry) {
111+
return "negative".equals(entry);
112+
}
113+
114+
@Override
115+
protected boolean equals(String entry, String otherEntry) {
116+
return entry.equals(otherEntry);
117+
}
118+
};
119+
}
120+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.