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


##########
store/src/main/java/org/apache/rocketmq/store/timer/TimerWheel.java:
##########
@@ -150,17 +157,68 @@ public void flush() {
         this.mappedByteBuffer.force();
     }
 
-    /* Caller holds lockWhenFlush; the frozen buffer stays immutable until the 
next copy. */
+    /*
+     * Caller holds lockWhenFlush; the frozen buffer stays immutable until the 
next copy.
+     * The first copy freezes the whole wheel; later copies only refresh slots 
dirtied since
+     * the previous freeze, so the lock is held for the changed slots instead 
of all 36.9 MiB.
+     */
     public void copyForSnapshot() {
         if (snapshotBuffer == null) {
             snapshotBuffer = ByteBuffer.allocateDirect(wheelLength);
         }
-        ByteBuffer bf = localBuffer.get();
+        if (!snapshotInitialized || dirtySlots == null) {
+            copyWholeWheel();
+            return;
+        }
+        // A dense dirty set is cheaper to copy in one sequential memcpy than 
slot by slot.
+        if (dirtySlots.cardinality() > slotsTotal * 2 / 4) {
+            copyWholeWheel();
+            return;
+        }
+        int copied = 0;
+        for (int slotIndex = dirtySlots.nextSetBit(0); slotIndex >= 0;
+            slotIndex = dirtySlots.nextSetBit(slotIndex + 1)) {
+            copySlot(slotIndex);

Review Comment:
   Fixed in c3179b6. `copyForSnapshot()` now restores 
`snapshotBuffer.limit(wheelLength)` before the absolute dirty-slot writes, so a 
segment write or force failure can no longer poison the next sparse freeze.
   
   The regression test injects a force failure in `backup()` after the first 1 
MiB segment (`snapshotCopyAfterFailedSegmentForceTest`), dirties a slot beyond 
that segment, and freezes again. Without the limit reset the second freeze 
throws `IndexOutOfBoundsException`; with it the freeze completes and the next 
backup round-trips.
   
   Timer tests: 52 run, 0 failed, 2 skipped. No on-disk format change.



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