fix(r2dbc): bound batch resource normalization by jordan-wong · Pull Request #12935 · DataDog/dd-trace-java · GitHub
Skip to content

fix(r2dbc): bound batch resource normalization - #12935

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 4 commits into
masterfrom
fix/r2dbc-batch-normalization-12932
Oct 21, 2026
Merged

gh-worker-dd-mergequeue-cf854d[bot] merged 4 commits into
masterfrom
fix/r2dbc-batch-normalization-12932

Conversation

@jordan-wong

Copy link
Copy Markdown
Contributor

What

Bounds the work R2DBC does to name a Batch span. The resource is now built from the batch's first 10 statements, as their distinct normalized forms, instead of normalizing all statements joined together.

Why

Fixes #12932. A Batch reaches TraceProxyExecutionListener.beforeQuery once with all its statements, on the thread executing the query (usually an I/O event loop). The listener joined them with "; " and normalized the result. SQLNormalizer.normalize copies the rest of the buffer for each literal it replaces, so its cost grows quadratically with input size. A batch of thousands of distinct INSERTs with inlined values kept the event loop busy for tens of seconds.

Measured with SQLNormalizer.normalize on joined batches of ~280-character INSERT statements:

Statements Time
8,760 1.2 s
17,520 4.5 s
35,040 18.3 s

After this change a batch costs at most 10 single-statement normalizations (~16 µs each), whatever its size.

Behavior change

  • Same-template batches get that one template as their resource. For example, 10,000 inlined-value inserts produce INSERT INTO t (id, name) VALUES (?, ?). Before, the resource was every normalized statement joined together.
  • Mixed batches list their distinct templates in order, joined with ; , taken from the first 10 statements.
  • Batch entries use DBQueryInfo.ofStatement, so statements with distinct inlined literals don't fill the prepared-statement cache.
  • Single statements are unchanged.

Testing

  • R2dbcInstrumentationTest:
    • A 10,000-statement batch with distinct values produces one span with the single normalized INSERT resource. This test fails on master, where the resource is the whole joined batch.
    • A mixed INSERT/UPDATE/DELETE batch lists the distinct templates in order.
  • check, muzzle, latestDepTest: all pass.

SQLNormalizer itself is still quadratic on very large inputs, which also affects JDBC. That is left for a separate change.

🤖 Generated with Claude Code

A Batch reaches beforeQuery once with all its statements. The listener
joined them into one string and normalized it on the thread executing the
query (usually an I/O event loop). SQLNormalizer's cost grows
quadratically with input size, so a batch of thousands of distinct INSERTs
with inlined values kept the event loop busy for tens of seconds.

Build a batch's resource from its first 10 statements only, as their
distinct normalized forms; a batch of same-template statements now gets
that one template as its resource. Batch entries use DBQueryInfo.ofStatement
so distinct inlined-literal statements don't churn the prepared-statement
cache.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@jordan-wong
jordan-wong requested a review from a team as a code owner October 19, 2026 16:34
@jordan-wong
jordan-wong requested review from ValentinZakharov and removed request for a team October 19, 2026 16:34
@jordan-wong jordan-wong added type: bug fix Bug fix inst: others All other instrumentations tag: ai generated Largely based on code generated by an AI or LLM tag: apm integration toolkit Changes generated by DataDog/apm-instrumentation-toolkit as part of IDM AIT experimentation labels Oct 19, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T16:38:48.953757Z e13a030 PR opened
🔒 Security Review ✅ Completed 2026-10-19T16:37:46.751475Z e13a030 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e13a030e67

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: PASS

More details

The ten-statement cap is applied before batch normalization, so normalization work no longer grows with the number of batch entries. Single-statement resource naming remains unchanged.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit e13a030

@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

🎯 Code Coverage (details)
• Patch Coverage: 100.00%
• Overall Coverage: 68.82% (+8.97%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 70e2ac7 | Docs | Give us feedback!

@dd-octo-sts

dd-octo-sts Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

🟢 Java Benchmark SLOs — All performance SLOs passed

Suite Status
Startup 🟢 pass

SLO thresholds are defined here based on automatically generated metrics. A warning is raised when results are within 5% of the threshold.

PR vs. master results
Scenario Candidate master Δ (95% CI of mean)
startup:insecure-bank:iast:Agent 14.00 s 14.04 s [-0.9%; +0.4%] (no difference)
startup:insecure-bank:tracing:Agent 12.89 s 13.04 s [-2.0%; -0.3%] (maybe better)
startup:petclinic:appsec:Agent 17.16 s 16.47 s [-0.0%; +8.5%] (no difference)
startup:petclinic:iast:Agent 16.83 s 17.10 s [-2.4%; -0.8%] (maybe better)
startup:petclinic:profiling:Agent 16.09 s 16.84 s [-8.7%; -0.1%] (maybe better)
startup:petclinic:sca:Agent 17.01 s 16.99 s [-0.7%; +1.0%] (no difference)
startup:petclinic:tracing:Agent 16.10 s 16.25 s [-1.8%; -0.0%] (maybe better)

Commit: 70e2ac70 · CI Pipeline · Benchmarking Platform UI


Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion.

@jordan-wong
jordan-wong requested a review from mcculls October 20, 2026 14:27
…cache

The first statement of a batch still went through DBQueryInfo's shared
prepared-statement cache. With changing inlined values each batch added a new
key, which can evict reusable templates. Use ofStatement for all batch
entries; single statements keep the cache.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

@mcculls mcculls left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense - thanks!

jordan-wong and others added 2 commits October 21, 2026 12:08
Keep the normalized forms as UTF8BytesString instead of converting each to a
String; UTF8BytesString compares by content, so deduplication is unchanged,
and String.join accepts CharSequences.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@jordan-wong
jordan-wong enabled auto-merge October 21, 2026 17:53
@jordan-wong
jordan-wong added this pull request to the merge queue Oct 21, 2026
@dd-octo-sts

dd-octo-sts Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 21, 2026
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot merged commit fe25fd4 into master Oct 21, 2026
609 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

inst: others All other instrumentations tag: ai generated Largely based on code generated by an AI or LLM tag: apm integration toolkit Changes generated by DataDog/apm-instrumentation-toolkit as part of IDM AIT experimentation type: bug fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

R2DBC: SQLNormalizer runs on the I/O thread for every statement of a large Batch, blocking the event loop for ~55 s

2 participants