From 1978b1f30114bf6f8e038118965f6b5bd7009394 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Mon, 4 Mar 2024 10:18:46 +0100 Subject: [PATCH] Cache set of metric names in NodesInfoMetrics (#105888) No need to compute this over and over. Saw a couple of duplicates of this in a heap dump and noticed this method popping up randomly in profiles as well => just cache this thing and use a faster immutable set for it. --- .../cluster/node/info/NodesInfoMetrics.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/node/info/NodesInfoMetrics.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/node/info/NodesInfoMetrics.java index 3e632f9bdd212..39e210571f37b 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/node/info/NodesInfoMetrics.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/node/info/NodesInfoMetrics.java @@ -14,6 +14,7 @@ import java.io.IOException; import java.util.Arrays; +import java.util.HashSet; import java.util.Set; import java.util.stream.Collectors; @@ -21,13 +22,14 @@ * This class is a container that encapsulates the necessary information needed to indicate which node information is requested. */ public class NodesInfoMetrics implements Writeable { - private Set requestedMetrics = Metric.allMetrics(); + private final Set requestedMetrics; - public NodesInfoMetrics() {} + public NodesInfoMetrics() { + requestedMetrics = new HashSet<>(Metric.allMetrics()); + } public NodesInfoMetrics(StreamInput in) throws IOException { - requestedMetrics.clear(); - requestedMetrics.addAll(Arrays.asList(in.readStringArray())); + requestedMetrics = in.readCollectionAsImmutableSet(StreamInput::readString); } public Set requestedMetrics() { @@ -36,7 +38,7 @@ public Set requestedMetrics() { @Override public void writeTo(StreamOutput out) throws IOException { - out.writeStringArray(requestedMetrics.toArray(new String[0])); + out.writeStringCollection(requestedMetrics); } /** @@ -58,6 +60,10 @@ public enum Metric { AGGREGATIONS("aggregations"), INDICES("indices"); + private static final Set ALL_METRICS = Arrays.stream(values()) + .map(Metric::metricName) + .collect(Collectors.toUnmodifiableSet()); + private final String metricName; Metric(String name) { @@ -69,7 +75,7 @@ public String metricName() { } public static Set allMetrics() { - return Arrays.stream(values()).map(Metric::metricName).collect(Collectors.toSet()); + return ALL_METRICS; } } }