Repository navigation
perf(kernel): cut ~530ms off SELECT 1 — drop wait_timeout, skip redundant CloseStatement - #21
Conversation
7d07dbb to
1b81dc8
Compare
…execute SELECT-1 paid ~530ms of avoidable fixed overhead vs Thrift: 1. The kernel always sent wait_timeout=10s + on_wait_timeout=CONTINUE on sync execute. Direct HTTP probes showed the server's own no-timeout default already waits up to 10s anyway, but on a trivial query the wait_timeout=10s path returns ~260ms slower (~655ms vs ~397ms). Dropping the parameter is pure win — same effective behaviour, faster completion, and the server now returns state=CLOSED on the inline-result fast path, eliminating the second round of overhead below. 2. ExecutedStatement always issued a CloseStatement RPC on explicit close or Drop. When the server has already moved the statement to CLOSED in its execute response (which it does whenever it can return results in the same response, i.e. the common SELECT-1 case), that second RPC is redundant. Thread server_state through ExecuteResult and pre-set the handle's closed flag from it so both explicit close() and Drop skip the extra round-trip. Statement::submit still passes wait_timeout=0s explicitly — that path is defined by 'return immediately with a PENDING/RUNNING statement_id', and omitting wait_timeout there would change behaviour rather than just speed it up. Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
1e3aa70 to
5f9c283
Compare
|
Critical (block merge) C1. cargo +stable fmt --all -- --check fails on mutable.rs:160 let already_closed = matches!(result.server_state, crate::types::sea::StatementState::Closed); rustfmt wants to split this across four lines (verified locally — Diff in /private/tmp/pr21-check/src/statement/mutable.rs:157). The pr-checks.yml CI gate (per CLAUDE.md) runs cargo +stable fmt --all -- Fix: run cargo +stable fmt --all and commit. Or — better, since the matches! form is verbose for a one-variant check — collapse it: Or import the type once and shorten the right-hand side: Any of the three works. I'd take the third — most readable. Important I1. Only Closed short-circuits; what about other terminal states the server could return? mutable.rs:160: StatementState has 6 variants: Pending, Running, Succeeded, Failed, Canceled, Closed. The PR correctly treats only Closed as "server-side cleaned up." But:
The PR description establishes that omitting wait_timeout typically yields Closed on the inline-result fast path, but doesn't guarantee the server can't return Succeeded on this path (e.g., for results that The current code is correct but narrow: only the actually-observed fast path triggers the optimization. If wait_timeout is reintroduced (e.g., by a caller passing Some("5s") to prepare_for_dispatch), the Suggestion (small): add one comment line at mutable.rs:160: Not blocking. Just makes the design intent explicit. I2. ExecutedAsyncStatement::new got closed parameter but the submit-path comment in mutable.rs says "the server can't return CLOSED yet" mutable.rs:202-205: Why not also propagate submit_result.server_state and pre-set the flag if the server did return Closed? Async-submit with a deliberate wait_timeout=0s shouldn't return Closed in practice — but the existing let already_closed = matches!(response.status.state, StatementState::Closed); If the server changes its behavior or some edge case (network hiccup, server-side state machine quirk), the submit path benefits the same way. Symmetric with execute. Not blocking — the current submit-path comment is honest. Worth thinking about for cleanliness. I3. ExecutedStatement::close swap is now correct against the share, but does the ExecutedStatement::Drop path also see the pre-set true? Cross-checking executed.rs:128-145: impl Drop for ExecutedStatement { When ExecutedStatement::new is called with closed = Arc::new(AtomicBool::new(true)) (server-pre-closed case), both close() and Drop short-circuit immediately. ✓ When closed is Arc::new(AtomicBool::new(false)) (server returned Succeeded):
But: what about the executed handle dropped before the parent statement is re-executed, while the close-spawn is still in flight? Timeline:
So if the user drops the executed handle and immediately re-executes, two close-statement RPCs race (one from the spawned task, one from invalidate_prior). The server is idempotent on close (the doc comment The pre-existing ExecutedStatement::Drop doesn't flip the flag because there's no other reader that cares. But now there's Statement::last_closed watching the same Arc, and Drop should set it so Suggestion: add self.closed.store(true, Ordering::Release); (or swap) at the start of Drop for ExecutedStatement, before the load. The current if self.closed.load(...) { return } becomes if Caveat: this is also fine to leave as-is — the server idempotency makes the race harmless. The cost is one wasted CloseStatement on the rare drop-then-reexecute pattern. Worth a follow-up note rather than I4. Arc is now shared between Statement and the executed handle — verify Send + Sync invariant still holds Statement::last_closed: Option<Arc> — Arc is Send + Sync. ✓ The kernel's lib.rs (per CLAUDE.md) asserts Send + Sync on the public handles; this addition doesn't break those. Quick sanity: did the lib-level assertion catch this if we'd accidentally used Rc? Yes — the existing assert_send_sync!(Statement) in lib.rs would fail to compile. ✓ Good defense. |
C1 (rustfmt): the inline matches!() on result.server_state failed cargo +stable fmt --all -- --check. Replace with == against an imported StatementState (PartialEq + Eq are already derived) — both shorter and rustfmt-clean. I1 (clarify intent): only Closed means the server reclaimed the statement. Succeeded/Failed/Canceled are terminal but still server-side-tracked and need an explicit CloseStatement. Add a comment so future readers don't widen the short-circuit by accident. I3 (close flag race): ExecutedStatement::Drop and ExecutedAsyncStatement::Drop previously did load(Acquire) then spawn(close_statement), never flipping the shared closed flag. After this PR the parent Statement::last_closed holds the same Arc, so a drop(executed) → execute() again sequence would have invalidate_prior race a second CloseStatement against the Drop-spawned one. The server is idempotent so it's harmless, but the extra RPC is wasted. Change both Drop impls to swap(true, AcqRel) so invalidate_prior sees the flag and short-circuits. Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
|
Addressed in f75b605:
Skipping I2 on purpose:
|
Summary
SELECT-1 through the kernel was ~700ms TTFR vs Thrift's ~400ms. This PR removes two avoidable sources of that fixed overhead on the sync-execute path (
Statement::execute):Drop
wait_timeout=10sfrom the request. Direct HTTP probes showed the server's own no-timeout default already waits up to ~10s anyway, but on a trivial query thewait_timeout=10spath returns ~260ms slower (~655ms vs ~397ms) — and more importantly, withwait_timeoutset the server returnsstate=SUCCEEDED(statement still alive), forcing an explicitCloseStatementround-trip. Without it, the server returnsstate=CLOSEDfor inline-result fast-path queries and reclaims server-side resources itself.Skip
CancelStatement+CloseStatementwhen the server already returnedstate=CLOSED. Plumbserver_statethroughExecuteResultand share anArc<AtomicBool> closedflag betweenStatementandExecutedStatement. When the server reports CLOSED in its execute response, the flag is pre-set totrue; explicitclose()on either handle, the lifecycle owner'sinvalidate_priorcleanup path, andDrop's fire-and-forget cleanup all short-circuit. (The PySQL connector callsStatement.close()after everyexecute(), so without sharing the flag the savings from Initial import: Databricks ADBC Rust driver as databricks-sql-kernel #1 are immediately undone by an extra round-trip in the lifecycle close.)Statement::submitstill passeswait_timeout=0sexplicitly — that path is defined by "return immediately with a PENDING/RUNNING statement_id", so omitting it would change behaviour rather than just speed it up.Benchmark results
scripts/bench_kernel_vs_thrift.pyagainst Dogfood, median over 5 samples (warm-up dropped).Before this PR (
fix/reader-nested-block-on, the PR #20 baseline this stacks on):After this PR:
SELECT 1 is now at parity with Thrift.
range(10k)got a much larger lift than expected (kernel now beats Thrift) — same root cause: the server can return inline-result +state=CLOSEDon small results, and both round-trips we used to fire are now redundant.Test plan
cargo build && cargo clippy --all-targets -- -D warningscargo test --lib— 366 passedbench_kernel_vs_thrift.py(each cell opens a fresh connection, executes, drains, closes — so it exercises the full statement lifecycle including the close path)Stacked on
fix/reader-nested-block-on)This pull request and its description were written by Claude Code.