Skip to content

[ISSUE #10441] Reduce per-RPC allocation in metrics by caching static AttributeKey instances#10443

Open
wang-jiahua wants to merge 1 commit into
apache:developfrom
wang-jiahua:perf/static-attributekey-metrics-caching
Open

[ISSUE #10441] Reduce per-RPC allocation in metrics by caching static AttributeKey instances#10443
wang-jiahua wants to merge 1 commit into
apache:developfrom
wang-jiahua:perf/static-attributekey-metrics-caching

Conversation

@wang-jiahua
Copy link
Copy Markdown

Cache OpenTelemetry AttributeKey instances as static finals instead of creating new instances per RPC call. Also optimize TopicMessageType lookup, add volatile inline cache for RemotingCodeDistributionHandler, and add Logback NopStatusListener to suppress startup log noise.

Which Issue(s) This PR Fixes

Brief Description

Cache OpenTelemetry AttributeKey instances as static finals instead of creating new ones per RPC call. JFR heap dump shows 38,182 AttributeKey instances with only 6 distinct key names — each RPC allocates 3-4 throwaway key objects.

Changes:

  • BrokerMetricsConstant / RemotingMetricsConstant: String labels → AttributeKey<T> static finals
  • BrokerMetricsManager / RemotingMetricsManager / PopMetricsManager: use put(AttributeKey, value) overloads
  • RemotingCodeDistributionHandler: volatile inline cache for counter methods
  • TopicMessageType: direct if-else lookup replacing values() iteration
  • rmq.broker.logback.xml: add NopStatusListener

How Did You Test This Change?

  • JFR profiling (ObjectAllocationSample) on 4×ECS (8c/30G), 128 producer threads, 1KB body, Temurin JDK 21
  • Verified all existing metrics report identically before/after
  • Zero functional change — only metric label construction paths affected

Copilot AI review requested due to automatic review settings June 8, 2026 09:21
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR focuses on reducing allocation/CPU overhead on metrics and remoting hot paths by introducing AttributeKey singletons and adding small caches for frequently repeated attribute/metric lookups.

Changes:

  • Added fast-path caching for remoting request/response code distribution counting.
  • Introduced typed OpenTelemetry AttributeKey constants and attribute caching/build helpers in remoting and broker metrics.
  • Reduced repeated string allocations (e.g., cached lowercase metrics value, avoided Map.getOrDefault boxing paths).

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
remoting/src/main/java/org/apache/rocketmq/remoting/netty/RemotingCodeDistributionHandler.java Adds “last code” fast path to reduce map lookups for hot request/response codes.
remoting/src/main/java/org/apache/rocketmq/remoting/metrics/RemotingMetricsManager.java Adds attribute caching and typed label keys to reduce OpenTelemetry attribute overhead.
remoting/src/main/java/org/apache/rocketmq/remoting/metrics/RemotingMetricsConstant.java Introduces typed AttributeKey singletons for labels.
remoting/src/main/java/org/apache/rocketmq/remoting/common/RemotingHelper.java Minor optimization to avoid getOrDefault overhead for code descriptions.
common/src/main/java/org/apache/rocketmq/common/attribute/TopicMessageType.java Caches lowercase metrics value to avoid repeated conversions.
broker/src/main/resources/rmq.broker.logback.xml Suppresses Logback status output via NopStatusListener.
broker/src/main/java/org/apache/rocketmq/broker/metrics/PopMetricsManager.java Switches to typed label keys for attributes.
broker/src/main/java/org/apache/rocketmq/broker/metrics/BrokerMetricsManager.java Adds topic attributes cache and switches to typed label keys.
broker/src/main/java/org/apache/rocketmq/broker/metrics/BrokerMetricsConstant.java Introduces typed AttributeKey singletons for broker metrics labels.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread broker/src/main/resources/rmq.broker.logback.xml
@wang-jiahua wang-jiahua force-pushed the perf/static-attributekey-metrics-caching branch from 9813211 to 607c58a Compare June 8, 2026 09:41
…static AttributeKey instances

Cache OpenTelemetry AttributeKey instances as static finals instead of
creating new instances per RPC call. Also optimize TopicMessageType
lookup, add volatile inline cache for RemotingCodeDistributionHandler,
and add Logback NopStatusListener to suppress startup log noise.
@wang-jiahua wang-jiahua force-pushed the perf/static-attributekey-metrics-caching branch from 607c58a to 52c49e8 Compare June 8, 2026 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement] Reduce per-RPC allocation in metrics and remoting by caching static AttributeKey instances

2 participants