spark-test commented on code in PR #60657:
URL: https://github.com/apache/spark/pull/60657#discussion_r4622640880


##########
docs/streaming/ss-migration-guide.md:
##########
@@ -25,6 +25,8 @@ Please refer [Migration Guide: SQL, Datasets and 
DataFrame](../sql-migration-gui
 
 ## Upgrading from Structured Streaming 4.3 to 4.4
 
+- Since Spark 4.4, with artifact isolation on (the default in Spark Connect, 
and in Scala and Java sessions without Hive support), 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` are handled by 
[SPARK-62000](https://issues.apache.org/jira/browse/SPARK-62000). Code on the 
stream thread outside the execution of a batch, such as `SparkFiles.get` in a 
custom source or a listener, also resolves to the folder of that session. To 
restore the previous behavior, set 
`spark.sql.streaming.propagateArtifactState.enabled` t
 o `false`. (See 
[SPARK-61959](https://issues.apache.org/jira/browse/SPARK-61959) for more 
details.)

Review Comment:
   Thanks, the entry now covers spark-shell, the onQueryStarted callback and 
the --py-files case, and the conf's doc names the onQueryStarted callback. This 
review raises one related wording point about how the entry describes 
SPARK-62000.
   
   <!-- SPARK_DEV_REVIEW_REPLY 
{"feedback_id":"inline:4621492430","thread_id":"inline:4621492430","verdict_sha256":"fe4ee7cc51654f92721f14793fab3a91dd31ebae8056fe371dcd8a0dda0e7f11"}
 -->



##########
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:
   **Nit (P3):** "SPARK-62000 handles them" can be read as saying that this 
release already makes these Python files importable again, which contradicts 
the clause just before it, while nothing in this PR implements or names 
SPARK-62000 apart from this entry. A user of a Spark Connect server started in 
client mode outside YARN whose streaming Python code imports such a module 
cannot tell from the entry whether the import still works in 4.5 or whether 
they need to act. Saying that SPARK-62000 tracks making these files importable 
again, and that until then they can be added to the session with 
`addArtifacts(..., pyfile=True)` or the previous behavior restored by setting 
`spark.sql.streaming.propagateArtifactState.enabled` to `false`, would remove 
the ambiguity.
   
   This extends an [existing 
discussion](https://github.com/apache/spark/pull/60657#discussion_r4621492430).
   
   **Verification:**
   - **Inspection:** The 4.4-to-4.5 entry of the Structured Streaming migration 
guide describes SPARK-62000 as tracking the --py-files case rather than 
handling it, and names adding the files to the session as Python file artifacts 
and setting spark.sql.streaming.propagateArtifactState.enabled to false as the 
ways to keep them importable until it is fixed.



##########
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:
   **Nit (P3):** Leaving the StreamingPythonRunner workers to SPARK-62045 is 
fine, but only a review reply says so: this TODO list names SPARK-61997 and 
SPARK-61998, and nothing in the code mentions SPARK-62045. Those workers, the 
transformWithState pre-init worker that the stream thread starts while it plans 
a batch and the Spark Connect foreachBatch worker, still get no artifact UUID 
or files root (StreamingPythonRunner.scala:76-122), so with isolation on a 
stateful processor whose class comes from a session Python file, or a 
foreachBatch function that imports one, still fails with ModuleNotFoundError, 
as you confirmed. Without a marker in the code, a later change could assume 
that all streaming Python code now sees the session's files. A TODO next to the 
other two would mark it, for example:
   
   ```suggestion
           //  worker pool until it stops.
           // TODO(SPARK-62045): The Python workers that StreamingPythonRunner 
starts on the driver,
           //  for the transformWithState pre-init step and for Spark Connect 
foreachBatch, get no
           //  artifact UUID or files root, so they do not see the session's 
files yet.
   ```
   
   This extends an [existing 
discussion](https://github.com/apache/spark/pull/60657#discussion_r4621492437).
   
   **Verification:**
   - **Inspection:** The deferral comments above the stream-thread 
artifact-state wrapper in StreamExecution.scala name SPARK-62045 together with 
the transformWithState pre-init worker and the Spark Connect foreachBatch 
worker that StreamingPythonRunner starts, next to the existing SPARK-61997 and 
SPARK-61998 TODOs, and nothing but comments changes.



##########
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:
   **Nit (P3):** This helper covers the executor-side reader, but nothing 
covers the other Python data source path that the change fixes and documents, 
the streaming writer. It takes its artifact UUID through its own path: V2Writes 
builds PythonStreamingWrite with write.toStreaming while the micro-batch is 
optimized on the stream thread (V2Writes.scala:106), and its constructor 
captures the current state (PythonStreamingWrite.scala:39). The new tests write 
to the memory or noop sink, so reading the code, if the writer stopped getting 
the stream session's UUID and looked in the default files directory again, a 
DataSourceStreamWriter whose write opens a session file would fail with 
FileNotFoundError while every new test still passed. A writer counterpart of 
this helper in PythonStreamingDataSourceWriteSuite, whose write reads a session 
file through SparkFiles.get, run with the conf on and off, would cover it.
   
   **Verification:**
   - **Regression:** With artifact isolation on and 
spark.sql.streaming.propagateArtifactState.enabled at its default, a streaming 
query whose Python data source writer reads a file added to its session through 
SparkFiles.get in write completes and writes the file's content, and the check 
fails if the writer's Python worker gets no artifact UUID and looks in the 
default files directory instead.
   - **Compatibility:** With spark.sql.streaming.propagateArtifactState.enabled 
set to false, the same query fails with FileNotFoundError from the writer, as 
on the base branch.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/python/streaming/PythonStreamingSourceRunner.scala:
##########
@@ -108,6 +108,15 @@ class PythonStreamingSourceRunner(
     envVars.put("SPARK_AUTH_SOCKET_TIMEOUT", authSocketTimeout.toString)
     envVars.put("SPARK_BUFFER_SIZE", bufferSize.toString)
     envVars.put("SPARK_PYTHON_RUNTIME", "PYTHON_WORKER")
+    // The stream thread creates the source under the artifact state of the 
stream's session, so
+    // that this worker sees the files of that session, as its executor-side 
readers do.
+    val jobArtifactUUID =

Review Comment:
   Thanks for confirming and for filing SPARK-62045. Leaving these two workers 
to that follow-up is fine: this PR does not change StreamingPythonRunner, so 
they behave as on master, and giving them the session's files needs changes to 
both the runner and the Python modules it starts. This review raises two small 
related points: nothing in the code marks this deferral yet, and the conf's 
doc, now narrowed to Python functions on the executors, still claims the Spark 
Connect Python foreach writer.
   
   <!-- SPARK_DEV_REVIEW_REPLY 
{"feedback_id":"inline:4621492437","thread_id":"inline:4621492437","verdict_sha256":"fe4ee7cc51654f92721f14793fab3a91dd31ebae8056fe371dcd8a0dda0e7f11"}
 -->



##########
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:
   **Nit (P3):** The narrowed wording still includes the Spark Connect Python 
foreach writer, a Python function of the query that runs on the executors. 
Spark Connect builds it while it handles the writeStream request, under the 
artifact state of the session that starts the query 
(SparkConnectPlanner.scala:3756), and the writer keeps that UUID in a field 
(PythonForeachWriter.scala:88). Reading the code, the conf therefore changes 
nothing for it at true or false; the TODO(SPARK-61997) above the stream-thread 
wrapper already says that its worker does not see the stream session's files. 
Since this doc is the only description of the escape hatch in the code, someone 
checking whether the conf fixes or restores a foreach writer's file lookup 
would conclude that it does. Excluding the writer, with a pointer to 
SPARK-61997, would make the doc accurate, for example:
   
   ```suggestion
           "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. It does not cover the 
Python foreach " +
           "writer of Spark Connect, which keeps the artifacts of the session 
that starts the " +
           "query (SPARK-61997). This only " +
   ```
   
   This extends existing discussions 
[1](https://github.com/apache/spark/pull/60657#discussion_r4616581920), 
[2](https://github.com/apache/spark/pull/60657#discussion_r4621492437).
   
   **Verification:**
   - **Inspection:** The doc of 
spark.sql.streaming.propagateArtifactState.enabled in SQLConf.scala says that 
the Spark Connect Python foreach writer is not covered, with a reference to 
SPARK-61997, and still lists the Python UDFs and other Python functions on the 
executors, 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.



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