Skip to content

Commit 480ec35

Browse files
authored
fix: make HierarchicalWheelTimer threads daemon and bound its task queue (#6668) (#7518)
1 parent 75d4241 commit 480ec35

2 files changed

Lines changed: 58 additions & 5 deletions

File tree

‎shenyu-common/src/main/java/org/apache/shenyu/common/timer/HierarchicalWheelTimer.java‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@
1818
package org.apache.shenyu.common.timer;
1919

2020
import java.util.Objects;
21+
import org.apache.shenyu.common.concurrent.MemorySafeTaskQueue;
2122
import org.apache.shenyu.common.concurrent.ShenyuThreadFactory;
23+
import org.apache.shenyu.common.concurrent.ShenyuThreadPoolExecutor;
24+
import org.apache.shenyu.common.constant.Constants;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
2227

2328
import java.util.concurrent.DelayQueue;
2429
import java.util.concurrent.ExecutorService;
25-
import java.util.concurrent.LinkedBlockingQueue;
30+
import java.util.concurrent.RejectedExecutionException;
2631
import java.util.concurrent.ThreadFactory;
2732
import java.util.concurrent.ThreadPoolExecutor;
2833
import java.util.concurrent.TimeUnit;
@@ -41,6 +46,8 @@ public class HierarchicalWheelTimer implements Timer {
4146
private static final AtomicIntegerFieldUpdater<HierarchicalWheelTimer> WORKER_STATE_UPDATER =
4247
AtomicIntegerFieldUpdater.newUpdater(HierarchicalWheelTimer.class, "workerState");
4348

49+
private static final Logger LOG = LoggerFactory.getLogger(HierarchicalWheelTimer.class);
50+
4451
private final ExecutorService taskExecutor;
4552

4653
private final DelayQueue<TimerTaskList> delayQueue = new DelayQueue<>();
@@ -80,9 +87,9 @@ public HierarchicalWheelTimer(final String executorName,
8087
final Long tickMs,
8188
final Integer wheelSize,
8289
final Long startMs) {
83-
ThreadFactory threadFactory = ShenyuThreadFactory.create(executorName, false);
84-
taskExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
85-
new LinkedBlockingQueue<>(), threadFactory);
90+
ThreadFactory threadFactory = ShenyuThreadFactory.create(executorName, true);
91+
taskExecutor = new ShenyuThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
92+
new MemorySafeTaskQueue<Runnable>(Constants.THE_256_MB), threadFactory, new ThreadPoolExecutor.AbortPolicy());
8693
workerThread = threadFactory.newThread(new Worker(this));
8794
timingWheel = new TimingWheel(tickMs, wheelSize, startMs, taskCounter, delayQueue);
8895
}
@@ -106,7 +113,11 @@ public void add(final TimerTask timerTask) {
106113
private void addTimerTaskEntry(final TimerTaskList.TimerTaskEntry timerTaskEntry) {
107114
if (!timingWheel.add(timerTaskEntry)) {
108115
if (!timerTaskEntry.cancelled()) {
109-
taskExecutor.submit(() -> timerTaskEntry.getTimerTask().run(timerTaskEntry));
116+
try {
117+
taskExecutor.submit(() -> timerTaskEntry.getTimerTask().run(timerTaskEntry));
118+
} catch (RejectedExecutionException e) {
119+
LOG.warn("Dropping the expired timer task because the timer task queue has no remaining memory", e);
120+
}
110121
}
111122
}
112123
}

‎shenyu-common/src/test/java/org/apache/shenyu/common/timer/HierarchicalWheelTimerTest.java‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717

1818
package org.apache.shenyu.common.timer;
1919

20+
import org.apache.shenyu.common.concurrent.MemorySafeTaskQueue;
21+
import org.apache.shenyu.common.concurrent.ShenyuThreadPoolExecutor;
2022
import org.junit.jupiter.api.AfterEach;
2123
import org.junit.jupiter.api.BeforeEach;
2224
import org.junit.jupiter.api.Test;
2325

2426
import java.lang.reflect.Field;
27+
import java.util.concurrent.CountDownLatch;
2528
import java.util.concurrent.TimeUnit;
29+
import java.util.concurrent.atomic.AtomicBoolean;
2630
import java.util.concurrent.atomic.AtomicInteger;
2731

2832
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -128,6 +132,44 @@ public void run(final TaskEntity taskEntity) {
128132
}));
129133
}
130134

135+
/**
136+
* Test that the worker and task threads are daemons so the timer cannot block JVM shutdown.
137+
*
138+
* @throws Exception reflection exception
139+
*/
140+
@Test
141+
public void testTimerThreadsAreDaemon() throws Exception {
142+
CountDownLatch taskLatch = new CountDownLatch(1);
143+
AtomicBoolean taskThreadDaemon = new AtomicBoolean();
144+
timer.add(new TimerTask(1) {
145+
@Override
146+
public void run(final TaskEntity taskEntity) {
147+
taskThreadDaemon.set(Thread.currentThread().isDaemon());
148+
taskLatch.countDown();
149+
}
150+
});
151+
assertTrue(taskLatch.await(5, TimeUnit.SECONDS));
152+
153+
Field workerThreadField = HierarchicalWheelTimer.class.getDeclaredField("workerThread");
154+
workerThreadField.setAccessible(true);
155+
Thread workerThread = (Thread) workerThreadField.get(timer);
156+
assertTrue(workerThread.isDaemon());
157+
assertTrue(taskThreadDaemon.get());
158+
}
159+
160+
/**
161+
* Test that the task executor queue is bounded by available memory instead of growing without limit.
162+
*
163+
* @throws Exception reflection exception
164+
*/
165+
@Test
166+
public void testTaskExecutorQueueIsMemorySafe() throws Exception {
167+
Field taskExecutorField = HierarchicalWheelTimer.class.getDeclaredField("taskExecutor");
168+
taskExecutorField.setAccessible(true);
169+
ShenyuThreadPoolExecutor executor = (ShenyuThreadPoolExecutor) taskExecutorField.get(timer);
170+
assertTrue(executor.getQueue() instanceof MemorySafeTaskQueue);
171+
}
172+
131173
/**
132174
* Test list foreach.
133175
*/

0 commit comments

Comments
 (0)