Skip to content

Conversation

Spikhalskiy
Copy link
Contributor

ThreadPoolExecutor#getActiveThreads implementation is taking a pool-wide lock and implements an actual counting inside this lock. This PR replaces it with an explicit counter to remove the points of threads serialization.

task.run();
} finally {
tasksCount = tasksInFlight.decrementAndGet();
metricsScope.gauge(MetricsType.WORKFLOW_ACTIVE_THREAD_COUNT).update(tasksCount);
Copy link
Member

Choose a reason for hiding this comment

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

Technically this is racy (line 55 can run on thread A, then line 50 and 51 can run on thread B, then line 56 runs back on thread A setting to old value), but after off-PR discussion: 1) this is how the old one worked, 2) it will get right eventually, 3) a bit off is acceptable, and 4) the alternative of synchronized access to gauge update is bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ACK. I consider it's fine to publish sometimes a slightly outdated value to this gauge (and override a newer value). Because alternatives either bring synchronization of all workflow threads or are blocked by uber-tally interface.

@Spikhalskiy Spikhalskiy merged commit 2ad867c into temporalio:master Sep 13, 2022
@Spikhalskiy Spikhalskiy deleted the optimize-workflow-threads-reporting branch September 13, 2022 20:37
@Spikhalskiy Spikhalskiy added the enhancement User experience label Sep 15, 2022
@Spikhalskiy Spikhalskiy added this to the 1.17.0 milestone Sep 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement User experience

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants