bvolpato opened a new pull request, #41571:
URL: https://github.com/apache/beam/pull/41571

   Remove the global synchronization bottleneck in `ExecutionStateSampler` and 
avoid unnecessary per-bundle allocations during metrics reset and monitoring 
collection.
   
   For some context, `ExecutionStateSampler` currently tracks all active bundle 
trackers in a single `HashSet` guarded by `synchronized (activeStateTrackers)`. 
On workers running many concurrent small bundles, every bundle `start()` and 
`reset()` contends on that shared monitor. At the same time, per-bundle 
`reset()` and `getMonitoringData()` calls iterate `ConcurrentHashMap.forEach` 
across metric containers even when no user metrics are registered, allocating 
`ConcurrentHashMap$Traverser` and wrapper lambda objects on every bundle.
   
   What changed:
   
   - Track active state trackers in `ConcurrentHashMap.newKeySet()` and 
synchronize per tracker between `takeSample()` and `reset()` instead of locking 
globally across all worker threads. Gate `takeSample()` on a volatile `active` 
flag, skip locking untouched `ExecutionStateImpl` entries during `reset()` via 
a volatile `hasReportedValue` check, and iterate `executionStates` by index.
   - Short-circuit empty `MetricsMap` iterations and iterate `values()` 
directly in `forEachValue()` without a capturing wrapper `BiConsumer`.
   - Keep a lazily refreshed volatile snapshot list of step containers in 
`MetricsContainerStepMap` (invalidated when a new container is registered) so 
per-bundle `reset()` and `getMonitoringData()` do indexed iteration without 
`ConcurrentHashMap$Traverser` allocations, and skip empty metric maps in 
`MetricsContainerImpl.getMonitoringData()`.
   
   Why this is safe:
   
   - The background sampling thread and bundle teardown still synchronize on 
that specific tracker's monitor, and `start()` writes `active = true` before 
adding the tracker to the active set, so samples cannot observe partially 
initialized state or leak into the next bundle.
   - `BundleProgressReporter` methods (`updateIntermediateMonitoringData`, 
`updateFinalMonitoringData`, `reset`) are mutually exclusive under 
`BundleProcessor#getProgressRequestLock`, so checking `volatile 
hasReportedValue` before locking in `ExecutionStateImpl.reset()` cannot race 
with progress reporting.
   - Step containers in `MetricsContainerStepMap` are append-only, and the 
transient cached snapshot rebuilds lazily after Java deserialization or new 
container registration.
   
   Benchmarks (JMH `ExecutionStateSamplerBenchmark.*Harness`, `-prof gc`, 
OpenJDK 21.0.11, AMD Ryzen 9 9950X3D):
   
   | Benchmark | Threads | Before (ops/s) | After (ops/s) | Speedup | Alloc 
Before (B/op) | Alloc After (B/op) | Alloc Delta |
   | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: |
   | `testLargeBundleHarnessStateSampler` | 16 | 808,836 | 1,162,575 | 1.44x | 
272.4 | 32.3 | -88.1% |
   | `testTinyBundleHarnessStateSampler` | 512 | 4,462,968 | 80,470,342 | 
18.03x | 272.3 | 32.0 | -88.2% |
   | `testTinyBundleHarnessStateSamplerScoped` | 512 | 4,357,928 | 75,264,878 | 
17.27x | 272.4 | 32.0 | -88.2% |
   
   Validation:
   
   Verified locally with the `runners:core-java` metrics test suite, 
`ExecutionStateSamplerTest`, `ProcessBundleHandlerTest`, and 
`ExecutionStateSamplerBenchmarkTest`, plus Spotless, Checkstyle, and SpotBugs 
on both modules. Added coverage for empty and populated `MetricsMap` 
short-circuits and concurrent multi-threaded tracker start, reset, and 
sampling. Full `:javaPreCommit` and runner post-commit suites were left for CI.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

  • [PR] [Java] Reduce ExecutionSt... bvolpato (via GitHub) via github

Reply via email to