HeartSaVioR commented on code in PR #60657:
URL: https://github.com/apache/spark/pull/60657#discussion_r4622859941


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -5560,6 +5560,25 @@ 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, so that the 
query uses the " +
+        "artifacts of that session, as a batch query of the session does. This 
covers its " +
+        "Python UDFs and other Python functions on the executors, the readers 
of its Python " +
+        "data sources on the executors and on the driver, the writers of its 
Python data " +
+        "sources on the executors, and other code that runs on the stream 
thread outside the " +
+        "execution of a batch, such as SparkFiles.get or SparkContext.addFile 
in a source or " +
+        "in the onQueryStarted callback of a listener. This only " +

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, fixed in 9574df977fd along the lines of your suggestion. The config 
doc now says that it doesn't cover the Python foreach writer of Spark Connect, 
which keeps the artifacts of the session that starts the query (SPARK-61997).
   



##########
docs/streaming/ss-migration-guide.md:
##########
@@ -23,6 +23,10 @@ Note that this migration guide describes the items specific 
to Structured Stream
 Many items of SQL migration can be applied when migrating Structured Streaming 
to higher versions.
 Please refer [Migration Guide: SQL, Datasets and 
DataFrame](../sql-migration-guide.html).
 
+## Upgrading from Structured Streaming 4.4 to 4.5
+
+- Since Spark 4.5, with artifact isolation on (the default in Spark Connect, 
and in Scala and Java sessions without Hive support, except in spark-shell, 
which turns it off), a streaming query uses the artifacts of its own session, a 
clone of the session that started it, as a batch query of the session does. Its 
Python UDFs and the readers and writers of its Python data sources now see the 
files and Python files that the session added with `addArtifact` or 
`addArtifacts`. As a result, 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` that reach the Python workers only through the default 
context, as for a Spark Connect server started in client mode outside YARN, are 
no longer importable from them either, as already for the batch Python UDFs of 
a Spark Connect session; [SPARK-62000](https://issues.apache.org/jira/browse/SPA
 RK-62000) handles them. Code on the stream thread outside the execution of a 
batch, such as `SparkFiles.get` in a custom source or in the `onQueryStarted` 
callback of a streaming query listener, also resolves to the folder of that 
session; the other callbacks of a listener keep the previous behavior. To 
restore the previous behavior, set 
`spark.sql.streaming.propagateArtifactState.enabled` to `false`. (See 
[SPARK-61959](https://issues.apache.org/jira/browse/SPARK-61959) for more 
details.)

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, fixed in 9574df977fd. The entry now says that 
[SPARK-62000](https://issues.apache.org/jira/browse/SPARK-62000) tracks making 
these files importable again. It also says that until then, they can be added 
to the session with `addArtifacts(..., pyfile=True)`, or the config can be set 
to `false`.
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/runtime/StreamExecution.scala:
##########
@@ -233,7 +230,24 @@ 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.
+        // TODO(SPARK-61997): Spark Connect builds the Python foreach writer 
on the request
+        //  thread, under the artifact state of the session that starts the 
query, so its Python
+        //  worker does not see the files of this stream's session yet.
+        // TODO(SPARK-61998): With artifact isolation on, each run gives its 
Python workers its
+        //  own artifact UUID, so each executor that runs its Python tasks 
keeps one more Python
+        //  worker pool until it stops.

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-62045)` next to the other two in 9574df977fd. It 
names the transformWithState pre-init worker and the Spark Connect foreachBatch 
worker that `StreamingPythonRunner` starts on the driver.
   



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/python/streaming/PythonStreamingDataSourceSuite.scala:
##########
@@ -588,6 +639,128 @@ 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] = {

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 9574df977fd. `PythonStreamingDataSourceWriteSuite` now has 
a test where the streaming writer of a Python data source reads a session file 
through `SparkFiles.get` in `write` and writes its content. It runs with the 
config on, and with the config off, where it fails with `FileNotFoundError` as 
before. Without the stream-thread artifact state, the config-on test fails.
   



-- 
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