Skip to content

Read fetched Kafka headers without copying them first - #13737

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

mcculls wants to merge 2 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

Achieves the fetched Kafka header allocation savings of #13600 without changing the shape of Kafka’s RecordHeader class. Known propagation keys are read without decoding them, and UTF-8 values are decoded directly from the batch buffer without Kafka’s intermediate byte[] copy.

The buffered-header constructor captures a private lookup once per class loader. Extraction uses getter handles through static-final call-site invokers. This adds no fields, interfaces or accessor methods to RecordHeader; the advice changes the existing constructor body. Unsupported layouts, subclasses and Base64 extraction retain the public-accessor path.

Motivation

Preserve the same allocation-saving read path as #13600 while avoiding its structural instrumentation and FieldAccessors prerequisite. The comparison below shows similar allocation and competitive steady-state consumer cost with the constructor approach included.

Additional Notes

Comparison with #13600

Local comparison of this branch at f06028bcc31 with #13600 at cdee7363337, on one common tracer/runtime and Kafka 3.8.0 baseline. Only the header access/extractor implementations and their transformations differ.

The workload parses fresh serialized 500-record batches, uses Kafka’s ByteArrayDeserializer, and drains ConsumerRecords through the production TracingIterator and CoreTracer, including extraction, decoration, activation and span completion. Each record has six Datadog/W3C headers plus 0/3 application headers, a 100-byte value and a distinct upstream context; 10% are sampled. DSM and time-in-queue are off; applications consume payloads without reading headers afterward.

A small premain harness applies the real constructor advice or generated getters to the exact RecordHeader class. No subclass/getter simulation or manually supplied lookup is used. Each fork verifies the buffered fast path and all 500 completed spans’ parents and sampling priorities.

Five independent JVM forks per case, serial with alternating implementation order; 5 × 2 s warmup, 7 × 3 s measurement, one thread and the JMH GC profiler. Same macOS arm64 machine, Zulu HotSpot 8u514 / 17.0.21.1 / 21.0.13.1, 512 MiB G1 heap and four JVM-visible CPUs (GC threads 2/1). This measures steady consumer processing with a blackhole writer; startup, broker/network traffic, full client bookkeeping and trace serialization are excluded.

Time is ns/record, mean ± between-fork standard deviation; allocation is B/record.

Java App headers This branch: time #13600: time This branch: allocation #13600: allocation
8 0 1,962 ± 46 2,033 ± 51 5,117 5,113
8 3 2,376 ± 75 2,387 ± 100 6,278 6,283
17 0 1,674 ± 22 1,710 ± 57 4,601 4,601
17 3 2,043 ± 56 2,117 ± 24 5,481 5,485
21 0 2,327 ± 154 2,359 ± 101 4,602 4,625
21 3 2,639 ± 102 2,682 ± 115 5,523 5,568

Allocations are broadly similar. The strongest repeated indication favors this branch on Java 17 with three application headers: all five pairs favor it, averaging about 3.5% lower time. The other configurations remain inconclusive against between-fork variation, particularly on Java 21. These measurements support comparable cost, rather than a general speedup claim over #13600.

Constructor cost and a possible Java 9+ follow-up

The steady-state advice checks a volatile initialization field; capturing the lookup and resolving the four getters happen once per class loader. All branch timings above include the actual advice on every buffered-header construction: six or nine constructor-advice checks per record. With nine headers, the complete branch path takes 2,376 / 2,043 / 2,639 ns per record on Java 8 / 17 / 21, versus 2,387 / 2,117 / 2,682 ns for generated getters. That makes constructor capture look inexpensive in this workload, although this is a whole-path comparison and does not isolate an incremental ns/constructor figure.

We could still consider MethodHandles.privateLookupIn on Java 9+ later, once module access is solved. It works on the ordinary classpath, but a shared class loader does not imply a shared module: injected helpers live in the unnamed module. Across modules, it requires a caller lookup with PRIVATE and MODULE access, readability of the target module, and the target package to be open to the caller; exporting it is insufficient. Helper injection adds readability without opening Kafka’s package. Closed named-module deployments therefore need a targeted opening or a retained fallback before constructor capture can be replaced generally. Capturing the lookup inside Kafka’s constructor avoids requiring that package opening.

See the privateLookupIn access requirements.

As with #13600, leaving headers lazy can retain backing batch storage longer when applications retain records without reading their headers. This preserves Kafka’s uninstrumented lazy behavior.

Validation

  • All 14 new test cases passed on Java 17 and Java 8. Java 8 used a temporary isolated setup: these three Java test classes compiled with --release 8, with the unrelated Java 17 Spring dependencies excluded; the module’s standard suite has a Java 17 minimum.
  • All 60 comparison forks passed fast-path, parent-link and sampling-priority validation; artifact/source hashes and matching JVM settings were audited.
  • Spotless and CODEOWNERS checks passed. The affected JMH dependency lockfile is included; new files inherit the existing Kafka ownership rule.
  • SDK, performance and technical-debt reviews completed locally. Muzzle and latest-dependency validation remain for CI.

Contributor Checklist

  • Follow title and label conventions.
  • Include the affected dependency lockfile and check CODEOWNERS inheritance.
  • Format and run focused validation.
  • Await CI compatibility and latest-dependency results.

@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.48%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 43011f5 | 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.93 s 14.83 s [-0.2%; +1.6%] (no difference)
startup:insecure-bank:tracing:Agent 13.74 s 13.84 s [-1.5%; +0.1%] (no difference)
startup:petclinic:appsec:Agent 17.77 s 17.54 s [+0.6%; +2.0%] (maybe worse)
startup:petclinic:iast:Agent 17.56 s 17.60 s [-1.1%; +0.6%] (no difference)
startup:petclinic:profiling:Agent 17.28 s 17.44 s [-2.0%; +0.2%] (no difference)
startup:petclinic:sca:Agent 17.69 s 17.69 s [-1.1%; +1.1%] (no difference)
startup:petclinic:tracing:Agent 16.73 s 16.93 s [-2.1%; -0.3%] (maybe better)

Commit: 43011f52 · 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 1796068071 1796071664
git_commit_sha dd09e24 43011f5
See matching parameters
Baseline Candidate
ci_job_date 1796072560 1796072560
ci_job_id 2312608014 2312608014
ci_pipeline_id 157067265 157067265
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-jwrd3s6v 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-jwrd3s6v 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 same
scenario:only-tracing-dsm-enabled-benchmarks/KafkaProduceBenchmark.benchProduce unsure
[+295.899op/s; +6125.406op/s] or [+0.221%; +4.566%]

@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 1796071664
git_commit_sha dd09e24 43011f5
See matching parameters
Baseline Candidate
ci_job_date 1796072727 1796072727
ci_job_id 2312608015 2312608015
ci_pipeline_id 157067265 157067265
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-wb7npvwa 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-wb7npvwa 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/KafkaConsumerBenchmark.benchConsume same
scenario:only-tracing-dsm-disabled-benchmarks/KafkaConsumerBenchmark.benchConsume same
scenario:only-tracing-dsm-enabled-benchmarks/KafkaConsumerBenchmark.benchConsume unsure
[-8736.117op/s; -903.227op/s] or [-5.242%; -0.542%]

@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

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.

1 participant