HeartSaVioR opened a new pull request, #60674:
URL: https://github.com/apache/spark/pull/60674

   ### What changes were proposed in this pull request?
   
   This PR lets the Python workers of a session with artifact isolation, such 
as a Spark Connect session, import the Python files that the application was 
started with (`--py-files` or `spark.submit.pyFiles`).
   
   - `PythonWorkerFactory` adds these files to the `PYTHONPATH` of the workers 
it starts for an isolated session, that is, for a `SPARK_JOB_ARTIFACT_UUID` 
other than `default`. A `.py` file is reached through the default files 
directory, and a `.zip` or `.egg` file through its own path.
   - The worker adds the session's own files in front of `PYTHONPATH`, as 
before. So on a name clash, the session's file wins.
   - Only the files that are in the default files directory 
(`SparkFiles.getRootDirectory()`) are added. spark-submit and `SparkContext` 
put them there. On YARN they are not there, because YARN puts them on 
`PYTHONPATH` itself, so nothing is added twice.
   - This covers the workers on the executors and the Python workers on the 
driver that use the session's folder, such as the planner workers of Python 
data sources and Python UDTFs.
   - A new internal config, 
`spark.python.isolatedSession.globalPyFiles.enabled` (default `true`), turns 
this off.
   
   Files given with `--files` are not changed.
   
   ### Why are the changes needed?
   
   With artifact isolation on, the Python workers of a session use `<files 
root>/<session UUID>` as their files directory, and get only the session's own 
Python files. The application's Python files are in the default files directory 
instead, so a Python UDF in a Spark Connect session can't import them. The same 
UDF works on YARN, which puts these files on `PYTHONPATH`.
   
   ```python
   # Spark Connect server started with:
   #   bin/spark-submit --class 
org.apache.spark.sql.connect.service.SparkConnectServer \
   #     --master "local-cluster[1,1,1024]" --py-files /tmp/pyfiles/pyfmod.py 
spark-internal
   
   @udf("string")
   def f(x):
       import pyfmod
       return pyfmod.value
   
   spark.range(1).select(f("id")).collect()
   # Before: ModuleNotFoundError: No module named 'pyfmod'
   ```
   
   The JVM side already shares the application's dependencies with isolated 
sessions: SPARK-51537 makes an isolated session's class loader on the executors 
inherit the default session's class loader, so that the global `--jars` stay 
visible. This PR does the same for the application's Python files.
   
   This is related to SPARK-61959 (#60657). With that change, streaming Python 
UDFs also run in the stream session's folder, and this PR keeps the 
application's Python files importable for them.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. With artifact isolation on (the default in Spark Connect), Python UDFs, 
Python UDTFs and Python data sources can now import the Python files given with 
`--py-files` or `spark.submit.pyFiles`, outside YARN. Before, they failed with 
`ModuleNotFoundError`. The session's own Python files still come first.
   
   Setting `spark.python.isolatedSession.globalPyFiles.enabled` to `false` 
restores the previous behavior. Sessions without artifact isolation, such as 
PySpark Classic, don't change.
   
   ### How was this patch tested?
   
   New tests:
   
   - `PythonGlobalPyFilesSuite`, with Spark started with `spark.files` and 
`spark.submit.pyFiles` set, as spark-submit does: a Python UDTF in an isolated 
session imports a global `.py` module and a global zip package, both in 
`analyze` (a driver-side worker) and in `eval` (on an executor); a session's 
own Python file wins over a global one with the same name; with the config off, 
the import fails again.
   - `test_artifact.py`, with Spark Connect locally and on a local cluster, and 
with Spark Classic: a batch and a streaming Python UDF import the application's 
Python files; with Spark Connect, a session's own Python file wins over a 
global one with the same name.
   
   Without the change, the new batch and name-clash tests with isolation on 
fail with `ModuleNotFoundError`. If the config is ignored, the config test 
fails. The streaming UDF test and the Spark Classic tests pass either way: on 
master, streaming Python UDFs don't use the session's folder yet (SPARK-61959 
changes that), and Spark Classic has no isolation. They check that these cases 
keep working.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5
   
   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]

Reply via email to