Skip to content

perf(python): share one tokio runtime across sessions - #457

Open
vuanhphung wants to merge 3 commits into
mainfrom
perf/rd-shared-runtime
Open

vuanhphung wants to merge 3 commits into
mainfrom
perf/rd-shared-runtime

Conversation

@vuanhphung

@vuanhphung vuanhphung commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Every databricks_sql_kernel.Session builds its own multi-thread tokio runtime, which starts one worker per CPU. A Python process that holds many sessions therefore runs sessions × CPUs kernel threads: 200 sessions on 8 cores held 1,600 dbx-sql-kernel threads, and the connector's 800-session benchmark (4 processes, 16 cores) peaked at 3,635 threads per process. Each runtime also has its own I/O driver and timer wheel, so every request pays for wakeups across thousands of mostly idle threads.

Sessions now share one process-wide runtime, created on first use. A child process after fork() builds its own, because the parent's worker threads don't exist there; the inherited runtime is leaked rather than dropped. KernelRuntime::release still shuts down standalone runtimes but never the shared one. A side effect: background cleanup queued when a session is dropped without close() (the fire-and-forget DeleteSession) used to be cancelled when that session's private runtime shut down. It now completes. Conversely, close() no longer waits for a runtime to stop, so queued work such as that cleanup or telemetry can still run after it returns. Abandoned result streams still stop their downloads: the CloudFetch and inline-streaming providers cancel their tasks when dropped.

Trade-off: CPU-heavy tasks (CloudFetch chunk decode) now share one pool of workers across all sessions instead of being time-sliced by the OS among per-session workers.

Measurements. databricks-sql-connector use_kernel=True, the C800 cold-start benchmark (each session runs a small query back to back). Main vs this PR, interleaved, 2 runs each:

Local TLS mock, 50 ms server time, 4 × 200 sessions, 6 client cores main this PR
avg QPS 3,452 / 3,500 4,037 / 3,972 (+15%)
peak QPS 4,132 / 4,081 4,828 / 4,679 (+15%)
steady QPS 3,790 / 3,861 4,411 / 4,335 (+14%)
query p50 203 / 201 ms 174 / 176 ms
client CPU 202 / 208 s 202 / 201 s (for ~15% more queries)
threads per process 1,415 221
peak RSS per process 294 / 289 MB 247 / 246 MB
Live Reyden warehouse, 1 × 200 sessions, 8 cores, 3 runs (run 1 under heavy shared load) main this PR
avg QPS 110 / 489 / 484 173 / 531 / 567
peak QPS 209 / 844 / 827 720 / 751 / 823
query p50 1,225 / 239 / 236 ms 651 / 253 / 246 ms
failed queries (server errors) 242 / 521 / 555 362 / 317 / 380
client CPU 18.9 / 54.4 / 57.6 s 20.2 / 42.8 / 45.8 s
threads per process 2,019 253–260

The live warehouse is the bottleneck at this load (doubling sessions does not raise its QPS), so live QPS stays within noise. Client CPU per query drops ~20% and threads drop 87%.

Connect is where this shows on live. Opening 200 connector sessions at once (8-core client, 2 runs each), main took 5.38 / 5.29 s, with 52 / 19 sessions stuck about 5 s and open p99 5.3 s. With this PR it took 0.31 / 0.34 s, with no stalls and p99 306 / 326 ms. The 5 s shape matches glibc's DNS retry, and every session open on main also starts a runtime with a worker per CPU, so a 200-session burst spawns 1,600 threads. In full 1 × 200 benchmark runs, all sessions were open after 0.66 / 0.67 s instead of 5.71 / 5.87 s.

With the connector's pending small-result conversion fix (databricks/databricks-sql-python#1048) applied, the kernel becomes a larger share of the client cost. On the same mock 4 × 200 setup: main 7,684 / 7,621 avg QPS (peak 9,213 / 9,014), this PR 10,470 / 10,273 (+35%; peak 11,313 / 11,584), and 11,205 / 11,171 (+46%) together with #458. A separate run on 16 client cores with a 20 ms mock (medians of 2): avg 14,851 → 17,002 QPS (+14.5%), peak 16,227 → 18,501, CPU per query 0.718 → 0.601 ms, threads per process 3,419 → 235, and all 800 sessions open in 1.94 s instead of 2.73 s.

Tests. New no-network tests in pyo3/tests/test_runtime.py: extra sessions add no runtime threads, a forked child opens a session and queries without hanging (and the parent keeps working), and the CloseStatement/DeleteSession work of a closed and a dropped session completes while another session keeps running. On main the thread-count and dropped-session tests fail (82 threads vs a bound of 26; DeleteSession never sent). Also a Rust unit test that the shared runtime is reused and survives release. cargo +stable fmt, cargo clippy --all-targets -- -D warnings and cargo test in pyo3/ pass; the pyo3 pytest suite passes, live tier included (50 passed).

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 a1d14fd. Median (min-max):

kernel main this PR
Average QPS (setup included) 2219 (1657-2234) 2153 (1976-2178)
Steady QPS (after all sessions open) 2268 (1741-2275) 2189 (1998-2214)
Query latency p50 (ms) 334 (333-345) 352 (339-386)
Client CPU per query (ms) 1.24 (1.22-1.29) 1.05 (1.04-1.06)
All 800 sessions open (s) 8.01 (2.94-17.47) 7.31 (7.27-7.75)
Threads per process 3674 (3637-3675) 444 (438-445)
Peak RSS per process (MB) 406 (392-410) 396 (394-402)

QPS is slightly lower here (within ~3%). 29-86 of 800 sessions per round hit a ~5 s DNS-retry stall at connect (vs 0-3 on kernel main), which #453 removes: with #453, avg QPS 2141 -> 2198, all sessions open 5.0 -> 2.37 s, 0 stalled sessions.

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 a1d14fd, run in rotation with the other factory PRs, 2 rounds. Median (min-max):

kernel main this PR
Average QPS (setup included) 6158 (6135-6181) 6279 (6206-6351)
Peak QPS (best second) 6572 (6555-6590) 6684 (6641-6728)
Query latency p50 (ms) 123 (123-123) 120 (120-121)
Client CPU per query (ms) 1.33 (1.32-1.34) 1.24 (1.23-1.25)

Average QPS change per round: +1.2%, +2.8%. With #453 and #457 together: +1.3%, +2.1%.

This pull request and its description were written by Isaac.

Every pyo3 Session built its own multi-thread runtime with a worker per
CPU, so 200 sessions on 16 cores held 3,200 kernel threads. Sessions now
share one process-wide runtime, rebuilt in a forked child.

Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
Co-authored-by: Isaac <no-reply@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, with one low-severity fork-safety concern. The shared-runtime change is sound: the pid check plus leaking the inherited runtime correctly handles fork(), release can't shut down the shared runtime because the static keeps a reference, and the new Python and Rust tests cover reuse, fork and background cleanup.

Comment thread pyo3/src/runtime.rs Resolved
Building the runtime spawns every worker, and a fork() taken while the
lock is held would leave it locked forever in the child. The lock now
covers only the pid check and the install; a runtime built by a thread
that lost the race is dropped after the lock is released.

Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
Co-authored-by: Isaac <no-reply@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: 3 Low

Looks good overall: the shared-runtime design is sound, the fork handling is reasoned through, and the new tests fail on main. Three low concerns: close() no longer drains or stops background work, so the existing docs overstate what it guarantees; the fork-safety claim about the slot mutex is stronger than the code supports; and in-flight tasks from a closed session are no longer cancelled.

Comment thread pyo3/src/runtime.rs Resolved
Comment thread pyo3/src/runtime.rs Resolved
Comment thread pyo3/src/session.rs Resolved
release() never stops the shared runtime, so work a closed session
queued can still run after close() returns. Say so in the
KernelRuntime and release docs, and narrow the fork note on the slot
lock to what holds: Python forks hold the GIL, which every critical
section also holds.

Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
Co-authored-by: Isaac <no-reply@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 — a1d14fda Deployed Nov 13, 2026 by vuanhphung via followup #2818
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