[SPARK-54353][SQL] Make CollectMetricsExec.collectedMetrics thread-safe #53064
+42
−10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
This PR makes
CollectMetricsExec.collectedMetricsthread-safe by introducing synchronization and caching at the accumulator level.Key changes:
collectedMetricsRowfield to cache the converted metrics rowcollectedMetricsmethod to useaccumulator.synchronizedblock with lazy initialization and cachingresetMetrics()to clear the cache and reset the accumulator within a synchronized blockobservedMetricsLockfromQueryExecution.observedMetricssince thread-safety is now handled at theCollectMetricsExeclevelThe synchronization ensures that:
collectedMetricsconcurrentlytoRowConverter) is performed only once and cachedWhy are the changes needed?
The
CollectMetricsExec.collectedMetricsmethod can be called concurrently from multiple threads when accessingQueryExecution.observedMetricsin connect mode. Without proper synchronization, this can lead to:Does this PR introduce any user-facing change?
No. This is an internal bug fix that improves thread-safety. Users should not observe any behavioral changes except for the elimination of potential race conditions and incorrect results when accessing metrics concurrently.
How was this patch tested?
build/sbt "sql/testOnly *DatasetSuite -- -z SPARK-54353"Was this patch authored or co-authored using generative AI tooling?
Generated-by: Cursor 1.7.54