Repository navigation
perf(client): share one cached DNS lookup per host across sessions - #453
vuanhphung wants to merge 12 commits into
Conversation
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>
There was a problem hiding this comment.
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.
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>
There was a problem hiding this comment.
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.
…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>
|
On the Nit ( |
Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
There was a problem hiding this comment.
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.
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>
There was a problem hiding this comment.
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.
…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>
There was a problem hiding this comment.
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.
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>
There was a problem hiding this comment.
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.
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>
Every
Sessionbuilds its ownreqwest::Client, and reqwest's default resolver runs an uncachedgetaddrinfofor 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 concurrentgetaddrinfocalls 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::Resolveimplementation, installed inDatabricksHttpClient::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 afterfork()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. Kernelmainbuilt from source with and without this change (same build flags), fresh process per run, runs interleaved:mainconnect(), 5 runs: all sessions open (median, range)connect()p99 (median)connect()p50 (median)connect()p99connect()p50On 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
mainabove hit them, two later 1 × 200 runs onmaindid not, and two of the three c800 runs onmaindid. 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 onmain, 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 warningsandcargo fmt --checkpass. The connector's kernel e2e suites (test_kernel_backend.py,test_kernel_tls.py) give the same results with this wheel as withmain. The 11 failures on this Reyden warehouse fail identically onmain(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):
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):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.