Repository navigation
perf(client): reuse the connection after CloseStatement and CancelStatement - #459
vuanhphung wants to merge 3 commits into
Conversation
…tement
The close and cancel replies were dropped unread. Over HTTP/1.1 hyper only
returns a connection to the pool once its body has been read, so when the
server flushes the chunked {} body after the headers, the next request
opened a new TCP/TLS connection. Read the body before returning.
Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
There was a problem hiding this comment.
Verdict: 1 Low
The fix looks correct: reading the {} body lets hyper put the HTTP/1.1 connection back in the pool, and the raw-socket test checks this. One low concern: a body that never finishes now blocks close/cancel until the client-wide request timeout runs out.
Wait at most 500 ms for the reply body, so a stalled body no longer holds a close or cancel that already succeeded until the request timeout; after the bound the connection is dropped as before. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
There was a problem hiding this comment.
Verdict: 2 Low
Looks good. The bounded drain is correct: a read error is ignored after the call has already succeeded, and the 500 ms cap keeps a stalled body from delaying callers. Two low-severity notes: delete_session still drops its reply body the same way, and the exact connection-count assertion in the new test may be timing-sensitive.
hyper returns an idle HTTP/1 connection to the pool from a spawned task, so let it run before the next call instead of relying on scheduling order. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
There was a problem hiding this comment.
Verdict: 1 Low
Looks good: reading the close/cancel body is the right fix for HTTP/1.1 connection reuse, and bounding the read means a stalled body can't hold up a close that already succeeded. My only concern is a timing-sensitive assertion in the new reuse test that could fail at random in CI.
Problem
close_statementandcancel_statementdrop the server's reply without reading its body. Over HTTP/1.1, hyper returns a connection to the pool only after the body has been read. So when the server sends the chunked{}reply after the headers, hyper closes the connection, and the next request opens a new TCP/TLS connection. On the Reyden realtime route the kernel sends a CloseStatement after every query, so a server or proxy that flushes the headers first causes roughly one reconnect per query.Fix
Read the (two-byte) body before returning, for at most 500 ms. The close or cancel has already succeeded at that point, so a read error is ignored, and a body that stalls past the bound is dropped as before (it costs only the connection, not the caller's time).
Verification
cargo fmt,cargo clippy --all-targets -D warningsandcargo testpass. The newclose_and_cancel_reuse_the_connectiontest sends the gateway's chunked{}reply a moment after the headers. Four close/cancel calls use 1 connection with this change and 4 without it.close_and_cancel_do_not_wait_for_a_stalled_bodysends headers with no body: close + cancel return in about 1 s, versus 120 s (2 × the request timeout) with an unbounded drain. With the Python connector, therealkerneltests intest_session.pyandtest_kernel_async_readers.pypass on a wheel built from this branch.New connections were counted from the HTTP client's connect logs, with 1.4.0 and this branch interleaved, 3 pairs each (medians below). The mock is a local HTTP/1.1 server that flushes the headers first, with 50 ms execute and 25 ms close server time. Live is a Reyden warehouse with 50 sessions × 300 queries.
On the mock, throughput rises from 1,128 to 1,507 QPS. The live gateway usually sends the body together with the headers, so the live gain is small. Reconnects beyond the two connections each session keeps (one for the execute, one for the overlapping close) drop from about 0.9 to 0.5 per 1,000 queries, and CPU and latency changes are within noise. The fix matters most behind a server or proxy that flushes the headers first.
Live benchmark (staging Large Reyden warehouse)
TPC-H Q6 on a Large Real-Time warehouse, 800 sessions (4 processes x 200 threads), cold start, each session querying back to back for 120 s, 16 client cores. Isolated A/B: only this change vs connector main 0c1b9e80 + kernel main db23442, 3 interleaved rounds, nothing else on the warehouse. Measured at 4dc6d91. Median (min-max):
No change on this gateway, which sends the close reply body with its headers.
SELECT 1 on an X-Large Real-Time warehouse (driver-bound). Q6 above is limited by the warehouse, so the same harness also ran
SELECT 1, where the client is the limit: X-Large warehouse, 4 processes x 200 threads, 120 s, 16 client cores, this change vs connector main 0c1b9e80 + kernel main db23442, measured at 4dc6d91, run in rotation with the other factory PRs, 2 rounds. Median (min-max):Average QPS change per round: -0.9%, -1.6%.
This pull request and its description were written by Isaac.