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

   ### What changes were proposed in this pull request?
   
   Backport of https://github.com/apache/spark/pull/60678 (0b9d2429469) to 
branch-4.4.
   
   The cherry-pick applies cleanly. Eight of the nine touched files are the 
same on branch-4.4 and on master before #60678. The ninth, 
`BaseScriptTransformationSuite.scala`, differs only by the branch-4.4 change 
from `Thread.threadId()` to `Thread.getId` (SPARK-60753 follow-up ce066e05af6), 
away from the new test, so the change is the same as on master. The new code 
uses no API added after Java 17. The `skip-checks: true` trailer inherited from 
the master commit is dropped from the commit message.
   
   The reader of a native data source (`NativePartitionReader`, SPARK-60101) 
now frees the buffers of the last batch that it returned when it is closed in a 
task, unless a thread that the task does not wait for may still read that batch.
   
   - **`TaskInputThreads`** (new, `core`, `private[spark]`) records the tasks 
that have a thread that reads the input of the task on its own, and that the 
task does not wait for when it completes. The three operators of Spark that 
start such a thread register it for the task before they start it, with one 
call each: `PipedRDD` (the stdin writer thread), script transformation 
(`TRANSFORM`, for Spark and Hive), and the R runner (`BaseRRunner`). They are 
otherwise unchanged.
   - **`NativePartitionReader.close()`** frees the buffers of the batch at once 
when the reader was created in a task without such a thread. In a task with 
such a thread, and outside of a task, where the reader does not know its 
consumers, it leaves them to a Cleaner, as before (SPARK-60753).
   
   The writer of pipelined Python UDFs is not registered: its task waits for it 
before its completion listeners run (SPARK-60113).
   
   The interface (`spark_datasource.h`) and its docs are unchanged: Spark may 
still release the last array of a stream later, from a Cleaner thread, in a 
task with such a thread.
   
   ### Why are the changes needed?
   
   Since SPARK-60753, the column vectors of a native scan read the buffers of a 
batch at their addresses. A reader that is closed before the end of its stream 
could not tell whether a thread still reads the batch that it returned last, so 
it always left the buffers of that batch to a Cleaner, which frees them once 
the reader and the batch are unreachable, at a garbage collection that this 
off-heap memory does not trigger.
   
   Under `DataSourceRDD`, this is every scan whose task ends before the stream 
does: `LIMIT`, `show()`, `take()`, and failed or killed tasks. Each holds up to 
one batch per partition until a garbage collection, as raised in the review of 
SPARK-60753 (https://github.com/apache/spark/pull/59961#discussion_r4500198812).
   
   Only three operators of Spark read the input of a task on a thread that the 
task does not wait for. With this change, only the tasks that run one of them 
keep the batch until a garbage collection.
   
   This is a smaller alternative to #60641 (SPARK-61195), which makes these 
operators stop their threads and wait for them, so that the Cleaner can be 
removed. That changes how the three operators shut down, and can follow later.
   
   SPARK-60753 is in branch-4.4, so without this backport, 4.4.0 keeps one 
batch per partition of every native scan that stops early until a garbage 
collection.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. Native data sources are not released yet (4.4.0).
   
   One thing changes for code outside of Spark that reads the batches of a 
native scan on a thread of its own, without waiting for that thread when the 
task completes, such as an operator of an extension, or a function of 
`mapPartitions` that hands its iterator to a thread. Before, a batch that such 
a thread still read was kept until it was unreachable. Now it is freed when the 
task completes, unless the thread is registered with 
`TaskInputThreads.registerUnawaited`, which is `private[spark]`. Such a thread 
then reads freed memory.
   
   ### How was this patch tested?
   
   On this branch, locally on Linux with g++, with sbt on Temurin 25.0.2 so 
that the native data source tests run (they need Java 22 or later). The code 
still compiles with `-release 17`. The Java version check of the build, which 
requires 25.0.3 or later for Java 25, was bypassed (`set every 
TaskKey[Unit]("checkJavaVersion") := {}`), with every JVM on the parallel 
collector (`JAVA_TOOL_OPTIONS=-XX:+UseParallelGC`), and the 
`snowflake-jdbc-thin` test dependency, which the local Maven mirror does not 
serve, was excluded:
   
   - `build/sbt 'core/testOnly org.apache.spark.rdd.PipedRDDSuite'`: 13 tests 
passed.
   - `build/sbt 'sql/testOnly *NativeDataSourceSuite 
*SparkScriptTransformationSuite'`: 125 tests passed (90 and 35), and 2 of 
`NativeDataSourceSuite` canceled: `native data sources require Java 22 or 
later`, which only runs on older Java versions, and `Arrow UDFs on the columns 
of a native data source`, which needs pandas.
   - `build/sbt -Phive 'hive/testOnly *HiveScriptTransformationSuite'`: 49 
tests passed.
   
   These runs include the five new `SPARK-62031` tests: one in `PipedRDDSuite`, 
two in `NativeDataSourceSuite`, and one in each script transformation suite.
   
   See #60678 for the tests that it adds. With `PipedRDD.scala`, 
`BaseScriptTransformationExec.scala` and `NativeDataSource.scala` as before 
#60678, its four new tests fail.
   
   Not tested: the registration in the R runner, whose tests need R.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   
   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