Skip to content

perf(client): share one cached DNS lookup per host across sessions - #453

Open
vuanhphung wants to merge 12 commits into
mainfrom
perf/rd-connect-dns-cache-2
Open

vuanhphung wants to merge 12 commits into
mainfrom
perf/rd-connect-dns-cache-2

Conversation

@vuanhphung

@vuanhphung vuanhphung commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Every Session builds its own reqwest::Client, and reqwest's default resolver runs an uncached getaddrinfo for each new connection. When many sessions open at once, every one of them looks up the same host at the same moment. On an Arclet (Ubuntu, /etc/resolv.conf → systemd-resolved stub at 127.0.0.53), the stub drops part of such a burst and glibc retries only after its 5 s timeout. 200 concurrent getaddrinfo calls for the workspace host from one process: 21–42% took exactly 5.0 s, even with the name already cached by resolved. With 1600 concurrent calls, lookups took 10–20 s and some failed. Through the Python connector (use_kernel=True), 200 threads connecting at once left 10–20% of the sessions stuck for ~5.7 s.

This adds a process-wide reqwest::dns::Resolve implementation, installed in DatabricksHttpClient::build_reqwest_client, so every kernel HTTP client uses it. It runs one lookup per host at a time and shares it with concurrent callers, and it reuses a successful answer for 30 s (the JVM's default positive TTL). Failures go to the callers already waiting on that lookup and are reused for 2 s, so retries against a host that doesn't resolve don't each start their own lookup. The blocking lookup runs on its own thread, which publishes the answer itself. So a caller that is cancelled (connect timeout) or whose runtime shuts down doesn't restart the lookup for the others. A lookup that hasn't succeeded after 1 s, or whose attempt failed, gets another attempt (up to 3). The first successful answer reaches every caller waiting on it, and an error reaches them only if every attempt failed. A child process after fork() starts its own lookup rather than join one its parent left in flight. A single dropped query therefore can't hold the burst that joined it for glibc's 5 s retry. Each call starts at the next address within the leading address family, as round-robin DNS would. Answers aren't invalidated on connect failure, so after a failover new connections may try old addresses for up to 30 s. This is documented in the module. Whatever the resolver, this turns the 800 identical lookups of an 800-session cold start into 1. It also still applies if sessions later share one HTTP client, because that client's new connections go through the same resolver.

Live pecotesting Reyden warehouse, Python connector main + databricks/databricks-sql-python#1024, 8 cores. Kernel main built from source with and without this change (same build flags), fresh process per run, runs interleaved:

kernel main this PR
1 proc × 200 concurrent connect(), 5 runs: all sessions open (median, range) 5.73 s (5.70–6.05) 0.70 s (0.68–0.78)
same, connect() p99 (median) 5.70 s 0.70 s
same, connect() p50 (median) 0.69 s 0.66 s
4 procs × 200 sessions × 10 queries (c800), all sessions open, 3 runs 1.16, 5.91, 5.89 s 1.19, 1.17, 2.12 s
same, connect() p99 0.88, 5.71, 5.73 s 0.84, 0.85, 0.91 s
same, connect() p50 0.69, 0.69, 0.73 s 0.68, 0.68, 0.75 s

On a second machine (also with the systemd-resolved stub, 16 cores, 4 × 200 live), no stall showed up and connect times were the same with and without this change. There it is a robustness fix rather than a speedup, and an isolated 4 × 200 re-measurement there (PAT, local resolver) also showed no measurable difference. It matters where the resolver drops or throttles a burst of identical lookups, as the systemd-resolved stub does here. The stalls are intermittent. All 5 of the 1 × 200 runs on main above hit them, two later 1 × 200 runs on main did not, and two of the three c800 runs on main did. Query-phase QPS in these short c800 runs moves with warehouse load in both directions, so it isn't a signal here. The last two c800 pairs and two extra 1 × 200 pairs (all-open 0.71 / 0.77 s with the change vs 0.75 / 0.77 s on main, neither stalled) used the revised resolver from the review round.

New unit tests cover single-flight across 64 concurrent callers, per-host entries, TTL expiry, failures shared but not cached, empty answers, a lookup that outlives the runtime that started it and a cancelled caller (exactly one lookup each), and address rotation. cargo test, cargo clippy --all-targets -- -D warnings and cargo fmt --check pass. The connector's kernel e2e suites (test_kernel_backend.py, test_kernel_tls.py) give the same results with this wheel as with main. The 11 failures on this Reyden warehouse fail identically on main (DDL and metadata are not supported on Real-Time warehouses).

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

kernel main this PR
Average QPS (setup included) 2181 (1453-2183) 2073 (1371-2102)
Steady QPS (after all sessions open) 2220 (1489-2220) 2108 (1410-2138)
Query latency p50 (ms) 349 (348-525) 360 (358-552)
Client CPU per query (ms) 1.20 (1.14-1.23) 1.16 (1.14-1.23)
All 800 sessions open (s) 2.86 (2.60-5.50) 2.63 (2.47-3.64)
Threads per process 3638 (3638-3652) 3436 (3425-3436)

One round of each arm hit a warehouse-wide slowdown. The 5 s DNS-retry connect stall this PR removes is rare on kernel main (0-1 sessions/round) but common with the shared runtime (#457: 29-86 of 800 sessions/round at ~6.5 s); #457 + this PR together: 0 stalled sessions, all sessions open 5.0 -> 2.37 s, avg QPS 2141 -> 2198.

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

kernel main this PR
Average QPS (setup included) 6158 (6135-6181) 6167 (6142-6192)
Peak QPS (best second) 6572 (6555-6590) 6512 (6508-6517)
Query latency p50 (ms) 123 (123-123) 124 (123-124)
Client CPU per query (ms) 1.33 (1.32-1.34) 1.32 (1.32-1.32)

Average QPS change per round: +0.9%, -0.6%. With #453 and #457 together: +1.3%, +2.1%.

This pull request and its description were written by Isaac.

Every Session builds its own reqwest client, and reqwest's default
resolver runs an uncached getaddrinfo per new connection. When hundreds
of sessions open at once the burst overflows local stub resolvers
(systemd-resolved drops the excess, glibc retries after 5 s), so part
of the opens stall for 5 s or more. Route every kernel client through a
process-wide resolver that runs one lookup per host at a time and
reuses a successful answer for 30 s.

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: 1 Medium · 2 Low

The PR looks sound overall: one in-flight lookup per host with a 30 s TTL fixes the burst problem it targets. One medium concern: the dedicated lookup thread doesn't actually let the lookup outlive the caller that started it, because OnceCell re-runs init when that caller is dropped, and the test meant to cover this can't fail.

Comment thread src/client/dns.rs Resolved
Comment thread src/client/dns.rs Resolved
Comment thread src/client/dns.rs Resolved
With tokio's OnceCell, a cancelled caller (e.g. reqwest's connect timeout)
handed initialization to the next waiter, which started a second lookup
and dropped the first thread's answer. The lookup thread now publishes
into a watch channel that every waiter reads, so a cancelled caller no
longer restarts it. Also rotate the first address per call within the
leading address family, as round-robin DNS would.

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: 1 Medium · 1 Low

The PR is well built and well tested; I have one medium concern about how hedged attempts handle errors, plus a low-severity concern about fork safety. Nit: lookup_survives_the_runtime_that_started_it relies on a 20 ms sleep to make sure the first runtime's task has started. On a slow CI machine the test can pass without testing what its name says.

Comment thread src/client/dns.rs Resolved
Comment thread src/client/dns.rs Resolved
…d lookups

The first attempt to answer was published even when it was an error, so a
fast EAI_AGAIN from an overloaded stub could fail every waiting session
while another attempt would have succeeded. A failed attempt now starts
the next one at once, and the error is returned only when every attempt
failed. Attempt threads catch panics so each one reports back.

An unanswered entry older than 60 s is replaced: after fork() the child
inherits in-flight entries whose lookup threads don't exist there.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
@vuanhphung

Copy link
Copy Markdown
Collaborator Author

On the Nit (lookup_survives_the_runtime_that_started_it relied on a 20 ms sleep): fixed in c5b9cbb. The test now waits until the first runtime's lookup has started (the lookup's call count, 10 s cap, then the test fails) before it shuts that runtime down.

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: 1 Low

Looks good. The single-flight, hedging and TTL logic is correct, a lookup isn't lost when its caller is cancelled or its runtime shuts down, and reqwest's 30 s connect_timeout bounds the wait for an entry orphaned by fork(). I found one low-severity issue: an exact attempt-count assertion that can flake under CI scheduling delays.

Comment thread src/client/dns.rs Resolved
On a loaded runner the coordinator can start another attempt before the
second one reports back. What the tests check is that waiters get the
successful answer; the attempt count only has to stay within the cap.

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: 3 Low

Looks good overall. The single-flight resolver is sound: cancellation and runtime shutdown are handled by the publishing thread, wait_for correctly reads already-published answers after the sender drops, and IP-literal hosts and port override still go through reqwest/hyper as before. There are a few low-severity concerns: hedging also re-runs final (NXDOMAIN) failures, the post-fork orphaned-entry window is long, and one test depends on system uptime.

Comment thread src/client/dns.rs Resolved
Comment thread src/client/dns.rs Resolved
Comment thread src/client/dns.rs Resolved
vuanhphung and others added 2 commits November 14, 2026 01:30
…t process

A host that doesn't resolve got three attempts per lookup, and failures
weren't reused, so retries from every session multiplied the load on the
stub resolver this exists to protect. A failure is now reused for 2 s
(the JVM caches negative lookups for 10 s by default).

Each entry records the process that started it. After fork() the child
replaces an inherited in-flight entry at once instead of after the 60 s
backstop. The backstop and the negative TTL are fields, so the tests set
short ones instead of depending on system uptime.

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

Looks good. The resolver is carefully built: it covers cancellation, runtime shutdown, fork, panic and empty answers, and the tests are thorough. Two low-severity notes: the lookup threads have no overall time limit, and no test checks that the HTTP client actually installs the resolver.

Comment thread src/client/dns.rs Resolved
Comment thread src/client/http.rs Resolved
If every attempt blocked (getaddrinfo stuck on an unreachable nameserver),
the coordinator never published, and the 60 s backstop started another
coordinator and attempts beside the stuck ones each time. The coordinator
now publishes TimedOut once that deadline passes, so waiters get an answer
and the slot settles. A test also checks that DatabricksHttpClient actually
resolves through the shared cache.

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: 3 Low

Looks good: the design is careful (per-host single flight, lookup threads that outlive cancelled callers, hedging, fork and stuck-lookup handling) and well tested. Three low-severity notes: extra queries for hosts that don't resolve, a thread spawn while the global lock is held, and a timing-sensitive test.

Comment thread src/client/dns.rs Resolved
Comment thread src/client/dns.rs Resolved
Comment thread src/client/dns.rs Resolved
entry() spawned the coordinator thread while holding the process-wide
entries mutex, so every resolve for every host waited behind each spawn.
The slot (and its channel) is now inserted under the lock and the thread is
spawned after it is released. The immediate retry after any failure is
documented as intended (getaddrinfo errors can't be told apart), and the
negative-TTL test is split into two that don't depend on timing.

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 — d8d7e14f Deployed Nov 14, 2026 by vuanhphung via followup #3027
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