forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceUsageCollectorService.java
More file actions
168 lines (148 loc) · 6.21 KB
/
ResourceUsageCollectorService.java
File metadata and controls
168 lines (148 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.node;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.cluster.ClusterChangedEvent;
import org.opensearch.cluster.ClusterStateListener;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.lifecycle.AbstractLifecycleComponent;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.concurrent.ConcurrentCollections;
import org.opensearch.node.resource.tracker.NodeResourceUsageTracker;
import org.opensearch.threadpool.Scheduler;
import org.opensearch.threadpool.ThreadPool;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentMap;
/**
* This collects node level resource usage statistics such as cpu, memory, IO of each node and makes it available for
* coordinator node to aid in throttling, ranking etc
*/
public class ResourceUsageCollectorService extends AbstractLifecycleComponent implements ClusterStateListener {
/**
* This refresh interval denotes the polling interval of ResourceUsageCollectorService to refresh the resource usage
* stats from local node
*/
private static long REFRESH_INTERVAL_IN_MILLIS = 1000;
private static final Logger logger = LogManager.getLogger(ResourceUsageCollectorService.class);
private final ConcurrentMap<String, NodeResourceUsageStats> nodeIdToResourceUsageStats = ConcurrentCollections.newConcurrentMap();
private ThreadPool threadPool;
private volatile Scheduler.Cancellable scheduledFuture;
private NodeResourceUsageTracker nodeResourceUsageTracker;
private ClusterService clusterService;
public ResourceUsageCollectorService(
NodeResourceUsageTracker nodeResourceUsageTracker,
ClusterService clusterService,
ThreadPool threadPool
) {
this.threadPool = threadPool;
this.nodeResourceUsageTracker = nodeResourceUsageTracker;
this.clusterService = clusterService;
clusterService.addListener(this);
}
@Override
public void clusterChanged(ClusterChangedEvent event) {
if (event.nodesRemoved()) {
for (DiscoveryNode removedNode : event.nodesDelta().removedNodes()) {
removeNodeResourceUsageStats(removedNode.getId());
}
}
}
void removeNodeResourceUsageStats(String nodeId) {
nodeIdToResourceUsageStats.remove(nodeId);
}
/**
* Collect node resource usage stats along with the timestamp
*/
public void collectNodeResourceUsageStats(
String nodeId,
long timestamp,
double memoryUtilizationPercent,
double cpuUtilizationPercent
) {
nodeIdToResourceUsageStats.compute(nodeId, (id, resourceUsageStats) -> {
if (resourceUsageStats == null) {
return new NodeResourceUsageStats(nodeId, timestamp, memoryUtilizationPercent, cpuUtilizationPercent);
} else {
resourceUsageStats.cpuUtilizationPercent = cpuUtilizationPercent;
resourceUsageStats.memoryUtilizationPercent = memoryUtilizationPercent;
resourceUsageStats.timestamp = timestamp;
return resourceUsageStats;
}
});
}
/**
* Get all node resource usage statistics which will be used for node stats
*/
public Map<String, NodeResourceUsageStats> getAllNodeStatistics() {
Map<String, NodeResourceUsageStats> nodeStats = new HashMap<>(nodeIdToResourceUsageStats.size());
nodeIdToResourceUsageStats.forEach((nodeId, resourceUsageStats) -> {
nodeStats.put(nodeId, new NodeResourceUsageStats(resourceUsageStats));
});
return nodeStats;
}
/**
* Optionally return a {@code NodeResourceUsageStats} for the given nodeid, if
* resource usage stats information exists for the given node. Returns an empty
* {@code Optional} if the node was not found.
*/
public Optional<NodeResourceUsageStats> getNodeStatistics(final String nodeId) {
return Optional.ofNullable(nodeIdToResourceUsageStats.get(nodeId))
.map(resourceUsageStats -> new NodeResourceUsageStats(resourceUsageStats));
}
public Optional<NodeResourceUsageStats> getLocalNodeStatistics() {
if(clusterService.state() != null) {
return Optional.ofNullable(nodeIdToResourceUsageStats.get(clusterService.state().nodes().getLocalNodeId()))
.map(resourceUsageStats -> new NodeResourceUsageStats(resourceUsageStats));
}
return Optional.empty();
}
/**
* Returns collected resource usage statistics of all nodes
*/
public NodesResourceUsageStats stats() {
return new NodesResourceUsageStats(getAllNodeStatistics());
}
/**
* Fetch local node resource usage statistics and add it to store along with the current timestamp
*/
private void collectLocalNodeResourceUsageStats() {
if (nodeResourceUsageTracker.isReady() && clusterService.state() != null) {
collectNodeResourceUsageStats(
clusterService.state().nodes().getLocalNodeId(),
System.currentTimeMillis(),
nodeResourceUsageTracker.getMemoryUtilizationPercent(),
nodeResourceUsageTracker.getCpuUtilizationPercent()
);
}
}
@Override
protected void doStart() {
/**
* Fetch local node resource usage statistics every second
*/
scheduledFuture = threadPool.scheduleWithFixedDelay(() -> {
try {
collectLocalNodeResourceUsageStats();
} catch (Exception e) {
logger.warn("failure in ResourceUsageCollectorService", e);
}
}, new TimeValue(REFRESH_INTERVAL_IN_MILLIS), ThreadPool.Names.GENERIC);
}
@Override
protected void doStop() {
if (scheduledFuture != null) {
scheduledFuture.cancel();
}
}
@Override
protected void doClose() {}
}