Skip to content

perf(client): reuse the connection after CloseStatement and CancelStatement - #459

Open
vuanhphung wants to merge 3 commits into
mainfrom
perf/rd-roundtrips-drain-close
Open

vuanhphung wants to merge 3 commits into
mainfrom
perf/rd-roundtrips-drain-close

Conversation

@vuanhphung

@vuanhphung vuanhphung commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

close_statement and cancel_statement drop 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 warnings and cargo test pass. The new close_and_cancel_reuse_the_connection test 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_body sends 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, the realkernel tests in test_session.py and test_kernel_async_readers.py pass 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.

new connections / 1k queries client CPU / query query p50
mock, 200 sessions: 1.4.0 → this PR 766 → 9 1.94 → 1.26 ms 156 → 126 ms
live, 50 sessions: 1.4.0 → this PR 7.6 → 7.2 1.16 → 1.14 ms 67.7 → 66.5 ms

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):

kernel main this PR
Average QPS (setup included) 2084 (2032-2218) 2077 (2013-2149)
Steady QPS (after all sessions open) 2127 (2086-2261) 2117 (2048-2186)
Query latency p50 (ms) 358 (341-365) 364 (356-376)
Client CPU per query (ms) 1.24 (1.21-1.28) 1.28 (1.20-1.28)

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):

kernel main this PR
Average QPS (setup included) 6158 (6135-6181) 6081 (6079-6082)
Peak QPS (best second) 6572 (6555-6590) 6476 (6468-6485)
Query latency p50 (ms) 123 (123-123) 124 (124-124)
Client CPU per query (ms) 1.33 (1.32-1.34) 1.34 (1.34-1.35)

Average QPS change per round: -0.9%, -1.6%.

This pull request and its description were written by Isaac.

…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>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/client/sea.rs Resolved
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>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/client/sea.rs Resolved
Comment thread src/client/sea.rs Resolved
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>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/client/sea.rs Resolved

This branch was previously deployed

1 inactive deployment
azure-prod — 4dc6d919 Deployed Nov 13, 2026 by vuanhphung via followup #2797
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant