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


##########
core/src/main/scala/org/apache/spark/api/python/PythonWorkerFactory.scala:
##########
@@ -147,7 +148,32 @@ private[spark] class PythonWorkerFactory(
   private val pythonPath = PythonUtils.mergePythonPaths(
     PythonUtils.sparkPythonPath,
     envVars.getOrElse("PYTHONPATH", ""),
-    sys.env.getOrElse("PYTHONPATH", ""))
+    sys.env.getOrElse("PYTHONPATH", ""),
+    globalPythonFilesPath)
+
+  /**
+   * SPARK-62000: The Python path of the Python files that the application was 
started with, for
+   * the workers of a session with artifact isolation, like the global JARs 
that the class loader
+   * of such a session inherits (SPARK-51537). The workers add the session's 
own files in front
+   * of it. Only the files in the default files directory are used: 
spark-submit and
+   * `SparkContext` put them there, except on YARN, which puts them on 
PYTHONPATH instead.
+   */
+  private def globalPythonFilesPath: String = {
+    val jobArtifactUUID = envVars.getOrElse("SPARK_JOB_ARTIFACT_UUID", 
"default")
+    if (jobArtifactUUID == "default" ||
+        !conf.get(PYTHON_ISOLATED_SESSION_GLOBAL_PY_FILES_ENABLED)) {
+      ""
+    } else {
+      val rootDir = new File(SparkFiles.getRootDirectory()).getAbsoluteFile
+      val files = conf.get(SUBMIT_PYTHON_FILES)
+        .map(path => new File(rootDir, new 
File(Utils.resolveURI(path).getPath).getName))
+        .filter(_.isFile)
+      val (modules, packages) = files.partition(_.getName.endsWith(".py"))
+      // A module is imported from its directory, and a package from its 
archive.
+      PythonUtils.mergePythonPaths(
+        packages.map(_.getPath) ++ modules.headOption.map(_ => 
rootDir.getPath): _*)

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, this was a real problem. I confirmed it: with a `token.py` in 
`spark.files`, the workers of an isolated session failed at start-up.
   
   Fixed in 1fe984f86f9, along the lines you suggested:
   - `PythonWorkerFactory` now copies the application's `.py` files, as found 
by name in the default files directory, into a directory under the local 
directory. That directory holds only these files, like `__pyfiles__` on YARN, 
and is created once for each set of modules.
   - It adds that directory to `PYTHONPATH` instead of the default files 
directory. The archives are still added by their own paths.
   - The order is the same as before: the session's own files first, then 
Spark's paths and any user `PYTHONPATH`, then these files.
   - The directory is created before the monitor thread starts, and a failure 
fails the factory.
   
   Tests:
   - `PythonGlobalPyFilesSuite` now also puts a module that isn't in 
`spark.submit.pyFiles` into `spark.files`. Both the driver-side `analyze` and 
the executor-side `eval` check that it can't be imported.
   - It also puts a `token.py` that raises when imported into `spark.files`.
   - If the whole default files directory is put on the path again, the tests 
fail with that `token.py` error.
   



##########
python/pyspark/sql/tests/connect/client/test_artifact_localcluster.py:
##########
@@ -37,6 +41,20 @@ def master(cls):
         return os.environ.get("SPARK_CONNECT_TESTING_REMOTE", 
"local-cluster[2,2,512]")
 
 
+class LocalClusterGlobalPyFilesTests(

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 1fe984f86f9: `LocalClusterGlobalPyFilesTests` now has the 
same `@unittest.skipIf(is_remote_only(), "Requires JVM access")` as 
`GlobalPyFilesTests`, with the imports it needs.
   



##########
core/src/main/scala/org/apache/spark/api/python/PythonWorkerFactory.scala:
##########
@@ -147,7 +148,32 @@ private[spark] class PythonWorkerFactory(
   private val pythonPath = PythonUtils.mergePythonPaths(
     PythonUtils.sparkPythonPath,
     envVars.getOrElse("PYTHONPATH", ""),
-    sys.env.getOrElse("PYTHONPATH", ""))
+    sys.env.getOrElse("PYTHONPATH", ""),
+    globalPythonFilesPath)

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 1fe984f86f9. The Python path is now computed before `new 
MonitorThread().start()`, so a failure there, such as an invalid value of the 
config, fails the constructor before the thread exists.
   



##########
core/src/main/scala/org/apache/spark/api/python/PythonWorkerFactory.scala:
##########
@@ -147,7 +148,32 @@ private[spark] class PythonWorkerFactory(
   private val pythonPath = PythonUtils.mergePythonPaths(
     PythonUtils.sparkPythonPath,
     envVars.getOrElse("PYTHONPATH", ""),
-    sys.env.getOrElse("PYTHONPATH", ""))
+    sys.env.getOrElse("PYTHONPATH", ""),
+    globalPythonFilesPath)
+
+  /**
+   * SPARK-62000: The Python path of the Python files that the application was 
started with, for
+   * the workers of a session with artifact isolation, like the global JARs 
that the class loader
+   * of such a session inherits (SPARK-51537). The workers add the session's 
own files in front
+   * of it. Only the files in the default files directory are used: 
spark-submit and
+   * `SparkContext` put them there, except on YARN, which puts them on 
PYTHONPATH instead.

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 1fe984f86f9. The Scaladoc and the config's doc no longer 
say that YARN keeps these files out of the default files directory. They now 
say that YARN puts the files on the `PYTHONPATH` of its application master and 
executors only, not of a client-mode driver.
   



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