Repository navigation
perf(python): share one tokio runtime across sessions #457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vuanhphung
wants to merge
3
commits into
main
Choose a base branch
from
perf/rd-shared-runtime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| # Copyright (c) 2026 Databricks, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Sessions share one process-wide tokio runtime (no network).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import base64 | ||
| import contextlib | ||
| import gc | ||
| import itertools | ||
| import json | ||
| import os | ||
| import sys | ||
| import threading | ||
| import time | ||
| from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer | ||
|
|
||
| import databricks_sql_kernel as k | ||
| import pyarrow as pa | ||
| import pytest | ||
|
|
||
| SESSIONS = "/api/2.0/sql/warehouses/test-wh/sessions" | ||
|
|
||
|
|
||
| def _attachment(): | ||
| batch = pa.record_batch([pa.array([1, 2], type=pa.int64())], names=["n"]) | ||
| buffer = pa.BufferOutputStream() | ||
| with pa.ipc.new_stream(buffer, batch.schema) as writer: | ||
| writer.write_batch(batch) | ||
| return base64.b64encode(buffer.getvalue()).decode() | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def _server(): | ||
| """Local SEA stand-in: unique session / statement ids, inline results. | ||
| Yields ``(port, requests)``; ``requests`` collects ``(method, path)``.""" | ||
| requests = [] | ||
| ids = itertools.count(1) | ||
| attachment = _attachment() | ||
|
|
||
| class Handler(BaseHTTPRequestHandler): | ||
| def respond(self, body, headers=()): | ||
| requests.append((self.command, self.path)) | ||
| self.rfile.read(int(self.headers.get("Content-Length", 0))) | ||
| payload = json.dumps(body).encode() | ||
| try: | ||
| self.send_response(200) | ||
| self.send_header("Content-Length", str(len(payload))) | ||
| for name, value in headers: | ||
| self.send_header(name, value) | ||
| self.end_headers() | ||
| self.wfile.write(payload) | ||
| except (BrokenPipeError, ConnectionResetError): | ||
| pass # The client went away (e.g. a pooled connection was dropped). | ||
|
|
||
| def do_POST(self): | ||
| if self.path == SESSIONS: | ||
| self.respond({"session_id": f"s{next(ids)}"}, [("x-databricks-rt-session", "true")]) | ||
| return | ||
| self.respond( | ||
| { | ||
| "statement_id": f"q{next(ids)}", | ||
| "status": {"state": "SUCCEEDED"}, | ||
| "manifest": { | ||
| "format": "ARROW_STREAM", | ||
| "schema": { | ||
| "column_count": 1, | ||
| "columns": [ | ||
| {"name": "n", "type_name": "BIGINT", "type_text": "BIGINT", "position": 0} | ||
| ], | ||
| }, | ||
| "total_chunk_count": 1, | ||
| "total_row_count": 2, | ||
| }, | ||
| "result": {"chunk_index": 0, "row_offset": 0, "row_count": 2, "attachment": attachment}, | ||
| } | ||
| ) | ||
|
|
||
| def do_DELETE(self): | ||
| self.respond({}) | ||
|
|
||
| def log_message(self, *_args): | ||
| pass | ||
|
|
||
| with ThreadingHTTPServer(("127.0.0.1", 0), Handler) as server: | ||
| threading.Thread(target=server.serve_forever, daemon=True).start() | ||
| try: | ||
| yield server.server_port, requests | ||
| finally: | ||
| server.shutdown() | ||
|
|
||
|
|
||
| def _session(port): | ||
| return k.Session( | ||
| host=f"http://127.0.0.1:{port}", | ||
| http_path="/sql/1.0/warehouses/test-wh", | ||
| access_token="test-token", | ||
| telemetry_enabled=False, | ||
| ) | ||
|
|
||
|
|
||
| def _query(session): | ||
| stmt = session.statement() | ||
| stmt.set_sql("SELECT 1") | ||
| executed = stmt.execute() | ||
| rows = 0 | ||
| while (batch := executed.fetch_next_batch()) is not None: | ||
| rows += batch.num_rows | ||
| executed.close() | ||
| stmt.close() | ||
| return rows | ||
|
|
||
|
|
||
| def _wait_for(predicate, timeout_s=5.0): | ||
| deadline = time.monotonic() + timeout_s | ||
| while not predicate(): | ||
| if time.monotonic() > deadline: | ||
| return False | ||
| time.sleep(0.02) | ||
| return True | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not os.path.isdir("/proc/self/task"), reason="counts threads via /proc") | ||
| def test_sessions_do_not_add_runtime_threads(): | ||
| with _server() as (port, _): | ||
| first = _session(port) | ||
| baseline = len(os.listdir("/proc/self/task")) | ||
| others = [_session(port) for _ in range(8)] | ||
| try: | ||
| # A runtime per session added a worker per CPU for each one. | ||
| assert len(os.listdir("/proc/self/task")) < baseline + len(others) | ||
| finally: | ||
| for session in [first, *others]: | ||
| session.close() | ||
|
|
||
|
|
||
| def test_cleanup_of_closed_and_dropped_sessions_completes(): | ||
| with _server() as (port, requests): | ||
| keep = _session(port) | ||
| closed = _session(port) | ||
| assert _query(closed) == 2 | ||
| closed.close() | ||
| dropped = _session(port) | ||
| assert _query(dropped) == 2 | ||
| dropped_path = f"{SESSIONS}/{dropped.session_id}" | ||
| # Drop fires DeleteSession in the background on the shared runtime. | ||
| del dropped | ||
| gc.collect() | ||
| assert _query(keep) == 2 | ||
|
|
||
| def deletes(): | ||
| return [path for method, path in requests if method == "DELETE"] | ||
|
|
||
| def session_deletes(): | ||
| return [path for path in deletes() if "/statements/" not in path] | ||
|
|
||
| assert _wait_for(lambda: len(deletes()) - len(session_deletes()) == 3) | ||
| assert _wait_for(lambda: dropped_path in session_deletes()) | ||
| keep.close() | ||
| assert len(session_deletes()) == 3 | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not hasattr(os, "fork"), reason="needs fork()") | ||
| def test_forked_child_opens_sessions(): | ||
| with _server() as (port, _): | ||
| parent = _session(port) | ||
| assert _query(parent) == 2 | ||
| pid = os.fork() | ||
| if pid == 0: | ||
| # The parent's workers don't exist here; the child needs its own runtime. | ||
| code = 1 | ||
| try: | ||
| child = _session(port) | ||
| if _query(child) == 2: | ||
| code = 0 | ||
| child.close() | ||
| finally: | ||
| sys.stdout.flush() | ||
| os._exit(code) | ||
| deadline = time.monotonic() + 30 | ||
| while time.monotonic() < deadline: | ||
| done, status = os.waitpid(pid, os.WNOHANG) | ||
| if done: | ||
| assert os.waitstatus_to_exitcode(status) == 0 | ||
| break | ||
| time.sleep(0.05) | ||
| else: | ||
| os.kill(pid, 9) | ||
| os.waitpid(pid, 0) | ||
| pytest.fail("query hung in the forked child") | ||
| assert _query(parent) == 2 | ||
| parent.close() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.