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

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: 
https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: 
https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., 
'[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a 
faster review.
     7. If you want to add a new configuration, please read the guideline first 
for naming configurations in
        
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the 
guideline first in
        'common/utils/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   
   Add an opt-in pipelined execution mode for Python UDFs 
(spark.python.udf.pipelined.enabled). When enabled, a dedicated writer thread 
serializes input and writes directly to the Python worker socket in blocking 
mode, while the task main thread reads output from the same socket 
concurrently. TCP full-duplex allows both directions to overlap, achieving 
pipeline parallelism.                                                           
    
                                                                 
   Key changes:
     - PipelinedWriterRunnable in PythonRunner.scala: serializes input + writes 
to socket in a pool thread
     - pipelined_process() in worker.py: background reader thread pre-fetches 
input batches; lazy iterators from grouped/aggregate serializers are eagerly 
materialized to avoid cross-thread socket conflicts                             
                                                     
     - InMemoryRowQueue gains a lockFree parameter: when true (pipelined mode 
only), synchronized is skipped since blocking socket I/O already provides 
happens-before guarantees                                                       
                                                                 
     - Proper propagation of TaskContext and InputFileBlockHolder to the writer 
pool thread                                                            
     - Socket idle timeout handling via SO_TIMEOUT + SocketTimeoutException 
wrapper        
   
     Benchmark: `python/pyspark/sql/tests/pandas/bench_pipelined_udf.py`
     Environment: local[*] (16 cores), 5 iterations, 2 warmup
   
     | Scenario                     | Sync (ms) | Pipelined (ms) | Speedup   |
     |------------------------------|-----------|----------------|-----------|
     | Light UDF (1M rows)          |       100 |             90 |   1.11x   |
     | CPU-bound UDF (1M rows)      |       143 |            146 |   0.98x   |
     | Heavy UDF (10ms sleep/batch) |      1180 |           1176 |   1.00x   |
     | Large data (5M rows)         |       338 |            300 | **1.13x** |
     | Multi-UDF (3 columns, 1M)    |       123 |            100 | **1.24x** |
   
   ### Why are the changes needed?
   
   The current single-threaded NIO selector model serializes input and reads 
output in the same thread alternately. For multi-column UDFs or compute-heavy 
UDFs, this leaves the JVM idle while waiting for Python output. Pipelined mode 
overlaps serialization with output reading, improving throughput by up to 22% 
for multi-UDF queries and 10% for large data workloads.     
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as 
new features, bug fixes, or other behavior changes. Documentation-only updates 
are not considered user-facing changes.
   
   If yes, please clarify the previous behavior and the change this PR proposes 
- provide the console output, description and/or an example to show the 
behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to 
the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   No
   
   ### How was this patch tested?
   
     - All PySpark UDF test suites pass with pipelined=true as the compile-time 
default: test_pandas_udf_scalar, test_pandas_udf_grouped_agg, 
test_pandas_udf_window, test_udf, test_udtf
     - JVM test suites pass: PythonUDFSuite, BatchEvalPythonExecSuite, 
ArrowColumnarPythonUDFSuite                                                     
     - Benchmark (bench_pipelined_udf.py) across 5 scenarios confirms no 
regression vs sync mode and up to 22% speedup for multi-UDF workloads         
                                                                                
                                                                       
   ### Was this patch authored or co-authored using generative AI tooling?
   <!--
   If generative AI tooling has been used in the process of authoring this 
patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling 
Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   
   Generated-by: Claude Code


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