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

   ### What changes were proposed in this pull request?
   
   The Rust client now reaches the Spark Connect server through the HTTP proxy 
of the environment, with the rules that grpcio (and so PySpark's client) 
applies:
   
   - The proxy is the first of `grpc_proxy`, `https_proxy` and `http_proxy` 
that is set. An empty value means no proxy.
   - `no_grpc_proxy`, or else `no_proxy`, lists the hosts that are not proxied. 
An entry matches the host itself or its subdomains, with an optional leading 
`.`, ignoring case and on a label boundary. An IP host also matches a CIDR 
range.
   - Only the lowercase names are read. Only an `http://` proxy is used; 
another scheme, or a value without a scheme or host, connects directly with a 
warning. The percent-decoded userinfo is sent as `Proxy-Authorization: Basic`. 
A proxy without a port is on port 443.
   
   The new `proxy` module selects the proxy. The channel is dialed with 
`Endpoint::connect_with_connector_lazy` and a small connector that opens an 
HTTP/1.1 `CONNECT` tunnel (RFC 9110 §9.3.6). tonic then negotiates TLS 
**inside** the tunnel with the existing `ClientTlsConfig`: the same SNI, 
certificate checks against the Spark server's name, and roots. The proxy only 
relays opaque bytes, so it sees neither the bearer token nor the data.
   
   The connector reads the proxy's response **one byte at a time up to the 
blank line**, and no further. The bytes after it belong to the server, and a 
plaintext HTTP/2 server speaks first by sending its SETTINGS frame. 
hyper-util's `Tunnel` (tried first) assumes the response ends its read buffer. 
It stalls when the proxy delivers its `200` and the server's first bytes in one 
read, which happened through Envoy under load; the regression test 
`server_bytes_in_the_proxy_response_read_are_kept` reproduces it. **No crate is 
added**, and `Cargo.lock` is unchanged: `hyper-util` (for `TokioIo`) and 
tokio's `net`/`io-util` are already compiled in through tonic.
   
   There are two deliberate differences from grpcio, both on the side of safety:
   
   1. **A loopback host is never proxied** (`localhost`, `127.0.0.0/8`, `::1`). 
The client sends a token to `localhost` without TLS, as PySpark does. grpcio 
proxies `localhost` unless `no_proxy` lists it, which hands that token to the 
proxy in clear. The test below shows grpcio doing it.
   2. **A proxy address with an invalid host or port is an error** when the 
session is created, naming the variable but not its value, which can hold a 
password. grpcio never connects to such an address, and it takes a port above 
65535 modulo 65536.
   
   On the proxy path, the TCP connection to the proxy and the `CONNECT` 
exchange are bounded together by the existing 10s `READY_TIMEOUT` inside the 
connector. A proxy that never answers is therefore `UNAVAILABLE` and retried, 
as in grpcio. Putting tonic's `connect_timeout` around the whole connection 
instead makes it `UNKNOWN: transport error`, which the retry policy does not 
retry. The direct path is unchanged.
   
   The docs are updated: `configuration.md` gains an *HTTP Proxies* section, 
and `compatibility.md` notes the two differences above.
   
   ### Why are the changes needed?
   
   `pyspark-client-rust` is a drop-in for `pyspark-client`. A user behind an 
egress proxy whose PySpark connects today could not connect with the Rust 
client: it dialed directly, and the call retried until its retry policy gave up.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A process with `grpc_proxy`/`https_proxy`/`http_proxy` set now connects 
through that proxy, as PySpark's client already does. Without those variables, 
nothing changes.
   
   ### How was this patch tested?
   
   - **New tests**, 25 in all:
     - 12 unit tests in `proxy.rs` cover precedence, case, empty values, 
schemes, URIs, percent-decoded credentials, `no_proxy` hosts, suffixes, CIDR 
and the IP-suffix quirk, invalid addresses, and redaction.
     - 13 integration tests in `tests/proxy.rs` use a loopback tonic server 
behind an in-test `CONNECT` proxy. They cover plaintext and TLS through the 
tunnel (the proxy sees a ClientHello with the server's SNI and no token), Basic 
auth, the loopback bypass, `no_proxy`, 403/407/502, a proxy that is down, a 
proxy that never answers (`UNAVAILABLE`), unsupported schemes, invalid 
addresses, uppercase variables, any 2xx, and server bytes that arrive in the 
same read as the proxy's answer.
   - **Mutation check:** each of these deliberate breakages fails at least one 
test: no loopback bypass, proxy unused, `no_proxy` ignored, uppercase variables 
read, no auth header, invalid address connecting directly, a buffered read of 
the proxy's answer, and 200 only.
   - `cargo test -p apache-spark-connect -p apache-spark-connect-core 
--release` with `SPARK_REMOTE` set to a local Spark 4.3.0 Connect server: 1388 
passed, 4 failed. The 4 failures (`e2e_evaluators`, two in 
`e2e_gaussian_mixture`, `types_roundtrip` nanosecond timestamps) fail 
identically on `apache/master` against the same server. `cargo clippy 
--workspace --exclude wasm-udfs --all-targets -- -D warnings` and `cargo fmt 
--all --check` are clean. `wasm-udfs` was excluded only because no 
`wasm32-unknown-unknown` std is available offline. 
`scripts/check_doc_snippets.py` passes: 60 snippets.
   - **End to end against `pyspark-client` 4.3.0 (grpcio 1.85.1).** Both 
clients ran the same 31 scenarios in fresh processes against a real Spark 4.3.0 
server, through Envoy 1.32.2 (TLS front end and `CONNECT` proxy, with and 
without Basic auth) and an instrumented fault-injecting proxy. **Every scenario 
had the same outcome in both clients:** 18 connected with identical results, 
and 13 failed to connect in both. The scenarios covered:
     - variable precedence and case
     - `no_proxy` hosts and CIDR
     - unsupported schemes
     - wrong or missing proxy credentials
     - a proxy that is down, refuses (403/502) or never answers
     - TLS opacity
     - MITM with an untrusted CA or the wrong name
     - an IPv6 proxy address
     - a tunnel cut mid-stream (reattached the same way)
     - a 75 s idle keepalive
   
     The workloads included `collect`, `createDataFrame`, `toPandas` and 8 
concurrent queries. The one expected difference is the invalid proxy port. The 
run also showed that `pyspark-client` sends `sc://localhost;token=…` through a 
configured proxy with **the token in clear**; this client does not.
   - **Latency** (TLS with a token, median of 2 rounds; cold = new session plus 
first query, 30×):
   
     | | Cold connect p50 | Config RPC p50 | Small query p50 | `toPandas` 64 MB 
|
     |---|---|---|---|---|
     | Rust direct (before → after) | 7.46 → 7.42 ms | 0.20 → 0.19 ms | 5.67 → 
5.50 ms | 289 → 347 MB/s |
     | Rust through Envoy | 7.72 ms | 0.22 ms | 5.52 ms | 360 MB/s |
     | PySpark through Envoy | 11.27 ms | 0.40 ms | 11.14 ms | 193 MB/s |
   
     The proxy adds about 0.3 ms to a cold connect, and nothing measurable 
afterwards. Direct connections are unchanged: the before/after throughput gap 
is run-to-run noise, about ±15% between rounds. With an emulated 20 ms round 
trip, bulk throughput is 72 MB/s against grpcio's 156 MB/s **with or without 
the proxy, and before this change too**. That is tonic's fixed HTTP/2 window 
against grpcio's adaptive one, a separate follow-up.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Isaac (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