wang-jiahua commented on code in PR #10443:
URL: https://github.com/apache/rocketmq/pull/10443#discussion_r3379618620
##########
remoting/src/main/java/org/apache/rocketmq/remoting/netty/RemotingCodeDistributionHandler.java:
##########
@@ -32,19 +32,37 @@ 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) {
+ LongAdder adder = lastInAdder;
+ if (requestCode == lastInCode && adder != null) {
+ adder.increment();
+ return;
+ }
LongAdder item = inboundDistribution.computeIfAbsent(requestCode, k ->
new LongAdder());
+ lastInAdder = item;
+ lastInCode = requestCode;
item.increment();
Review Comment:
Good catch. Fixed by introducing an immutable `CachedCounter { final int
code; final LongAdder adder; }` holder published via a single volatile write.
Reading threads now load one volatile reference and always observe a consistent
pair. The worst-case race is a redundant `computeIfAbsent` (no correctness
issue).
--
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]