lizhimins opened a new issue, #11521:
URL: https://github.com/apache/rocketmq/issues/11521

   ### Search before asking
   
   - [x] I had searched in the 
[issues](https://github.com/apache/rocketmq/issues) and found no similar issues.
   
   ### Description
   
   When `timerWheelSnapshotFlush=true` (the snapshot mode introduced by #9735 / 
#9736), `TimerFlushService.flush()` runs the whole persistence sequence 
**inside** `lockWhenFlush`:
   
   ```java
   synchronized (lockWhenFlush) {
       prepareTimerCheckPoint();
       timerLog.getMappedFileQueue().flush(0);
       if (interval elapsed) {
           timerWheel.backup(timerLog.getMappedFileQueue().getFlushedWhere()); 
// whole 36.9 MiB wheel write + force
       }
       timerCheckpoint.flush();
   }
   ```
   
   The enqueue path (`TimerEnqueuePutService.fetchAndPutTimerRequest`) acquires 
the same lock per request. As a result, **enqueue is blocked for the entire I/O 
duration** of the flush, not just for the in-memory wheel update. The 
full-wheel `backup()` writes ~36.9 MiB and forces it in one shot; on slow disks 
(e.g. cloud ESSD PL0, ~150 MB/s) this takes hundreds of milliseconds. Two 
consequences:
   
   1. Timer enqueue stalls behind the flush lock; under high timer TPS the 
`enqueuePutQueue` (capacity 1024) can back up and trigger 
`holdMomentForUnknownError` retries.
   2. The single large writeback burst saturates the shared disk and inflates 
CommitLog fsync tail latency, showing up as send RT long-tail (p999).
   
   In addition, the lock is applied at the **call site** in 
`fetchAndPutTimerRequest()` rather than inside `putMessageToTimerWheel()` 
itself, while `lockWhenFlush` is `private`. Any subclass that overrides the 
enqueue loop and calls `putMessageToTimerWheel()` directly therefore bypasses 
the lock, breaking the atomicity premise the snapshot relies on.
   
   ### Proposal
   
   1. **Move the lock inside `putMessageToTimerWheel()`** so every enqueue 
entry (including subclass overrides) is serialized against the snapshot copy.
   2. **Copy-first, I/O outside the lock**: in `flush()`, only freeze the wheel 
into a reusable buffer under `lockWhenFlush` (a fast memcpy, ~9 ms for 36.9 
MiB) and capture `wrotePosition` as the snapshot flag. Then, outside the lock, 
flush the timerlog until it is durable up to the copy's end, write the 
snapshot, and flush the checkpoint. The timerlog must cover the records 
referenced by the copy before the snapshot becomes recoverable; `flush(0)` 
advances one file per call, so loop until the target is covered (with a stall 
guard).
   3. **Segment the snapshot write**: write the frozen copy in 1 MiB segments 
with `force(false)` per segment and a final `force(true)`, instead of one 
full-wheel write + force, to bound the writeback burst.
   4. Keep the checkpoint's `lastTimerLogFlushPos` at the **pre-flush** 
position so it never runs ahead of the latest snapshot flag (preserves the 
rollback-to-non-snapshot replay coverage).
   
   The snapshot flag becomes the `wrotePosition` at copy time instead of the 
post-flush `flushedWhere`; both are valid 52-byte record boundaries, so 
recovery (replay timerlog from the max snapshot flag) is unchanged and old/new 
snapshots remain mutually readable. The on-disk format is unchanged.
   
   ### Are you willing to submit a PR?
   
   - [x] Yes, I am willing to submit a PR.


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