slfan1989 commented on code in PR #1346:
URL: https://github.com/apache/ratis/pull/1346#discussion_r2793783554


##########
ratis-grpc/src/main/java/org/apache/ratis/grpc/metrics/MessageMetrics.java:
##########
@@ -58,17 +58,9 @@ private static RatisMetricRegistry createRegistry(String 
endpointId, String endp
   }
 
   private void inc(String metricNamePrefix, Type t) {
-    types.get(t)

Review Comment:
   The `types.get(t)` returns a `ConcurrentHashMap`, and `computeIfAbsent` 
itself is thread-safe. It guarantees that under concurrent access, only one 
`LongCounter` instance will be created and returned (the lambda function may be 
invoked multiple times, but only one value will be stored). The original 
synchronized block redundantly duplicated the responsibility of 
`computeIfAbsent` and also caused double counting. Now, keeping only one 
`computeIfAbsent(...).inc()` call ensures both thread safety and avoids 
duplicate counting.



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