Drifter-yh commented on code in PR #11595:
URL: https://github.com/apache/rocketmq/pull/11595#discussion_r4682098074


##########
store/src/main/java/org/apache/rocketmq/store/timer/TimerMessageStore.java:
##########
@@ -1511,25 +1518,69 @@ protected void fetchAndPutTimerRequest() throws 
Exception {
                 return;
             }
 
+            List<TimerRequest> pending = trs;
             while (!isStopped()) {
-                CountDownLatch latch = new CountDownLatch(trs.size());
-                for (TimerRequest req : trs) {
-                    req.setLatch(latch);
+                // Only the requests whose current attempt has not succeeded 
are retried, otherwise
+                // the already-succeeded requests would be enqueued again and 
delivered more than once
+                List<TimerRequest> candidates = new 
ArrayList<>(pending.size());
+                List<TimerRequest.Attempt> previousAttempts = new 
ArrayList<>(pending.size());
+                for (TimerRequest req : pending) {
+                    TimerRequest.Attempt previousAttempt = req.getAttempt();
+                    if (previousAttempt == null || !previousAttempt.isSucc()) {
+                        candidates.add(req);
+                        previousAttempts.add(previousAttempt);
+                    }
+                }
+                if (candidates.isEmpty()) {
+                    break;
+                }
+
+                CountDownLatch latch = new CountDownLatch(candidates.size());
+                List<TimerRequest.Attempt> currentAttempts = new 
ArrayList<>(candidates.size());
+                for (TimerRequest req : candidates) {
+                    // Re-arm completion for this round. Each attempt owns its 
latch and success
+                    // flag, so a late release from the previous owner can 
only complete the
+                    // previous attempt, never this one.
+                    currentAttempts.add(req.beginAttempt(latch));
+                }
+
+                for (int i = 0; i < candidates.size(); i++) {
+                    TimerRequest req = candidates.get(i);
+                    TimerRequest.Attempt previousAttempt = 
previousAttempts.get(i);
+                    TimerRequest.Attempt currentAttempt = 
currentAttempts.get(i);
+                    if (previousAttempt != null && 
previousAttempt.isHandedToPipeline() && !previousAttempt.isDone()) {
+                        // The dequeue pipeline still owns the previous 
attempt. Reusing the
+                        // request now could enqueue or deliver the same 
message twice, so wait
+                        // until that owner has finished before retrying the 
request.
+                        while (!previousAttempt.isDone()) {
+                            if (isStopped()) {
+                                // Do not advance the commit offset for a 
batch that was not
+                                // fully enqueued.
+                                return;
+                            }
+                            previousAttempt.await(50, TimeUnit.MILLISECONDS);
+                        }
+                    }
+                    if (previousAttempt != null && previousAttempt.isSucc()) {
+                        // The previous owner succeeded while we were waiting, 
there is nothing
+                        // left to enqueue for this attempt.
+                        currentAttempt.release(true);
+                        continue;
+                    }
                     if (storeConfig.isTimerWheelSnapshotFlush()) {
                         synchronized (lockWhenFlush) {
-                            this.putMessageToTimerWheel(req);
+                            this.putMessageToTimerWheel(req, currentAttempt);
                         }
                     } else {
-                        this.putMessageToTimerWheel(req);
+                        this.putMessageToTimerWheel(req, currentAttempt);
                     }
                 }
                 checkDequeueLatch(latch, -1);
-                boolean allSuccess = 
trs.stream().allMatch(TimerRequest::isSucc);
-                if (allSuccess) {
+                pending = candidates;
+                if (pending.stream().allMatch(TimerRequest::isSucc)) {
                     break;
-                } else {
-                    holdMomentForUnknownError();
                 }
+                holdMomentForUnknownError();

Review Comment:
   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.



-- 
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]

Reply via email to