Skip to content

Conversation

@slfan1989
Copy link
Contributor

What changes were proposed in this pull request?

Problem

The MessageMetrics.inc() method contains duplicate code that calls computeIfAbsent twice for the same key, along with an unnecessary synchronized block.

This leads to:

  • Redundant map lookups
  • Code duplication and poor readability
  • Potential performance overhead

What is the link to the Apache JIRA

RATIS-2405. Remove duplicate computeIfAbsent call in MessageMetrics.inc() method.

How was this patch tested?

  • Test Changes
    Updated TestGrpcMessageMetrics to verify the counter increment behavior more reliably by comparing before/after values instead of relying on absolute counts.

}

private void inc(String metricNamePrefix, Type t) {
types.get(t)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@slfan1989
Copy link
Contributor Author

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

Comment on lines 61 to 63
final LongCounter counter = types.get(t)
.computeIfAbsent(metricNamePrefix, prefix -> getRegistry().counter(prefix + t.getSuffix()));
counter.inc();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  private void inc(String metricNamePrefix, Type t) {
    types.get(t)
        .computeIfAbsent(metricNamePrefix, prefix -> getRegistry().counter(prefix + t.getSuffix()))
        .inc();
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

@szetszwo szetszwo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 the change looks good.

@szetszwo szetszwo merged commit 72f46fb into apache:master Feb 11, 2026
16 checks passed
@slfan1989
Copy link
Contributor Author

+1 the change looks good.

@szetszwo Thank you so much for reviewing the code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants