Skip to content

[ISSUE #11521] Copy only dirty slots when freezing timer wheel snapshot - #11647

Open
Drifter-yh wants to merge 2 commits into
apache:developfrom
Drifter-yh:fix-11521-timer-snapshot-dirty-slots
Open

Drifter-yh wants to merge 2 commits into
apache:developfrom
Drifter-yh:fix-11521-timer-snapshot-dirty-slots

Conversation

@Drifter-yh

@Drifter-yh Drifter-yh commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #11534 for #11521. #11534 moved the timerlog flush and snapshot write out of lockWhenFlush, but the freeze itself still memcpy'd the full wheel under that lock: 604800 slots x 2 x 32 B = 36.9 MiB with the default 7-day wheel. Timer enqueue (TimerMessageStore#doEnqueue) takes the same lock per request, so every snapshot interval blocked enqueue for the whole copy.

This change:

  • tracks the slots written since the previous freeze (putSlot and reviseSlot mark them; snapshot-mode enqueue already serializes wheel writes with copyForSnapshot under lockWhenFlush);
  • copies the whole wheel on the first freeze, then refreshes only the dirty slots;
  • falls back to the sequential full-wheel copy when more than a quarter of the slots are dirty, where one memcpy is faster than per-slot copies.

No on-disk format, recovery, or snapshot-ordering change: the frozen buffer is complete at every freeze, only the bytes copied under the lock differ. A failed 1 MiB segment write or force narrows the snapshot buffer limit; the next sparse freeze restores the full limit before the absolute slot writes.

Fixes #11521.

Benchmark

TimerWheel.copyForSnapshot(), 604800 slots (36.9 MiB), JDK 17/Windows, 20 iterations after warm-up:

dirty slots copied median p90
all (first freeze / dense fallback) 36.9 MiB 4.00 ms 4.69 ms
50,000 1.6 MiB 0.41 ms 0.57 ms
10,000 320 KiB 0.19 ms 0.38 ms
1,000 32 KiB 0.11 ms 0.25 ms

At 1k timer TPS and a 10s snapshot interval, at most ~10k slots are touched between snapshots, so the enqueue pause from the freeze drops from ~4 ms to ~0.2 ms in that case. These are microbenchmark numbers, not a broker run.

Test plan

  • mvn -pl store -Dtest='TimerWheelTest,TimerMessageStoreTest,TimerCheckPointTest,TimerLogTest,TimerMetricsTest' -Djacoco.skip=true test: 52 passed, 0 failed, 2 skipped.
  • Checkstyle: 0 violations. SpotBugs: 0 bugs.
  • New TimerWheelTest.dirtySlotSnapshotCopyTest: full first freeze; zero-slot freeze when nothing changed; one-slot refresh with both snapshot files round-tripped; dense fallback to the full copy.
  • New TimerWheelTest.snapshotCopyAfterFailedSegmentForceTest: a segment force failure leaves the snapshot buffer limit shortened; the next freeze restores it, copies a slot beyond that limit, and the next backup round-trips.

…snapshot

The snapshot freeze still memcpy'd the full 36.9 MiB wheel while holding
lockWhenFlush, so each snapshot cycle stalled timer enqueue for the whole
copy. Track the slots written since the previous freeze and refresh only
those; the first freeze after startup still copies the full wheel, and a
dense dirty set falls back to the sequential full copy.

On a 604800-slot wheel the freeze drops from ~4.0 ms to ~0.19 ms for 10k
dirty slots in a local microbenchmark.
@codecov-commenter

codecov-commenter commented Nov 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 51.35%. Comparing base (19352a2) to head (c3179b6).

Additional details and impacted files
@@              Coverage Diff              @@
##             develop   #11647      +/-   ##
=============================================
+ Coverage      51.33%   51.35%   +0.02%     
- Complexity     15036    15048      +12     
=============================================
  Files           1391     1391              
  Lines         103942   103983      +41     
  Branches       13643    13648       +5     
=============================================
+ Hits           53354    53402      +48     
+ Misses         44329    44326       -3     
+ Partials        6259     6255       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

int copied = 0;
for (int slotIndex = dirtySlots.nextSetBit(0); slotIndex >= 0;
slotIndex = dirtySlots.nextSetBit(slotIndex + 1)) {
copySlot(slotIndex);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

backup() narrows the snapshot buffer limit while writing each 1 MiB
segment. If a segment write or force throws, the limit stays at the
segment end, and a later sparse freeze fails on any dirty slot beyond
it with IndexOutOfBoundsException. Reset the limit before the absolute
slot writes.

Cover the failure path with a test that injects a segment force failure
and then freezes a slot beyond the shortened limit; without the reset
it reproduces the IndexOutOfBoundsException, with it the next backup
round-trips.
@Drifter-yh
Drifter-yh force-pushed the fix-11521-timer-snapshot-dirty-slots branch from 8c4648c to c3179b6 Compare November 29, 2026 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement] Reduce enqueue stall and write long-tail of timer wheel snapshot flush

3 participants