Re: [PR] RATIS-2405. Remove duplicate computeIfAbsent call in MessageMetrics.inc() method. [ratis]

2026-02-11 Thread via GitHub


slfan1989 commented on PR #1346:
URL: https://github.com/apache/ratis/pull/1346#issuecomment-3887505996

   > +1 the change looks good.
   
   @szetszwo Thank you so much for reviewing the 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]



Re: [PR] RATIS-2405. Remove duplicate computeIfAbsent call in MessageMetrics.inc() method. [ratis]

2026-02-11 Thread via GitHub


szetszwo merged PR #1346:
URL: https://github.com/apache/ratis/pull/1346


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



Re: [PR] RATIS-2405. Remove duplicate computeIfAbsent call in MessageMetrics.inc() method. [ratis]

2026-02-11 Thread via GitHub


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


##
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)
-.computeIfAbsent(metricNamePrefix, prefix -> 
getRegistry().counter(prefix + t.getSuffix()))
-.inc();
-final Map counters = types.get(t);
-LongCounter c = counters.get(metricNamePrefix);
-if (c == null) {
-  synchronized (counters) {
-c = counters.computeIfAbsent(metricNamePrefix, prefix -> 
getRegistry().counter(prefix + t.getSuffix()));
-  }
-}
-c.inc();
+final LongCounter counter = types.get(t)
+.computeIfAbsent(metricNamePrefix, prefix -> 
getRegistry().counter(prefix + t.getSuffix()));
+counter.inc();

Review Comment:
   Thank you for reviewing the code! I’ve improved it based on your feedback.



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



Re: [PR] RATIS-2405. Remove duplicate computeIfAbsent call in MessageMetrics.inc() method. [ratis]

2026-02-11 Thread via GitHub


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


##
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)
-.computeIfAbsent(metricNamePrefix, prefix -> 
getRegistry().counter(prefix + t.getSuffix()))
-.inc();
-final Map counters = types.get(t);
-LongCounter c = counters.get(metricNamePrefix);
-if (c == null) {
-  synchronized (counters) {
-c = counters.computeIfAbsent(metricNamePrefix, prefix -> 
getRegistry().counter(prefix + t.getSuffix()));
-  }
-}
-c.inc();
+final LongCounter counter = types.get(t)
+.computeIfAbsent(metricNamePrefix, prefix -> 
getRegistry().counter(prefix + t.getSuffix()));
+counter.inc();

Review Comment:
   @slfan1989 , thanks a lot for working on this!
   
   You are right that the second computeIfAbsent is useless.  The change looks 
good.  Could you keep the first three lines unchanged as below?  
   
   ```java
 private void inc(String metricNamePrefix, Type t) {
   types.get(t)
   .computeIfAbsent(metricNamePrefix, prefix -> 
getRegistry().counter(prefix + t.getSuffix()))
   .inc();
 }
   ```



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



Re: [PR] RATIS-2405. Remove duplicate computeIfAbsent call in MessageMetrics.inc() method. [ratis]

2026-02-11 Thread via GitHub


slfan1989 commented on PR #1346:
URL: https://github.com/apache/ratis/pull/1346#issuecomment-3885020520

   @szetszwo Could you help review this PR? Thank you so much!


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



Re: [PR] RATIS-2405. Remove duplicate computeIfAbsent call in MessageMetrics.inc() method. [ratis]

2026-02-11 Thread via GitHub


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]