Skip to content

[ISSUE #11047] Keep timer enqueue completion state per attempt to avoid duplicate delivery - #11595

Open
Drifter-yh wants to merge 3 commits into
apache:developfrom
Drifter-yh:fix-11047-timer-attempt-scoped
Open

Drifter-yh wants to merge 3 commits into
apache:developfrom
Drifter-yh:fix-11047-timer-attempt-scoped

Conversation

@Drifter-yh

@Drifter-yh Drifter-yh commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Which Issue(s) This PR Fixes

Brief Description

TimerEnqueuePutService#fetchAndPutTimerRequest retried the whole batch after any single request failed, so requests whose doEnqueue had already succeeded were enqueued again on every retry round. Each retry appended another TimerLog unit for the same message into the same timer-wheel slot, and every unit is converted and delivered independently on dequeue, so the scheduled message was delivered more than once.

The retry bookkeeping also had the completion race described in the review of #11048: released stayed true after the first attempt, so re-arming only the latch meant a successful retry never completed the new latch, while a late release from a previous dequeue owner could complete it and make allSuccess observe work the retry had not done yet.

This patch makes completion state per attempt:

  • TimerRequest.Attempt now owns the latch, the done signal, succ and the "handed to the dequeue pipeline" flag. fetchAndPutTimerRequest starts a new attempt for each round and only reprocesses requests whose current attempt has not succeeded.
  • A request handed to dequeuePutQueue carries the attempt it was handed over with (pipelineAttempt). TimerDequeuePutMessageService captures that attempt when it takes the request and releases exactly that attempt in finally, so a retry that re-armed the request can never have its new attempt completed by the previous owner.
  • Before reusing a request whose previous attempt is still owned by the dequeue pipeline, the retry waits for that attempt to finish (isDone()). If the previous owner succeeded in the meantime, the message is not enqueued again; the retry only completes its own attempt bookkeeping. If the previous owner failed (for example because dequeue stopped running), the request is retried once through the normal path.
  • Round 1 is unchanged: no request has succeeded yet, so the candidate list is the whole batch.

This keeps the commit-offset contract unchanged: commitQueueOffset is still advanced only after every request of the batch has a successful attempt.

How Did You Test This Change?

Three new deterministic tests in TimerMessageStoreTest (they do not need a real TimerLog, so they also run on Windows):

  1. testEnqueueRetryReprocessesOnlyFailedRequests — two requests, one transient doEnqueue failure; the failed request is enqueued twice (first failure + retry), the already-succeeded one exactly once, and both rounds complete their own latch.
  2. testEnqueueRetryWaitsForPreviousDequeueOwner — the first attempt hands a request to dequeuePutQueue; the test holds that owner, lets the retry re-arm completion, then releases the owner with succ = true. The retry must not enqueue the message again, must not commit early, and the late release must only complete the attempt it owned.
  3. testEnqueueRetryAfterPreviousDequeueOwnerFails — the previous owner fails after dequeue stops running; the retry then enqueues the message exactly once through the direct path.

Commands and results on Windows 11, Microsoft OpenJDK 17.0.14, Maven 3.9.2:

mvn -B -ntp -pl store -am -Dtest=TimerMessageStoreTest -DfailIfNoTests=false -Djacoco.skip=true -Dspotbugs.skip=true test
Tests run: 14, Failures: 0, Errors: 0, Skipped: 2   (the two pre-existing Windows skips)
BUILD SUCCESS

mvn -B -ntp -pl store -am -DskipTests -Djacoco.skip=true -Dspotbugs.skip=true validate
0 Checkstyle violations

mvn -B -ntp -pl store -DskipTests -Djacoco.skip=true spotbugs:check
BugInstance size is 0 — BUILD SUCCESS

JaCoCo 0.8.5 cannot instrument the JDK 17 Mockito classes used by this test class, hence jacoco.skip for the local run; the test itself only relies on Java 8 APIs and the module still compiles with -source 1.8.

AI assistance: the analysis, implementation and tests were produced with an AI coding agent and reviewed before submission.

@Drifter-yh
Drifter-yh force-pushed the fix-11047-timer-attempt-scoped branch from 46b0a6b to 258cf01 Compare November 23, 2026 14:47
…empt

TimerEnqueuePutService retried the whole batch whenever a single request
failed, so requests whose doEnqueue had already succeeded were enqueued
again. Every retry appended another TimerLog unit for the same message and
the scheduled message was delivered once per redundant unit.

Replace the shared latch/released/succ fields of TimerRequest with an
Attempt object. Each retry round re-arms the request with a new attempt,
and the dequeue pipeline releases the attempt it was handed, so a late
release from a previous owner can never complete the attempt that replaced
it.

The retry round now only reprocesses requests whose current attempt has not
succeeded, and waits for a previous attempt that is still owned by the
dequeue pipeline before reusing the request. This prevents a concurrent
second enqueue while the previous owner may still deliver the message.
@Drifter-yh
Drifter-yh force-pushed the fix-11047-timer-attempt-scoped branch from 258cf01 to 1682c78 Compare November 23, 2026 14:54
@codecov-commenter

codecov-commenter commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.72340% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 51.39%. Comparing base (19352a2) to head (b899f71).

Files with missing lines Patch % Lines
...apache/rocketmq/store/timer/TimerMessageStore.java 79.24% 6 Missing and 5 partials ⚠️
.../org/apache/rocketmq/store/timer/TimerRequest.java 78.04% 3 Missing and 6 partials ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             develop   #11595      +/-   ##
=============================================
+ Coverage      51.33%   51.39%   +0.06%     
- Complexity     15036    15057      +21     
=============================================
  Files           1391     1391              
  Lines         103942   104016      +74     
  Branches       13643    13661      +18     
=============================================
+ Hits           53354    53455     +101     
+ Misses         44329    44299      -30     
- Partials        6259     6262       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

} else {
holdMomentForUnknownError();
}
holdMomentForUnknownError();

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.

If the enqueue service stops during this backoff, the retry loop exits and commitQueueOffset still advances past requests that did not succeed. Could we only advance it once the entire batch has succeeded?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in f926611. commitQueueOffset now advances only when every request in the batch has a successful attempt; a stop during the backoff leaves the offset where it was. A fully successful batch is still committed even if the stop lands before the next loop check.

testEnqueueStopDuringBackoffDoesNotAdvanceCommitOffset stops the service from holdMomentForUnknownError after the single failed attempt and asserts the offset stays unchanged. TimerMessageStoreTest: 15 tests, 0 failures, 2 pre-existing Windows skips; Checkstyle and SpotBugs clean.

…ssful batch

If the enqueue service stops while backing off before a retry, the loop
exits with requests that did not succeed. Do not acknowledge that batch:
commitQueueOffset now moves only when every request of the batch has a
successful attempt. A batch that did complete successfully is still
committed even if the stop happens before the next loop condition runs.

testEnqueueStopDuringBackoffDoesNotAdvanceCommitOffset stops the service
from holdMomentForUnknownError and asserts the offset stays unchanged.
…tempt-scoped

# Conflicts:
#	store/src/main/java/org/apache/rocketmq/store/timer/TimerMessageStore.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Timer enqueue retry re-enqueues already-succeeded requests, delivering scheduled messages multiple times

3 participants