szetszwo commented on code in PR #744:
URL: https://github.com/apache/ratis/pull/744#discussion_r1281023213
##########
ratis-grpc/src/main/java/org/apache/ratis/grpc/metrics/MessageMetrics.java:
##########
@@ -82,7 +82,7 @@ public void rpcCompleted(String metricNamePrefix){
/**
* Increments the count of RPCs received on the server.
*/
- public void rpcReceived(String metricNamePrefix){
+ synchronized public void rpcReceived(String metricNamePrefix){
Review Comment:
I see. Could you remove the `synchronized` and test the following change?
It seems that the initialization (`computeIfAbsent`) may have a race condition.
```java
+++
b/ratis-grpc/src/main/java/org/apache/ratis/grpc/metrics/MessageMetrics.java
@@ -58,9 +58,14 @@ public class MessageMetrics extends RatisMetrics {
}
private void inc(String metricNamePrefix, Type t) {
- types.get(t)
- .computeIfAbsent(metricNamePrefix, prefix ->
getRegistry().counter(prefix + t.getSuffix()))
- .inc();
+ final Map<String, LongCounter> 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();
}
```
--
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]