Repository navigation
perf(python): share one tokio runtime across sessions - #457
vuanhphung wants to merge 3 commits into
Conversation
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>
There was a problem hiding this comment.
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.
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>
There was a problem hiding this comment.
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.
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>
Every
databricks_sql_kernel.Sessionbuilds 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,600dbx-sql-kernelthreads, 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::releasestill shuts down standalone runtimes but never the shared one. A side effect: background cleanup queued when a session is dropped withoutclose()(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: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 survivesrelease.cargo +stable fmt,cargo clippy --all-targets -- -D warningsandcargo testinpyo3/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):
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):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.