Skip to content

Commit 7bc130b

Browse files
Use set-based interface for NodesStatsRequest (#53637)
The NodesStatsRequest class uses a set of strings for its internal serialization. This commit updates the class's interface so that we no longer use hard-coded getters and setters, but rather methods that add strings directly. For example, the old way of adding "os" metrics to a request would be to call request.os(true). The new way of doing this is to call request.addMetric("os"). For the time being, the canonical list of metrics is an enum in NodesStatsRequest. This will eventually be replaced with something pluggable.
1 parent ae7da16 commit 7bc130b

File tree

13 files changed

+193
-270
lines changed

13 files changed

+193
-270
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ task verifyVersions {
222222
* after the backport of the backcompat code is complete.
223223
*/
224224

225-
boolean bwc_tests_enabled = true
226-
final String bwc_tests_disabled_issue = "" /* place a PR link here when committing bwc changes */
225+
boolean bwc_tests_enabled = false
226+
final String bwc_tests_disabled_issue = "https://github.com/elastic/elasticsearch/pull/53637" /* place a PR link here when committing bwc changes */
227227
if (bwc_tests_enabled == false) {
228228
if (bwc_tests_disabled_issue.isEmpty()) {
229229
throw new GradleException("bwc_tests_disabled_issue must be set when bwc_tests_enabled == false")

server/src/main/java/org/elasticsearch/action/admin/cluster/node/stats/NodesStatsRequest.java

Lines changed: 57 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
import org.elasticsearch.action.support.nodes.BaseNodesRequest;
2525
import org.elasticsearch.common.io.stream.StreamInput;
2626
import org.elasticsearch.common.io.stream.StreamOutput;
27-
2827
import java.io.IOException;
2928
import java.util.Arrays;
3029
import java.util.HashSet;
3130
import java.util.Set;
31+
import java.util.SortedSet;
32+
import java.util.TreeSet;
3233
import java.util.stream.Collectors;
3334

3435
/**
@@ -49,18 +50,18 @@ public NodesStatsRequest(StreamInput in) throws IOException {
4950
indices = new CommonStatsFlags(in);
5051
requestedMetrics.clear();
5152
if (in.getVersion().before(Version.V_7_7_0)) {
52-
addOrRemoveMetric(in.readBoolean(), Metric.OS.metricName());
53-
addOrRemoveMetric(in.readBoolean(), Metric.PROCESS.metricName());
54-
addOrRemoveMetric(in.readBoolean(), Metric.JVM.metricName());
55-
addOrRemoveMetric(in.readBoolean(), Metric.THREAD_POOL.metricName());
56-
addOrRemoveMetric(in.readBoolean(), Metric.FS.metricName());
57-
addOrRemoveMetric(in.readBoolean(), Metric.TRANSPORT.metricName());
58-
addOrRemoveMetric(in.readBoolean(), Metric.HTTP.metricName());
59-
addOrRemoveMetric(in.readBoolean(), Metric.BREAKER.metricName());
60-
addOrRemoveMetric(in.readBoolean(), Metric.SCRIPT.metricName());
61-
addOrRemoveMetric(in.readBoolean(), Metric.DISCOVERY.metricName());
62-
addOrRemoveMetric(in.readBoolean(), Metric.INGEST.metricName());
63-
addOrRemoveMetric(in.readBoolean(), Metric.ADAPTIVE_SELECTION.metricName());
53+
optionallyAddMetric(in.readBoolean(), Metric.OS.metricName());
54+
optionallyAddMetric(in.readBoolean(), Metric.PROCESS.metricName());
55+
optionallyAddMetric(in.readBoolean(), Metric.JVM.metricName());
56+
optionallyAddMetric(in.readBoolean(), Metric.THREAD_POOL.metricName());
57+
optionallyAddMetric(in.readBoolean(), Metric.FS.metricName());
58+
optionallyAddMetric(in.readBoolean(), Metric.TRANSPORT.metricName());
59+
optionallyAddMetric(in.readBoolean(), Metric.HTTP.metricName());
60+
optionallyAddMetric(in.readBoolean(), Metric.BREAKER.metricName());
61+
optionallyAddMetric(in.readBoolean(), Metric.SCRIPT.metricName());
62+
optionallyAddMetric(in.readBoolean(), Metric.DISCOVERY.metricName());
63+
optionallyAddMetric(in.readBoolean(), Metric.INGEST.metricName());
64+
optionallyAddMetric(in.readBoolean(), Metric.ADAPTIVE_SELECTION.metricName());
6465
} else {
6566
requestedMetrics.addAll(in.readStringList());
6667
}
@@ -92,10 +93,21 @@ public NodesStatsRequest clear() {
9293
return this;
9394
}
9495

96+
/**
97+
* Get indices. Handles separately from other metrics because it may or
98+
* may not have submetrics.
99+
* @return flags indicating which indices stats to return
100+
*/
95101
public CommonStatsFlags indices() {
96102
return indices;
97103
}
98104

105+
/**
106+
* Set indices. Handles separately from other metrics because it may or
107+
* may not involve submetrics.
108+
* @param indices flags indicating which indices stats to return
109+
* @return This object, for request chaining.
110+
*/
99111
public NodesStatsRequest indices(CommonStatsFlags indices) {
100112
this.indices = indices;
101113
return this;
@@ -114,178 +126,58 @@ public NodesStatsRequest indices(boolean indices) {
114126
}
115127

116128
/**
117-
* Should the node OS be returned.
118-
*/
119-
public boolean os() {
120-
return Metric.OS.containedIn(requestedMetrics);
121-
}
122-
123-
/**
124-
* Should the node OS be returned.
125-
*/
126-
public NodesStatsRequest os(boolean os) {
127-
addOrRemoveMetric(os, Metric.OS.metricName());
128-
return this;
129-
}
130-
131-
/**
132-
* Should the node Process be returned.
133-
*/
134-
public boolean process() {
135-
return Metric.PROCESS.containedIn(requestedMetrics);
136-
}
137-
138-
/**
139-
* Should the node Process be returned.
140-
*/
141-
public NodesStatsRequest process(boolean process) {
142-
addOrRemoveMetric(process, Metric.PROCESS.metricName());
143-
return this;
144-
}
145-
146-
/**
147-
* Should the node JVM be returned.
148-
*/
149-
public boolean jvm() {
150-
return Metric.JVM.containedIn(requestedMetrics);
151-
}
152-
153-
/**
154-
* Should the node JVM be returned.
155-
*/
156-
public NodesStatsRequest jvm(boolean jvm) {
157-
addOrRemoveMetric(jvm, Metric.JVM.metricName());
158-
return this;
159-
}
160-
161-
/**
162-
* Should the node Thread Pool be returned.
163-
*/
164-
public boolean threadPool() {
165-
return Metric.THREAD_POOL.containedIn(requestedMetrics);
166-
}
167-
168-
/**
169-
* Should the node Thread Pool be returned.
170-
*/
171-
public NodesStatsRequest threadPool(boolean threadPool) {
172-
addOrRemoveMetric(threadPool, Metric.THREAD_POOL.metricName());
173-
return this;
174-
}
175-
176-
/**
177-
* Should the node file system stats be returned.
129+
* Get the names of requested metrics, excluding indices, which are
130+
* handled separately.
178131
*/
179-
public boolean fs() {
180-
return Metric.FS.containedIn(requestedMetrics);
132+
public Set<String> requestedMetrics() {
133+
return Set.copyOf(requestedMetrics);
181134
}
182135

183136
/**
184-
* Should the node file system stats be returned.
137+
* Add metric
185138
*/
186-
public NodesStatsRequest fs(boolean fs) {
187-
addOrRemoveMetric(fs, Metric.FS.metricName());
188-
return this;
189-
}
190-
191-
/**
192-
* Should the node Transport be returned.
193-
*/
194-
public boolean transport() {
195-
return Metric.TRANSPORT.containedIn(requestedMetrics);
196-
}
197-
198-
/**
199-
* Should the node Transport be returned.
200-
*/
201-
public NodesStatsRequest transport(boolean transport) {
202-
addOrRemoveMetric(transport, Metric.TRANSPORT.metricName());
203-
return this;
204-
}
205-
206-
/**
207-
* Should the node HTTP be returned.
208-
*/
209-
public boolean http() {
210-
return Metric.HTTP.containedIn(requestedMetrics);
211-
}
212-
213-
/**
214-
* Should the node HTTP be returned.
215-
*/
216-
public NodesStatsRequest http(boolean http) {
217-
addOrRemoveMetric(http, Metric.HTTP.metricName());
218-
return this;
219-
}
220-
221-
public boolean breaker() {
222-
return Metric.BREAKER.containedIn(requestedMetrics);
223-
}
224-
225-
/**
226-
* Should the node's circuit breaker stats be returned.
227-
*/
228-
public NodesStatsRequest breaker(boolean breaker) {
229-
addOrRemoveMetric(breaker, Metric.BREAKER.metricName());
230-
return this;
231-
}
232-
233-
public boolean script() {
234-
return Metric.SCRIPT.containedIn(requestedMetrics);
235-
}
236-
237-
public NodesStatsRequest script(boolean script) {
238-
addOrRemoveMetric(script, Metric.SCRIPT.metricName());
239-
return this;
240-
}
241-
242-
243-
public boolean discovery() {
244-
return Metric.DISCOVERY.containedIn(requestedMetrics);
245-
}
246-
247-
/**
248-
* Should the node's discovery stats be returned.
249-
*/
250-
public NodesStatsRequest discovery(boolean discovery) {
251-
addOrRemoveMetric(discovery, Metric.DISCOVERY.metricName());
139+
public NodesStatsRequest addMetric(String metric) {
140+
if (Metric.allMetrics().contains(metric) == false) {
141+
throw new IllegalStateException("Used an illegal metric: " + metric);
142+
}
143+
requestedMetrics.add(metric);
252144
return this;
253145
}
254146

255-
public boolean ingest() {
256-
return Metric.INGEST.containedIn(requestedMetrics);
257-
}
258-
259147
/**
260-
* Should ingest statistics be returned.
148+
* Add an array of metric names
261149
*/
262-
public NodesStatsRequest ingest(boolean ingest) {
263-
addOrRemoveMetric(ingest, Metric.INGEST.metricName());
150+
public NodesStatsRequest addMetrics(String... metrics) {
151+
// use sorted set for reliable ordering in error messages
152+
SortedSet<String> metricsSet = new TreeSet<>(Set.of(metrics));
153+
if (Metric.allMetrics().containsAll(metricsSet) == false) {
154+
metricsSet.removeAll(Metric.allMetrics());
155+
String plural = metricsSet.size() == 1 ? "" : "s";
156+
throw new IllegalStateException("Used illegal metric" + plural + ": " + metricsSet);
157+
}
158+
requestedMetrics.addAll(metricsSet);
264159
return this;
265160
}
266161

267-
public boolean adaptiveSelection() {
268-
return Metric.ADAPTIVE_SELECTION.containedIn(requestedMetrics);
269-
}
270-
271162
/**
272-
* Should adaptiveSelection statistics be returned.
163+
* Remove metric
273164
*/
274-
public NodesStatsRequest adaptiveSelection(boolean adaptiveSelection) {
275-
addOrRemoveMetric(adaptiveSelection, Metric.ADAPTIVE_SELECTION.metricName());
165+
public NodesStatsRequest removeMetric(String metric) {
166+
if (Metric.allMetrics().contains(metric) == false) {
167+
throw new IllegalStateException("Used an illegal metric: " + metric);
168+
}
169+
requestedMetrics.remove(metric);
276170
return this;
277171
}
278172

279173
/**
280-
* Helper method for adding and removing metrics.
174+
* Helper method for adding metrics during deserialization.
281175
* @param includeMetric Whether or not to include a metric.
282-
* @param metricName Name of the metric to include or remove.
176+
* @param metricName Name of the metric to add.
283177
*/
284-
private void addOrRemoveMetric(boolean includeMetric, String metricName) {
178+
private void optionallyAddMetric(boolean includeMetric, String metricName) {
285179
if (includeMetric) {
286180
requestedMetrics.add(metricName);
287-
} else {
288-
requestedMetrics.remove(metricName);
289181
}
290182
}
291183

@@ -315,7 +207,7 @@ public void writeTo(StreamOutput out) throws IOException {
315207
* An enumeration of the "core" sections of metrics that may be requested
316208
* from the nodes stats endpoint. Eventually this list will be pluggable.
317209
*/
318-
private enum Metric {
210+
public enum Metric {
319211
OS("os"),
320212
PROCESS("process"),
321213
JVM("jvm"),
@@ -327,15 +219,15 @@ private enum Metric {
327219
SCRIPT("script"),
328220
DISCOVERY("discovery"),
329221
INGEST("ingest"),
330-
ADAPTIVE_SELECTION("adaptiveSelection");
222+
ADAPTIVE_SELECTION("adaptive_selection");
331223

332224
private String metricName;
333225

334226
Metric(String name) {
335227
this.metricName = name;
336228
}
337229

338-
String metricName() {
230+
public String metricName() {
339231
return this.metricName;
340232
}
341233

0 commit comments

Comments
 (0)