Skip to content

perf(statement): close a complete inline result from execute(), not from the first fetch - #472

Open
vuanhphung wants to merge 2 commits into
perf/rd-roundtrips-drain-closefrom
perf/rd-close-path
Open

vuanhphung wants to merge 2 commits into
perf/rd-roundtrips-drain-closefrom
perf/rd-close-path

Conversation

@vuanhphung

Copy link
Copy Markdown
Collaborator

Problem

Real-Time warehouses answer an inline SELECT with state=SUCCEEDED and the whole result in the response (checked on three staging warehouses, both dispositions, with and without wait_timeout; never CLOSED), so the kernel sends a CloseStatement after every query. That close is spawned by the fetch that reaches end-of-stream, which the Python binding runs with the GIL held: a tokio task allocation and a worker wake per query, inside the section every Python thread of the process takes turns on. Each close also formats and re-parses its URL and arms a 500 ms timer to drain a two-byte {} body.

Fix

When the execute response holds the whole result (SUCCEEDED, an inline attachment, at most one chunk, no next chunk or links), execute() spawns the close itself. The bindings run execute() without the GIL, and the drain and drop paths see closed and skip. An explicit close() before the stream is drained waits for that close, as it waited for its own before; after a drain it returns at once, as before. cancel() on a closed statement returns without a request. Statement URLs come from a {base}/statements URL parsed once the session route is set, and discard_body takes a body that has already arrived without arming its timer.

Stacked on #459 (it changes discard_body, which #459 adds).

Verification

cargo fmt, cargo clippy --all-targets -D warnings and cargo test pass. New: execute_closes_complete_inline_result_before_it_is_read, close_waits_for_the_early_close_only_before_the_stream_is_drained, and unit tests for the completeness check and the URL builder. primary_keys_rejects_invalid_current_namespace_and_closes_lookup now waits for the lookup's close before verifying: that lookup reads to the end, so its close is fire-and-forget (as before), and the test had relied on it landing before DeleteSession.

Python driver end to end, SELECT 1 back to back in one process, on the full factory stack (#453 #455 #456 #457 #458 #459 #461 #467 #468 #469) with and without this change. Mock: a TLS server replaying the live Real-Time response bytes and headers (execute 80 ms, close 60 ms), client on 6 cores, 3 interleaved runs, medians:

1 process × 800 threads before this PR
client CPU / query 0.359 ms 0.347 ms (-3.3%)
steady QPS 9,039 9,073
query p99 171 ms 165 ms

Live, staging X-Large Real-Time warehouse, 8 client cores, one run each: an env-gated prototype of this change together with the request-path changes in #470 (same code paths) cut client CPU per query at 1 process × 800 threads from 0.433 to 0.405 ms (-6.5%); steady QPS 7,651 → 7,961 and query p99 249 → 220 ms (that load is round-trip bound). Live, Medium Real-Time warehouse (server-bound at about 860 QPS), 1 × 200 threads, 60 s, 2 interleaved rounds, the exact commits of this PR, #470 and #471 together: 0.398 → 0.386 ms CPU per query, same QPS, same server-side error counts.

This pull request and its description were written by Isaac.

…rom the first fetch

A SUCCEEDED response that already holds the whole result (an inline
attachment, at most one chunk, nothing to page) needs nothing more from
the server statement, so execute() now spawns its CloseStatement. The
Python binding runs execute() with the GIL released; before, the close
was spawned by the fetch that reached end-of-stream, with the GIL held.

An explicit close() before the stream is drained waits for that close,
as it waited for its own before; after a drain it returns at once, as
before. cancel() on a closed statement returns without a request.

Statement URLs are built from a {base}/statements URL parsed once the
session route is set, and discard_body takes a body that has already
arrived without arming its timer.

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 · 1 Nit

Looks good. The early close for a complete inline result is gated correctly: SUCCEEDED, an inline attachment, at most one chunk, nothing left to fetch. The closed, close() and Drop paths fit together, and no correctness bugs turned up. There are three low-severity notes: the retry-after-failed-early-close branch has no test, the warning logged when a close fails is misleading on the retry path, and a second concurrent close() can return Ok before the server has finished closing.

Other findings

  • ⚪ Nit — Two concurrent ExecutedStatementHandle::close(&self) calls behave differently. The first takes the JoinHandle and waits for the early close. The second sees None with closed=true and returns Ok at once, before the CloseStatement has finished, and even if it then fails and the first caller re-sends it. Before this change both callers would have hit the wire. The existing comment accepts racing closes, but the second caller now gets a premature Ok. Consider documenting this, or sharing the result (for example a Shared future or tokio::sync::OnceCell) if callers depend on close() returning only after the server-side close.

Comment thread src/statement/executed.rs Resolved
Comment thread src/session.rs Resolved
…e-failure log

close_resends_a_failed_early_close answers the first DELETE with a
non-retryable 400 and checks that close() sends it again, returns Ok,
and that dropping the handle afterwards sends nothing more.

The background close's failure log no longer says the statement is
abandoned: an explicit close() may still send it again.

primary_keys_rejects_invalid_current_namespace_and_closes_lookup now uses
a statement id per case. wiremock reuses servers across tests, and a late
fire-and-forget close of another test's `stmt-test` could land on this
one's server and break its expect(1).

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.

✅ No issues identified by the review bot.

This branch was previously deployed

1 inactive deployment
azure-prod — dd8249ff Deployed Nov 15, 2026 by oss-reviewer-bot[bot] via followup #3184
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