spark-test commented on code in PR #60674:
URL: https://github.com/apache/spark/pull/60674#discussion_r4629560050
##########
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:
**Nit (P3):** Read literally, 'also import the Python files ...: the modules
and the .zip, .egg and .jar archives among them' says that every such worker
now imports each listed module, and the archives themselves, so a user checking
whether the upgrade affects them could expect each listed module's import-time
code to run in these workers. The change only makes the files importable:
setup_spark_files adds their paths to sys.path
(python/pyspark/worker_util.py:217-220), and the setting's doc for the same
change says 'can also import'.
```suggestion
- 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, 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, 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 p
lace of 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.)
```
**Verification:**
- **Inspection:** The core migration guide's entry for the change says the
Python workers that run in the folder of a session can also import the Python
files the application was started with, and nothing in the entry says that they
import those files or archives themselves.
##########
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:
**Nit (P3):** This stands in for SparkEnv.stop by calling the clear itself,
and no other test looks at the copy directory after a SparkContext stops. From
reading the tests, removing the call at
core/src/main/scala/org/apache/spark/SparkEnv.scala:295 would leave every test
green while each stopped SparkContext in a long-running driver JVM again leaves
its copy directory behind until the JVM exits; your reply in the earlier thread
on the directory's lifetime notes that this call is not under test. A test that
starts its own SparkContext, creates the directory with
PythonWorkerFactory.globalPythonModulesDirectory(sc.getConf, Seq(module)),
calls sc.stop() and asserts that the directory no longer exists would pin it
without starting a Python worker. It cannot live in this suite:
SharedSparkContext keeps one context active for every test, and a second
SparkContext fails to start while it is. A suite in this package that mixes in
LocalSparkContext, such as PythonRDDSuite, can hold it.
This extends an [existing
discussion](https://github.com/apache/spark/pull/60674#discussion_r4626790939).
See **Shared repair plan 1** in the review body.
##########
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:
**Nit (P3):** The sentence about the workers started without the session's
artifact state, such as the foreachBatch and streaming listener workers, comes
right before 'each of these workers' at line 111 and 'where these workers can
only import the session's own Python files' at line 114, so both read as those
workers. The doc then says that they pay the archive-index cost and get their
previous behavior back when the setting is off, although they never get these
files. Moving that sentence to the end of the doc makes both refer back to 'The
workers' of an isolated session; the reflowed lines keep the rest of the text
as it is:
```suggestion
"session's own files still come first. 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. Workers started without the
session's " +
"artifact state, such as the Spark Connect foreachBatch and
streaming listener " +
"workers, keep their previous path.")
```
This extends an [existing
discussion](https://github.com/apache/spark/pull/60674#discussion_r4626790930).
**Verification:**
- **Inspection:** In the doc of
spark.python.isolatedSession.globalPyFiles.enabled, the sentences about reading
the index of every listed archive and about restoring the previous behavior
refer to the workers that run in an isolated session's folder, and the sentence
about the foreachBatch and streaming listener workers no longer comes between
them and that description.
##########
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:
**Non-blocking (P2):** This delete runs after isStopped is set, so if it
throws, nothing after it in this method ever runs: the map output tracker, the
shuffle managers, the block manager, the metrics system and the RPC env stay
up, and the driver's temp dir stays on disk. Utils.deleteRecursively throws an
IOException when both rm -rf and the Java fallback fail, and rethrows an
InterruptedException while it waits for rm -rf. Reading the code, the driver's
SparkContext.stop only logs the IOException and goes on, leaving those
components running in a JVM that can start another SparkContext, while an
InterruptedException escapes SparkContext.stop; an executor exits through its
uncaught-exception handler instead of stopping cleanly. It needs a copy
directory, which only a worker factory for an isolated session creates, and a
delete that fails, so it is rare. Guarding the call the way this method guards
the driver's temp-dir deletion, catching Exception rather than only NonFatal
errors
so that an interrupted delete is covered too, keeps the teardown going;
clearGlobalPythonModulesDirectory
(core/src/main/scala/org/apache/spark/api/python/PythonWorkerFactory.scala:639-642)
should also reset its cache entry in a finally block.
```suggestion
try {
PythonWorkerFactory.clearGlobalPythonModulesDirectory()
} catch {
case e: Exception =>
logWarning("Exception while deleting the copies of the
application's Python modules", e)
}
```
This extends an [existing
discussion](https://github.com/apache/spark/pull/60674#discussion_r4626790939).
See **Shared repair plan 1** in the review body.
--
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]