19
19
20
20
package org .elasticsearch .action .admin .cluster .node .info ;
21
21
22
+ import org .elasticsearch .Version ;
22
23
import org .elasticsearch .action .support .nodes .BaseNodesRequest ;
23
24
import org .elasticsearch .common .io .stream .StreamInput ;
24
25
import org .elasticsearch .common .io .stream .StreamOutput ;
25
26
26
27
import java .io .IOException ;
28
+ import java .util .Arrays ;
29
+ import java .util .Set ;
30
+ import java .util .stream .Collectors ;
27
31
28
32
/**
29
33
* A request to get node (cluster) level information.
30
34
*/
31
35
public class NodesInfoRequest extends BaseNodesRequest <NodesInfoRequest > {
32
36
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 ();
43
38
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
+ */
44
45
public NodesInfoRequest (StreamInput in ) throws IOException {
45
46
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
+ }
56
64
}
57
65
58
66
/**
@@ -61,144 +69,127 @@ public NodesInfoRequest(StreamInput in) throws IOException {
61
69
*/
62
70
public NodesInfoRequest (String ... nodesIds ) {
63
71
super (nodesIds );
72
+ all ();
64
73
}
65
74
66
75
/**
67
76
* Clears all info flags.
68
77
*/
69
78
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 ();
80
80
return this ;
81
81
}
82
82
83
83
/**
84
84
* Sets to return all the data.
85
85
*/
86
86
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 ());
97
88
return this ;
98
89
}
99
90
100
91
/**
101
92
* Should the node settings be returned.
102
93
*/
103
94
public boolean settings () {
104
- return this . settings ;
95
+ return Metrics . SETTINGS . containedIn ( requestedMetrics ) ;
105
96
}
106
97
107
98
/**
108
99
* Should the node settings be returned.
109
100
*/
110
101
public NodesInfoRequest settings (boolean settings ) {
111
- this . settings = settings ;
102
+ addOrRemoveMetric ( settings , Metrics . SETTINGS . metricName ()) ;
112
103
return this ;
113
104
}
114
105
115
106
/**
116
107
* Should the node OS be returned.
117
108
*/
118
109
public boolean os () {
119
- return this . os ;
110
+ return Metrics . OS . containedIn ( requestedMetrics ) ;
120
111
}
121
112
122
113
/**
123
114
* Should the node OS be returned.
124
115
*/
125
116
public NodesInfoRequest os (boolean os ) {
126
- this . os = os ;
117
+ addOrRemoveMetric ( os , Metrics . OS . metricName ()) ;
127
118
return this ;
128
119
}
129
120
130
121
/**
131
122
* Should the node Process be returned.
132
123
*/
133
124
public boolean process () {
134
- return this . process ;
125
+ return Metrics . PROCESS . containedIn ( requestedMetrics ) ;
135
126
}
136
127
137
128
/**
138
129
* Should the node Process be returned.
139
130
*/
140
131
public NodesInfoRequest process (boolean process ) {
141
- this . process = process ;
132
+ addOrRemoveMetric ( process , Metrics . PROCESS . metricName ()) ;
142
133
return this ;
143
134
}
144
135
145
136
/**
146
137
* Should the node JVM be returned.
147
138
*/
148
139
public boolean jvm () {
149
- return this . jvm ;
140
+ return Metrics . JVM . containedIn ( requestedMetrics ) ;
150
141
}
151
142
152
143
/**
153
144
* Should the node JVM be returned.
154
145
*/
155
146
public NodesInfoRequest jvm (boolean jvm ) {
156
- this . jvm = jvm ;
147
+ addOrRemoveMetric ( jvm , Metrics . JVM . metricName ()) ;
157
148
return this ;
158
149
}
159
150
160
151
/**
161
152
* Should the node Thread Pool info be returned.
162
153
*/
163
154
public boolean threadPool () {
164
- return this . threadPool ;
155
+ return Metrics . THREAD_POOL . containedIn ( requestedMetrics ) ;
165
156
}
166
157
167
158
/**
168
159
* Should the node Thread Pool info be returned.
169
160
*/
170
161
public NodesInfoRequest threadPool (boolean threadPool ) {
171
- this . threadPool = threadPool ;
162
+ addOrRemoveMetric ( threadPool , Metrics . THREAD_POOL . metricName ()) ;
172
163
return this ;
173
164
}
174
165
175
166
/**
176
167
* Should the node Transport be returned.
177
168
*/
178
169
public boolean transport () {
179
- return this . transport ;
170
+ return Metrics . TRANSPORT . containedIn ( requestedMetrics ) ;
180
171
}
181
172
182
173
/**
183
174
* Should the node Transport be returned.
184
175
*/
185
176
public NodesInfoRequest transport (boolean transport ) {
186
- this . transport = transport ;
177
+ addOrRemoveMetric ( transport , Metrics . TRANSPORT . metricName ()) ;
187
178
return this ;
188
179
}
189
180
190
181
/**
191
182
* Should the node HTTP be returned.
192
183
*/
193
184
public boolean http () {
194
- return this . http ;
185
+ return Metrics . HTTP . containedIn ( requestedMetrics ) ;
195
186
}
196
187
197
188
/**
198
189
* Should the node HTTP be returned.
199
190
*/
200
191
public NodesInfoRequest http (boolean http ) {
201
- this . http = http ;
192
+ addOrRemoveMetric ( http , Metrics . HTTP . metricName ()) ;
202
193
return this ;
203
194
}
204
195
@@ -208,61 +199,116 @@ public NodesInfoRequest http(boolean http) {
208
199
* @return The request
209
200
*/
210
201
public NodesInfoRequest plugins (boolean plugins ) {
211
- this . plugins = plugins ;
202
+ addOrRemoveMetric ( plugins , Metrics . PLUGINS . metricName ()) ;
212
203
return this ;
213
204
}
214
205
215
206
/**
216
207
* @return true if information about plugins is requested
217
208
*/
218
209
public boolean plugins () {
219
- return plugins ;
210
+ return Metrics . PLUGINS . containedIn ( requestedMetrics ) ;
220
211
}
221
212
222
213
/**
223
214
* Should information about ingest be returned
224
215
* @param ingest true if you want info
225
216
*/
226
217
public NodesInfoRequest ingest (boolean ingest ) {
227
- this . ingest = ingest ;
218
+ addOrRemoveMetric ( ingest , Metrics . INGEST . metricName ()) ;
228
219
return this ;
229
220
}
230
221
231
222
/**
232
223
* @return true if information about ingest is requested
233
224
*/
234
225
public boolean ingest () {
235
- return ingest ;
226
+ return Metrics . INGEST . containedIn ( requestedMetrics ) ;
236
227
}
237
228
238
229
/**
239
230
* Should information about indices (currently just indexing buffers) be returned
240
231
* @param indices true if you want info
241
232
*/
242
233
public NodesInfoRequest indices (boolean indices ) {
243
- this . indices = indices ;
234
+ addOrRemoveMetric ( indices , Metrics . INDICES . metricName ()) ;
244
235
return this ;
245
236
}
246
237
247
238
/**
248
239
* @return true if information about indices (currently just indexing buffers)
249
240
*/
250
241
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
+ }
252
256
}
253
257
254
258
@ Override
255
259
public void writeTo (StreamOutput out ) throws IOException {
256
260
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
+ }
267
313
}
268
314
}
0 commit comments