Duansg opened a new pull request, #4201:
URL: https://github.com/apache/hertzbeat/pull/4201
## What's changed?
close #3534
`TimerDispatcher.cyclicJob(WheelTimerTask, long, TimeUnit)` overwrites the
per-job entry in `currentCyclicTaskMap` with a bare `put`, **without cancelling
the previously scheduled `Timeout`**:
```java
Timeout timeout = wheelTimer.newTimeout(timerTask, interval,
TimeUnit.SECONDS);
currentCyclicTaskMap.put(timerTask.getJob().getId(), timeout); // previous
timeout leaked
```
Since HashedWheelTimer is one-shot, a cyclic job re-arms itself by calling
cyclicJob each round. If cyclicJob is called more than once for the same job
while a previously scheduled timeout is still pending, the previous timeout is
orphaned: the map no longer references it and nobody cancels it, yet it still
fires. Each orphan fires a redundant collection round which re-arms again, so
the orphaned HashedWheelTimeout instances accumulate without bound. This
matches the heap symptom reported in #3534 (millions of HashedWheelTimeout /
LinkedBlockingQueue$Node instances under sustained load).
### Example timeline (interval = 600s, one collection that takes ~300s)
```markdown
T+0s round starts; collection begins (will take ~300s)
T+240..260s timeout monitor (scans every 20s) detects the 4-min timeout
-> logs "[Collect Timeout]"
-> dispatchCollectData -> cyclicJob-1 (schedules next round,
fires ~T+600s)
T+300s collection finally succeeds
-> dispatchCollectData -> cyclicJob-2 (Prometheus list-path
calls
cyclicJob unconditionally)
-> overwrites the map entry but does NOT cancel cyclicJob #1's
timeout
-> that timeout (fires ~T+600s) is now an ORPHAN in the wheel
```
Each such round leaks one `HashedWheelTimeout`. Because every orphan later
fires a redundant round that also times-out-and-recovers, the orphans compound,
matching the millions of `HashedWheelTimeout` / `LinkedBlockingQueue$Node`
instances in the heap dump of #3534.
## Fix
Cancel the displaced timeout when overwriting, exactly like addJob.
## Checklist
- [x] I have read the [Contributing
Guide](https://hertzbeat.apache.org/docs/community/code_style_and_quality_guide)
- [ ] I have written the necessary doc or comment.
- [x] I have added the necessary unit tests and all cases have passed.
## Add or update API
- [ ] I have added the necessary [e2e
tests](https://github.com/apache/hertzbeat/tree/master/e2e) and all cases have
passed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]