HeartSaVioR commented on code in PR #60657:
URL: https://github.com/apache/spark/pull/60657#discussion_r4620155681
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -5560,6 +5560,21 @@ object SQLConf {
.booleanConf
.createWithDefault(true)
+ val STREAMING_PROPAGATE_ARTIFACT_STATE_ENABLED =
Review Comment:
> [!NOTE]
> **DISCLAIMER:** This comment was written by an LLM: Claude Opus 5.5
(`claude-opus-5-5`, 1M context), running as an SP2K agent on behalf of
@HeartSaVioR.
Thanks, added in cedfc451d96. `docs/streaming/ss-migration-guide.md` now has
an entry under "Upgrading from Structured Streaming 4.3 to 4.4".
The entry says:
- with artifact isolation on, streaming Python code and code on the stream
thread use the stream session's artifacts;
- they no longer see files that are only in the default context, such as
`--files` or `spark.files` entries, or files added with `SparkContext.addFile`;
- Python files given with `--py-files` or `spark.submit.pyFiles` are handled
by [SPARK-62000](https://issues.apache.org/jira/browse/SPARK-62000);
- `spark.sql.streaming.propagateArtifactState.enabled=false` restores the
previous behavior.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -5560,6 +5560,21 @@ object SQLConf {
.booleanConf
.createWithDefault(true)
+ val STREAMING_PROPAGATE_ARTIFACT_STATE_ENABLED =
+ buildConf("spark.sql.streaming.propagateArtifactState.enabled")
+ .internal()
+ .doc("When true, a streaming query runs its stream thread with the
artifact state of its " +
+ "own session, a clone of the session that started it. Its Python UDFs
and the readers " +
+ "of its Python data sources, on the executors and on the driver, then
see the files " +
+ "and Python files of that session, as in a batch query of the session.
This only " +
+ "matters when artifact isolation is on. Set to false to restore the
previous " +
+ "behavior, where these use the default files directory of the Spark
context, or a " +
+ "fixed directory for the driver-side reader of a Python data source.")
Review Comment:
> [!NOTE]
> **DISCLAIMER:** This comment was written by an LLM: Claude Opus 5.5
(`claude-opus-5-5`, 1M context), running as an SP2K agent on behalf of
@HeartSaVioR.
Thanks, updated in cedfc451d96 along the lines of your suggestion. The doc
now names everything the config moves: Python UDFs and other Python functions,
the readers of Python data sources on the executors and the driver, their
writers on the executors, and other code on the stream thread outside the
execution of a batch. It also says that `false` restores the previous behavior
for all of them.
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/python/streaming/PythonStreamingDataSourceSuite.scala:
##########
@@ -588,6 +639,119 @@ class PythonStreamingDataSourceSuite extends
PythonDataSourceSuiteBase {
q.awaitTermination()
}
+ /**
+ * Reads a file of a new session with the executor-side reader of a
streaming Python data
+ * source, and returns the rows that the query wrote.
+ */
+ private def readSessionFileWithStreamReader(propagateArtifactState:
Boolean): Seq[Row] = {
+ val fileName = "stream_reader_session_file.txt"
+ val sourceName = "SessionFileDataSource"
+ val dataSourceScript =
+ s"""
+ |from pyspark.sql.datasource import DataSource,
DataSourceStreamReader, InputPartition
+ |
+ |class FileStreamReader(DataSourceStreamReader):
+ | def initialOffset(self):
+ | return {"offset": 0}
+ | def latestOffset(self):
+ | return {"offset": 1}
+ | def partitions(self, start: dict, end: dict):
+ | return [InputPartition(i) for i in range(start["offset"],
end["offset"])]
+ | def commit(self, end: dict):
+ | pass
+ | def read(self, partition):
+ | from pyspark import SparkFiles
+ | with open(SparkFiles.get("$fileName")) as f:
+ | yield (f.read(),)
+ |
+ |class $sourceName(DataSource):
+ | def schema(self) -> str:
+ | return "value STRING"
+ | def streamReader(self, schema):
+ | return FileStreamReader()
+ |""".stripMargin
+ // A new session, so that its file is not visible to the other tests.
+ val session = spark.newSession()
+ session.conf.set(
+ SQLConf.STREAMING_PROPAGATE_ARTIFACT_STATE_ENABLED.key,
propagateArtifactState.toString)
+ var rows: Seq[Row] = Nil
+ withTempDir { dir =>
+ val file = new File(dir, fileName)
+ Files.writeString(file.toPath, "session file")
+ session.artifactManager.addArtifact(Paths.get(s"files/$fileName"),
file.toPath, None)
+ session.dataSource.registerPython(
+ sourceName, createUserDefinedPythonDataSource(sourceName,
dataSourceScript))
+ val q = session.readStream.format(sourceName).load()
+
.writeStream.format("memory").queryName("stream_reader_session_file").start()
+ try {
+ q.processAllAvailable()
+ } finally {
+ q.stop()
+ }
+ rows = session.table("stream_reader_session_file").collect().toSeq
+ }
+ rows
+ }
+
+ test("SPARK-61959: Stream reader reads the files of the session") {
+ assume(shouldTestPandasUDFs)
+ assert(readSessionFileWithStreamReader(propagateArtifactState = true) ===
+ Seq(Row("session file")))
+ }
+
+ test("SPARK-61959: Stream reader misses the files of the session without
propagation") {
+ assume(shouldTestPandasUDFs)
+ // The previous behavior: the reader looks in the default files directory.
+ val e = intercept[StreamingQueryException] {
+ readSessionFileWithStreamReader(propagateArtifactState = false)
+ }
+ assert(e.getMessage.contains("FileNotFoundError"), e.getMessage)
+ }
+
+ test("SPARK-61959: Driver-side worker of a stream reader uses the current
artifact state") {
+ assume(shouldTestPandasUDFs)
+ val dataSourceScript =
+ s"""
+ |from pyspark.sql.datasource import DataSource, DataSourceStreamReader
+ |
+ |class RootDirectoryStreamReader(DataSourceStreamReader):
+ | def initialOffset(self):
+ | from pyspark import SparkFiles
+ | return {"root": SparkFiles.getRootDirectory()}
Review Comment:
> [!NOTE]
> **DISCLAIMER:** This comment was written by an LLM: Claude Opus 5.5
(`claude-opus-5-5`, 1M context), running as an SP2K agent on behalf of
@HeartSaVioR.
Thanks, added in cedfc451d96. The driver-side worker test now also returns
`os.getcwd()`. It checks that the working directory ends with the state's UUID
when the state is propagated, and doesn't when the config is off. Without the
`SPARK_JOB_ARTIFACT_UUID` line in `PythonStreamingSourceRunner`, the test now
fails.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/StreamExecution.scala:
##########
@@ -233,7 +230,18 @@ abstract class StreamExecution(
// To fix call site like "run at <unknown>:0", we bridge the call site
from the caller
// thread to this micro batch thread
sparkSession.sparkContext.setCallSite(callSite)
- runStream()
+ // The batches run in `sparkSessionForStream`, a clone of the session
with its own copy
+ // of the artifacts. `SQLExecution` applies its artifact state only to
run the jobs, but
+ // the batches are planned before that, and some plan nodes keep the
state they are built
+ // with, for example the Python UDFs.
Review Comment:
> [!NOTE]
> **DISCLAIMER:** This comment was written by an LLM: Claude Opus 5.5
(`claude-opus-5-5`, 1M context), running as an SP2K agent on behalf of
@HeartSaVioR.
Thanks, added a `TODO(SPARK-61997)` to that comment in cedfc451d96. It says
that Spark Connect builds the Python foreach writer under the artifact state of
the session that starts the query, so its worker doesn't see the stream
session's files yet.
--
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]