Skip to content

Read fetched Kafka headers without copying them first - #13737

Open
mcculls wants to merge 5 commits into
masterfrom
mcculls/kafka-record-header-lookup
Open

mcculls wants to merge 5 commits into
masterfrom
mcculls/kafka-record-header-lookup

Conversation

@mcculls

@mcculls mcculls commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

What Does This Do

Extracts propagation keys and UTF-8 values directly from accessible Kafka header-buffer arrays to avoid copies.

Motivation

Use the approach from #13600 without changing the structure of RecordHeader or requiring FieldAccessors.

Additional Notes

  • Initialize access once per class loader through Metadata.update(...). This avoids initialization checks on each header operation. After late attachment, activation can wait for the next metadata response.
  • Use private getter handles, with field reflection during setup on Java 8. Kafka's public accessors remain the fallback before setup and for unsupported layouts, subclasses, and Base64 decoding.
  • Use Kafka's key() accessor for buffers without accessible arrays. This preserves synchronization for concurrent header reads in Kafka 4.4.
  • Headers remain lazy, so retained records can retain the backing batch storage.

Validation

On Java 17, all 16 focused cases passed with Kafka 3.8.0 and all 17 passed with Kafka 4.4.0, including concurrent reads. Tests cover delayed activation, buffer variants, accessor fallback, and propagation equivalence. The regression test for keys without accessible arrays fails without the fallback.

Both test source sets, JMH compilation, and Spotless pass. Earlier validation covered Java 8. Muzzle and full Kafka suites remain for CI.

Benchmarks

Mean allocation was 12.3–15.9% lower than baseline across all six cases. Mean processing time was 2.5–10.9% lower on Java 8/17 and 3.3–4.4% higher on Java 21. Time means relative to #13600 ranged from -0.7% to +2.6%; the tables report between-fork variation.

Revisions: branch a986d0c0976, baseline dd09e2418b7, #13600 cdee7363337. Header implementations are compiled from these revisions. The shared tracer/runtime and timed workload are unchanged from the previous comparison; all use Kafka 3.8.0.

Method: JMH 1.36 with -prof gc, on the same macOS arm64 machine. Zulu HotSpot 8u514, 17.0.21.1, and 21.0.13.1. Five fresh forks per case, run serially (90 total); JDK and header-count order rotate, while implementation order is fixed within each case. One thread, 5 × 2 s warmup, 7 × 3 s measurement. JVM flags: -Xms512m -Xmx512m -XX:+UseG1GC -XX:ActiveProcessorCount=4 -XX:ParallelGCThreads=2 -XX:ConcGCThreads=1.

Each invocation parses and consumes 500 records through the production TracingIterator and CoreTracer: six propagation headers, 0/3 application headers, 100-byte values, distinct parent contexts, and 10% sampling. DSM and time-in-queue are off. The application reads payloads only. All 90 forks passed checks for extraction, header structure, 500 completed spans, parent links, and sampling; candidate forks also verified metadata-driven setup activation.

This measures the latest header code in steady-state processing of heap-backed Kafka 3.8 headers with a blackhole writer. It excludes network traffic, full client bookkeeping, setup, and trace serialization. Kafka 4.4 and direct/read-only key fallback performance are outside this measurement.

Processing time: ns/record, mean ± sample standard deviation across five forks.

Java Extra headers Baseline This branch #13600
8 0 2155.8 ± 65.6 1920.2 ± 29.9 1934.0 ± 33.7
8 3 2496.3 ± 68.3 2334.5 ± 38.9 2334.3 ± 78.9
17 0 1862.5 ± 43.2 1716.4 ± 45.3 1696.6 ± 40.8
17 3 2135.4 ± 67.7 2081.5 ± 30.0 2095.1 ± 24.9
21 0 2126.2 ± 89.7 2220.0 ± 46.1 2162.9 ± 94.7
21 3 2450.6 ± 30.1 2531.2 ± 16.5 2534.4 ± 13.1

Allocation: bytes/record, mean across five forks.

Java Extra headers Baseline This branch #13600
8 0 6090.6 5121.0 5105.0
8 3 7363.9 6263.1 6266.3
17 0 5280.6 4623.0 4597.4
17 3 6282.0 5512.4 5494.8
21 0 5218.2 4575.0 4563.6
21 3 6222.7 5458.0 5459.6

@mcculls mcculls added tag: performance Performance related changes type: refactoring inst: kafka Kafka instrumentation tag: ai generated Largely based on code generated by an AI or LLM labels Nov 30, 2026
@mcculls
mcculls force-pushed the mcculls/kafka-record-header-lookup branch from f06028b to bd11ab6 Compare November 30, 2026 20:12
@mcculls mcculls changed the title Avoid Kafka header copies without changing the class shape Read fetched Kafka headers without copying them first Nov 30, 2026
@datadog-official

datadog-official Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

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

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

@dd-octo-sts

dd-octo-sts Bot commented Nov 30, 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.09 s 14.00 s [-0.1%; +1.3%] (no difference)
startup:insecure-bank:tracing:Agent 12.99 s 13.05 s [-1.2%; +0.3%] (no difference)
startup:petclinic:appsec:Agent 17.27 s 17.27 s [-0.8%; +0.7%] (no difference)
startup:petclinic:iast:Agent 17.21 s 17.19 s [-0.6%; +0.9%] (no difference)
startup:petclinic:profiling:Agent 16.78 s 17.11 s [-3.0%; -0.8%] (maybe better)
startup:petclinic:sca:Agent 17.33 s 16.58 s [+0.3%; +8.7%] (maybe worse)
startup:petclinic:tracing:Agent 16.28 s 16.36 s [-1.5%; +0.5%] (no difference)

Commit: a986d0c0 · 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.

@mcculls
mcculls force-pushed the mcculls/kafka-record-header-lookup branch from bd11ab6 to 43011f5 Compare November 30, 2026 20:48
@mcculls

mcculls commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

@DataDog review

@datadog-official datadog-official 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

Buffered-header extraction retains public-accessor fallbacks for unsupported layouts, subclasses, and Base64 decoding. The completed static review identified no actionable defect, and human PR discussion raised no additional concerns.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 43011f5

@pr-commenter

pr-commenter Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Kafka / producer-benchmark

Parameters

Baseline Candidate
baseline_or_candidate baseline candidate
git_branch master mcculls/kafka-record-header-lookup
git_commit_date 1796153780 1796160139
git_commit_sha 06ab7dd a986d0c
See matching parameters
Baseline Candidate
ci_job_date 1796161356 1796161356
ci_job_id 2317194625 2317194625
ci_pipeline_id 157252086 157252086
cpu_model Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
jdkVersion 11.0.31 11.0.31
jmhVersion 1.36 1.36
jvm /usr/lib/jvm/java-11-openjdk-amd64/bin/java /usr/lib/jvm/java-11-openjdk-amd64/bin/java
jvmArgs -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=15002 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=15002 -Dhttp.nonProxyHosts=localhost *.localhost
kernel_version Linux runner-zfyrx7zua-project-304-concurrent-0-rh3hx2ja 6.8.0-1031-aws #33~22.04.1-Ubuntu SMP Thu Jun 26 14:22:30 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux Linux runner-zfyrx7zua-project-304-concurrent-0-rh3hx2ja 6.8.0-1031-aws #33~22.04.1-Ubuntu SMP Thu Jun 26 14:22:30 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
vmName OpenJDK 64-Bit Server VM OpenJDK 64-Bit Server VM
vmVersion 11.0.31+11-post-1ubuntu1-22.04.2-Ubuntu 11.0.31+11-post-1ubuntu1-22.04.2-Ubuntu

Summary

Found 0 performance improvements and 0 performance regressions! Performance is the same for 3 metrics, 0 unstable metrics.

See unchanged results
scenario Δ mean throughput
scenario:not-instrumented/KafkaProduceBenchmark.benchProduce same
scenario:only-tracing-dsm-disabled-benchmarks/KafkaProduceBenchmark.benchProduce unsure
[-7691.732op/s; -1459.701op/s] or [-4.437%; -0.842%]
scenario:only-tracing-dsm-enabled-benchmarks/KafkaProduceBenchmark.benchProduce same

@pr-commenter

pr-commenter Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Kafka / consumer-benchmark

Parameters

Baseline Candidate
baseline_or_candidate baseline candidate
git_branch master mcculls/kafka-record-header-lookup
git_commit_date 1796068071 1796142897
git_commit_sha dd09e24 c87df81
See matching parameters
Baseline Candidate
ci_job_date 1796151603 1796151603
ci_job_id 2316449412 2316449412
ci_pipeline_id 157223304 157223304
cpu_model Intel(R) Xeon(R) Platinum 8175M CPU @ 2.50GHz Intel(R) Xeon(R) Platinum 8175M CPU @ 2.50GHz
jdkVersion 11.0.31 11.0.31
jmhVersion 1.36 1.36
jvm /usr/lib/jvm/java-11-openjdk-amd64/bin/java /usr/lib/jvm/java-11-openjdk-amd64/bin/java
jvmArgs -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=15002 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=15002 -Dhttp.nonProxyHosts=localhost *.localhost
kernel_version Linux runner-zfyrx7zua-project-304-concurrent-0-5ftjchi1 6.8.0-1031-aws #33~22.04.1-Ubuntu SMP Thu Jun 26 14:22:30 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux Linux runner-zfyrx7zua-project-304-concurrent-0-5ftjchi1 6.8.0-1031-aws #33~22.04.1-Ubuntu SMP Thu Jun 26 14:22:30 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
vmName OpenJDK 64-Bit Server VM OpenJDK 64-Bit Server VM
vmVersion 11.0.31+11-post-1ubuntu1-22.04.2-Ubuntu 11.0.31+11-post-1ubuntu1-22.04.2-Ubuntu

Summary

Found 0 performance improvements and 1 performance regressions! Performance is the same for 2 metrics, 0 unstable metrics.

scenario Δ mean throughput
scenario:only-tracing-dsm-enabled-benchmarks/KafkaConsumerBenchmark.benchConsume worse
[-8812.672op/s; -3132.375op/s] or [-5.256%; -1.868%]
See unchanged results
scenario Δ mean throughput
scenario:not-instrumented/KafkaConsumerBenchmark.benchConsume same
scenario:only-tracing-dsm-disabled-benchmarks/KafkaConsumerBenchmark.benchConsume same

@mcculls
mcculls marked this pull request as ready for review November 30, 2026 21:54
@mcculls
mcculls requested review from a team as code owners November 30, 2026 21:54
@mcculls
mcculls requested review from ValentinZakharov and bric3 and removed request for a team November 30, 2026 21:54
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-30T21:58:09.960350Z 43011f5 Draft marked ready
ℹ️ 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.

@datadog-official datadog-official 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

Static review found no actionable regression in buffered-header propagation or the public-accessor fallbacks for subclasses and Base64 extraction. Runtime and Kafka-version compatibility remain unverified.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 43011f5

@datadog-official datadog-official 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 static review found no reportable defect in the lazy Kafka header path and its accessor fallbacks. Runtime compatibility and allocation savings were not independently exercised.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 7bc7b2d

@mcculls
mcculls force-pushed the mcculls/kafka-record-header-lookup branch from 7bc7b2d to c87df81 Compare December 1, 2026 16:42

@datadog-official datadog-official 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 static review identified no actionable propagation regression. Extraction retains public-accessor fallback before lookup initialization, for subclasses with overridden accessors, and for Base64 decoding.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit c87df81

@mcculls
mcculls force-pushed the mcculls/kafka-record-header-lookup branch from c87df81 to 4f9ea44 Compare December 1, 2026 18:17

@datadog-official datadog-official 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 optimized path retains public-accessor fallbacks for unavailable lookups, header subclasses, and Base64 decoding. The completed static review found no reportable defects; runtime behavior was not exercised.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 4f9ea44

@mcculls
mcculls force-pushed the mcculls/kafka-record-header-lookup branch from 1eb07d0 to c87df81 Compare December 1, 2026 18:42

@datadog-official datadog-official 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

Delayed metadata activation and unsupported private-field layouts retain Kafka’s public-accessor extraction path, so propagation does not depend on successful lookup initialization.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit c87df81

@mcculls
mcculls force-pushed the mcculls/kafka-record-header-lookup branch from c87df81 to 808fbf5 Compare December 1, 2026 20:43

@datadog-official datadog-official 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

Static analysis identified no actionable defect in lookup initialization, buffer decoding, or public-accessor fallbacks. The human PR discussion contained no substantive review concerns.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 808fbf5

@mcculls
mcculls force-pushed the mcculls/kafka-record-header-lookup branch from 703d4c7 to a986d0c Compare December 1, 2026 21:23

@datadog-official datadog-official 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

Static review returned no actionable findings. Runtime compatibility and the reported performance gains remain unverified.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit a986d0c

@dougqh

dougqh commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for putting this together. The steady-state numbers look solid, and I agree the two approaches perform the same once warm.

My remaining concern is the late-attach path, since that's the main argument for lookup over loading-time accessors. Nothing in the PR or the #13656 thread measures what that path costs, so I measured it two ways.

1. Late retransformation re-warms a warm app (PetClinic, JDK 17, trigger at 60 s, well after warmup)

Arm Before Worst second Recovery
Retransform 1,538 loaded classes, identical bytes, one call 13.3k req/s 3.2k (−76%) ~14 s
dd.experimental.defer.integrations.until=60s 15.0k 4.8k (−68%) ~33 s to 95%
Identical bytes, one call per class 13.2k 150–600 (−97%) for 58 s ~5 s after the last call

No code changed in the first arm, yet the JVM discarded compiled code for those classes and everything that inlined them. It then ran about 2,600 compilations under live traffic. Retransformation invalidates whole classes plus their dependents, so a late attach is a second warmup, not a free way to move work out of startup.

2. This PR adds another invalidation after the attach (Kafka consumer, JDK 17)

I attached each agent to a consumer that had been running for 60 s and kept consuming past the next metadata refresh. With this PR, setup happens at that refresh (setTarget + syncAll), so on a warm consumer it lands well after the attach has settled. In my run it came about 2 minutes later, and at Kafka's default metadata.max.age.ms it can be up to 5 minutes. When it fires, the JIT throws away the compiled code that depends on those call sites:

  • RecordHeaderAccess::{key,knownKey,matches,utf8Value}
  • TextMapExtractAdapter::forEachKey
  • Kafka's RecordHeaders iterator
  • the application's poll loop (the compiled loop in the benchmark's main), because extraction had been inlined into it

Neither master nor #13600 lost any app code at the same refresh.

On this thin consumer the recompile was cheap: no throughput dip I could separate from noise. But the cost grows with how much is inlined around extraction, and a real consumer's loop carries far more than this benchmark's. It's the same pattern as (1) on a smaller scale: a late invalidation into the application's own hot code, after warmup.

What I'd like to see

Either:

  • numbers showing that late attach to a warm consumer, the case this design is built for, is worth its re-warmup cost, or
  • setup that happens only when classes load, with the copying path as the fallback for late attach. That avoids both the retransformation and the later call-site invalidation.

I think the policy should be to defer work only to points before the target classes load. Retransforming classes that are already loaded should count as a real cost that needs justification.

Caveats: both are single runs on a laptop, and single runs vary (a later PetClinic control run dropped 65% rather than 76%). PetClinic's load generator waits for each response before sending the next, so it under-reports latency during a stall; read its throughput drop as the main signal. Both harnesses are up to reproduce these: PetClinic in #13770 (retransform-warmup/run.sh <arm>) and the Kafka late-attach run in #13769 (run-late-attach.sh NoAgent base=... head=...).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

inst: kafka Kafka instrumentation tag: ai generated Largely based on code generated by an AI or LLM tag: performance Performance related changes type: refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants