lizhimins opened a new pull request, #11522:
URL: https://github.com/apache/rocketmq/pull/11522

   ## Summary
   
   Fixes #11521.
   
   In timer wheel snapshot mode (`timerWheelSnapshotFlush=true`), 
`TimerFlushService.flush()` held `lockWhenFlush` across the entire timerlog 
flush + full-wheel `backup()` (~36.9 MiB write + force) + checkpoint flush. 
Since the enqueue path takes the same lock per request, enqueue was blocked for 
the whole I/O duration, and the single large writeback burst inflated 
shared-disk CommitLog fsync tail latency. The lock also sat at the call site in 
`fetchAndPutTimerRequest()` rather than inside `putMessageToTimerWheel()`, so a 
subclass overriding the enqueue loop bypassed it.
   
   This PR:
   - Moves the `synchronized (lockWhenFlush)` guard **inside** 
`putMessageToTimerWheel()` so every enqueue entry (including subclass 
overrides) is serialized against the snapshot copy.
   - Rewrites the snapshot branch of `flush()` to **copy-first**: freeze the 
wheel into a reusable buffer under the lock (fast memcpy) and capture 
`wrotePosition` as the snapshot flag, then flush the timerlog until it is 
durable up to the copy's end, write the snapshot, and flush the checkpoint — 
all **outside** the lock. A stall guard skips the snapshot if the timerlog 
flush cannot advance.
   - Writes the snapshot in **1 MiB segments** (`force(false)` per segment, 
final `force(true)`) instead of one full-wheel write + force, bounding the 
writeback burst.
   - Keeps the checkpoint's `lastTimerLogFlushPos` at the **pre-flush** 
position so it never runs ahead of the latest snapshot flag, preserving 
rollback-to-non-snapshot replay coverage.
   
   The on-disk format is unchanged. The snapshot flag is now the 
`wrotePosition` at copy time (previously the post-flush `flushedWhere`); both 
are valid 52-byte record boundaries, so recovery via timerlog replay from the 
max snapshot flag is unchanged and old/new snapshots remain mutually readable. 
The non-snapshot (default) path is untouched.
   
   ## Test plan
   
   - [x] Existing timer unit tests pass (`TimerWheelTest`, 
`TimerMessageStoreTest`, `TimerCheckPointTest`, `TimerLogTest`, 
`TimerMetricsTest`, `TimerEngineSwitchVerifyTest`).
   - [x] New `TimerWheelTest` cases: snapshot copy isolation (live writes after 
the copy do not leak into the snapshot) and a multi-MiB segmented backup 
round-trip (per-slot equality across segments).
   - [x] New `TimerMessageStoreTest` cases: snapshot flush + recovery with 
concurrent enqueue and long-delay messages (snapshot covers all records, 
checkpoint stays at/below the flag, offsets/read-time restored after restart, 
long-delay slots intact); snapshot-mode roll message delivery.
   - [x] Micro-benchmark on a 1C/4Gi broker with a PL0 cloud disk, snapshot 
mode, 1500 TPS normal + 1000 TPS timer load: send RT p999 reduced ~40-50% 
versus the previous full-wheel-under-lock backup, across two rebuild-isolated 
rounds; p50 within round-to-round noise.


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