Skip to content

Port akka-core#31834: Add ScheduledClock to reduce nanoTime overhead in recency-based passivation#2766

Merged
pjfanning merged 3 commits intoapache:mainfrom
pjfanning:copilot/port-akka-core-pr-31834
Mar 22, 2026
Merged

Port akka-core#31834: Add ScheduledClock to reduce nanoTime overhead in recency-based passivation#2766
pjfanning merged 3 commits intoapache:mainfrom
pjfanning:copilot/port-akka-core-pr-31834

Conversation

@pjfanning
Copy link
Copy Markdown
Member

part of #2730

Recency-based passivation strategies (the new default) call System.nanoTime() on every message. This introduces a ScheduledClock that refreshes the timestamp on a configurable interval (default 1s) and increments a counter between updates — eliminating per-message nanoTime calls under high throughput.

New: Clock actor extension (org.apache.pekko.util)

  • ClockExtension trait with currentTime(): Long and earlierTime(duration): Long
  • NanoClock — direct System.nanoTime() delegate (used when scheduled-clock-interval = 0)
  • ScheduledClock — updates via scheduler at the configured interval; increments atomically between updates so time never goes backwards

Config

pekko.scheduled-clock-interval = 1 s  # set to 0 to use System.nanoTime() directly

Internal API changes

  • RecencyList — removed inner Clock/NanoClock; added apply(clock: Clock) factory
  • FrequencyList.withOverallRecency.empty and SegmentedRecencyList.withOverallRecency.empty — now take an explicit Clock parameter instead of constructing a NanoClock internally
  • EntityPassivationStrategy.apply — signature changed to accept clock: () => Clock; all strategy and replacement-policy constructors propagate the clock
  • Shard — passes () => Clock(context.system) to EntityPassivationStrategy
  • Simulator — updated to pass clock factory through to all strategy creators

Tests

  • TestClock — shared controlled clock extracted from per-spec companion objects
  • ScheduledClockSpec — covers scheduled update, per-call increment, and non-regression (time never decreases under concurrent access)
  • Passivation specs — replaced System.nanoTime() with clock.currentTime() for consistency; test config sets pekko.scheduled-clock-interval = 100 ms

Copy link
Copy Markdown
Member

@He-Pin He-Pin left a comment

Choose a reason for hiding this comment

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

nice to have

@pjfanning pjfanning merged commit ab12a8f into apache:main Mar 22, 2026
9 checks passed
@pjfanning pjfanning added this to the 2.0.0-M2 milestone Mar 22, 2026
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.

3 participants