Skip to content

[bugfix] Fix duplicate collector job timeouts#4169

Merged
tomsun28 merged 2 commits into
apache:masterfrom
hutiefang76:codex/fix-hertzbeat-4153-timer-duplicate
Jul 4, 2026
Merged

[bugfix] Fix duplicate collector job timeouts#4169
tomsun28 merged 2 commits into
apache:masterfrom
hutiefang76:codex/fix-hertzbeat-4153-timer-duplicate

Conversation

@hutiefang76

Copy link
Copy Markdown
Contributor

What's changed?

Fixes #4153.

When the collector receives the same job id again, TimerDispatcher replaced the timeout stored in the task map but did not cancel the previous HashedWheelTimer timeout. The replaced timeout could stay active until it fired, so repeated job updates may accumulate unnecessary HashedWheelTimeout instances.

This change cancels the previous timeout when a cyclic or temporary job is replaced by a new timeout for the same job id. It keeps the latest timeout in the map and only cancels the old one.

Added regression coverage for both cyclic and temporary jobs to verify that:

  • the timeout map is updated to the new timeout
  • the old timeout is cancelled
  • the new timeout remains active

Validation:

  • git diff --check -- hertzbeat-collector/hertzbeat-collector-common/src/main/java/org/apache/hertzbeat/collector/timer/TimerDispatcher.java hertzbeat-collector/hertzbeat-collector-common/src/test/java/org/apache/hertzbeat/collector/timer/TimerDispatcherTest.java
  • ./mvnw -pl hertzbeat-collector/hertzbeat-collector-common -am -Dsurefire.failIfNoSpecifiedTests=false test

Checklist

  • I have read the Contributing Guide
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

Add or update API

No API is added or updated in this PR.

  • I have added the necessary e2e tests and all cases have passed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a collector memory/timeout accumulation issue by ensuring that when a job with an existing ID is re-added, the previous HashedWheelTimer timeout is cancelled instead of being left scheduled in the wheel.

Changes:

  • Cancel the previous Timeout when replacing entries in currentCyclicTaskMap / currentTempTaskMap.
  • Add regression tests verifying timeout replacement and cancellation behavior for cyclic and temporary jobs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
hertzbeat-collector/hertzbeat-collector-common/src/main/java/org/apache/hertzbeat/collector/timer/TimerDispatcher.java Cancels the previous scheduled timeout when a job ID is replaced to avoid accumulating pending wheel timeouts.
hertzbeat-collector/hertzbeat-collector-common/src/test/java/org/apache/hertzbeat/collector/timer/TimerDispatcherTest.java Adds regression tests that validate the old timeout is cancelled and the new timeout remains active for both job types.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +261 to +271
@SuppressWarnings("unchecked")
private Map<Long, Timeout> currentTempTaskMap() {
try {
Field field = TimerDispatcher.class.getDeclaredField("currentTempTaskMap");
field.setAccessible(true);
return (Map<Long, Timeout>) field.get(timerDispatcher);
} catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
}
}

@tomsun28 tomsun28 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm👍

@tomsun28
tomsun28 merged commit 3c87b97 into apache:master Jul 4, 2026
5 checks passed
@github-project-automation github-project-automation Bot moved this from To do to Done in Apache HertzBeat Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG] 内存溢出,HashedWheelTimer无法回收

3 participants