HeartSaVioR commented on code in PR #60674:
URL: https://github.com/apache/spark/pull/60674#discussion_r4629737621
##########
core/src/main/scala/org/apache/spark/SparkEnv.scala:
##########
@@ -291,6 +291,8 @@ class SparkEnv (
}
if (shouldStop) {
pythonWorkers.values.foreach(_.stop())
+ // After the factories, whose workers import from it, have stopped.
+ PythonWorkerFactory.clearGlobalPythonModulesDirectory()
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 e1977d65fc0. `SparkEnv.stop` now guards the call the way it
guards the driver's temp-dir deletion: it catches `Exception`, so an
interrupted delete is covered too, and logs a warning.
`clearGlobalPythonModulesDirectory` forgets its cache entry in a `finally`
block, so it does that even when the delete throws.
##########
core/src/main/scala/org/apache/spark/internal/config/Python.scala:
##########
@@ -94,6 +94,31 @@ private[spark] object Python {
.booleanConf
.createWithDefault(false)
+ val PYTHON_ISOLATED_SESSION_GLOBAL_PY_FILES_ENABLED =
+ ConfigBuilder("spark.python.isolatedSession.globalPyFiles.enabled")
+ .internal()
+ .doc("When true, the Python workers that run in the folder of a session
with artifact " +
+ "isolation, such as those of Python UDFs, UDTFs and data sources in a
Spark Connect " +
+ "session, can 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 that 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, and the " +
+ "session's own files still come first. Workers started without the
session's artifact " +
+ "state, such as the Spark Connect foreachBatch and streaming listener
workers, keep " +
+ "their previous path. The archives include the jars that --packages
resolves for " +
+ "applications such as the Spark Connect server, and each of these
workers reads the " +
+ "index of every listed archive, so large archives make its first tasks
slower and use " +
+ "memory. Set to false when starting the application to avoid this and
to restore the " +
+ "previous behavior, where these workers can only import the session's
own Python " +
+ "files, except on YARN's application master and executors, which YARN
gives these " +
+ "files on their PYTHONPATH.")
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 e1977d65fc0 with your reordering. The sentence about the
foreachBatch and streaming listener workers now comes last, so the cost and
rollback sentences refer to the workers of an isolated session.
##########
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. The listed archives include the
jars that `--packages` resolves for applications such as the Spark Connect
server, and each of these workers reads the index of every listed archive, so
large archives make its first tasks slower and use memory. To avoid this or 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 e1977d65fc0: the entry now says that these workers "can
also import" the application's Python files.
##########
core/src/test/scala/org/apache/spark/api/python/PythonWorkerFactorySuite.scala:
##########
@@ -29,11 +31,44 @@ import scala.concurrent.duration._
import org.apache.spark.SharedSparkContext
import org.apache.spark.SparkException
import org.apache.spark.SparkFunSuite
-import org.apache.spark.util.ThreadUtils
+import org.apache.spark.util.{ThreadUtils, Utils}
// Tests for PythonWorkerFactory.
class PythonWorkerFactorySuite extends SparkFunSuite with SharedSparkContext {
+ test("SPARK-62000: Copies of the application's Python modules are cached and
cleared") {
+ val conf = sc.getConf
+ val dir = Utils.createTempDir()
+ def module(name: String): File = {
+ val file = new File(dir, name)
+ Files.writeString(file.toPath, s"value = '$name'")
+ file
+ }
+ val first = module("cache_first.py")
+ val second = module("cache_second.py")
+ try {
+ val copies = PythonWorkerFactory.globalPythonModulesDirectory(conf,
Seq(first))
+ val copy = new File(copies, first.getName)
+ assert(Files.readString(copy.toPath) === "value = 'cache_first.py'")
+ // The same modules share their copies.
+ assert(PythonWorkerFactory.globalPythonModulesDirectory(conf,
Seq(first)) === copies)
+ // Other modules get a directory of their own.
+ val others = PythonWorkerFactory.globalPythonModulesDirectory(conf,
Seq(first, second))
+ assert(others !== copies)
+ assert(new File(others, second.getName).isFile)
+ // A removed directory is made again.
+ Utils.deleteRecursively(others)
+ val again = PythonWorkerFactory.globalPythonModulesDirectory(conf,
Seq(first, second))
+ assert(new File(again, second.getName).isFile)
+ // Clearing, as SparkEnv.stop does, deletes the directory.
+ PythonWorkerFactory.clearGlobalPythonModulesDirectory()
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 e1977d65fc0. `PythonRDDSuite`, which mixes in
`LocalSparkContext`, now has a test that starts its own SparkContext, creates
the directory of copies with
`PythonWorkerFactory.globalPythonModulesDirectory`, stops the context, and
checks that the directory no longer exists. Without the call in
`SparkEnv.stop`, 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]