HyukjinKwon opened a new pull request, #353:
URL: https://github.com/apache/spark-connect-rust/pull/353

   ### What changes were proposed in this pull request?
   
   [SPARK-61162](https://issues.apache.org/jira/browse/SPARK-61162): PySpark 
Connect's `spark.streams` is one `StreamingQueryManager` of the session, whose 
`StreamingQueryListenerBus` holds the client-side listeners. This PR makes the 
clients' listeners work the same way.
   
   This PR is stacked on #350 (SPARK-61151): its first commit is that PR's, 
which vendors PySpark's `StreamingQueryListener`, whose `_set_spark_session` 
`addListener` now calls. The second commit is this PR's.
   
   **Rust core** (`spark-connect`):
   - The managers of a session (`session.streams()`) share the listener bus of 
the session, rather than each having one of its own.
   - `add_listener` of the first listener returns once the server has added its 
listener (`listener_bus_listener_added`), as PySpark's 
`_register_server_side_listener` waits for it. So a query started right after 
it sends its started event.
   - `DataStreamWriter` posts the started event of a query, which the server 
sends with the result of the start (`query_started_event_json`), to the 
listeners before `start` returns, as `QUERY_STARTED_EVENT`.
   - `remove_listener` of an id of no listener does nothing. For the last 
listener, it asks the server to remove its listener, waits for the events left, 
and should that request fail, keeps the listener and returns the error, as 
PySpark's.
   - `listeners()` gives the listeners, in their order. 
`StreamingQueryListener` gains `as_any`, `None` by default, for a client that 
wraps its listeners.
   - The events are posted to the listeners without any lock held, so a 
listener can add or remove listeners.
   
   **Python client**:
   - `spark.streams` is the same object at each access.
   - `addListener` gives the listener the session (`listener.spark`), as 
PySpark Connect's `_set_spark_session`.
   - A failure to add or remove the listener of the server warns, with 
PySpark's messages, rather than raising.
   - `StreamingQueryManager._sqlb._listener_bus` lists the listeners, as 
PySpark Connect's, which Apache Spark's tests read to remove them.
   - The callbacks get the started events (`onQueryStarted`). An exception of a 
callback warns with PySpark's message.
   
   ### Why are the changes needed?
   
   - `onQueryStarted` was never called: the client ignored 
`query_started_event_json`.
   - Listeners had no session (`self.spark` was `None`), so a listener that 
runs queries failed.
   - A listener added through one `spark.streams` was removed through another 
only because removing from an empty bus asked the server to remove the listener 
of the session, which also cut the other listeners off.
   - `addListener` returned before the server had added its listener, so a 
query started right after could miss its events.
   - `spark.streams == spark.streams` was `False`, and the manager had no 
`_sqlb`.
   
   Six tests of Apache Spark's `test_parity_listener.py`, and 
`test_query_manager_no_recreation` of `test_parity_streaming.py`, failed on 
this client.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. The listeners of streaming queries get their events, and their session, 
as with PySpark 4.3.0. For the Rust API, `QUERY_STARTED_EVENT`, 
`StreamingQueryManager::listeners` and `StreamingQueryListener::as_any` are new.
   
   ### How was this patch tested?
   
   - A new Rust e2e test adds a listener through one manager of a session, and 
checks:
     - that it gets the started event before `start` returns, and progress 
events
     - that the other managers of the session list it
     - that it gets no more events once removed through another manager
   - A new live test checks:
     - that `spark.streams` is the same object
     - the session of the listeners, and `_sqlb._listener_bus`
     - that two listeners get the started event before `start` returns
     - that a removed listener gets no more events, while the other gets the 
terminated event
   
     It passes with official PySpark 4.3.0, and fails without this change.
   - Apache Spark's `test_parity_listener.py` and `test_parity_streaming.py` 
pass on this client against a local server, all 9 and 30 tests. Before this 
change, 6 and 1 failed.
   - The lib tests of both crates (597 passed), the build of all tests, also 
with `wasm-udf`, the offline Python suites (2214 passed, 8 skipped), and the 
live drop-in and Arrow suites (200 passed) and live cases (10 passed) against a 
local Spark 4.3.0 server. Clippy reports no diagnostics in the changed lines.
   
   ### 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]

Reply via email to