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


##########
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:
   If `backup()` throws during a segment write or force, it can leave 
`snapshotBuffer` with a shortened limit; the next sparse freeze then throws for 
any dirty slot beyond that limit, even after I/O recovers. Could we reset the 
buffer limit before the loop?



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