gengliangwang opened a new pull request, #60678: URL: https://github.com/apache/spark/pull/60678
### What changes were proposed in this pull request? 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. ### 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? New tests: - `NativeDataSourceSuite`: - "SPARK-62031: free the buffers of a reader closed in a task, unless a thread that the task does not wait for reads its input": a reader that is closed in a task before the end of its stream frees the buffers of its batch at once, while the test still references the reader and the batch. With a thread registered for the task, before the reader is created or before it is closed, the batch can still be read after the close. - "SPARK-62031: free the buffers of a scan when its task completes before its end": after a `LIMIT 1` query over a scan of several batches, no buffer of a reader is allocated, without a garbage collection. - `PipedRDDSuite`, "SPARK-62031: tell that the stdin writer thread reads the input of its task": the thread is registered for a task that pipes its input, and not for another task. - `BaseScriptTransformationSuite`, "SPARK-62031: tell that the writer thread reads the input of its task", which runs in `SparkScriptTransformationSuite` and `HiveScriptTransformationSuite`. With `PipedRDD.scala`, `BaseScriptTransformationExec.scala` and `NativeDataSource.scala` as in master, these four tests fail. The existing tests of the deferred free ("close a reader while another thread reads the batch that it returned", "keep the buffers of a closed reader while its batch is held without the reader") create their readers outside of a task, and pass unchanged. Run locally on Linux with Temurin 25.0.2 and g++. 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 *PipedRDDSuite'`: 13 tests passed, on the commit of this PR. - `core/scalastyle`, `core/Test/scalastyle`, `sql/scalastyle` and `sql/Test/scalastyle`: 0 errors, on the commit of this PR. - `build/sbt 'sql/testOnly *SparkScriptTransformationSuite *NativeDataSourceSuite *NativeColumnVectorSuite *NativeDataSourceClusterSuite'`: 138 tests passed (35, 90, 11 and 2), 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. The `sql` and `hive` runs were on this PR's code before the order of two imports of `PipedRDDSuite` was fixed for scalastyle, which changes no other file. Not tested: the registration in the R runner, whose tests need R. `dev/lint-java` was not run; the only Java change is a Javadoc comment. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Claude Fable 5.2) 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]
