[ISSUE #10441] Reduce per-RPC allocation in metrics by caching static AttributeKey instances#10443
Open
wang-jiahua wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
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
AttributeKeyconstants and attribute caching/build helpers in remoting and broker metrics. - Reduced repeated string allocations (e.g., cached lowercase metrics value, avoided
Map.getOrDefaultboxing 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.
9813211 to
607c58a
Compare
…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.
607c58a to
52c49e8
Compare
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.
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
AttributeKeyinstances #10441Brief Description
Cache OpenTelemetry
AttributeKeyinstances as static finals instead of creating new ones per RPC call. JFR heap dump shows 38,182AttributeKeyinstances with only 6 distinct key names — each RPC allocates 3-4 throwaway key objects.Changes:
BrokerMetricsConstant/RemotingMetricsConstant:Stringlabels →AttributeKey<T>static finalsBrokerMetricsManager/RemotingMetricsManager/PopMetricsManager: useput(AttributeKey, value)overloadsRemotingCodeDistributionHandler: volatile inline cache for counter methodsTopicMessageType: direct if-else lookup replacingvalues()iterationrmq.broker.logback.xml: addNopStatusListenerHow Did You Test This Change?