gaogaotiantian commented on code in PR #60108:
URL: https://github.com/apache/spark/pull/60108#discussion_r4519337861
##########
python/pyspark/worker_util.py:
##########
@@ -68,6 +70,50 @@ def add_path(path: str) -> bool:
return False
+def _zip_archive_of(module_name: str) -> Optional[str]:
+ spec = importlib.util.find_spec(module_name)
+ loader = spec.loader if spec is not None else None
+ return loader.archive if isinstance(loader, zipimport.zipimporter) else
None
+
+
+# The zip archives this worker's pyspark and py4j were loaded from, such as
+# $SPARK_HOME/python/lib/pyspark.zip or the copy that Spark ships to YARN
containers. They are
+# not replaced while an application runs, so _invalidate_import_caches keeps
their cached
+# directory listings. Other archives, including user archives on PYTHONPATH,
may be replaced and
+# are not kept.
+_SPARK_ARCHIVES = frozenset(
+ archive for archive in map(_zip_archive_of, ("pyspark", "py4j")) if
archive is not None
+)
+
+
+def _invalidate_import_caches() -> None:
+ """
+ Like importlib.invalidate_caches(), but keeps the zipimporters of Spark's
own archives
+ (_SPARK_ARCHIVES).
+
+ Up to Python 3.12, invalidating a zipimporter re-reads the archive's whole
directory, and an
+ archive has one zipimporter per package imported from it, so pyspark.zip
is re-read once for
+ every package the worker imported from it. Python 3.13+ re-reads each
archive once on its
+ next lookup. All other archives, such as user archives on PYTHONPATH and
the ones in
+ python_includes, are still invalidated.
+ """
+ kept = {
Review Comment:
Done in fa217e33b9c: `_invalidate_import_caches` now calls
`importlib.invalidate_caches()` and returns right away when `_SPARK_ARCHIVES`
is empty. I checked that path by running the tests with py4j unpacked into a
directory: the set is empty, and a user zip on `sys.path` is still refreshed.
Thanks also for the correction about pip installs, answered in the PR
comment.
##########
python/pyspark/tests/test_worker.py:
##########
@@ -413,6 +419,52 @@ def fail(_, it):
self.assertIn("data error",
outfile.read(read_int(outfile)).decode("utf-8"))
+class SetupSparkFilesImportCacheTests(unittest.TestCase):
+ def setUp(self):
+ temp_dir = tempfile.TemporaryDirectory()
+ self.addCleanup(temp_dir.cleanup)
+ self.temp_dir = temp_dir.name
+ # setup_spark_files changes these, so restore them for later tests.
+ self.addCleanup(sys.path.__setitem__, slice(None), list(sys.path))
+ self.addCleanup(setattr, SparkFiles, "_root_directory",
SparkFiles._root_directory)
+ self.addCleanup(
+ setattr, SparkFiles, "_is_running_on_worker",
SparkFiles._is_running_on_worker
+ )
+
+ def test_spark_archives_are_not_invalidated(self):
+ archives = {
+ module.__loader__.archive
+ for module in (pyspark, py4j)
+ if isinstance(module.__loader__, zipimport.zipimporter)
+ }
+ if not archives:
+ self.skipTest("neither pyspark nor py4j was loaded from a zip
archive")
+
+ with patch.object(zipimport.zipimporter, "invalidate_caches",
autospec=True) as invalidate:
+ worker_util.setup_spark_files(self.temp_dir, [])
+
+ invalidated = {call.args[0].archive for call in
invalidate.call_args_list}
+ self.assertTrue(archives.isdisjoint(invalidated))
+
+ def test_user_archive_changes_are_picked_up(self):
+ user_zip = os.path.join(self.temp_dir, "user.zip")
+ with zipfile.ZipFile(user_zip, "w") as zf:
+ zf.writestr("_spark_user_a.py", "")
+ sys.path.insert(0, user_zip)
Review Comment:
Thanks. That's right: neither test catches a return to keeping every zip on
the startup path. I'd still like to leave this out. We simplified the tests to
check what `setup_spark_files` is for, and the only way to put a zip on the
path at process start is a child process, which is the extra machinery we
removed on purpose. The rule itself is now spelled out in the `_SPARK_ARCHIVES`
comment and the description: archives other than the ones pyspark and py4j were
loaded from, including user zips on `PYTHONPATH`, are refreshed. A change back
to keeping every startup zip would have to contradict that comment, which
review should catch.
--
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]