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

   ### What changes were proposed in this pull request?
   
   [SPARK-61185](https://issues.apache.org/jira/browse/SPARK-61185): Remove 
each registration of a streaming query listener exactly once, also of a 
listener added twice or removed through another session, in the Python client 
as PySpark does.
   
   The Python client kept a single `_rust_listener_id` attribute on the 
listener object, so it could hold only one registration of a listener:
   
   - `addListener` of the same listener object twice overwrote the id. The two 
`removeListener` calls then removed the same entry of the bus, and one entry 
stayed on the bus, which kept delivering query events to the listener.
   - `removeListener` deleted the id even when the bus of the session did not 
have the listener, for example when the listener was added through 
`spark.streams` and removed through `spark2.streams`. The later 
`spark.streams.removeListener` then found no id and did nothing.
   
   Now each `addListener` appends its id to the listener 
(`_rust_listener_ids`), and `removeListener` removes the first registration 
that the bus of the session has, as the bus removes the first occurrence in 
PySpark. The core `StreamingQueryManager` gets `remove_listener_found`, which 
reports whether the bus had the id; `remove_listener` keeps its signature and 
delegates to it. The Python client forgets an id only when the bus removed it.
   
   Official PySpark 4.3.0 behaves the same on both cases: the bus is empty 
after the removals, and no event reaches the listener after the removal.
   
   ### Why are the changes needed?
   
   A listener that the user removed kept receiving the events of the streaming 
queries. The repro, add twice and remove twice:
   
   ```python
   l = L()
   spark.streams.addListener(l)
   spark.streams.addListener(l)
   spark.streams.removeListener(l)
   spark.streams.removeListener(l)
   # ours: len(spark.streams._sqlb._listener_bus) == 1, and the query events 
still reach l
   # official 4.3.0: 0, and no event reaches l
   ```
   
   The cross-session case, `spark.streams.addListener(l)`, 
`spark2.streams.removeListener(l)`, `spark.streams.removeListener(l)`, leaves 
one entry in the bus of `spark` and delivers the events, while official 4.3.0 
leaves none and delivers none.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. `removeListener` now removes a listener added twice once per call, and 
a removal through another session no longer forgets the listener of the 
session. A new Rust method, `StreamingQueryManager::remove_listener_found`, 
reports whether the bus had the listener.
   
   ### How was this patch tested?
   
   - `test_streaming_listeners_are_removed_once_per_registration_as_pyspark` 
(live, `test_dropin_e2e.py`): add twice and remove twice leaves an empty bus; a 
removal through another session leaves the listener in this bus, and the next 
removal empties it; after the removals a query delivers no events. Official 
PySpark 4.3.0 gives the same results for the same probes (`probe_dup`: 2 adds, 
1 remove, 0 after the second removal, 0 events; `probe_xsess`: 1 entry after 
the removal through `spark2`, 0 after the removal through `spark`, 0 events).
   - `remove_listener_of_an_id_the_bus_does_not_have_is_not_found` (Rust unit 
test): an id that the bus does not have is not found and is a no-op.
   - The lib tests of both crates (600 passed), the build of all tests, also 
with `wasm-udf`, and the offline Python suites (2227 passed, 8 skipped), and 
the live tests of streaming queries and listeners (15 passed) against a local 
Spark 4.3.0 server; rustfmt passes, and 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