gengliangwang opened a new pull request, #60640:
URL: https://github.com/apache/spark/pull/60640
### What changes were proposed in this pull request?
This PR makes `SparkDsTaskContext.is_interrupted` of a read of a native data
source (SPARK-60101) return true in two more cases, so that a library whose
`get_next` runs for long, or blocks, learns that Spark no longer wants its data
and can stop:
1. **Once the task starts to complete**, before its pre-completion listeners
(SPARK-60113) run, rather than only once its completion listeners ran.
- `TaskContext` gets `isCompletionStarted()` (`private[spark]`, abstract;
`BarrierTaskContext` delegates), which returns the existing `completionStarted`
state of `TaskContextImpl`. The field becomes `@volatile`, so the check takes
no lock; it is still set under the lock of the task, at the start of
`markTaskCompleted()`.
- The context of a read (`NativeTask.create(read = true)`) also reports
it: `ended || task.isInterrupted() || task.isCompletionStarted()`. The flag
`ended` and its failure and completion listeners stay: they cover a failed
task, and a task whose `TaskContext` was garbage collected.
2. **Once Spark closes the reader while another thread is in `get_next`**,
also outside of a task and before the task ends.
`NativePartitionReader.close()` already leaves the release of the stream to the
thread in `next()` (SPARK-60705); it now also calls
`NativeLibrary.Task.stop()`, a volatile flag of the context of that read, which
`is_interrupted` reports.
The contexts of data writers are unchanged: a data writer only runs within
its task.
The comments of `spark_datasource.h`, the Native Data Sources guide, and the
docs of the Rust SDK (`TaskContext::is_interrupted`, README) say when
`is_interrupted` returns true for a read. No struct changes: the interface
stays version 1.
### Why are the changes needed?
Spark cannot interrupt native code, so the header asks a read that runs for
long, or blocks, to check `is_interrupted` regularly. SPARK-60705 left this
JIRA to tell a library to stop when its reader is closed during `get_next`.
SPARK-60726 then covered most of it: since then, `is_interrupted` of a read
returns true once the task was killed, failed or completed. Two gaps remain,
which this PR closes:
- **The pre-completion window.** `TaskContextImpl.markTaskCompleted()` runs
the pre-completion listeners before it marks the task completed and runs the
completion listeners. A pre-completion listener may stop a thread that reads
the input of the task and wait for it: today the writer of pipelined Python
UDFs (`spark.python.udf.pipelined.enabled`,
`BasePythonRunner.startPipelinedWriter`), and SPARK-61195 proposes the same for
script transformation, `PipedRDD` and the R runner. If that thread is in
`get_next` of a native data source, the interrupt of the thread does not reach
the library, and `is_interrupted` stays false during the wait, because the
completion listener that sets the flag only runs after it. So a library that
polls `is_interrupted` keeps reading, and the task waits until `get_next`
returns on its own, for example under a `LIMIT` over a source that waits for
data. Another pre-completion listener cannot set the flag reliably: these
listeners run in reverse order of re
gistration, so a waiting listener that was registered after the reader was
created runs first.
- **A reader closed outside of a task, or before its task ends.** Outside of
a task, `is_interrupted` was constantly false, so the `get_next` that a
`close()` on another thread leaves running never learned of it.
### Does this PR introduce _any_ user-facing change?
No. Native data sources are not released yet (4.4.0). For libraries,
`is_interrupted` of a read now also returns true once the task starts to
complete, and once Spark closes its reader during `get_next`; the header and
the guide document it.
### How was this patch tested?
New and updated tests:
- `TaskContextSuite`, "SPARK-60733: a task tells that it started to complete
before it has completed": `isCompletionStarted()` is false before the
completion, true for another thread and for a pre-completion listener while
`isCompleted()` is still false, and true afterwards.
- `NativeDataSourceSuite`:
- "tell a read to stop once its reader is closed": outside of a task, a
thread is in a `get_next` that waits until `is_interrupted` returns true, for
at most a minute. Closing the reader makes it return, and `next()` throws the
error of the read.
- "tell a read that its task completes before the task waits for its
threads": a thread with the `TaskContext` of a task is in such a `get_next`,
and a pre-completion listener waits for that thread. The read stops while the
listener waits, before the task is marked completed.
- "close a reader while another thread reads it" (SPARK-60705) expected
`is_interrupted` to be false after the close, as a check that the context was
not freed. It now expects true, and checks instead that the context is still
open while `get_next` runs and closed once it returned.
Without the changes in `NativeDataSource.scala` (reverted locally, then
restored), these three `NativeDataSourceSuite` tests fail: the first two with
`"timed out" did not equal "interrupted"`, after the library waited for its
minute, and the third with `"false" did not equal "true"`. The
`TaskContextSuite` test needs the accessor to compile; it was not run without
the change.
Run locally on Linux with Temurin 25.0.2 and g++, on this PR's code before
the last rewording of one paragraph of `docs/sql-native-data-sources.md`, which
changes no code. 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 of `sql/core`, which the local Maven mirror does not serve, was
excluded:
- `build/sbt 'core/testOnly *TaskContextSuite'`: 81 tests passed
(`TaskContextSuite` and `BarrierTaskContextSuite`).
- `build/sbt 'sql/testOnly *NativeDataSourceSuite
*NativeDataSourceClusterSuite'`: 92 tests passed, 2 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).
- `core/scalastyle`, `core/Test/scalastyle`, `sql/scalastyle` and
`sql/Test/scalastyle`: 0 errors. `cargo fmt --all -- --check` in
`sql/native/rust` passed (Rust 1.85); the other Rust checks were not run, since
only a doc comment and the README changed there.
- MiMa: `dev/mima` was not run. `build/sbt core/mimaReportBinaryIssues`,
which lacks the excludes that `dev/mima` generates for `private[spark]`
classes, reports `TaskContext.isCompletionStarted`
(`ReversedMissingMethodProblem`) without the new entry in `MimaExcludes.scala`,
and no longer with it. The entry is in `v44excludes`, as native data sources
are new in 4.4.0; the one of `addTaskPreCompletionListener` (SPARK-60113) is in
`v43excludes`.
### 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]