HyukjinKwon opened a new pull request, #558:
URL: https://github.com/apache/spark-connect-rust/pull/558
### What changes were proposed in this pull request?
A follow-up of #539 (SPARK-61481), from a review of it. Three of its parts
did not match PySpark 4.3.0.
1. **One stop lock for the process.** #539's `StopLock` was shared by the
clones of one session. PySpark's `stop()` runs under `SparkSession._lock`, one
class-wide `RLock` (pyspark/sql/connect/session.py:122 and :948). So in PySpark
the stops of all sessions run one at a time, and here a second session's
release could overlap the first's.
- The lock is now one static lock of the process
(`crates/spark-connect/src/session.rs`), and the per-session field is gone.
- It stays reentrant on the same thread, and its wait still ends at a
signal in the Python client.
- A hold of it (`StopLockHold`) leaves the entry of the thread that took
it.
2. **`getOrCreate` waits for a stop in progress.** #539 sets `stopped` once
the release has ended, as PySpark's `is_stopped` follows `client.close()`. But
the drop-in's `getOrCreate` did not take the lock that `stop()` holds. So
during a slow release on another thread it returned the session being stopped,
whose next call then raised NO_ACTIVE_SESSION. PySpark's `getOrCreate` holds
`SparkSession._lock` (session.py:256), so it waits for the stop and then
creates a new session.
- The drop-in's `getOrCreate` now enters the stop lock before its lookup,
through `spark_connect::session::enter_stop_lock`, a new public function of the
core. The wait releases the GIL, and a signal ends it.
- The drop-in's `stop()` without a release (`release_session_on_close =
False`) holds the lock too, as PySpark's `stop()` does.
- `getActiveSession` takes no lock, as in PySpark.
3. **A signal ends the join of a listener worker.** Removing the last
listener, `streams.close()`, and an `addListener` that joins an earlier worker
sent the REMOVE and then joined the worker with a blocking `JoinHandle::join`.
While a listener callback ran, a Ctrl-C did nothing until it returned.
PySpark's `remove()` sends the REMOVE, then joins the bus thread
(`self._execution_thread.join()`, a Python join that a signal interrupts on the
main thread), and only then drops the listener.
- The joins now wait for the worker to finish through
`block_on_abortable`.
- On a signal, the worker and the listeners stay in the bus, as PySpark's
bus keeps them, and the call raises `KeyboardInterrupt`. An interrupted add
puts the bus back as it was before the add.
`docs/streaming.md` also says now that `streams.close()` removes every
listener. PySpark's `close()` iterates its list while `remove()` shortens it,
so with listeners `[a, b, c]` it leaves `[b]` registered. The drop-in does not
mirror that.
### Why are the changes needed?
To match PySpark 4.3.0, with the repo's mock server:
- Thread 1 stops a session whose ReleaseSession the mock holds, and thread 2
calls `getOrCreate()` meanwhile. Before, `getOrCreate()` returned the session
being stopped, at once. Now it returns a new, open session after the release,
as PySpark's does.
- `spark.stop()` and `spark.newSession().stop()` on two threads: before,
both ReleaseSessions were in flight at once. Now the second waits for the first.
- A listener whose `onQueryIdle` blocks for 10 s, then `removeListener` (or
`streams.close()`) and a SIGINT at 0.5 s: before, the `KeyboardInterrupt` came
at 10 s. Now it comes at once, as PySpark's at 0.51 s, and the listener stays
registered.
### Does this PR introduce _any_ user-facing change?
Yes, on unreleased changes:
- In both clients, the stops of all sessions run one at a time.
- In the Python client, `getOrCreate` waits for a stop in progress, and
Ctrl-C ends `removeListener`, `streams.close()` and `addListener` while a
listener callback runs.
- The Rust API has a new public function,
`spark_connect::session::enter_stop_lock`, and its type `StopLockHold`.
### How was this patch tested?
Each new test fails without the change and passes with it:
- Rust:
-
`session::tests::stops_of_different_sessions_run_one_at_a_time_as_pyspark`;
-
`streaming::tests::an_interrupted_join_of_a_removed_worker_keeps_the_bus_and_the_worker`.
- Python (`python/tests/test_dropin_offline.py`):
-
`test_get_or_create_waits_for_a_stop_in_progress_and_creates_a_new_session_as_pyspark`;
- `test_stops_of_two_sessions_run_one_at_a_time_as_pyspark`;
-
`test_sigint_during_the_join_of_a_busy_listener_worker_raises_keyboard_interrupt`
(`removeListener` and `close`);
-
`test_sigint_during_the_add_that_joins_a_failing_listener_worker_raises_keyboard_interrupt`.
Also run:
- `cargo test -p apache-spark-connect --release --lib` (624 passed) and
`cargo test -p apache-spark-connect -p apache-spark-connect-core --release
--no-run`;
- the offline drop-in and ML suites (2021 passed);
- against a Spark 4.3.0 server: `e2e_streaming`, the session-stop test of
`e2e_new_methods`, and the listener, stop, stream and interrupt tests of
`test_dropin_e2e.py`;
- `cargo fmt --all -- --check`, `ruff check`, and
`scripts/audit_no_stubs.sh`.
### 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]