wang-jiahua commented on code in PR #10443:
URL: https://github.com/apache/rocketmq/pull/10443#discussion_r3389165135


##########
remoting/src/main/java/org/apache/rocketmq/remoting/netty/RemotingCodeDistributionHandler.java:
##########
@@ -32,19 +32,35 @@ public class RemotingCodeDistributionHandler extends 
ChannelDuplexHandler {
 
     private final ConcurrentMap<Integer, LongAdder> inboundDistribution;
     private final ConcurrentMap<Integer, LongAdder> outboundDistribution;
+    private volatile int lastInCode = Integer.MIN_VALUE;
+    private volatile LongAdder lastInAdder;
+    private volatile int lastOutCode = Integer.MIN_VALUE;
+    private volatile LongAdder lastOutAdder;
 
     public RemotingCodeDistributionHandler() {
         inboundDistribution = new ConcurrentHashMap<>();
         outboundDistribution = new ConcurrentHashMap<>();
     }
 
     private void countInbound(int requestCode) {
+        if (requestCode == lastInCode) {
+            lastInAdder.increment();
+            return;
+        }
         LongAdder item = inboundDistribution.computeIfAbsent(requestCode, k -> 
new LongAdder());
+        lastInCode = requestCode;
+        lastInAdder = item;
         item.increment();
     }
 
     private void countOutbound(int responseCode) {
+        if (responseCode == lastOutCode) {
+            lastOutAdder.increment();
+            return;
+        }
         LongAdder item = outboundDistribution.computeIfAbsent(responseCode, k 
-> new LongAdder());
+        lastOutCode = responseCode;
+        lastOutAdder = item;
         item.increment();
     }

Review Comment:
   Fixed: introduced an immutable `CachedCounter { final int code; final 
LongAdder adder; }` holder published via a single volatile write. Racing 
threads always observe a consistent (code, adder) pair. The `Integer.MIN_VALUE` 
sentinel issue is also resolved since the cached pair is initialized as `null`, 
not with a sentinel code.



-- 
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