Repository navigation
perf(statement): use INLINE results for repeated small queries on Real-Time sessions - #460
vuanhphung wants to merge 1 commit into
Conversation
…eal-Time sessions With the INLINE_OR_EXTERNAL_LINKS disposition, Lakehouse Real-Time persists every result to cloud storage, even small inline ones, and keeps its internal result stream open while it does. At a few hundred concurrent statements this stalls the warehouse and fails statements with "h2 protocol error" / "execution already exists". On Real-Time sessions, a read-only statement now requests INLINE once the same SQL text has returned a small result (<= 4 MiB) in the last 10 minutes. If the result outgrows the inline limit (BAD_REQUEST or a truncated manifest), the statement runs again once with INLINE_OR_EXTERNAL_LINKS. The client conf realtime_inline_small_results=false turns this off. Signed-off-by: Vu Anh Phung <vu.phung@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
efbeb38 to
45d15ab
Compare
There was a problem hiding this comment.
Verdict: 2 Medium · 1 Nit
The adaptive-INLINE design is sound and well-tested. I found two medium issues: the read-only verb check lets DML that starts with WITH/FROM through, and the new truncated-manifest check also changes behavior for non-Real-Time sessions that use the CloudFetch-disabled INLINE fallback. There is also one low note: the re-run gets a fresh full timeout instead of the remaining budget.
Other findings
- ⚪ Nit — The re-run after an INLINE overflow is given the same full
timeoutas the first attempt. The first attempt has already used up part of the budget, so a sync execute with a client query timeout can now take up to about 2× the configured timeout before it returns. Consider passing the remaining budget (deadline minus elapsed) to the re-run so the caller's timeout stays an upper bound.
| .to_ascii_uppercase(); | ||
| matches!( | ||
| verb.as_str(), | ||
| "SELECT" | "WITH" | "VALUES" | "TABLE" | "FROM" | "SHOW" | "DESCRIBE" | "DESC" | "EXPLAIN" |
There was a problem hiding this comment.
🟡 Medium — WITH and FROM don't always mean a read-only statement in Spark/Databricks SQL. WITH cte AS (...) INSERT INTO t SELECT * FROM cte is valid, and so is the Hive-style multi-insert FROM src INSERT INTO t1 SELECT ... INSERT INTO t2 SELECT .... Both pass is_read_only_query, so the doc comment's claim that re-running is side-effect free doesn't hold for them.
Today a DML result (affected-row counts) is tiny, so the overflow/truncation re-run is unlikely to fire. But the overflow re-run path in execute_statement depends on this check to be safe, and any server-side truncation or byte-limit error on such a statement would execute the insert twice. Two fixes would work: look past a leading WITH/FROM for an INSERT/MERGE/UPDATE/DELETE keyword, or drop FROM (which is rare as a leading verb for reads) and document the WITH caveat. Also add a negative case like WITH t AS (SELECT 1) INSERT INTO x SELECT * FROM t to read_only_query_detection_skips_comments_and_parentheses.
There was a problem hiding this comment.
Fixed in 023b734. WITH and FROM now count as read-only only when no INSERT, MERGE, UPDATE or DELETE token appears anywhere in the text. A false negative only skips the optimization. The unit test has new negative cases: WITH t AS (SELECT 1) INSERT INTO x ..., WITH ... MERGE ... UPDATE SET *, and the Hive multi-insert FROM src INSERT INTO t1 ... INSERT INTO t2 .... It also has a positive case where a column is named updated_at.
There was a problem hiding this comment.
I couldn't find the change described for 023b734 in the current head. is_read_only_query (line 115) still ends in the same matches! on the leading verb, with "WITH" and "FROM" accepted unconditionally. Grepping src/client/sea.rs finds no INSERT/MERGE/UPDATE/DELETE token scan inside that function. The test has no WITH t AS (SELECT 1) INSERT INTO x ... case, no Hive multi-insert case and no updated_at case either; the only WITH case is the read-only WITH t AS (SELECT 1) SELECT * FROM t. The commit may not have been pushed to this branch, or it may have landed somewhere else. Once it's on the PR head, I'll re-check it. The approach you described (treat WITH/FROM as read-only only when no DML keyword appears, and accept the occasional false negative) addresses the concern.
| return Err(error); | ||
| } | ||
| }; | ||
| if inline |
There was a problem hiding this comment.
🟡 Medium — The new truncated-manifest check runs whenever inline is true, and inline also covers the existing inline_disposition fallback (CloudFetch disabled, warehouse rejected can_cloud_download). That fallback applies to standard sessions too, and to metadata calls that go through execute_and_read.
Before this PR, those sessions returned the inline result as-is when manifest.truncated was set. Now they get a hard data_loss error and the statement is closed. No retry happens for them, because only execute_statement retries and only when prefers_inline was true. This contradicts the PR description's "Standard (non-Real-Time) sessions are unchanged", and no test covers it. If the change is intended (refusing silently truncated data is arguably better), call it out in the description and add a wiremock test for the CloudFetch-disabled path. If not, apply the check only to the adaptive-inline attempt (pass the caller's inline flag separately from the effective disposition).
There was a problem hiding this comment.
Kept on purpose: with CloudFetch disabled there are no links to re-run with, and returning a server-truncated result as complete is the data-loss case ES-2127499 describes. In 023b734 the PR description calls this out as a behavior change for the #456 fallback, and the new wiremock test cloudfetch_disabled_inline_fallback_rejects_truncated_result pins it. A row_limit that explains the truncation still passes through unchanged.
There was a problem hiding this comment.
Rejecting server-truncated inline results when CloudFetch is disabled seems like a reasonable call, and I'm fine with keeping the behavior change. What I can't confirm is the follow-through. In the checked-out head, nothing matches cloudfetch_disabled_inline_fallback_rejects_truncated_result, and the only references to inline_result_truncated are its definition (line 154) and the call at line 1147 in src/client/sea.rs. No test exercises the CloudFetch-disabled path. Commit 023b734 may not be in this checkout yet, or the test may not have been pushed. Once the test and the description note are on the branch, I'm happy to close this out.
|
Closing: the INLINE_OR_EXTERNAL_LINKS high-concurrency gap is being fixed server-side (ES-2200519 / SC-249818), so the kernel keeps using INLINE_OR_EXTERNAL_LINKS for Real-Time sessions. |
|
On the Nit about the re-run timeout: the re-run shares the caller's budget. |
What did you change, and why?
Behavior change (Lakehouse Real-Time sessions only): a read-only statement whose SQL text recently returned a small result is now executed with
disposition: INLINEinstead ofINLINE_OR_EXTERNAL_LINKS. The client confrealtime_inline_small_results=falseturns this off. It is also accepted throughsession_conf, so for the Python connectorsession_configuration={"realtime_inline_small_results": "false"}works.Problem. For the hybrid
INLINE_OR_EXTERNAL_LINKSdisposition, a Real-Time warehouse persists every result to cloud storage, small inline results included. It keeps its internal Reyder-to-Reyden HTTP/2 stream open for the whole upload. With a few hundred concurrent statements, this saturates the server's streams:h2 protocol error: error reading a body from connection,h2 protocol error: http2 error, oroperation was canceled;execution already exists;Server tickets: ES-2200519 (the server-side fix is merged, but its rollout is paused), SC-249818, SC-248618, ES-2349637. The same requests sent with
INLINEshow none of this.INLINEcan't be the default, though: results over 25 MiB fail with "Inline byte limit exceeded", or come back silently truncated on servers without the ES-2127499 fix.When
INLINEis used. All of these must hold:x-databricks-rt-session: true);SELECT,VALUES,TABLE,SHOW,DESCRIBE, orEXPLAIN, or it isWITH/FROMand noINSERT,MERGE,UPDATEorDELETEkeyword appears anywhere in the text;The first execution of any SQL text keeps
INLINE_OR_EXTERNAL_LINKS. The session remembers hashes of SQL texts only, never parameter values, with at most 1,024 entries.When a statement is re-run. An
INLINEattempt is discarded, and the statement runs exactly once more withINLINE_OR_EXTERNAL_LINKS, in two cases:BAD_REQUEST"Inline byte limit exceeded";truncated: truethat the caller'srow_limitdoes not explain.That SQL text then leaves the small set. A truncated result is never returned. The cost: a remembered-small query whose result suddenly exceeds 25 MiB executes twice, once wasted. Live, a parameterized query that grew from 1k to 1M rows took 1.5 s instead of 0.8 s for that one execution. Standard (non-Real-Time) sessions keep
INLINE_OR_EXTERNAL_LINKS. One more behavior change: a session that already usesINLINEbecause CloudFetch is disabled (the #456 fallback) now gets aDataLosserror for atruncatedmanifest, instead of silently receiving partial rows. It has no links to re-run with.Stacked on #456.
How do you know it works?
SELECTswitches toINLINE, and DML never does;INLINE;WITH ... INSERTand Hive multi-insert never useINLINE;INLINEresult on a CloudFetch-disabled session fails instead of returning partial rows.-D warningspass.cargo test --no-fail-fastpasses.mainwithuse_kernel=True, TPC-H Q6 back to back. Runs were isolated (query history showed no other sessions) and interleaved, client on 8 cores:With this change no 1-second bucket fell below 480 QPS; on 1.4.0 the series repeatedly dropped to zero. Large results still come back complete: a 600k-row (~100 MB) result twice, and the growing parameterized query above.
This pull request and its description were written by Isaac.