RongtongJin opened a new pull request, #11507: URL: https://github.com/apache/rocketmq/pull/11507
### Which Issue(s) This PR Fixes No issue linked yet; draft for discussion. ### Brief Description `TimerWheel.flush()` currently compares every byte in the direct buffer with the mapped file on every flush, even when most slots have not changed. Track modified **logical 4 KiB pages** and skip comparisons for clean pages. Retain the original byte comparison and difference writes within each claimed page. - Initialize all pages as dirty; mark both `putSlot` overloads and `reviseSlot` in `finally`, including partial writes followed by an exception. - Atomically clear a page flag **before** copying it. A writer racing with the scan leaves work pending for a subsequent flush. Restore the current flag when copying throws. - Keep `MappedByteBuffer.force()` on every successfully completed flush, including calls with no dirty pages. Keep the flush schedule, slot/file format and snapshot/backup behavior. - Use long intermediate arithmetic for page-count and page-end calculations. Based on Apache `develop` at **`164322b91e26fca2a53e6580003e582ff0621514`** (rechecked before opening this draft). Head: `1ff4fc7fbb8c8c08832ef3c5517e20cfa83d00b5`. Only `TimerWheel.java` and its new regression-test class are changed. ### Correctness Argument and Its Assumptions This argument preserves the existing **single-flusher** usage and valid wheel/slot layout. It is an argument about eventually copying completed mutations to the mapped buffer and retaining the existing force path; it does **not** introduce transactional slot updates or claim that a racing flush is a linearizable snapshot. 1. **Complete mutation coverage.** The direct buffer is private, and the three mutation entry points publish their page flag in `finally`, after their writes, even if a mutator only partially writes before throwing. A 32-byte slot is aligned within one 4,096-byte page (128 slots/page), including the partial last page. All page flags start at 1 after construction/reopen; no persisted bitmap or file-format migration is involved. 2. **A completed mutation cannot lose its publication.** Let `M` be a writer's atomic `set(page, 1)` after its buffer writes and `C` the flusher's `getAndSet(page, 0)`. If `M` precedes `C`, `C` claims that page; the atomic publication/acquisition orders the preceding writes before the scan. If `M` follows `C`, it leaves the flag at 1 for the next flush. Clearing after the scan instead would lose that second case, so clearing must occur first. Several writers on one page are covered by the same ordering. A writer still in progress may need a later flush; after writers quiesce, the next successful flush copies their final bytes. Concurrent writes to a slot retain the original, non-transactional semantics. 3. **Failure does not silently consume pending copy work.** If a page copy throws, its flag is restored before the exception is propagated. Flags for pages not yet visited remain pending. Earlier completed page copies already reside in the mapped buffer. Partial mutator failures also publish the affected page in `finally`. 4. **Force is not optimized away.** Even if all flags have been consumed, a later normal flush still calls `force()`. Thus a previous force failure does not disable subsequent force attempts merely because there are no new mutations. This does not prove the OS/storage device's power-loss behavior or the complete Broker checkpoint/recovery protocol. 5. **Snapshot independence.** `backup()` continues to write/force/rename the full snapshot; it does not consume dirty-page flags. When the snapshot constructor has no mapped buffer, `flush()` keeps its existing early return. This change targets ordinary mapped-file flushing, not snapshot-backup performance. The flag is in-memory bookkeeping for avoiding comparisons, not a substitute for OS dirty-page tracking. Byte-for-byte convergence after quiescence and error retry are the tested properties; concurrent reads/writes were not made atomic by this change. ### Executed Source Correctness Tests The exact production and JUnit source files in this head were compiled in an isolated test Pod. No startup agent was used for these **source correctness** runs. The upstream baseline inputs and candidate source hashes were matched against the tested inputs. | Run | Actually executed result | | --- | --- | | Baseline: 6 existing + 8 new JUnit tests | `OK (14 tests)`, 0.615 s | | Candidate: same 6 existing + 8 new JUnit tests | `OK (14 tests)`, 0.581 s | | Candidate, separate out-of-tree functional oracle | **5,237 checks passed** | | Source/test compatibility and verification | `javac --release 8`, class major 52; JDK 21.0.9.0.8-AJDK, `-Xverify:all` | | Patch hygiene | `git diff --check` passed | The committed JUnit regressions exercise the following cases (they also pass on the original full-scan algorithm, as expected for behavior-preserving tests): | New test | Property exercised | | --- | --- | | `testWritesAcrossPagesAndPartialLastPage` | Page boundaries, partial final page and full persisted-byte equality | | `testAllMutatorsAfterPageHasBeenFlushed` | Both puts, revise, wraparound and repeated writes after flags were consumed | | `testFailedCopyIsRetried` | Injected copy failure followed by a successful retry | | `testPartialPutIsFlushedAfterFailure` | Short-put exception after partial buffer writes | | `testPartialFullSlotPutIsFlushedAfterFailure` | Full-slot-put exception after partial buffer writes | | `testPartialReviseIsFlushedAfterFailure` | Revise exception after partial buffer writes | | `testConcurrentWritesAndFlushEventuallyPersistAllBytes` | Four writers, 10,000 writes each, disjoint slots sharing pages; concurrent flushes, then join/final flush/full-byte comparison | | `testSnapshotBackupRemainsIndependentOfDirtyPages` | Snapshot backup and reopen retain full contents independently of flags | [Committed regression-test source](https://github.com/RongtongJin/rocketmq/blob/1ff4fc7fbb8c8c08832ef3c5517e20cfa83d00b5/store/src/test/java/org/apache/rocketmq/store/timer/TimerWheelDirtyPageTest.java#L66). The six existing tests cover put/get, counts, position checking, revise and recovery (including fixed TTL). The out-of-tree oracle adds mutator/page-boundary, concurrent/quiescent, copy-failure retry, reopen and child-process exit/reopen **after force** checks. The concurrent test is a stress test, not deterministic enumeration of every interleaving. The 5,237 figure counts oracle checks, not 5,237 independent JUnit tests. The oracle was compiled separately with `-source 8 -target 8` on JDK 21; the committed production/JUnit sources used the stronger `--release 8` API check. This was focused direct compilation using existing binary dependencies, **not** the complete upstream Maven reactor/store suite, and it was **not** execution on a Java 8 runtime. Near-2-GiB arithmetic uses long intermediates, but a 2-GiB wheel was not allocated in this test. ### Measured Performance: Startup Prototype, Not This Source Commit **The following are actual earlier OFF/ON measurements of a startup-agent prototype in a downstream distribution on Hygon. They are evidence motivating the algorithm, not a performance benchmark of this Apache source head. Source correctness validation above is a separate run. No production throughput or architecture-exclusive speedup is claimed.** OFF = original full scan. ON = dirty-page algorithm. In each matched sampling-engine cell, only TimerWheel implementation selection changes on the service side. There are two OFF and two ON rounds per cell; percentages below use all successful inputs in that cell. They are not additive across cells or message sizes. #### Fixed Workload and Resources - **Four independent SimpleConsumers** throughout. Original POP/receive/scan, retry/reset/offset/notification and synchronous ACK policy retained; no skipped measured sends. - Fixed original settings: worker enabled, selector 3, Pull 24, ACK 16; properties-map capacity 128, POP buffer 16,384, GetMessageResult capacity 100. No reduced thread counts or capacities. - Broker: 4 CPU, 2 GiB memory, 768 MiB heap, 512 MiB direct memory, G1. Proxy: 1 CPU, 24 gRPC threads. Client: 4 CPU, 1,536 MiB memory, 128–512 MiB heap, Serial GC. - One isolated Pod with **loopback networking** between containers. This is not a real-NIC or multi-node production experiment. - 32 send threads; fixed offered rate **1,000 messages/s**; 120 s warmup followed by **300 s measurement**. Receive max 16, invisible duration 15 s, await 1 s. `timerWheelSnapshotFlush=false`. - 1/4/16 KiB high-entropy bodies from 32 deterministic PRNG templates, with a 40-byte validation header, full-body CRC and duplicate checks. SDK `Encoding.IDENTITY` was verified; no compression. - **Every measured round below sent and ACKed exactly 300,000 messages**; no recorded send/receive/ACK failures, duplicate or body-integrity errors, skipped sends or warmup spill. Minute-level achieved input was checked against 99.9–100.1% of target. CPU metric = `sum(Broker cgroup CPU seconds) / sum(successful inputs) * 1,000,000`. Reduction = `1 - ON/OFF`. The Broker+Proxy metric uses the sum of their CPU seconds, normalized by the same successful input (not loopback byte counters). The predeclared acceptance gates were Broker CPU/input reduction >=5%; each ON round's Delivery, ACK-complete and Scheduled-ACK P99 <=110% of the **arithmetic mean of the two OFF round P99s in the same cell**; and hard P99 limits of Send <=20 ms, the other three <=50 ms. This mean of round P99s is **not** a pooled request P99. Fixed-rate CPU savings are not a measured maximum-throughput increase. #### Summary, Including the Earlier Failure | Campaign / body | Sampler | Broker CPU s / million, OFF -> ON | Broker CPU reduction | Broker+Proxy reduction | Combined CPU + latency gates | | --- | --- | ---: | ---: | ---: | --- | | Repeated 1 KiB | queue | 930.210 -> 850.277 | 8.59% | 4.34% | PASS | | Repeated 1 KiB | array | 942.931 -> 848.893 | 9.97% | 6.31% | PASS | | Original 1 KiB | queue | 923.901 -> 845.229 | 8.52% | 4.55% | **FAIL (relative tail latency)** | | Original 4 KiB | queue | 1020.112 -> 915.541 | 10.25% | 7.11% | PASS | | Original 16 KiB | queue | 1209.994 -> 1103.377 | 8.81% | 4.97% | PASS | The repeated 1 KiB candidate ACK-complete P99s were 9.163/8.636 ms (queue) and 8.338/8.053 ms (array). The original 1 KiB second ON round had **13.459625 ms ACK-complete P99, +49.18%** versus its OFF-round mean; Delivery was +17.26% and Scheduled-ACK +74.58%. It passed the absolute 50 ms P99 limit but **failed the relative gate**. That result is retained, not overwritten by later passes. <details> <summary>All eight repeated 1 KiB rounds: actual CPU seconds and per-round P99 in ms</summary> Order is the actual execution order. Queue OFF–ON–ON–OFF and array ON–OFF–OFF–ON were interleaved using one compiled dual-sampler harness; compare ON/OFF only within the same sampler. | Round | Sampler | Timer | Broker CPU s | Proxy CPU s | Send P99 | Delivery P99 | ACK-complete P99 | Scheduled-ACK P99 | | --- | --- | --- | ---: | ---: | ---: | ---: | ---: | ---: | | TT7QOFF1 | queue | OFF | 274.428544 | 224.074961 | 2.641220 | 6.360435 | 8.638766 | 8.994219 | | TT7AON1 | array | ON | 258.173824 | 221.618926 | 2.641160 | 6.102405 | 8.337507 | 8.444437 | | TT7QON1 | queue | ON | 264.813145 | 225.391889 | 2.737189 | 6.721723 | 9.163013 | 9.515336 | | TT7AOFF1 | array | OFF | 277.506790 | 226.342674 | 2.671839 | 6.052006 | 8.582786 | 8.733726 | | TT7AOFF2 | array | OFF | 288.251524 | 225.604852 | 2.661940 | 6.078345 | 8.365537 | 8.533883 | | TT7QON2 | queue | ON | 245.353160 | 226.086767 | 2.672990 | 6.375674 | 8.636375 | 8.970453 | | TT7AON2 | array | ON | 251.162054 | 222.487394 | 2.584140 | 5.813517 | 8.052668 | 8.171365 | | TT7QOFF2 | queue | OFF | 283.697530 | 223.115710 | 2.651719 | 6.363264 | 8.697815 | 9.051957 | TimerFlush thread CPU s / million fell from 79.812 to 1.884 (queue) and 79.335 to 1.881 (array). This supports the attribution to eliminating scans, but the Broker total is the reported service-level gain. </details> <details> <summary>All twelve original message-size rounds, including the failing 1 KiB round</summary> Each body-size campaign used OFF–ON–ON–OFF. Every row has 300,000 successful sends and ACKs. Large baseline variation is visible rather than hidden by the summary. | Body KiB | Round | Timer | Broker CPU s | Proxy CPU s | Send P99 ms | Delivery P99 ms | ACK-complete P99 ms | Scheduled-ACK P99 ms | | --- | --- | --- | ---: | ---: | ---: | ---: | ---: | ---: | | 1 | TB51OFF1 | OFF | 285.490940 | 224.023426 | 2.835279 | 6.714393 | 9.247853 | 9.518612 | | 1 | TB51ON1 | ON | 254.245501 | 225.493344 | 2.786239 | 6.518263 | 8.870644 | 9.184552 | | 1 | TB51ON2 | ON | 252.891971 | 225.464445 | 2.932198 | 7.665060 | 13.459625 | 16.288817 | | 1 | TB51OFF2 | OFF | 268.849945 | 225.405042 | 2.687569 | 6.359104 | 8.796655 | 9.142336 | | 4 | TB54OFF1 | OFF | 306.162350 | 250.117517 | 3.168208 | 7.526830 | 10.542198 | 10.899580 | | 4 | TB54ON1 | ON | 277.059641 | 235.370046 | 2.955778 | 7.147121 | 10.035500 | 10.454868 | | 4 | TB54ON2 | ON | 272.265229 | 233.477453 | 3.126857 | 7.486840 | 10.517568 | 11.010362 | | 4 | TB54OFF2 | OFF | 305.905048 | 233.939176 | 3.411856 | 9.412652 | 15.963467 | 18.333883 | | 16 | TR16OFF1 | OFF | 353.196241 | 258.563535 | 4.353253 | 18.566005 | 28.532226 | 36.384708 | | 16 | TR16ON1 | ON | 332.260235 | 262.131578 | 4.078414 | 12.403390 | 19.679752 | 20.862436 | | 16 | TR16ON2 | ON | 329.765933 | 259.318345 | 3.988594 | 11.537194 | 18.452536 | 19.300927 | | 16 | TR16OFF2 | OFF | 372.800163 | 260.803866 | 3.863024 | 11.165785 | 17.476970 | 17.897535 | </details> #### Observer Effects and Limits on the Conclusion - The queue sampler allocated boxed samples and queue nodes (about 3 million objects for five metrics at 300k inputs). All four repeated queue runs had one measured client Serial Full GC, with maximum pauses of 149.610–157.746 ms; all four array runs had no measured Full GC and maximum GC pauses of 5.563–5.742 ms. Array sampling changes observation cost, not the server algorithm. The repeated queue still passed its whole-window P99 gates, so Full GC alone does **not** uniquely establish the cause of the original failing round. The repeated harness is not a byte-identical replay of the earlier harness. - A separate all-Timer-ON array experiment moved a heavy system snapshot from inside the measured interval to before it (HOT–PRE–PRE–HOT). ACK-complete P99 was 8.266/9.211 ms for HOT and 7.074/7.057 ms for PRE (about 19% lower versus the HOT-round mean). **This is a measurement-method effect, not a server optimization, and is not added to the CPU savings.** The old failing round's root cause remains unproven; missing original client/Proxy GC logs prevent a unique retrospective attribution. - P99 pass does not mean every request is below 50 ms: the repeated array ON rounds had ACK-complete maxima of 79.050/75.797 ms and >=50 ms shares of 0.13267%/0.11867%. The earlier failed 1 KiB round had ACK-complete P99.9 162.538 ms and maximum 594.184 ms. - Only two repeats per cell, one Hygon environment, one fixed offered rate and loopback transport; no confidence interval, Intel comparison or architecture-specific causal claim. This is a general scan-elimination optimization that was tested on Hygon. - The 4/16 KiB runs encountered Broker memory-limit pressure (`memory.events.max` increments approximately 8.6k/25.5k per run), although OOM/OOM-kill deltas were zero. Baseline variation and that resource envelope limit generalization. - Benefit depends on clean-page fraction and write contention. A mostly dirty wheel can lose the savings and still pays flag/atomic overhead. `timerWheelSnapshotFlush=true` uses the backup path and has no claimed speedup here. Broader timer occupancy, concurrency and source-version performance remain to be measured. ### Evidence Digests and Remaining Draft Validation SHA-256 digests identify the retained raw evidence archives (digests are provenance, not public download links): | Evidence | SHA-256 | | --- | --- | | Repeated 1 KiB, eight rounds | `9076f47eee08c25ae83fc0497ae92b317bb6b3e9ee8a371d788011c4d31df947` | | Original 1 KiB, four rounds | `5f0b98b22a33a83ef27da1b600b981e6f665afb29c570f2d15eec408fc7216fb` | | Original 4 KiB, four rounds | `88bed8f4e53da3dce349b82ef763383009f5614c865623be459d8bb9d8cce692` | | Original 16 KiB, four rounds | `6ad3ccdea1f1ef82692d551a131a227e0a4d91e91238dfb139614dcc1e029810` | | Observer-only snapshot timing, four rounds | `37193f7a47f411fb4c8c82be307889ca804e4c0ae8be29bec597f2763dfb00f9` | | Focused source tests, release-8 API check | `470e2f3b582c368bf093dd588719ce0f42a0e01f5c5e373294eef26647af7ac5` | Candidate production source SHA-256: `a470b689d39dcd4aed3c18c17e02e06edb291510162a80272fbc96b78f94988f`. Candidate new JUnit source: `eb0247122deb7386455f4529227396d4435cfcbd32222fa87fcf9c9331cbfff8`. Still pending: full upstream Maven reactor/store-suite CI, Java 8 runtime testing, deterministic scheduling/model checking of concurrent interleavings, complete Broker recovery/power-loss testing, and performance measurements of this actual source commit with broader occupancy/load/resource conditions. The tests and argument support review of the optimization; they are not a formal proof of every Broker durability scenario. Please keep this PR in **Draft** while that validation is completed. -- 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]
