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

   ### What changes were proposed in this pull request?
   
   The grouped and cogrouped Arrow Python runners write each group, and each 
side of each cogroup, as a separate Arrow stream through an 
`ArrowWriterWrapper`. The wrapper's constructor adds a task completion listener 
that closes it. This PR moves that listener from the wrapper to the runner:
   
   - `ArrowWriterWrapper` no longer takes a `TaskContext` and no longer adds a 
listener.
   - `GroupedPythonArrowInput.newWriter` and 
`CoGroupedArrowPythonRunner.newWriter` each add one listener, which closes the 
root and allocator of the group, or of the cogroup side, being written if the 
task completes before the end of it. At most one wrapper is open at a time per 
runner: a group's wrapper is closed at the end of the group, and the left side 
of a cogroup is closed before its right side is created. As with the listener 
that `PythonArrowInput.newWriter` adds (SPARK-60737), the listener runs after 
the writer is done: the writer runs on the task thread unless it is pipelined, 
and the pipelined writer is stopped and awaited before any completion listener 
runs (SPARK-60113).
   - `ArrowWriterWrapper.createAndStartArrowWriter` closes the 
`VectorSchemaRoot` and the allocator itself, with `AutoCloseables.close`, if 
creating the vectors or writing the schema fails, since the caller then has no 
wrapper to close. Until now, the per-group listener, which SPARK-60737 
registers before these steps, covered this case.
   
   The bytes sent to the Python worker are unchanged, and nothing changes on 
the Python side.
   
   ### Why are the changes needed?
   
   Since Apache Spark 4.1.0 (SPARK-53562, SPARK-53609, SPARK-53610, 
SPARK-53611), every group adds a task completion listener, and every cogroup 
adds two, including for an empty side. `TaskContextImpl` keeps its listeners 
until the task completes, and each one keeps its closure and its 
`ArrowWriterWrapper` reachable. `ArrowWriterWrapper.close()` releases the Arrow 
buffers and clears the wrapper's fields at the end of the group, but the 
listener (16 bytes), the closure (16 bytes) and the wrapper (40 bytes) stay on 
the heap until the task ends, plus a slot in the listener stack. The heap held 
by a task therefore grows linearly with its number of groups.
   
   This affects `applyInPandas` and `applyInArrow` on grouped data, grouped 
aggregate pandas and Arrow UDFs, window pandas and Arrow UDFs, incremental 
Python aggregators (`PythonIncrementalAggregateExec`), and `applyInPandas` and 
`applyInArrow` on cogrouped data.
   
   Measured with `applyInArrow` and an identity function over 10,000,000 rows 
in 1-row groups, 4 shuffle partitions (2,500,000 groups per task), `local[4]`, 
JDK 21, written to the `noop` sink, with `jmap -histo:live` of the JVM during 
the tasks (MB = 10^6 bytes):
   
   | | Before (master) | After (this PR) |
   |---|---|---|
   | Live `ArrowWriterWrapper` objects at 60 / 120 / 180 s | 2,925,928 / 
5,997,525 / 9,117,860 | 4 / 4 / 4 |
   | Live task completion listeners at 180 s | 9,117,869 | 13 |
   | Heap held by the wrappers, listeners and their closures at 180 s | 656 MB 
| under 1 KB |
   | Growth of the live heap from 60 to 180 s | 472 MB | none |
   | Summed task GC time, 4 GB driver memory | 7.3 s | 3.6 s |
   | Summed task GC time, 1 GB driver memory (the default) | 27.6 s | 5.1 s |
   | Wall time, 1 GB driver memory | 198.5 s | 199.7 s |
   
   On master, the wrappers, listeners and closures take 72 bytes per group, and 
the live heap at 180 s is 705 MB larger than with this PR, including the arrays 
of the listener stack. The 4 wrappers left with this PR are those of the group 
that each task is writing or has just written. With more groups per task, the 
heap on master keeps growing until the task ends.
   
   The change applies cleanly to branch-4.x. branch-4.3 and older don't have 
SPARK-60737, so they would need their own patch, which I can open if a backport 
is wanted.
   
   ```python
   from pyspark.sql import functions as F
   
   spark.conf.set("spark.sql.shuffle.partitions", "4")
   spark.conf.set("spark.sql.adaptive.coalescePartitions.enabled", "false")
   df = spark.range(0, 10_000_000, 1, 4).select(F.col("id").alias("k"), 
F.col("id").alias("v"))
   df.groupBy("k").applyInArrow(lambda t: t, "k long, v long") \
       .write.format("noop").mode("overwrite").save()
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. Tasks with many groups hold less heap; results and the data sent to 
Python workers are unchanged.
   
   ### How was this patch tested?
   
   - Added `GroupedArrowPythonInputSuite`, which drives the input writers of 
the runners without a Python worker:
     - The grouped input stops in the third of three groups: the writer added 
one task completion listener (three before this PR), and task completion closes 
the allocator of the open group.
     - The cogrouped input stops in the left or the right side of the third of 
three cogroups: one listener (five or six before this PR), and task completion 
closes the allocator of the open side.
   - Changed the SPARK-60737 test in `ArrowWriterSuite`, in which writing the 
schema fails in `ArrowWriterWrapper.createAndStartArrowWriter`: it no longer 
needs a task context to close the allocator.
   - Each of these changes makes one of the tests fail: removing either 
listener, closing only one side of a cogroup in the listener, registering a 
listener per group or per cogroup side again, and removing the cleanup in 
`createAndStartArrowWriter`.
   
   ```
   build/sbt 'sql/testOnly 
org.apache.spark.sql.execution.python.GroupedArrowPythonInputSuite'
   build/sbt 'sql/testOnly 
org.apache.spark.sql.execution.arrow.ArrowWriterSuite -- -z SPARK-60737'
   ```
   
   Also ran all suites in `org.apache.spark.sql.execution.python` and 
`ArrowWriterSuite` (396 tests in 27 suites), scalastyle for `catalyst` and 
`sql`, and the classic (292 tests) and Spark Connect (290 tests) PySpark 
modules for grouped map, cogrouped map, grouped aggregate, window and Python 
aggregators, with no test skipped:
   
   ```
   PYSPARK_PYTHON=<python with pandas and pyarrow> build/sbt -Phive package \
     catalyst/scalastyle catalyst/Test/scalastyle sql/scalastyle 
sql/Test/scalastyle \
     'sql/testOnly org.apache.spark.sql.execution.python.* 
org.apache.spark.sql.execution.arrow.ArrowWriterSuite'
   python/run-tests --testnames 
'pyspark.sql.tests.pandas.test_pandas_grouped_map,pyspark.sql.tests.pandas.test_pandas_cogrouped_map,pyspark.sql.tests.pandas.test_pandas_cogrouped_map_misc,pyspark.sql.tests.pandas.test_pandas_udf_grouped_agg,pyspark.sql.tests.pandas.test_pandas_udf_window,pyspark.sql.tests.arrow.test_arrow_grouped_map,pyspark.sql.tests.arrow.test_arrow_cogrouped_map,pyspark.sql.tests.arrow.test_arrow_cogrouped_map_misc,pyspark.sql.tests.arrow.test_arrow_udf_grouped_agg,pyspark.sql.tests.arrow.test_arrow_udf_window,pyspark.sql.tests.arrow.test_arrow_python_aggregator'
   python/run-tests --testnames 
'pyspark.sql.tests.connect.pandas.test_parity_pandas_grouped_map,pyspark.sql.tests.connect.pandas.test_parity_pandas_cogrouped_map,pyspark.sql.tests.connect.pandas.test_parity_pandas_cogrouped_map_misc,pyspark.sql.tests.connect.pandas.test_parity_pandas_udf_grouped_agg,pyspark.sql.tests.connect.pandas.test_parity_pandas_udf_window,pyspark.sql.tests.connect.arrow.test_parity_arrow_grouped_map,pyspark.sql.tests.connect.arrow.test_parity_arrow_cogrouped_map,pyspark.sql.tests.connect.arrow.test_parity_arrow_cogrouped_map_misc,pyspark.sql.tests.connect.arrow.test_parity_arrow_udf_grouped_agg,pyspark.sql.tests.connect.arrow.test_parity_arrow_udf_window,pyspark.sql.tests.connect.arrow.test_parity_arrow_python_aggregator'
   ```
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Co-authored with 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