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

   ### What changes were proposed in this pull request?
   
   [SPARK-61184](https://issues.apache.org/jira/browse/SPARK-61184): PySpark 
keeps the `server_side_session_id` of the first response as 
`_server_session_id`, sends it as `client_observed_server_side_session_id` on 
its requests, and checks it on each response. A response with a different ID, 
or an `INVALID_HANDLE.SESSION_CHANGED` error, closes the client, and its later 
calls raise `NO_ACTIVE_SESSION`.
   
   - `SparkConnectClient` keeps the observed ID in a slot shared by the clones 
of a session. A new session, or a clone from `clone_session`, starts with none.
   - Each typed request that has the field carries the observed ID: 
ExecutePlan, AnalyzePlan, Config, Interrupt, FetchErrorDetails, CloneSession, 
ReattachExecute, ReleaseExecute, AddArtifacts, ArtifactStatuses. The 
ReattachExecute of a reattachable execution takes the observed ID of its 
initial request, as PySpark's `reattach.py` does.
   - Each typed response that has the IDs is checked: a non-empty session ID 
must be this session's, and a non-empty server-side session ID that differs 
from the observed one closes the client and raises `PySparkAssertionError` with 
PySpark's message. The reattachable stream checks each response it reads.
   - An error with the `INVALID_HANDLE.SESSION_CHANGED` error class closes the 
client, in the error path of every typed RPC.
   - Raw requests and responses (`*_raw`) are not changed: their bytes are 
built by the caller, which sets the observed ID itself.
   
   ### Why are the changes needed?
   
   After the server restarts, PySpark raises `NO_ACTIVE_SESSION` on the next 
call. The Rust client sent no observed ID and checked no ID, so the calls ran 
on a fresh server session. On a server restart in a session that had set 
`spark.sql.shuffle.partitions=7`:
   
   ```
   before (this client): conf.get -> "200"; spark.table("v4") -> 
TABLE_OR_VIEW_NOT_FOUND; range(2).count() -> runs
   official 4.3.0:       conf.get, table and range -> SparkConnectException 
NO_ACTIVE_SESSION
   ```
   
   The "after" column below is from this build, against a Spark 4.3.0 Connect 
server restarted in the same way (`/tmp/r2/impl/SPARK-61184/v4b.py`):
   
   ```
   after restart conf -> ERR SparkConnectException NO_ACTIVE_SESSION
   after restart view -> ERR SparkConnectException NO_ACTIVE_SESSION
   after restart range -> ERR SparkConnectException NO_ACTIVE_SESSION
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. After a server restart, a session raises `NO_ACTIVE_SESSION` on its 
calls, and its clones do too. A response with a changed server-side session ID 
raises `PySparkAssertionError` ("Received incorrect server side session 
identifier for request. Please create a new Spark Session to reconnect. 
(...)"). Requests carry the observed ID.
   
   Differences from PySpark:
   - AddArtifacts, ArtifactStatus and ReleaseExecute carry the observed ID 
here. PySpark 4.3.0 does not set it on those three requests.
   - An empty server-side session ID is neither observed nor checked. PySpark 
keeps an empty first ID and then raises for a later non-empty one.
   - An empty session ID of a response is not checked either. The server leaves 
both IDs unset in some responses, such as those of `FetchErrorDetails`, which 
PySpark does not check.
   - FetchErrorDetails and AddArtifacts responses are checked here. PySpark 
4.3.0 does not check them.
   - The streaming `execute_plan` and `reattach_execute` (used only by tests) 
check no responses. The session's executions go through 
`execute_plan_reattachable`, which checks each response.
   
   ### How was this patch tested?
   
   - New `crates/spark-connect-core/tests/server_session.rs` with a loopback 
gRPC server whose server-side session ID a test can change:
     - `requests_carry_the_server_side_session_id_observed_in_the_responses`: 
the first Config carries none, then the observed ID; ExecutePlan and the 
reattach after a dropped stream carry it.
     - `a_restarted_server_raises_and_closes_the_client`: a response with 
another server-side session ID raises the PySpark message, closes the client 
and its clone, and the following calls raise `NO_ACTIVE_SESSION`.
     - `a_session_changed_error_closes_the_client`: an 
`INVALID_HANDLE.SESSION_CHANGED` error closes the client, and the next call 
raises `NO_ACTIVE_SESSION`.
     - The three new tests fail without this change (the client of master 
against the mock servers of this change).
   - The mock servers of `deadlines.rs`, `reattach_cleanup.rs`, 
`user_context.rs` and `artifact_uploads.rs` now echo the session ID of the 
request, as the server does, and the connections of the first two use the 
session ID of their requests. Without these, the session check fails.
   - `python/tests/_mock_connect_server.py` and the two mock servers in 
`python/tests/test_dropin_offline.py` echo the session ID in their analyze, 
execute and clone responses. No test was added to these files.
   - Against a live Spark 4.3.0 Connect server: `tests/e2e_integration.rs` (17 
passed) and `test_dropin_e2e.py -k "session or conf or artifact or clone or 
range"` (18 passed). The restart probe above.
   - Offline `test_dropin_offline.py -k "session or conf or artifact or clone 
or version"`: 60 passed.
   - `cargo test -p apache-spark-connect-core --release`: all targets pass.
   - The lib tests of both crates (615 passed), the build of all tests, also 
with `wasm-udf`, the offline Python suites (2434 passed, 8 skipped), and the 
live drop-in and Arrow suites (215 passed) and live cases (11 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