[ISSUE #10266] Fix OOM caused by OpenTelemetry 1.44 OtlpGrpcMetricExporter pool race#10267
Open
Houlong66 wants to merge 1 commit intoapache:developfrom
Open
[ISSUE #10266] Fix OOM caused by OpenTelemetry 1.44 OtlpGrpcMetricExporter pool race#10267Houlong66 wants to merge 1 commit intoapache:developfrom
Houlong66 wants to merge 1 commit intoapache:developfrom
Conversation
OpenTelemetry Java 1.44.0 ~ 1.46.x ships OtlpGrpcMetricExporter with MemoryMode.REUSABLE_DATA by default. The underlying MetricReusableDataMarshaler.marshalerPool is a non-thread-safe ArrayDeque accessed concurrently by the reader thread (poll) and the OkHttp callback thread (add, via whenComplete). With BatchSplittingMetricExporter issuing N concurrent sub-batch exports per cycle, the pool races and leaks marshalers (~132 KiB each) until OOM. Fixed upstream in 1.47.0 via open-telemetry/opentelemetry-java#7041 (ArrayDeque -> ConcurrentLinkedDeque). - Bump OpenTelemetry to 1.47.0 in pom.xml so the upstream race fix is in effect. - Default OtlpGrpcMetricExporter to MemoryMode.IMMUTABLE_DATA to preserve the pre-1.44 default behavior; exposed via brokerConfig.metricsExportOtelMemoryMode ("IMMUTABLE_DATA" / "REUSABLE_DATA", case-insensitive). Operators may opt in to REUSABLE_DATA when running on OTel >= 1.47. - Cap concurrent in-flight sub-batches in BatchSplittingMetricExporter with a Semaphore controlled by brokerConfig.metricsExportBatchMaxConcurrent (default 4; set to 1 to serialize and match pre-batch behavior; 0 or Integer.MAX_VALUE means unlimited). - Add brokerConfig.metricsExportBatchSplitEnabled (default true) as an escape hatch to bypass BatchSplittingMetricExporter entirely, restoring the raw OtlpGrpcMetricExporter wiring. - Defensively snapshot MetricData points before export to avoid ArrayIndexOutOfBoundsException in NumberDataPointMarshaler when async instrument callbacks mutate point collections during export.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #10267 +/- ##
=============================================
- Coverage 48.99% 48.89% -0.10%
+ Complexity 13459 13441 -18
=============================================
Files 1375 1375
Lines 100394 100432 +38
Branches 12964 12969 +5
=============================================
- Hits 49188 49108 -80
- Misses 45217 45315 +98
- Partials 5989 6009 +20 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #10266. Brokers running
BatchSplittingMetricExporter(#10240) on OpenTelemetry Java SDK 1.44.0 ~ 1.46.x leak heap linearly until OOM due to a non-thread-safeArrayDequepool inMetricReusableDataMarshaler. Upstream fixed it in OT 1.47.0 viaopen-telemetry/opentelemetry-java#7041. This PR applies a three-layer defense on the broker side.Changes
pom.xml: bump
opentelemetry.version1.44.1 → 1.47.0 andopentelemetry-exporter-prometheus.version→ 1.47.0-alpha. OT 1.47.0 replaces the racyArrayDequewithConcurrentLinkedDeque.BrokerConfig: three new fields, all hot-updatable via
updateBrokerConfig:metricsExportOtelMemoryMode(String, default"IMMUTABLE_DATA", case-insensitive; valid:IMMUTABLE_DATA/REUSABLE_DATA). ForcesOtlpGrpcMetricExporterto IMMUTABLE_DATA, bypassing the pool path entirely.metricsExportBatchMaxConcurrent(int, default 4). Bounds in-flight sub-batches inBatchSplittingMetricExportervia a Semaphore. Set to 1 to serialize (behaves like pre-batch); 0 orInteger.MAX_VALUEdisables the limit.metricsExportBatchSplitEnabled(boolean, default true). Escape hatch: set false to skip the splitter wrapper entirely and use the rawOtlpGrpcMetricExporter.BrokerMetricsManager:
metricsExportOtelMemoryModevia a newresolveMemoryMode(String)helper (invalid values → IMMUTABLE_DATA + WARN log).BatchSplittingMetricExporterpermetricsExportBatchSplitEnabled.BatchSplittingMetricExporter:
(MetricExporter, IntSupplier batchSize, IntSupplier maxConcurrent).export()builds a per-callSemaphorefrom the supplied concurrency limit; each sub-batch acquires a permit beforedelegate.export(batch)and releases it inwhenComplete.snapshotAllMetrics()on the collection before export to preventArrayIndexOutOfBoundsExceptioninNumberDataPointMarshaler.createRepeatedwhen async instrument callbacks mutate data point collections concurrently with export serialization.BatchSplittingMetricExporterTest: adds concurrency-limiting cases (
testConcurrencyLimitBoundsInFlightBatches,testConcurrencyLimitZeroMeansUnlimited), null-arg constructor rejection for the new parameter, and snapshot defensive-copy cases (testSnapshotCreatesNewMetricData,testSnapshotFallsBackToOriginal,testSnapshotPointsAreIndependentCopy).Why default to IMMUTABLE_DATA
IMMUTABLE_DATAmatches the de-facto behavior prior to OT 1.44.0 (no REUSABLE path shipped). Keeping it as default restores the pre-1.44 behavior users have been running safely on, while leaving operators a knob to opt into REUSABLE_DATA on OT >= 1.47 when allocation pressure matters more.Motivation
Verified via MAT analysis of an OOM heap dump from production:
BatchSplittingMetricExporter → MetricReusableDataMarshaler.marshalerPoolLowAllocationMetricsRequestMarshalerinstances in an unboundedArrayDequeArrayDequelogicalsize()vs actual non-null slots mismatch (32,233 vs 17,531) — direct evidence of cross-thread race corruption~3 ~ 10 MB / cycleold-gen growth (approx 2.25 GB / 600 cycles)Test Plan
BatchSplittingMetricExporterTestcases pass onorigin/develop + this commitmvn compilefull repository build passesRelated
open-telemetry/opentelemetry-java#7041(merged 2025-01-24, released in v1.47.0)open-telemetry/opentelemetry-java#7019BatchSplittingMetricExporter, which together with the OT 1.44 default triggered the race