Repository navigation
[ISSUE #11521] Copy only dirty slots when freezing timer wheel snapshot - #11647
Drifter-yh wants to merge 2 commits into
Conversation
…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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
| int copied = 0; | ||
| for (int slotIndex = dirtySlots.nextSetBit(0); slotIndex >= 0; | ||
| slotIndex = dirtySlots.nextSetBit(slotIndex + 1)) { | ||
| copySlot(slotIndex); |
There was a problem hiding this 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?
There was a problem hiding this comment.
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.
8c4648c to
c3179b6
Compare
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:
putSlotandreviseSlotmark them; snapshot-mode enqueue already serializes wheel writes withcopyForSnapshotunderlockWhenFlush);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: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.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.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.