Skip to content

Conversation

@Russole
Copy link
Contributor

@Russole Russole commented Jan 7, 2026

What changes were proposed in this pull request?

  • Refactored ProtocolMessageMetrics to use a single unmodifiable EnumMap for per-message metrics.
  • Consolidated counters and elapsed time into one per-type structure instead of maintaining multiple maps.
  • Reused static metric metadata (MetricsInfo) and removed dynamic creation of metric definitions.
  • Updated call sites to align generic type bounds (KEY extends Enum<KEY>) required by the new implementation.

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/HDDS-9438

How was this patch tested?

All CI checks passed.
https://github.com/Russole/ozone/actions/runs/20752706566

@adoroszlai adoroszlai requested a review from szetszwo January 7, 2026 14:18
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.

@Russole , thanks a lot for working on this! Please see the comments inlined.

Comment on lines 103 to 112
MetricsRecordBuilder builder =
collector.addRecord(name);
builder.add(
new MetricsTag(Interns.info("type", "Message type"), key.toString()));
builder.addCounter(new MetricName("counter", "Number of distinct calls"),
value.longValue());
new MetricsTag(TYPE_TAG_INFO, key.toString()));
builder.addCounter(COUNTER_INFO,
stat.counter());
builder.addCounter(
new MetricName("time", "Sum of the duration of the calls"),
elapsedTimes.get(key).longValue());
TIME_INFO,
stat.time());
builder.endRecord();
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's combine the short lines:

      collector.addRecord(name)
          .add(new MetricsTag(TYPE_TAG_INFO, key.toString()))
          .addCounter(COUNTER_INFO, stat.counter())
          .addCounter(TIME_INFO, stat.time())
          .endRecord();

Comment on lines 115 to 116
MetricsRecordBuilder builder = collector.addRecord(name);
builder.addCounter(new MetricName("concurrency",
"Number of requests processed concurrently"), concurrency.get());
builder.addCounter(CONCURRENCY_INFO, concurrency.get());
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly,

    collector.addRecord(name)
        .addCounter(CONCURRENCY_INFO, concurrency.get());

BTW, do we need .endRecord();?

* Metrics to count all the subtypes of a specific message.
*/
public class ProtocolMessageMetrics<KEY> implements MetricsSource {
public class ProtocolMessageMetrics<KEY extends Enum<KEY>> implements MetricsSource {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 the generic type should extend Enum.

Comment on lines 48 to 59
private static final MetricsInfo TYPE_TAG_INFO =
Interns.info("type", "Message type");

private static final MetricsInfo COUNTER_INFO =
Interns.info("counter", "Number of distinct calls");

private static final MetricsInfo TIME_INFO =
Interns.info("time", "Sum of the duration of the calls");

private static final MetricsInfo CONCURRENCY_INFO =
Interns.info("concurrency",
"Number of requests processed concurrently");
Copy link
Contributor

Choose a reason for hiding this comment

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

The line width is 120 characters. Let's avoid the short lines.

  private static final MetricsInfo TYPE_TAG_INFO = Interns.info("type", "Message type");
  private static final MetricsInfo COUNTER_INFO = Interns.info("counter", "Number of distinct calls");
  private static final MetricsInfo TIME_INFO = Interns.info("time", "Sum of the duration of the calls");
  private static final MetricsInfo CONCURRENCY_INFO = Interns.info("concurrency",
      "Number of requests processed concurrently");

Comment on lines 61 to 76
public static <KEY extends Enum<KEY>> ProtocolMessageMetrics<KEY> create(String name,
String description, KEY[] types) {
return new ProtocolMessageMetrics<KEY>(name, description, types);
return new ProtocolMessageMetrics<>(name, description, types);
}

public ProtocolMessageMetrics(String name, String description,
KEY[] values) {
this.name = name;
this.description = description;
final Class<KEY> enumClass = values[0].getDeclaringClass();
final EnumMap<KEY, Stats> map = new EnumMap<>(enumClass);
for (KEY value : values) {
counters.put(value, new AtomicLong(0));
elapsedTimes.put(value, new AtomicLong(0));
map.put(value, new Stats());
}
this.stats = Collections.unmodifiableMap(map);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Pass the class instead of values.
  • Change the constructor to private.
  public static <KEY extends Enum<KEY>> ProtocolMessageMetrics<KEY> create(String name,
      String description, Class<KEY> enumClass) {
    return new ProtocolMessageMetrics<>(name, description, enumClass);
  }

  private ProtocolMessageMetrics(String name, String description, Class<KEY> enumClass) {
    this.name = name;
    this.description = description;
    final EnumMap<KEY, Stats> map = new EnumMap<>(enumClass);
    for (KEY value : enumClass.getEnumConstants()) {
      map.put(value, new Stats());
    }
    this.stats = Collections.unmodifiableMap(map);
  }

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 0cc80f6 into apache:master Jan 8, 2026
44 checks passed
@Russole
Copy link
Contributor Author

Russole commented Jan 8, 2026

Thanks @szetszwo for the reviews.

@Russole Russole deleted the HDDS-9438 branch January 8, 2026 13:57
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