This is an automated email from the ASF dual-hosted git repository.
HyukjinKwon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark-connect-rust.git
The following commit(s) were added to refs/heads/master by this push:
new 125654b0 [SPARK-61355][CONNECT] Retry the requests that a connection
closed before sending them, as PySpark does
125654b0 is described below
commit 125654b0b37affb2139b25407fcaa9d6bd477d5b
Author: Hyukjin Kwon <[email protected]>
AuthorDate: Tue Nov 10 18:19:16 2026 +0900
[SPARK-61355][CONNECT] Retry the requests that a connection closed before
sending them, as PySpark does
### What changes were proposed in this pull request?
[SPARK-61355](https://issues.apache.org/jira/browse/SPARK-61355): the Rust
client retries a request that its connection closed before sending it, as
PySpark does.
- `RetryPolicy::can_retry` (`crates/spark-connect-core/src/retries.rs`)
retries a `CANCELLED` whose source is hyper's canceled error.
- hyper cancels a request only when the connection it was queued on
closes before sending it ("connection closed", or "connection was not ready"
for a connection that had already closed).
- tonic reports hyper's canceled error as `CANCELLED` "operation was
canceled", and keeps it as the status's source.
- Still not retried: a `CANCELLED` that the server sent (no source), and
tonic's timeout of a request (`TimeoutExpired`, also `CANCELLED`).
- The check for a reset (`UNKNOWN` with a transport error as its source)
and the new check share one walk over the status's sources.
### Why are the changes needed?
After a connection reset, a request that was queued on the closing
connection, but not sent yet, failed with `CANCELLED` "operation was canceled".
The retry policy did not retry `CANCELLED`, so the error went out of the call.
For example, `collect_reattaches_after_three_connection_resets`
(`crates/spark-connect/tests/e2e_reattach_drop.rs`) failed at its `collect` in
1 of 40 runs with 2 test threads:
```
SparkError { kind: Connect, message: "[The operation was cancelled]
operation was canceled", grpc_code: Some(1), ... }
```
In PySpark 4.3.0, grpc-python fails a call whose connection breaks with
`UNAVAILABLE`, which `DefaultPolicy` retries. Such a request was never sent, so
it is safe to send it again.
### Does this PR introduce _any_ user-facing change?
Yes. A request that a closing connection catches before sending it is now
retried. Before, it failed with `CANCELLED` "operation was canceled".
### How was this patch tested?
- Rust (`retries.rs`):
- `test_can_retry_a_request_its_connection_closed_before_sending`: a
request is queued on a hyper HTTP/2 client connection, which then closes before
sending it. The request gets hyper's canceled error, tonic makes it
`CANCELLED`, and the policy retries it. It fails without the change.
- `test_cannot_retry_a_cancellation_the_server_sent_or_a_timeout`.
- The test uses `hyper-util` as a new dev-dependency of
`apache-spark-connect-core`. It was already in the dependency tree through
tonic.
- `e2e_reattach_drop` against a local Spark 4.3.0 server: 80 runs with 2
test threads, with no failure (it failed 1 in 40 before).
- Checks:
- the lib tests of both crates (724 passed);
- the build of all tests, also with `wasm-udf` and `datafusion,polars`,
without warnings;
- `cargo fmt --all --check`, and clippy with no diagnostics on 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.
Closes #466 from HyukjinKwon/s16-retry-canceled-requests.
Authored-by: Hyukjin Kwon <[email protected]>
Signed-off-by: Hyukjin Kwon <[email protected]>
---
Cargo.lock | 1 +
crates/spark-connect-core/Cargo.toml | 2 +
crates/spark-connect-core/src/retries.rs | 74 ++++++++++++++++++++++++++------
3 files changed, 65 insertions(+), 12 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
index 22ef66dd..4bc2ce2e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -88,6 +88,7 @@ dependencies = [
"futures",
"h2",
"hyper",
+ "hyper-util",
"prost",
"prost-types",
"regex",
diff --git a/crates/spark-connect-core/Cargo.toml
b/crates/spark-connect-core/Cargo.toml
index 99bdfd7c..eee78bad 100644
--- a/crates/spark-connect-core/Cargo.toml
+++ b/crates/spark-connect-core/Cargo.toml
@@ -39,5 +39,7 @@ hyper = "1"
sha2 = { version = "0.10", features = ["asm"] }
[dev-dependencies]
+# A hyper HTTP/2 client connection, for a request that its connection closed
before sending.
+hyper-util = { version = "0.1", features = ["tokio"] }
tempfile = "3"
tonic-prost.workspace = true
diff --git a/crates/spark-connect-core/src/retries.rs
b/crates/spark-connect-core/src/retries.rs
index 7c6b191c..187c7da1 100644
--- a/crates/spark-connect-core/src/retries.rs
+++ b/crates/spark-connect-core/src/retries.rs
@@ -70,29 +70,42 @@ impl RetryPolicy {
/// Mirrors `pyspark.sql.connect.client.retries.DefaultPolicy.can_retry`:
/// retry transient `UNAVAILABLE`, and `INTERNAL` errors whose message
carries
/// `INVALID_CURSOR.DISCONNECTED` (a mid-stream cursor drop resumed via
reattach).
- /// A connection reset is `UNKNOWN` in tonic but `UNAVAILABLE` in
grpc-python, so it
- /// is retried too.
+ /// A connection that closes under a call is `UNAVAILABLE` in grpc-python,
so it is retried
+ /// too: tonic reports a reset during a call as `UNKNOWN`, and a call
queued on the
+ /// connection but not sent yet as `CANCELLED`.
pub fn can_retry(&self, status: &tonic::Status) -> bool {
match status.code() {
tonic::Code::Unavailable => true,
tonic::Code::Internal =>
status.message().contains("INVALID_CURSOR.DISCONNECTED"),
tonic::Code::Unknown => is_transport_error(status),
+ tonic::Code::Cancelled => is_unsent_request(status),
_ => false,
}
}
}
-/// Whether tonic built the status from a transport error, which it keeps as
the status's
-/// source. A status the server sent has no source.
+/// The errors that tonic keeps as the source of a status that it built from a
transport error.
+/// A status the server sent has none.
+fn transport_errors(
+ status: &tonic::Status,
+) -> impl Iterator<Item = &(dyn std::error::Error + 'static)> + '_ {
+ std::iter::successors(std::error::Error::source(status), |error|
error.source())
+}
+
+/// Whether tonic built the status from a transport error.
fn is_transport_error(status: &tonic::Status) -> bool {
- let mut source = std::error::Error::source(status);
- while let Some(error) = source {
- if error.is::<h2::Error>() || error.is::<hyper::Error>() {
- return true;
- }
- source = error.source();
- }
- false
+ transport_errors(status).any(|error| error.is::<h2::Error>() ||
error.is::<hyper::Error>())
+}
+
+/// Whether tonic built the status from a request that hyper canceled, which
hyper does only for
+/// a request that its connection closed before sending. Neither tonic's
timeout of a request
+/// nor a cancellation that the server sent, both `CANCELLED` too, is one.
+fn is_unsent_request(status: &tonic::Status) -> bool {
+ transport_errors(status).any(|error| {
+ error
+ .downcast_ref::<hyper::Error>()
+ .is_some_and(hyper::Error::is_canceled)
+ })
}
/// Stateful retry attempt tracker.
@@ -538,4 +551,41 @@ mod tests {
assert_eq!(status.message(), "boom");
assert!(!RetryPolicy::default().can_retry(&status));
}
+
+ /// The error of a request queued on an HTTP/2 connection that closes
before sending it, as
+ /// a request that a reset of the connection catches before it is sent.
+ async fn error_of_a_request_its_connection_closed_before_sending() ->
hyper::Error {
+ let (io, _server) = tokio::io::duplex(1 << 16);
+ let (mut sender, connection) = hyper::client::conn::http2::handshake(
+ hyper_util::rt::TokioExecutor::new(),
+ hyper_util::rt::TokioIo::new(io),
+ )
+ .await
+ .unwrap();
+ let response = sender.send_request(tonic::codegen::http::Request::new(
+ tonic::body::Body::empty(),
+ ));
+ drop(connection);
+ response.await.expect_err("the connection closed")
+ }
+
+ #[tokio::test]
+ async fn test_can_retry_a_request_its_connection_closed_before_sending() {
+ let error =
error_of_a_request_its_connection_closed_before_sending().await;
+ assert!(error.is_canceled(), "{error:?}");
+ // tonic reports it as CANCELLED, with hyper's error as its source.
+ let status = tonic::Status::from_error(Box::new(error));
+ assert_eq!(status.code(), tonic::Code::Cancelled);
+ assert!(RetryPolicy::default().can_retry(&status), "{status:?}");
+ }
+
+ #[test]
+ fn test_cannot_retry_a_cancellation_the_server_sent_or_a_timeout() {
+ let p = RetryPolicy::default();
+ assert!(!p.can_retry(&tonic::Status::cancelled("operation was
canceled")));
+ // tonic's timeout of a request is CANCELLED too.
+ let timeout =
tonic::Status::from_error(Box::new(tonic::TimeoutExpired(())));
+ assert_eq!(timeout.code(), tonic::Code::Cancelled);
+ assert!(!p.can_retry(&timeout));
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]