Houlong66 opened a new pull request, #10267:
URL: https://github.com/apache/rocketmq/pull/10267

   ## 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-safe \`ArrayDeque\` pool in \`MetricReusableDataMarshaler\`. 
Upstream fixed it in OT 1.47.0 via \`open-telemetry/opentelemetry-java#7041\`. 
This PR applies a three-layer defense on the broker side.
   
   ## Changes
   
   - **pom.xml**: bump \`opentelemetry.version\` 1.44.1 → **1.47.0** and 
\`opentelemetry-exporter-prometheus.version\` → 1.47.0-alpha. OT 1.47.0 
replaces the racy \`ArrayDeque\` with \`ConcurrentLinkedDeque\`.
   
   - **BrokerConfig**: three new fields, all hot-updatable via 
\`updateBrokerConfig\`:
     - \`metricsExportOtelMemoryMode\` (String, default \`"IMMUTABLE_DATA"\`, 
case-insensitive; valid: \`IMMUTABLE_DATA\` / \`REUSABLE_DATA\`). Forces 
\`OtlpGrpcMetricExporter\` to IMMUTABLE_DATA, bypassing the pool path entirely.
     - \`metricsExportBatchMaxConcurrent\` (int, default 4). Bounds in-flight 
sub-batches in \`BatchSplittingMetricExporter\` via a Semaphore. Set to 1 to 
serialize (behaves like pre-batch); 0 or \`Integer.MAX_VALUE\` disables the 
limit.
     - \`metricsExportBatchSplitEnabled\` (boolean, default true). Escape 
hatch: set false to skip the splitter wrapper entirely and use the raw 
\`OtlpGrpcMetricExporter\`.
   
   - **BrokerMetricsManager**:
     - Reads \`metricsExportOtelMemoryMode\` via a new 
\`resolveMemoryMode(String)\` helper (invalid values → IMMUTABLE_DATA + WARN 
log).
     - Conditionally wraps with \`BatchSplittingMetricExporter\` per 
\`metricsExportBatchSplitEnabled\`.
   
   - **BatchSplittingMetricExporter**:
     - Constructor extended to \`(MetricExporter, IntSupplier batchSize, 
IntSupplier maxConcurrent)\`.
     - \`export()\` builds a per-call \`Semaphore\` from the supplied 
concurrency limit; each sub-batch acquires a permit before 
\`delegate.export(batch)\` and releases it in \`whenComplete\`.
     - Adds defensive \`snapshotAllMetrics()\` on the collection before export 
to prevent \`ArrayIndexOutOfBoundsException\` in 
\`NumberDataPointMarshaler.createRepeated\` when 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_DATA\` matches 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:
   - 93% of heap retained by one \`BatchSplittingMetricExporter → 
MetricReusableDataMarshaler.marshalerPool\`
   - 17,531 leaked \`LowAllocationMetricsRequestMarshaler\` instances in an 
unbounded \`ArrayDeque\`
   - \`ArrayDeque\` logical \`size()\` vs actual non-null slots mismatch 
(32,233 vs 17,531) — direct evidence of cross-thread race corruption
   - Linear \`~3 ~ 10 MB / cycle\` old-gen growth (approx 2.25 GB / 600 cycles)
   
   ## Test Plan
   
   - [x] All 24 existing + new \`BatchSplittingMetricExporterTest\` cases pass 
on \`origin/develop + this commit\`
   - [x] \`mvn compile\` full repository build passes
   - [ ] Integration: run broker against high-cardinality load on OT 1.47.0 for 
24h, verify old gen does not climb linearly (recommended in staging prior to 
final merge)
   
   ## Related
   
   - Upstream fix: \`open-telemetry/opentelemetry-java#7041\` (merged 
2025-01-24, released in v1.47.0)
   - Upstream issue: \`open-telemetry/opentelemetry-java#7019\`
   - This repo: #10240 introduced \`BatchSplittingMetricExporter\`, which 
together with the OT 1.44 default triggered the race


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to