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

   ### What changes were proposed in this pull request?
   
   This is the second sub-task of SPARK-62207, after SPARK-62208 (#926). The 
sub-tasks close two rows of `dev/parity/known-differences.md`, "The `__dict__` 
of the objects of a session" and "Subclasses". This PR covers the streaming 
objects of a session: `StreamingQuery`, `StreamingQueryManager` 
(`spark.streams`) and its `StreamingQueryListenerBus` (`spark.streams._sqlb`).
   
   In PySpark these are plain Python classes:
   - `StreamingQuery(session, queryId, runId, name=None)` keeps `_session`, 
`_query_id`, `_run_id` and `_name`.
   - `StreamingQueryManager(session)` keeps `_session` and makes its bus, 
`_sqlb = StreamingQueryListenerBus(self)`.
   - The bus keeps `_sqm`, the `_listener_bus` list, `_execution_thread` and a 
`threading.Lock`.
   
   The methods read those attributes at each call. In the drop-in, the three 
classes held native state instead:
   - `vars()` held nothing of the state;
   - `StreamingQuery` and `StreamingQueryManager` could be neither constructed 
nor subclassed;
   - `_sqlb` was a new read-only object at each access;
   - `pyspark.sql.connect.streaming.query` had no `StreamingQueryListenerBus`.
   
   Of the 419 generic cases of `tests/test_dropin_object_state_offline.py` 
(added by SPARK-62208):
   
   | | Same as PySpark | Differ |
   |---|---|---|
   | Before, master ab03fcff | 160 | 259 |
   | After, this PR | 184 | 235 |
   
   The 24 cases that this PR fixes are those of the three classes: `vars`, 
`attributes`, `copies`, `construct`, `uninit`, `foreign` and `cycle`. This PR 
deletes their group from the strict-xfail table.
   
   **The changes:**
   - **`StreamingQuery`:**
     - The native object is empty.
     - `__init__` keeps its four arguments as they are.
     - `id`, `runId` and `name` return `_query_id`, `_run_id` and `_name`.
     - Each method reads `_query_id`, `_run_id` and then `_session`, at the 
call, in the order of PySpark's `_execute_streaming_query_cmd`. The session is 
resolved as in SPARK-62208: a drop-in session as is, otherwise the drop-in 
client of its `client`.
     - The queries that `active`, `get`, `start` and `toTable` return are 
`StreamingQuery(session, id, runId, name)`. For `active` and `get`, `session` 
is the manager's `_session`.
   - **`StreamingQueryManager`:**
     - `__init__(session)` keeps `_session` and makes `_sqlb` from the module's 
`StreamingQueryListenerBus`.
     - `active`, `get`, `awaitAnyTermination` and `resetTerminated` resolve 
`_session` at each call.
     - The listener methods follow PySpark:
       - `addListener` calls `listener._set_spark_session(self._session)`, then 
`self._sqlb.append(listener)`;
       - `removeListener` calls `self._sqlb.remove(listener)`;
       - `close` calls `self._sqlb.close()`.
   - **`StreamingQueryListenerBus`:**
     - It is now native, exported from `pyspark.sql.connect.streaming.query` as 
in PySpark, with PySpark's docstrings.
     - `scripts/gen_connect_docstrings.py` now also takes the own docstrings of 
its members (`OWN_MEMBERS`).
     - The listeners are the entries of `_listener_bus`:
       - `append` and `remove` change the list as PySpark's do. `remove` takes 
the first entry that is the listener or equal to it.
       - Each event is posted once to the bus's `post_to_all`, which calls the 
listeners in the list as it is at that moment. A listener that a user puts in 
the list gets the events too.
     - The subscription stays with the native listener bus of the session, as 
before:
       - each entry has a registration in it;
       - its first registration subscribes the session and its last one ends 
the subscription;
       - removal still waits for the delivery in progress, and signals and 
server failures behave as before.
     - A registration that the native bus ends takes its entry off the list. 
This happens when the server's stream ends or fails, and on a stop of the 
session or a close of its client.
     - `deserialize` is PySpark's static method.
   - **The three classes together:**
     - `__new__` takes any arguments, and the signature is PySpark's.
     - `__getstate__` and `__reduce_ex__` are those of a plain object. The 
previous `__copy__`, `__reduce__` and `_of_state` are removed, and so is 
`streaming_query_reduce` of `_state_reduce.py`.
     - All three can be subclassed. An object whose `__init__` never ran raises 
the `AttributeError` of `_query_id`, `_session` or `_lock`, as PySpark's does.
     - `StreamingQuery.__doc__` and `StreamingQueryManager.__doc__` are None, 
as PySpark's are. CPython would otherwise make them the empty str of their text 
signatures (`classes_of_no_docstring` of `lib.rs`).
   - **`spark.streams`:**
     - It is made once, as `StreamingQueryManager(self)`, which holds the 
session object. So the session and its manager form a reference cycle, as in 
PySpark (decision 1 of the design, as SPARK-62208 did for the catalog). The 
garbage collector frees the cycle, since the session's `__traverse__` already 
visits the manager.
     - A dropped session whose streams were read stays open until a collection. 
If the program exits before one, the exit sends its `clean_cache`, as PySpark's 
does.
   - **Docs:**
     - `dev/parity/known-differences.md`:
       - The `__dict__` row adds the three classes and the residue of the bus.
       - The Subclasses row lists the three classes among the subclassable 
ones, and drops them from the native list.
       - The exit row covers both cycles.
       - The `TYPE_ONLY` row drops the bus.
     - `docs/compatibility.md` gets the same changes.
   
   **What remains** is the residue that the KD row states:
   - **Requests:** as for the catalog, the requests of a query and of a manager 
go through the native transport of the session that they hold, not through the 
methods of a client of another class. This is inherent.
   - **`_execution_thread`:** it stays None, because the thread that posts the 
events is the native bus's, not a `threading.Thread`.
   - **`_lock`:** it is held while the list is read or changed, but not while 
the listeners are called. So a listener may add or remove listeners from its 
callbacks; PySpark's bus deadlocks there.
   - **Normal end of the subscription:** when the server's stream ends 
normally, the drop-in takes the listeners off the list. PySpark's handler 
clears its list only when the events fail.
   - **`close()`:** it removes every listener. PySpark's removes them from the 
list that it iterates, so it removes only every other listener; with four 
listeners, two stay.
   
   ### Why are the changes needed?
   
   In PySpark, code that reads or writes the state of these objects works 
because the state is in their `__dict__`: `vars()`, `StreamingQuery(spark, id, 
runId)` for a query of another session object, a manager of a proxy of a 
session, `spark.streams._sqlb._listener_bus`, subclasses, copies and pickles. 
Until now, none of this worked the same way in the drop-in.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A `StreamingQuery`, `spark.streams` and its listener bus keep their 
state in their `__dict__`, as PySpark's do:
   - the three classes can be constructed from PySpark's arguments, and 
subclassed;
   - `pyspark.sql.connect.streaming.query.StreamingQueryListenerBus` exists;
   - `spark.streams._sqlb` is the same object at each access;
   - copies and pickles carry the `__dict__`.
   
   Once `spark.streams` has been read, a dropped session is freed by the next 
garbage collection instead of at once, as PySpark's is.
   
   ### How was this patch tested?
   
   - **`tests/test_dropin_object_state_offline.py`:** 188 passed and 235 
xfailed on this branch; 423 passed with PySpark 4.3.0.
   - **The streaming and listener tests of `tests/test_dropin_offline.py`**, 
including the lifecycle cases of `streaming_listener_cases.py` (`-k "listener 
or stream or Streaming or streams or query or Query"`): 166 passed.
   - **`tests/test_exit_cleanup_offline.py`:** a new test,
     
`test_i_a_dropped_new_session_whose_streams_were_read_is_open_until_a_collection`.
 A dropped session whose streams were read sends `clean_cache` at exit, plus 
`release_session` with the variable, and nothing once `gc.collect()` has run. 
31 passed.
   - **`tests/test_dropin_class_modules_offline.py`:** the docstring golden 
compares the bus's docstrings too, and the bus is now a class of the module. 
290 passed.
   - **`tests/test_type_stubs_offline.py`:** `TYPE_ONLY` no longer lists the 
bus. 15 passed, 4 skipped.
   - **`tests/test_dropin_e2e.py`:** a new live test,
     
`test_the_streaming_queries_and_their_manager_make_the_requests_of_their_dict_as_pysparks`.
 Against a Spark 4.3.0 server, it covers:
     - `vars()` of the three;
     - a query of a proxy of the session, and a manager of one;
     - the `_session` of the queries that a manager gives.
   
     It passes with both clients. The streaming tests of the file (`-k "stream 
or listener or query"`): 21 passed.
   - **The gates:**
     - `cargo fmt --all --check`;
     - `cargo +1.100.0 clippy -p apache-spark-connect-core -p 
apache-spark-connect -p pyspark-rs --all-targets -- -D warnings`;
     - `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps -p apache-spark-connect 
-p apache-spark-connect-core`;
     - `cargo test -p pyspark-rs --lib`;
     - ruff 0.14.0, the no-stub audit, `gen_connect_docstrings.py --check`, 
`gen_type_stubs.py --check`, `gen_fn_dispatch.py --check`, the license checks 
and the guards;
     - the offline suite of `coverage.yml`;
     - `test_dropin_e2e.py` against a Spark 4.3.0 server.
   
   ### 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