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


##########
core/src/main/scala/org/apache/spark/api/python/PythonWorkerFactory.scala:
##########
@@ -571,4 +627,40 @@ private[spark] object PythonWorkerFactory {
   val IDLE_WORKER_TIMEOUT_NS = TimeUnit.MINUTES.toNanos(1)  // kill idle 
workers after 1 minute
 
   private[spark] val defaultDaemonModule = "pyspark.daemon"
+
+  // The directory that holds copies of the application's Python modules, and 
their paths.
+  @GuardedBy("this")
+  private var globalPythonModules: Option[(Seq[String], File)] = None
+
+  /**
+   * Deletes the directory of copies of the application's Python modules, when 
the `SparkEnv`
+   * whose worker factories use it stops, so that it does not outlive its 
`SparkContext`.
+   */
+  private[spark] def clearGlobalPythonModulesDirectory(): Unit = synchronized {
+    try {
+      globalPythonModules.foreach { case (_, dir) => 
Utils.deleteRecursively(dir) }
+    } finally {
+      globalPythonModules = None
+    }
+  }
+
+  /**
+   * Returns a directory under the local directory that holds copies of 
`modules`, the
+   * application's Python modules, and nothing else, like `__pyfiles__` on 
YARN. It is created
+   * once for each set of modules, which does not change while an application 
runs.
+   */
+  private[python] def globalPythonModulesDirectory(conf: SparkConf, modules: 
Seq[File]): File =
+    synchronized {
+      val paths = modules.map(_.getPath)
+      globalPythonModules.collect {
+        case (cachedPaths, dir) if cachedPaths == paths && dir.isDirectory => 
dir
+      }.getOrElse {
+        val dir = Utils.createTempDir(Utils.getLocalDir(conf), "pyfiles")
+        modules.foreach { module =>
+          Files.copy(module.toPath, dir.toPath.resolve(module.getName))

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 6af60766fd7. When a copy throws, 
`globalPythonModulesDirectory` now deletes the `pyfiles-*` directory it just 
created with `Utils.deleteRecursively`. It then rethrows the copy's exception, 
with any failure of that delete added as a suppressed exception. The cache 
stays as it was.
   
   A new `PythonWorkerFactorySuite` test asks for the copies of a module list 
whose second module doesn't exist. It checks that this fails with 
`NoSuchFileException`, leaves no new `pyfiles-*` directory, and keeps the 
cached directory of another list. Without the deletion, the test fails.
   



##########
core/src/main/scala/org/apache/spark/api/python/PythonWorkerFactory.scala:
##########
@@ -139,16 +141,64 @@ private[spark] class PythonWorkerFactory(
     conf.get(PYTHON_FACTORY_IDLE_WORKER_MAX_POOL_SIZE)
   @GuardedBy("self")
   private var lastActivityNs = 0L
-  new MonitorThread().start()
-
-  @GuardedBy("self")
-  private val simpleWorkers = new mutable.WeakHashMap[PythonWorker, Process]()
 
   private val pythonPath = PythonUtils.mergePythonPaths(
     PythonUtils.sparkPythonPath,
     envVars.getOrElse("PYTHONPATH", ""),
     sys.env.getOrElse("PYTHONPATH", ""))
 
+  // Before the monitor thread starts, because it can fail.
+  private val globalPythonPath = globalPythonFilesPath
+
+  new MonitorThread().start()
+
+  @GuardedBy("self")
+  private val simpleWorkers = new mutable.WeakHashMap[PythonWorker, Process]()
+
+  /**
+   * 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 get it in 
`SPARK_GLOBAL_PYTHON_PATH`
+   * and add it when they set up the files of a task, as the workers of a 
session without
+   * isolation add the default files directory and its archives. The session's 
own files still
+   * come first, because the workers start in the session's folder, which 
Python puts first on
+   * sys.path, and add the session's archives in front of these files.
+   * It is not on the PYTHONPATH that the workers start with, so a file named 
like a module of
+   * the standard library cannot stop them from starting.
+   *
+   * It takes the copies of these files in the default files directory, so 
that it works on every
+   * host. spark-submit makes them in client mode outside YARN, and PySpark 
when it starts the
+   * driver. A JVM driver on YARN in client mode and an application in 
standalone cluster deploy
+   * mode get no copies. It adds only the modules and the archives with 
PySpark's extensions,
+   * `.zip`, `.egg` and `.jar`, not the rest of that directory, which PySpark 
puts on its path. It
+   * leaves out an archive that the PYTHONPATH the workers start with already 
names, as YARN's
+   * does on its executors, so that the workers read it once.
+   */
+  private def globalPythonFilesPath: String = {
+    val jobArtifactUUID = envVars.getOrElse("SPARK_JOB_ARTIFACT_UUID", 
"default")
+    if (jobArtifactUUID == "default" ||

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 6af60766fd7. The Spark Classic `GlobalPyFilesTests` now has 
`test_global_py_files_without_artifact_state`, where a UDF imports a listed 
module and finds `SPARK_GLOBAL_PYTHON_PATH` unset in its worker's environment. 
If the factory also computes the application's Python path for the default 
artifact UUID, the 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