yx9o commented on code in PR #11595:
URL: https://github.com/apache/rocketmq/pull/11595#discussion_r4681740552


##########
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:
   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?



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