gaogaotiantian opened a new pull request, #60150:
URL: https://github.com/apache/spark/pull/60150

   ### What changes were proposed in this pull request?
   
   This PR adds `spark.sql.pyspark.udf.profiler.samplingAggregation` to choose 
how the sampling profiler (added in SPARK-60604) aggregates sampled call 
stacks. It accepts `query`, `stage` or `task`, and defaults to `stage`.
   
   - The conf is passed to the Python worker by `PythonUDFRunner` and 
`ArrowPythonRunner`, in the same way as 
`spark.sql.pyspark.udf.profiler.samplingInterval`.
   - `WorkerSamplingProfiler._get_task_key()` builds the result key at the 
chosen level. Results with the same key are merged by the existing 
`ProfileResultsParamV2`:
     - `query`: `sampling_query_<spark.sql.execution.root.id>`. The root id 
puts all micro-batches of a streaming query under one key. When the UDF does 
not run in a SQL query (e.g. under `df.rdd`), there is no execution id and the 
key is `sampling_query_unknown`.
     - `stage`: `sampling_stage_<stageId>`.
     - `task`: 
`sampling_task_<stageId>_<partitionId>.<attemptNumber>_<taskAttemptId>`, the 
same per-task key as before.
   - The `sampling_` prefix keeps these keys apart from the other profilers' 
keys, which are UDF result ids (int) or data source worker module names.
   - Call trees inside a result are now keyed by thread name instead of 
`threading.get_ident()`. Thread ids are only meaningful within one process at 
one time, so merging results from different tasks by thread id combined 
unrelated threads. Keyed by name, the `MainThread` trees of all tasks merge 
together.
   
   ### Why are the changes needed?
   
   Before this PR, the sampling profiler kept one result per task attempt. For 
a job with many tasks, the driver holds one call tree per task, even though 
users usually want a view of a whole stage or query. Aggregating on the worker 
side by a key the user picks up front keeps driver memory proportional to the 
number of stages or queries instead of the number of tasks.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, in unreleased code only. There is a new SQL conf, 
`spark.sql.pyspark.udf.profiler.samplingAggregation`. The sampling profiler is 
not released yet; with this PR its results are aggregated per stage by default 
instead of per task.
   
   ### How was this patch tested?
   
   Added tests in `pyspark.sql.tests.test_udf_profiler`:
   - `test_sampling_profiler_aggregation` (classic and Connect): runs one query 
with two tasks and checks that there are 2, 1 and 1 results for `task`, `stage` 
and `query`, that keys have the expected prefix, and that call trees are keyed 
by `MainThread`.
   - `test_sampling_profiler_aggregation_query_unknown` (classic only, since 
Connect has no `df.rdd`): checks that a UDF under `df.rdd` is grouped under 
`sampling_query_unknown`.
   - `test_sampling_profiler_udf` now sets the `task` level, since it checks 
one result per task.
   
   Ran locally:
   ```
   python/run-tests --testnames "pyspark.sql.tests.test_udf_profiler 
UDFProfiler2Tests,pyspark.sql.tests.connect.test_parity_udf_profiler"
   ```
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Isaac
   
   This pull request and its description were written by Isaac.
   


-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to