Skip to content

Commit

Permalink
Cache set of metric names in NodesInfoMetrics (elastic#105888)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
original-brownbear authored Mar 4, 2024
1 parent 42786aa commit 1978b1f
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
* 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<String> requestedMetrics = Metric.allMetrics();
private final Set<String> 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<String> requestedMetrics() {
Expand All @@ -36,7 +38,7 @@ public Set<String> requestedMetrics() {

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeStringArray(requestedMetrics.toArray(new String[0]));
out.writeStringCollection(requestedMetrics);
}

/**
Expand All @@ -58,6 +60,10 @@ public enum Metric {
AGGREGATIONS("aggregations"),
INDICES("indices");

private static final Set<String> ALL_METRICS = Arrays.stream(values())
.map(Metric::metricName)
.collect(Collectors.toUnmodifiableSet());

private final String metricName;

Metric(String name) {
Expand All @@ -69,7 +75,7 @@ public String metricName() {
}

public static Set<String> allMetrics() {
return Arrays.stream(values()).map(Metric::metricName).collect(Collectors.toSet());
return ALL_METRICS;
}
}
}

0 comments on commit 1978b1f

Please sign in to comment.