Skip to content

Commit d145b55

Browse files
Serialize NodesInfoRequest as a set of strings (#53140) (#53202)
For Node Info to be pluggable, NodesInfoRequest must be able to carry arbitrary strings. This commit reworks the internals of that class to use a set rather than hard-coded boolean fields. NodesInfoRequest defaults to specifying all values. We test for this behavior as we refactor and use random testing for the various combinations of metrics. Add backwards compatibility for transport requests.
1 parent 9bb9f63 commit d145b55

File tree

2 files changed

+260
-70
lines changed

2 files changed

+260
-70
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/node/info/NodesInfoRequest.java

Lines changed: 116 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,48 @@
1919

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

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

2627
import java.io.IOException;
28+
import java.util.Arrays;
29+
import java.util.Set;
30+
import java.util.stream.Collectors;
2731

2832
/**
2933
* A request to get node (cluster) level information.
3034
*/
3135
public class NodesInfoRequest extends BaseNodesRequest<NodesInfoRequest> {
3236

33-
private boolean settings = true;
34-
private boolean os = true;
35-
private boolean process = true;
36-
private boolean jvm = true;
37-
private boolean threadPool = true;
38-
private boolean transport = true;
39-
private boolean http = true;
40-
private boolean plugins = true;
41-
private boolean ingest = true;
42-
private boolean indices = true;
37+
private Set<String> requestedMetrics = Metrics.allMetrics();
4338

39+
/**
40+
* Create a new NodeInfoRequest from a {@link StreamInput} object.
41+
*
42+
* @param in A stream input object.
43+
* @throws IOException if the stream cannot be deserialized.
44+
*/
4445
public NodesInfoRequest(StreamInput in) throws IOException {
4546
super(in);
46-
settings = in.readBoolean();
47-
os = in.readBoolean();
48-
process = in.readBoolean();
49-
jvm = in.readBoolean();
50-
threadPool = in.readBoolean();
51-
transport = in.readBoolean();
52-
http = in.readBoolean();
53-
plugins = in.readBoolean();
54-
ingest = in.readBoolean();
55-
indices = in.readBoolean();
47+
requestedMetrics.clear();
48+
if (in.getVersion().before(Version.V_7_7_0)){
49+
// prior to version 8.x, a NodesInfoRequest was serialized as a list
50+
// of booleans in a fixed order
51+
addOrRemoveMetric(in.readBoolean(), Metrics.SETTINGS.metricName());
52+
addOrRemoveMetric(in.readBoolean(), Metrics.OS.metricName());
53+
addOrRemoveMetric(in.readBoolean(), Metrics.PROCESS.metricName());
54+
addOrRemoveMetric(in.readBoolean(), Metrics.JVM.metricName());
55+
addOrRemoveMetric(in.readBoolean(), Metrics.THREAD_POOL.metricName());
56+
addOrRemoveMetric(in.readBoolean(), Metrics.TRANSPORT.metricName());
57+
addOrRemoveMetric(in.readBoolean(), Metrics.HTTP.metricName());
58+
addOrRemoveMetric(in.readBoolean(), Metrics.PLUGINS.metricName());
59+
addOrRemoveMetric(in.readBoolean(), Metrics.INGEST.metricName());
60+
addOrRemoveMetric(in.readBoolean(), Metrics.INDICES.metricName());
61+
} else {
62+
requestedMetrics.addAll(Arrays.asList(in.readStringArray()));
63+
}
5664
}
5765

5866
/**
@@ -61,144 +69,127 @@ public NodesInfoRequest(StreamInput in) throws IOException {
6169
*/
6270
public NodesInfoRequest(String... nodesIds) {
6371
super(nodesIds);
72+
all();
6473
}
6574

6675
/**
6776
* Clears all info flags.
6877
*/
6978
public NodesInfoRequest clear() {
70-
settings = false;
71-
os = false;
72-
process = false;
73-
jvm = false;
74-
threadPool = false;
75-
transport = false;
76-
http = false;
77-
plugins = false;
78-
ingest = false;
79-
indices = false;
79+
requestedMetrics.clear();
8080
return this;
8181
}
8282

8383
/**
8484
* Sets to return all the data.
8585
*/
8686
public NodesInfoRequest all() {
87-
settings = true;
88-
os = true;
89-
process = true;
90-
jvm = true;
91-
threadPool = true;
92-
transport = true;
93-
http = true;
94-
plugins = true;
95-
ingest = true;
96-
indices = true;
87+
requestedMetrics.addAll(Metrics.allMetrics());
9788
return this;
9889
}
9990

10091
/**
10192
* Should the node settings be returned.
10293
*/
10394
public boolean settings() {
104-
return this.settings;
95+
return Metrics.SETTINGS.containedIn(requestedMetrics);
10596
}
10697

10798
/**
10899
* Should the node settings be returned.
109100
*/
110101
public NodesInfoRequest settings(boolean settings) {
111-
this.settings = settings;
102+
addOrRemoveMetric(settings, Metrics.SETTINGS.metricName());
112103
return this;
113104
}
114105

115106
/**
116107
* Should the node OS be returned.
117108
*/
118109
public boolean os() {
119-
return this.os;
110+
return Metrics.OS.containedIn(requestedMetrics);
120111
}
121112

122113
/**
123114
* Should the node OS be returned.
124115
*/
125116
public NodesInfoRequest os(boolean os) {
126-
this.os = os;
117+
addOrRemoveMetric(os, Metrics.OS.metricName());
127118
return this;
128119
}
129120

130121
/**
131122
* Should the node Process be returned.
132123
*/
133124
public boolean process() {
134-
return this.process;
125+
return Metrics.PROCESS.containedIn(requestedMetrics);
135126
}
136127

137128
/**
138129
* Should the node Process be returned.
139130
*/
140131
public NodesInfoRequest process(boolean process) {
141-
this.process = process;
132+
addOrRemoveMetric(process, Metrics.PROCESS.metricName());
142133
return this;
143134
}
144135

145136
/**
146137
* Should the node JVM be returned.
147138
*/
148139
public boolean jvm() {
149-
return this.jvm;
140+
return Metrics.JVM.containedIn(requestedMetrics);
150141
}
151142

152143
/**
153144
* Should the node JVM be returned.
154145
*/
155146
public NodesInfoRequest jvm(boolean jvm) {
156-
this.jvm = jvm;
147+
addOrRemoveMetric(jvm, Metrics.JVM.metricName());
157148
return this;
158149
}
159150

160151
/**
161152
* Should the node Thread Pool info be returned.
162153
*/
163154
public boolean threadPool() {
164-
return this.threadPool;
155+
return Metrics.THREAD_POOL.containedIn(requestedMetrics);
165156
}
166157

167158
/**
168159
* Should the node Thread Pool info be returned.
169160
*/
170161
public NodesInfoRequest threadPool(boolean threadPool) {
171-
this.threadPool = threadPool;
162+
addOrRemoveMetric(threadPool, Metrics.THREAD_POOL.metricName());
172163
return this;
173164
}
174165

175166
/**
176167
* Should the node Transport be returned.
177168
*/
178169
public boolean transport() {
179-
return this.transport;
170+
return Metrics.TRANSPORT.containedIn(requestedMetrics);
180171
}
181172

182173
/**
183174
* Should the node Transport be returned.
184175
*/
185176
public NodesInfoRequest transport(boolean transport) {
186-
this.transport = transport;
177+
addOrRemoveMetric(transport, Metrics.TRANSPORT.metricName());
187178
return this;
188179
}
189180

190181
/**
191182
* Should the node HTTP be returned.
192183
*/
193184
public boolean http() {
194-
return this.http;
185+
return Metrics.HTTP.containedIn(requestedMetrics);
195186
}
196187

197188
/**
198189
* Should the node HTTP be returned.
199190
*/
200191
public NodesInfoRequest http(boolean http) {
201-
this.http = http;
192+
addOrRemoveMetric(http, Metrics.HTTP.metricName());
202193
return this;
203194
}
204195

@@ -208,61 +199,116 @@ public NodesInfoRequest http(boolean http) {
208199
* @return The request
209200
*/
210201
public NodesInfoRequest plugins(boolean plugins) {
211-
this.plugins = plugins;
202+
addOrRemoveMetric(plugins, Metrics.PLUGINS.metricName());
212203
return this;
213204
}
214205

215206
/**
216207
* @return true if information about plugins is requested
217208
*/
218209
public boolean plugins() {
219-
return plugins;
210+
return Metrics.PLUGINS.containedIn(requestedMetrics);
220211
}
221212

222213
/**
223214
* Should information about ingest be returned
224215
* @param ingest true if you want info
225216
*/
226217
public NodesInfoRequest ingest(boolean ingest) {
227-
this.ingest = ingest;
218+
addOrRemoveMetric(ingest, Metrics.INGEST.metricName());
228219
return this;
229220
}
230221

231222
/**
232223
* @return true if information about ingest is requested
233224
*/
234225
public boolean ingest() {
235-
return ingest;
226+
return Metrics.INGEST.containedIn(requestedMetrics);
236227
}
237228

238229
/**
239230
* Should information about indices (currently just indexing buffers) be returned
240231
* @param indices true if you want info
241232
*/
242233
public NodesInfoRequest indices(boolean indices) {
243-
this.indices = indices;
234+
addOrRemoveMetric(indices, Metrics.INDICES.metricName());
244235
return this;
245236
}
246237

247238
/**
248239
* @return true if information about indices (currently just indexing buffers)
249240
*/
250241
public boolean indices() {
251-
return indices;
242+
return Metrics.INDICES.containedIn(requestedMetrics);
243+
}
244+
245+
/**
246+
* Helper method for adding and removing metrics.
247+
* @param includeMetric Whether or not to include a metric.
248+
* @param metricName Name of the metric to include or remove.
249+
*/
250+
private void addOrRemoveMetric(boolean includeMetric, String metricName) {
251+
if (includeMetric) {
252+
requestedMetrics.add(metricName);
253+
} else {
254+
requestedMetrics.remove(metricName);
255+
}
252256
}
253257

254258
@Override
255259
public void writeTo(StreamOutput out) throws IOException {
256260
super.writeTo(out);
257-
out.writeBoolean(settings);
258-
out.writeBoolean(os);
259-
out.writeBoolean(process);
260-
out.writeBoolean(jvm);
261-
out.writeBoolean(threadPool);
262-
out.writeBoolean(transport);
263-
out.writeBoolean(http);
264-
out.writeBoolean(plugins);
265-
out.writeBoolean(ingest);
266-
out.writeBoolean(indices);
261+
if (out.getVersion().before(Version.V_7_7_0)){
262+
// prior to version 8.x, a NodesInfoRequest was serialized as a list
263+
// of booleans in a fixed order
264+
out.writeBoolean(Metrics.SETTINGS.containedIn(requestedMetrics));
265+
out.writeBoolean(Metrics.OS.containedIn(requestedMetrics));
266+
out.writeBoolean(Metrics.PROCESS.containedIn(requestedMetrics));
267+
out.writeBoolean(Metrics.JVM.containedIn(requestedMetrics));
268+
out.writeBoolean(Metrics.THREAD_POOL.containedIn(requestedMetrics));
269+
out.writeBoolean(Metrics.TRANSPORT.containedIn(requestedMetrics));
270+
out.writeBoolean(Metrics.HTTP.containedIn(requestedMetrics));
271+
out.writeBoolean(Metrics.PLUGINS.containedIn(requestedMetrics));
272+
out.writeBoolean(Metrics.INGEST.containedIn(requestedMetrics));
273+
out.writeBoolean(Metrics.INDICES.containedIn(requestedMetrics));
274+
} else {
275+
out.writeStringArray(requestedMetrics.toArray(new String[0]));
276+
}
277+
}
278+
279+
/**
280+
* An enumeration of the "core" sections of metrics that may be requested
281+
* from the nodes information endpoint. Eventually this list list will be
282+
* pluggable.
283+
*/
284+
enum Metrics {
285+
SETTINGS("settings"),
286+
OS("os"),
287+
PROCESS("process"),
288+
JVM("jvm"),
289+
THREAD_POOL("threadPool"),
290+
TRANSPORT("transport"),
291+
HTTP("http"),
292+
PLUGINS("plugins"),
293+
INGEST("ingest"),
294+
INDICES("indices");
295+
296+
private String metricName;
297+
298+
Metrics(String name) {
299+
this.metricName = name;
300+
}
301+
302+
String metricName() {
303+
return this.metricName;
304+
}
305+
306+
boolean containedIn(Set<String> metricNames) {
307+
return metricNames.contains(this.metricName());
308+
}
309+
310+
static Set<String> allMetrics() {
311+
return Arrays.stream(values()).map(Metrics::metricName).collect(Collectors.toSet());
312+
}
267313
}
268314
}

0 commit comments

Comments
 (0)