HyukjinKwon opened a new pull request, #461: URL: https://github.com/apache/spark-connect-rust/pull/461
### What changes were proposed in this pull request? [SPARK-61333](https://issues.apache.org/jira/browse/SPARK-61333): a `pyspark.ml` model whose summary keeps a DataFrame of it is released, as PySpark's garbage collector releases it. - The native DataFrame of the Python drop-in (`crates/pyspark-rs/src/dataframe.rs`) gets garbage-collector support, `__traverse__` and `__clear__`: - `__traverse__` reports the Python objects that only this DataFrame keeps alive: the plans of `pyspark.ml` that `DataFrame(plan, session)` keeps. - `__clear__` drops the DataFrame's plan when the collector breaks a cycle. - The core gets two hidden helpers (`crates/spark-connect/src/dataframe.rs`): - `DataFrame::exclusive_relation_owners` returns the owners of the relations built outside the core that this DataFrame alone keeps. Every `Arc` from its plan down to the owner must be held once; an owner that another plan shares is not reported, so the collector never counts a reference twice. - `DataFrame::clear_plan`. - The collector can traverse an object while `object.__new__` is still creating it, because the allocation already tracks it and the instance dictionary is allocated before PyO3 writes the value. So `__traverse__` reads nothing until a flag that the constructor sets is set. ### Why are the changes needed? In the drop-in, a KMeans, BisectingKMeans or GaussianMixture model of `pyspark.ml` was never released on the server: - Every fit leaked one ML cache entry, until the session ended. Past the session's `maxStorageSize`, fits fail with `MLCacheSizeOverflowException`. Its client objects leaked too. - An elbow loop over k=2..5 left 4 models behind; official PySpark 4.3.0 leaves none. The reason: these models keep `model.transform(dataset)` as the dataset of their summary, and its plan keeps the model (`__source_transformer__`). - PySpark's garbage collector frees that cycle, because its DataFrame is a Python object. - The drop-in's native DataFrame kept the Python plan through a Rust owner, which the collector could not see. - Other models (LogisticRegression, LinearSVC, ...) return the training dataset as their summary dataset, so they have no cycle. ### Does this PR introduce _any_ user-facing change? Yes, in the Python client: these models are released on the server once nothing uses them, as in PySpark. A DataFrame that still uses a model (a lazy transform, a DataFrame derived from its summary) keeps it. ### How was this patch tested? - Live (`python/tests/test_ml_e2e.py`): `test_models_whose_summary_keeps_their_transform_are_released_as_pyspark`. - KMeans, BisectingKMeans and GaussianMixture models leave the server's ML cache once deleted. - A model stays cached while a transform or a DataFrame derived from its summary is alive, and they still collect. - It fails without the change. - Offline (`python/tests/test_dropin_offline.py`): `test_cycles_through_a_dataframe_of_a_python_plan_are_collected_as_pyspark`. - With a collection at every allocation (`gc.set_threshold(1, 1, 1)`), cycles through DataFrames of Python plans are freed, and a derived DataFrame keeps its plan alive. - It fails without the change, and crashes the process (SIGSEGV in the collector) without the flag that guards objects still being created. - Rust (`plan_construction_tests`): `exclusive_relation_owners_are_those_only_this_dataframe_keeps`. - The reviewer's probe against official PySpark 4.3.0 now gives the same releases: `kmeans/bisecting/gmm del 0`, elbow loop `left 0`. - A stress run with `gc.set_threshold(1, 1, 1)` over chains of withColumn/filter/join and model fits and transforms released every model and did not crash. - Checks: - lib tests of both crates (717 passed); - the build of all tests (also `wasm-udf`, `datafusion,polars`) without warnings; - `cargo fmt --all --check`, and clippy with no diagnostics on the changed lines; - the offline Python suites (2980 passed, 8 skipped); - the live drop-in, Arrow and ML suites against a local Spark 4.3.0 server (255 passed). ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Opus 5.5 This pull request and its description were written by Isaac. -- 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]
