Skip to content

Commit 1f4262e

Browse files
Lazy load stats in NodeMetrics (#113014)
No point in eager loading the metrics here, it just slows down node startup considerably, wasting lots of time in integTest runs in particular (as in e.g. ~200s of total CPU time in :server:internalClusterTest!!).
1 parent 3ab361a commit 1f4262e

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

server/src/main/java/org/elasticsearch/monitor/metrics/NodeMetrics.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
import org.apache.logging.log4j.Logger;
1414
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
1515
import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags;
16+
import org.elasticsearch.cluster.node.DiscoveryNode;
17+
import org.elasticsearch.cluster.node.VersionInformation;
1618
import org.elasticsearch.common.component.AbstractLifecycleComponent;
19+
import org.elasticsearch.common.transport.TransportAddress;
1720
import org.elasticsearch.common.util.SingleObjectCache;
1821
import org.elasticsearch.core.TimeValue;
1922
import org.elasticsearch.index.stats.IndexingPressureStats;
@@ -24,9 +27,12 @@
2427
import org.elasticsearch.telemetry.metric.MeterRegistry;
2528

2629
import java.io.IOException;
30+
import java.net.InetAddress;
2731
import java.util.ArrayList;
2832
import java.util.List;
33+
import java.util.Map;
2934
import java.util.Optional;
35+
import java.util.Set;
3036

3137
/**
3238
* NodeMetrics monitors various statistics of an Elasticsearch node and exposes them as metrics through
@@ -744,10 +750,38 @@ protected void doClose() throws IOException {
744750
* refresh() is called, cache is updated and the new instance returned.
745751
*/
746752
private class NodeStatsCache extends SingleObjectCache<NodeStats> {
753+
private static final NodeStats MISSING_NODE_STATS = new NodeStats(
754+
new DiscoveryNode(
755+
"_na",
756+
"_na",
757+
new TransportAddress(InetAddress.getLoopbackAddress(), 0),
758+
Map.of(),
759+
Set.of(),
760+
VersionInformation.CURRENT
761+
),
762+
0,
763+
null,
764+
null,
765+
null,
766+
null,
767+
null,
768+
null,
769+
null,
770+
null,
771+
null,
772+
null,
773+
null,
774+
null,
775+
null,
776+
null,
777+
null,
778+
null,
779+
null
780+
);
747781
private boolean refresh;
748782

749783
NodeStatsCache(TimeValue interval) {
750-
super(interval, getNodeStats());
784+
super(interval, MISSING_NODE_STATS);
751785
this.refresh = true;
752786
}
753787

@@ -756,6 +790,11 @@ protected NodeStats refresh() {
756790
return refresh ? getNodeStats() : getNoRefresh();
757791
}
758792

793+
@Override
794+
protected boolean needsRefresh() {
795+
return getNoRefresh() == MISSING_NODE_STATS || super.needsRefresh();
796+
}
797+
759798
public void stopRefreshing() {
760799
this.refresh = false;
761800
}

0 commit comments

Comments
 (0)