gaogaotiantian commented on code in PR #60108:
URL: https://github.com/apache/spark/pull/60108#discussion_r4474859300
##########
python/pyspark/tests/test_worker.py:
##########
@@ -413,6 +417,72 @@ def fail(_, it):
self.assertIn("data error",
outfile.read(read_int(outfile)).decode("utf-8"))
+class InvalidateImportCachesTests(unittest.TestCase):
+ def setUp(self):
+ temp_dir = tempfile.TemporaryDirectory()
+ self.addCleanup(temp_dir.cleanup)
+ self.temp_dir = temp_dir.name
+
+ def _write_zip(self, path, modules):
+ with zipfile.ZipFile(path, "w") as zf:
+ for name, source in modules.items():
+ zf.writestr(name, source)
+
+ def _add_zip(self, archive_name, modules):
+ path = os.path.join(self.temp_dir, archive_name)
+ self._write_zip(path, modules)
+ sys.path.insert(0, path)
+
+ def cleanup():
+ sys.path.remove(path)
+ for key in [k for k in sys.path_importer_cache if
k.startswith(path)]:
+ del sys.path_importer_cache[key]
+ zipimport._zip_directory_cache.pop(path, None)
+
+ self.addCleanup(cleanup)
+ return path
+
+ def _import(self, name):
+ self.addCleanup(sys.modules.pop, name, None)
+ return importlib.import_module(name)
+
+ def test_startup_archives_are_not_invalidated(self):
+ startup = self._add_zip(
+ "startup.zip",
+ {"_spark_startup_pkg/__init__.py": "",
"_spark_startup_pkg/mod.py": "x = 1"},
+ )
+ added = self._add_zip("added.zip", {"_spark_added_mod.py": "x = 1"})
+ self.addCleanup(sys.modules.pop, "_spark_startup_pkg", None)
+ self._import("_spark_startup_pkg.mod")
+ self._import("_spark_added_mod")
+ startup_finders = {
+ k: v for k, v in sys.path_importer_cache.items() if
k.startswith(startup)
+ }
+ # The archive itself and the package inside it each have a zipimporter.
+ self.assertEqual(len(startup_finders), 2)
+
+ with (
+ patch.object(worker_util, "_startup_sys_path",
frozenset([startup])),
+ patch.object(zipimport.zipimporter, "invalidate_caches",
autospec=True) as invalidate,
+ ):
+ worker_util._invalidate_import_caches()
Review Comment:
Follow-up: in c624fc324f7 I renamed
`test_startup_archive_import_caches_are_kept` to
`test_spark_archive_import_caches_are_kept`. With the kept set now limited to
Spark's own archives, py4j's zip is kept because it's the archive py4j was
loaded from, not because it was on the startup path. The test itself is
unchanged. I also fixed the `_spark_archives` comment to cover the copies Spark
ships to YARN containers.
--
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]