HeartSaVioR commented on code in PR #60674:
URL: https://github.com/apache/spark/pull/60674#discussion_r4628552997
##########
docs/core-migration-guide.md:
##########
@@ -22,6 +22,10 @@ license: |
* Table of contents
{:toc}
+## Upgrading from Core 4.4 to 4.5
+
+- Since Spark 4.5, with artifact isolation on (the default in Spark Connect),
the Python workers that run in the folder of a session, such as those of Python
UDFs, UDTFs and data sources, also import the Python files that the application
was started with (`--py-files` or `spark.submit.pyFiles`): the modules and the
`.zip`, `.egg` and `.jar` archives among them, when they have a copy in the
default files directory. spark-submit makes these copies in client mode outside
YARN, and PySpark does when it starts the driver; a JVM driver on YARN in
client mode and an application in standalone cluster deploy mode get none. The
workers add these files when they set up a task, after the session's own Python
files and before the standard library and installed packages, as the workers of
a session without isolation add the default files directory. Before, they could
import these files only on YARN's application master and executors. As a
result, a module from these files can now take the place o
f an installed package with the same name. To restore the previous behavior,
set `spark.python.isolatedSession.globalPyFiles.enabled` to `false` when
starting the application. (See
[SPARK-62000](https://issues.apache.org/jira/browse/SPARK-62000) for more
details.)
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 779c3d152d9. The migration note and the config doc now say
that the listed archives include the jars that `--packages` resolves for
applications such as the Spark Connect server. They say that each of these
workers reads the index of every listed archive, so large archives make its
first tasks slower and use memory. They also say that setting
`spark.python.isolatedSession.globalPyFiles.enabled` to `false` when starting
the application avoids it.
##########
core/src/main/scala/org/apache/spark/api/python/PythonWorkerFactory.scala:
##########
@@ -571,4 +622,28 @@ 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
+
+ /**
+ * 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 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")
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 779c3d152d9 along the lines of your plan:
- `PythonWorkerFactory.clearGlobalPythonModulesDirectory()` deletes the
cached copy directory and forgets the cache entry, under the object's lock.
- `SparkEnv.stop` calls it right after the Python worker factories stop, so
no worker still imports from it.
- The cache keeps its module-path and directory checks.
- A new `PythonWorkerFactorySuite` test checks that:
- the same modules share their copies;
- other modules get a new directory;
- a removed directory is made again;
- clearing deletes the directory.
Mutation checks:
- Without the module-path check, the directory check, or the deletion, that
test fails.
- I didn't add a test that stops and restarts a SparkContext, so the call
from `SparkEnv.stop` itself is not under test.
##########
core/src/main/scala/org/apache/spark/api/python/PythonWorkerFactory.scala:
##########
@@ -139,16 +141,59 @@ 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 `.zip`, `.egg` and
`.jar` archives, as
+ * PySpark does, not the rest of that directory.
+ */
+ 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
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 779c3d152d9. The factory now leaves out a listed archive
whose canonical path matches an absolute entry of the start-time `PYTHONPATH`
it builds. So on YARN executors each worker has the archive on `sys.path` once,
through YARN's entry.
A new test in `PythonGlobalPyFilesSuite` starts the workers with a
`PYTHONPATH` that names the archive's copy through a symlinked directory. It
then checks that `sys.path` has exactly one entry for it after task setup.
Without the check, it finds two.
##########
core/src/main/scala/org/apache/spark/api/python/PythonWorkerFactory.scala:
##########
@@ -139,16 +141,59 @@ 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 `.zip`, `.egg` and
`.jar` archives, as
+ * PySpark does, not the rest of that directory.
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 779c3d152d9 with your wording: the factory 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.
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/python/PythonGlobalPyFilesSuite.scala:
##########
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.python
+
+import java.io.File
+import java.nio.file.{Files, Paths}
+import java.util.zip.{ZipEntry, ZipOutputStream}
+
+import org.apache.spark.SparkConf
+import
org.apache.spark.internal.config.Python.PYTHON_ISOLATED_SESSION_GLOBAL_PY_FILES_ENABLED
+import org.apache.spark.sql.{QueryTest, Row}
+import
org.apache.spark.sql.IntegratedUDFTestUtils.{createUserDefinedPythonTableFunction,
shouldTestPythonUDFs}
+import org.apache.spark.sql.classic.SparkSession
+import org.apache.spark.sql.test.SharedSparkSession
+import org.apache.spark.util.Utils
+
+/**
+ * Tests that the Python workers of a session with artifact isolation, as in
Spark Connect, can
+ * import the Python files that the application was started with
(`spark.submit.pyFiles`).
+ */
+class PythonGlobalPyFilesSuite extends QueryTest with SharedSparkSession {
+
+ private lazy val filesDir = Utils.createTempDir()
+
+ // The application's Python files: a module, a package in a zip file, and a
module whose name
+ // a session can also use.
+ private lazy val globalPyFiles: Seq[String] = {
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 779c3d152d9. The suite now also lists
`global_py_files_egg.egg`, `global_py_files_jar.jar` and
`GLOBAL_PY_FILES_UPPER.ZIP`, each holding its own package, and both `analyze`
and `eval` import them. Without `.egg`, or with a case-sensitive match, the
suite 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]