Skip to content

Commit 51dd8d8

Browse files
Serialize NodesStatsRequest as set of strings (#53235)
* Add unit tests before refactoring * Convert boolean fields to set of strings In order to make nodes stats plugins pluggable, we need to make the NodesStatsRequest class capable of carrying a flexible list of metrics rather than a fixed list of boolean flags. This commit changes the internal storage of the class without changing its serialization. * Rename test class to match conventions * Change serialization of NodesStatsRequest * Set up BWC before merging * Singularize enum name
1 parent aea7aec commit 51dd8d8

File tree

3 files changed

+267
-86
lines changed

3 files changed

+267
-86
lines changed

build.gradle

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

223-
boolean bwc_tests_enabled = true
224-
final String bwc_tests_disabled_issue = "" /* place a PR link here when committing bwc changes */
223+
boolean bwc_tests_enabled = false
224+
final String bwc_tests_disabled_issue = "https://github.com/elastic/elasticsearch/pull/53235" /* place a PR link here when committing bwc changes */
225225
if (bwc_tests_enabled == false) {
226226
if (bwc_tests_disabled_issue.isEmpty()) {
227227
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: 116 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,51 @@
1919

2020
package org.elasticsearch.action.admin.cluster.node.stats;
2121

22+
import org.elasticsearch.Version;
2223
import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags;
2324
import org.elasticsearch.action.support.nodes.BaseNodesRequest;
2425
import org.elasticsearch.common.io.stream.StreamInput;
2526
import org.elasticsearch.common.io.stream.StreamOutput;
2627

2728
import java.io.IOException;
29+
import java.util.Arrays;
30+
import java.util.HashSet;
31+
import java.util.Set;
32+
import java.util.stream.Collectors;
2833

2934
/**
3035
* A request to get node (cluster) level stats.
3136
*/
3237
public class NodesStatsRequest extends BaseNodesRequest<NodesStatsRequest> {
3338

3439
private CommonStatsFlags indices = new CommonStatsFlags();
35-
private boolean os;
36-
private boolean process;
37-
private boolean jvm;
38-
private boolean threadPool;
39-
private boolean fs;
40-
private boolean transport;
41-
private boolean http;
42-
private boolean breaker;
43-
private boolean script;
44-
private boolean discovery;
45-
private boolean ingest;
46-
private boolean adaptiveSelection;
40+
private final Set<String> requestedMetrics = new HashSet<>();
4741

4842
public NodesStatsRequest() {
4943
super((String[]) null);
5044
}
5145

5246
public NodesStatsRequest(StreamInput in) throws IOException {
5347
super(in);
48+
5449
indices = new CommonStatsFlags(in);
55-
os = in.readBoolean();
56-
process = in.readBoolean();
57-
jvm = in.readBoolean();
58-
threadPool = in.readBoolean();
59-
fs = in.readBoolean();
60-
transport = in.readBoolean();
61-
http = in.readBoolean();
62-
breaker = in.readBoolean();
63-
script = in.readBoolean();
64-
discovery = in.readBoolean();
65-
ingest = in.readBoolean();
66-
adaptiveSelection = in.readBoolean();
50+
requestedMetrics.clear();
51+
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());
64+
} else {
65+
requestedMetrics.addAll(in.readStringList());
66+
}
6767
}
6868

6969
/**
@@ -79,18 +79,7 @@ public NodesStatsRequest(String... nodesIds) {
7979
*/
8080
public NodesStatsRequest all() {
8181
this.indices.all();
82-
this.os = true;
83-
this.process = true;
84-
this.jvm = true;
85-
this.threadPool = true;
86-
this.fs = true;
87-
this.transport = true;
88-
this.http = true;
89-
this.breaker = true;
90-
this.script = true;
91-
this.discovery = true;
92-
this.ingest = true;
93-
this.adaptiveSelection = true;
82+
this.requestedMetrics.addAll(Metric.allMetrics());
9483
return this;
9584
}
9685

@@ -99,18 +88,7 @@ public NodesStatsRequest all() {
9988
*/
10089
public NodesStatsRequest clear() {
10190
this.indices.clear();
102-
this.os = false;
103-
this.process = false;
104-
this.jvm = false;
105-
this.threadPool = false;
106-
this.fs = false;
107-
this.transport = false;
108-
this.http = false;
109-
this.breaker = false;
110-
this.script = false;
111-
this.discovery = false;
112-
this.ingest = false;
113-
this.adaptiveSelection = false;
91+
this.requestedMetrics.clear();
11492
return this;
11593
}
11694

@@ -139,180 +117,234 @@ public NodesStatsRequest indices(boolean indices) {
139117
* Should the node OS be returned.
140118
*/
141119
public boolean os() {
142-
return this.os;
120+
return Metric.OS.containedIn(requestedMetrics);
143121
}
144122

145123
/**
146124
* Should the node OS be returned.
147125
*/
148126
public NodesStatsRequest os(boolean os) {
149-
this.os = os;
127+
addOrRemoveMetric(os, Metric.OS.metricName());
150128
return this;
151129
}
152130

153131
/**
154132
* Should the node Process be returned.
155133
*/
156134
public boolean process() {
157-
return this.process;
135+
return Metric.PROCESS.containedIn(requestedMetrics);
158136
}
159137

160138
/**
161139
* Should the node Process be returned.
162140
*/
163141
public NodesStatsRequest process(boolean process) {
164-
this.process = process;
142+
addOrRemoveMetric(process, Metric.PROCESS.metricName());
165143
return this;
166144
}
167145

168146
/**
169147
* Should the node JVM be returned.
170148
*/
171149
public boolean jvm() {
172-
return this.jvm;
150+
return Metric.JVM.containedIn(requestedMetrics);
173151
}
174152

175153
/**
176154
* Should the node JVM be returned.
177155
*/
178156
public NodesStatsRequest jvm(boolean jvm) {
179-
this.jvm = jvm;
157+
addOrRemoveMetric(jvm, Metric.JVM.metricName());
180158
return this;
181159
}
182160

183161
/**
184162
* Should the node Thread Pool be returned.
185163
*/
186164
public boolean threadPool() {
187-
return this.threadPool;
165+
return Metric.THREAD_POOL.containedIn(requestedMetrics);
188166
}
189167

190168
/**
191169
* Should the node Thread Pool be returned.
192170
*/
193171
public NodesStatsRequest threadPool(boolean threadPool) {
194-
this.threadPool = threadPool;
172+
addOrRemoveMetric(threadPool, Metric.THREAD_POOL.metricName());
195173
return this;
196174
}
197175

198176
/**
199177
* Should the node file system stats be returned.
200178
*/
201179
public boolean fs() {
202-
return this.fs;
180+
return Metric.FS.containedIn(requestedMetrics);
203181
}
204182

205183
/**
206184
* Should the node file system stats be returned.
207185
*/
208186
public NodesStatsRequest fs(boolean fs) {
209-
this.fs = fs;
187+
addOrRemoveMetric(fs, Metric.FS.metricName());
210188
return this;
211189
}
212190

213191
/**
214192
* Should the node Transport be returned.
215193
*/
216194
public boolean transport() {
217-
return this.transport;
195+
return Metric.TRANSPORT.containedIn(requestedMetrics);
218196
}
219197

220198
/**
221199
* Should the node Transport be returned.
222200
*/
223201
public NodesStatsRequest transport(boolean transport) {
224-
this.transport = transport;
202+
addOrRemoveMetric(transport, Metric.TRANSPORT.metricName());
225203
return this;
226204
}
227205

228206
/**
229207
* Should the node HTTP be returned.
230208
*/
231209
public boolean http() {
232-
return this.http;
210+
return Metric.HTTP.containedIn(requestedMetrics);
233211
}
234212

235213
/**
236214
* Should the node HTTP be returned.
237215
*/
238216
public NodesStatsRequest http(boolean http) {
239-
this.http = http;
217+
addOrRemoveMetric(http, Metric.HTTP.metricName());
240218
return this;
241219
}
242220

243221
public boolean breaker() {
244-
return this.breaker;
222+
return Metric.BREAKER.containedIn(requestedMetrics);
245223
}
246224

247225
/**
248226
* Should the node's circuit breaker stats be returned.
249227
*/
250228
public NodesStatsRequest breaker(boolean breaker) {
251-
this.breaker = breaker;
229+
addOrRemoveMetric(breaker, Metric.BREAKER.metricName());
252230
return this;
253231
}
254232

255233
public boolean script() {
256-
return script;
234+
return Metric.SCRIPT.containedIn(requestedMetrics);
257235
}
258236

259237
public NodesStatsRequest script(boolean script) {
260-
this.script = script;
238+
addOrRemoveMetric(script, Metric.SCRIPT.metricName());
261239
return this;
262240
}
263241

264242

265243
public boolean discovery() {
266-
return this.discovery;
244+
return Metric.DISCOVERY.containedIn(requestedMetrics);
267245
}
268246

269247
/**
270248
* Should the node's discovery stats be returned.
271249
*/
272250
public NodesStatsRequest discovery(boolean discovery) {
273-
this.discovery = discovery;
251+
addOrRemoveMetric(discovery, Metric.DISCOVERY.metricName());
274252
return this;
275253
}
276254

277255
public boolean ingest() {
278-
return ingest;
256+
return Metric.INGEST.containedIn(requestedMetrics);
279257
}
280258

281259
/**
282260
* Should ingest statistics be returned.
283261
*/
284262
public NodesStatsRequest ingest(boolean ingest) {
285-
this.ingest = ingest;
263+
addOrRemoveMetric(ingest, Metric.INGEST.metricName());
286264
return this;
287265
}
288266

289267
public boolean adaptiveSelection() {
290-
return adaptiveSelection;
268+
return Metric.ADAPTIVE_SELECTION.containedIn(requestedMetrics);
291269
}
292270

293271
/**
294272
* Should adaptiveSelection statistics be returned.
295273
*/
296274
public NodesStatsRequest adaptiveSelection(boolean adaptiveSelection) {
297-
this.adaptiveSelection = adaptiveSelection;
275+
addOrRemoveMetric(adaptiveSelection, Metric.ADAPTIVE_SELECTION.metricName());
298276
return this;
299277
}
300278

279+
/**
280+
* Helper method for adding and removing metrics.
281+
* @param includeMetric Whether or not to include a metric.
282+
* @param metricName Name of the metric to include or remove.
283+
*/
284+
private void addOrRemoveMetric(boolean includeMetric, String metricName) {
285+
if (includeMetric) {
286+
requestedMetrics.add(metricName);
287+
} else {
288+
requestedMetrics.remove(metricName);
289+
}
290+
}
291+
301292
@Override
302293
public void writeTo(StreamOutput out) throws IOException {
303294
super.writeTo(out);
304295
indices.writeTo(out);
305-
out.writeBoolean(os);
306-
out.writeBoolean(process);
307-
out.writeBoolean(jvm);
308-
out.writeBoolean(threadPool);
309-
out.writeBoolean(fs);
310-
out.writeBoolean(transport);
311-
out.writeBoolean(http);
312-
out.writeBoolean(breaker);
313-
out.writeBoolean(script);
314-
out.writeBoolean(discovery);
315-
out.writeBoolean(ingest);
316-
out.writeBoolean(adaptiveSelection);
296+
if (out.getVersion().before(Version.V_7_7_0)) {
297+
out.writeBoolean(Metric.OS.containedIn(requestedMetrics));
298+
out.writeBoolean(Metric.PROCESS.containedIn(requestedMetrics));
299+
out.writeBoolean(Metric.JVM.containedIn(requestedMetrics));
300+
out.writeBoolean(Metric.THREAD_POOL.containedIn(requestedMetrics));
301+
out.writeBoolean(Metric.FS.containedIn(requestedMetrics));
302+
out.writeBoolean(Metric.TRANSPORT.containedIn(requestedMetrics));
303+
out.writeBoolean(Metric.HTTP.containedIn(requestedMetrics));
304+
out.writeBoolean(Metric.BREAKER.containedIn(requestedMetrics));
305+
out.writeBoolean(Metric.SCRIPT.containedIn(requestedMetrics));
306+
out.writeBoolean(Metric.DISCOVERY.containedIn(requestedMetrics));
307+
out.writeBoolean(Metric.INGEST.containedIn(requestedMetrics));
308+
out.writeBoolean(Metric.ADAPTIVE_SELECTION.containedIn(requestedMetrics));
309+
} else {
310+
out.writeStringArray(requestedMetrics.toArray(String[]::new));
311+
}
312+
}
313+
314+
/**
315+
* An enumeration of the "core" sections of metrics that may be requested
316+
* from the nodes stats endpoint. Eventually this list will be pluggable.
317+
*/
318+
private enum Metric {
319+
OS("os"),
320+
PROCESS("process"),
321+
JVM("jvm"),
322+
THREAD_POOL("threadPool"),
323+
FS("fs"),
324+
TRANSPORT("transport"),
325+
HTTP("http"),
326+
BREAKER("breaker"),
327+
SCRIPT("script"),
328+
DISCOVERY("discovery"),
329+
INGEST("ingest"),
330+
ADAPTIVE_SELECTION("adaptiveSelection");
331+
332+
private String metricName;
333+
334+
Metric(String name) {
335+
this.metricName = name;
336+
}
337+
338+
String metricName() {
339+
return this.metricName;
340+
}
341+
342+
boolean containedIn(Set<String> metricNames) {
343+
return metricNames.contains(this.metricName());
344+
}
345+
346+
static Set<String> allMetrics() {
347+
return Arrays.stream(values()).map(Metric::metricName).collect(Collectors.toSet());
348+
}
317349
}
318350
}

0 commit comments

Comments
 (0)