Skip to content

ref(core): Sample performance data on the timer executor (JAVA-837) - #6589

Open
runningcode wants to merge 3 commits into
mainfrom
no/java-837-collector-timer
Open

runningcode wants to merge 3 commits into
mainfrom
no/java-837-collector-timer

Conversation

@runningcode

@runningcode runningcode commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

DefaultCompositePerformanceCollector sampled CPU and memory on a java.util.Timer, which uses the wall clock, so sampling stopped after a backward clock step. It now runs on options.getTimerExecutorService(), which uses System.nanoTime(). Each run schedules the next one 100ms later, the same fixed delay as before.

Worth reviewing:

  • generation keeps a run that is already executing from rescheduling after close(). The run can trigger that close() itself by timing out the last transaction.
  • The setup task schedules the first collect. Otherwise a busy shared timer thread could run setup() and collect() back to back, giving AndroidCpuCollector a near-zero interval.

The 30s timeout still uses the wall clock. #6101 fixes it on 9.x.x, so expect a small conflict there. Based on #5816, which was closed without review.

#skip-changelog

🤖 Generated with Claude Code

@linear-code

linear-code Bot commented Nov 12, 2026

Copy link
Copy Markdown

JAVA-837

@sentry

sentry Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.65.1 (1) release

⚙️ sentry-android Build Distribution Settings

DefaultCompositePerformanceCollector sampled CPU and memory every 100ms
on a java.util.Timer, the last one in the SDK. Timer schedules on the
wall clock, so a backward clock step stopped sampling, and the 30s
collection budget check that runs in the same task, until the clock
caught back up.

The loop now runs on options.getTimerExecutorService(), which schedules
on System.nanoTime(). The executor only has one-shot schedule(), so each
run reschedules itself. A generation counter, bumped in close(), stops a
run that is already in flight from rescheduling a cancelled chain.

Based on #5816.

Fixes JAVA-837

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@runningcode
runningcode force-pushed the no/java-837-collector-timer branch from 8a0aeef to dd1a717 Compare November 17, 2026 10:30
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 23, 2026
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit 2f9f42d.

Main concerns: reach, host performance, data sent to Sentry.

This label is a hint for reviewers, not an approval.

@runningcode
runningcode marked this pull request as ready for review November 23, 2026 10:26
@runningcode
runningcode requested review from a team and adinauer as code owners November 23, 2026 10:26
@runningcode
runningcode requested review from markushi and romtsn and removed request for a team November 23, 2026 10:26
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 23, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit dd1a717. Configure here.

setup() and the first collect() were separate tasks on the shared timer thread, so a busy thread made them run back to back and AndroidCpuCollector divided by a near-zero interval.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 25, 2026
collector.start(fixture.id1)
verify(fixture.mockTimer)!!.schedule(any(), any<Long>(), eq(100))
Thread.sleep(300)
assertTrue(fixture.executorService.scheduledDelays.contains(100L))

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.

I think this only tests that collection runs once but not "every 100 ms". Can we change it to a fake executor that we advance by hand and then we could assert it multiple times?

There's also a pre-existing problem that we are leaking the executor/timer because the collector started for a string id has no timeout, so the collectors kept running until the JVM shuts down. I think switching to a fake executor also would fix it.

@romtsn romtsn left a comment

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.

one comment re. tests but LGTM otherwise!

There's one more behaviour change which is the executor.scheduleWithFixedDelay schedules the next read after the task has finished, whereas previously, the Timer would compute the next run before the task and schedule it exactly 100ms after scheduling the first task. However, I think the new approach is the right one and it does not break anything, and it also saves our shared executor from exhaustion.

The tests ran collection on a real executor and slept, so they only proved
a 100ms delay was requested, and a collector started for a string id kept
running until the JVM exited. A hand-advanced fake executor lets the tests
count each collect and leaves nothing running after a test ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 26, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move the performance collector's sampling loop off java.util.Timer

2 participants