spark-test commented on code in PR #60674:
URL: https://github.com/apache/spark/pull/60674#discussion_r4638158810
##########
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:
**Nit (P3):** Reading the code, `globalPythonModulesDirectory` creates the
`pyfiles-*` directory with `Utils.createTempDir` (line 658) and records it in
`globalPythonModules` (line 662) only after every copy succeeds. When the
`Files.copy` at line 660 throws, for example on an I/O or out-of-space error,
or because the source vanished while `SparkEnv.stop` deleted the driver's
`userFiles` directory during a late factory build, the directory and any copies
already in it are neither cached nor deleted, and
`clearGlobalPythonModulesDirectory` deletes only the cached directory. The
factory build fails with the copy's error, as intended, but
`SparkEnv.createPythonWorker` caches no factory, so while the failure lasts
every Python worker request of an isolated session on that host builds the
factory again and leaves one more directory and shutdown-hook entry until the
JVM exits. On a driver JVM that outlives its SparkContext, these directories
outlive it too, which the stop-time deletion
is meant to prevent. An earlier thread on repeated entries noted this
leftover; `.distinct` removed that trigger, but any other failed copy still
leaves the directory behind.
When a copy throws, deleting the directory with `Utils.deleteRecursively`,
which also drops its shutdown-hook entry, and then rethrowing the copy's
exception, with any failure of that delete added to it as a suppressed
exception, would fix this. The cache should stay as it was on that path,
because it can still hold the directory of another module list, which
`SparkEnv.stop` deletes.
This extends an [existing
discussion](https://github.com/apache/spark/pull/60674#discussion_r4621897767).
**Verification:**
- **Regression:** Asking for the copy directory of a module list whose
second module does not exist fails with the missing-file error and leaves no
new pyfiles-* directory under Spark's local directory, and a following call
with only existing modules returns a new directory that holds their copies.
- **Regression:** When the copy directory of one module list is cached, a
failed call for another list whose second module does not exist leaves that
directory in place, and the next call for the first list returns the same
directory.
##########
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:
**Nit (P3):** No test covers the side of the `jobArtifactUUID == "default"`
check in `globalPythonFilesPath` that keeps sessions without isolation
unchanged. Every new test that sets `spark.submit.pyFiles` either runs its
Python code in an isolated session, which gets the variable, or runs it with
the default UUID and only checks that the imports succeed: the Spark Classic
`GlobalPyFilesTests`, because PySpark turns session artifact isolation off by
default (`python/pyspark/core/context.py:89`), and the Spark Connect streaming
UDF test, because streaming Python UDFs do not run under the session's artifact
state yet. Those workers import the files from the default files directory
whether or not they also get `SPARK_GLOBAL_PYTHON_PATH`, so, from reading the
tests, dropping that check would keep every test green. Each PySpark Classic
worker on a standalone or Kubernetes executor, whose files root is `.`, would
then have every listed archive on `sys.path` twice, as `./name.zip` from t
he task's includes and as `<cwd>/./name.zip` from the new variable, and would
read its index twice; each such JVM would also copy the listed modules.
A case in the Spark Classic `GlobalPyFilesTests`
(`python/pyspark/sql/tests/test_artifact.py`) whose UDF checks that
`SPARK_GLOBAL_PYTHON_PATH` is unset while a listed module still imports would
pin it.
**Verification:**
- **Regression:** With the application listing Python files, a Python
function run for a session without an artifact state imports a listed module
and finds SPARK_GLOBAL_PYTHON_PATH unset in its worker's environment, and the
case fails when the worker factory also computes the application's Python path
for the default artifact UUID.
--
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]