19
19
20
20
package org .elasticsearch .action .admin .cluster .node .stats ;
21
21
22
+ import org .elasticsearch .Version ;
22
23
import org .elasticsearch .action .admin .indices .stats .CommonStatsFlags ;
23
24
import org .elasticsearch .action .support .nodes .BaseNodesRequest ;
24
25
import org .elasticsearch .common .io .stream .StreamInput ;
25
26
import org .elasticsearch .common .io .stream .StreamOutput ;
26
27
27
28
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 ;
28
33
29
34
/**
30
35
* A request to get node (cluster) level stats.
31
36
*/
32
37
public class NodesStatsRequest extends BaseNodesRequest <NodesStatsRequest > {
33
38
34
39
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 <>();
47
41
48
42
public NodesStatsRequest () {
49
43
super ((String []) null );
50
44
}
51
45
52
46
public NodesStatsRequest (StreamInput in ) throws IOException {
53
47
super (in );
48
+
54
49
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
+ }
67
67
}
68
68
69
69
/**
@@ -79,18 +79,7 @@ public NodesStatsRequest(String... nodesIds) {
79
79
*/
80
80
public NodesStatsRequest all () {
81
81
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 ());
94
83
return this ;
95
84
}
96
85
@@ -99,18 +88,7 @@ public NodesStatsRequest all() {
99
88
*/
100
89
public NodesStatsRequest clear () {
101
90
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 ();
114
92
return this ;
115
93
}
116
94
@@ -139,180 +117,234 @@ public NodesStatsRequest indices(boolean indices) {
139
117
* Should the node OS be returned.
140
118
*/
141
119
public boolean os () {
142
- return this . os ;
120
+ return Metric . OS . containedIn ( requestedMetrics ) ;
143
121
}
144
122
145
123
/**
146
124
* Should the node OS be returned.
147
125
*/
148
126
public NodesStatsRequest os (boolean os ) {
149
- this . os = os ;
127
+ addOrRemoveMetric ( os , Metric . OS . metricName ()) ;
150
128
return this ;
151
129
}
152
130
153
131
/**
154
132
* Should the node Process be returned.
155
133
*/
156
134
public boolean process () {
157
- return this . process ;
135
+ return Metric . PROCESS . containedIn ( requestedMetrics ) ;
158
136
}
159
137
160
138
/**
161
139
* Should the node Process be returned.
162
140
*/
163
141
public NodesStatsRequest process (boolean process ) {
164
- this . process = process ;
142
+ addOrRemoveMetric ( process , Metric . PROCESS . metricName ()) ;
165
143
return this ;
166
144
}
167
145
168
146
/**
169
147
* Should the node JVM be returned.
170
148
*/
171
149
public boolean jvm () {
172
- return this . jvm ;
150
+ return Metric . JVM . containedIn ( requestedMetrics ) ;
173
151
}
174
152
175
153
/**
176
154
* Should the node JVM be returned.
177
155
*/
178
156
public NodesStatsRequest jvm (boolean jvm ) {
179
- this . jvm = jvm ;
157
+ addOrRemoveMetric ( jvm , Metric . JVM . metricName ()) ;
180
158
return this ;
181
159
}
182
160
183
161
/**
184
162
* Should the node Thread Pool be returned.
185
163
*/
186
164
public boolean threadPool () {
187
- return this . threadPool ;
165
+ return Metric . THREAD_POOL . containedIn ( requestedMetrics ) ;
188
166
}
189
167
190
168
/**
191
169
* Should the node Thread Pool be returned.
192
170
*/
193
171
public NodesStatsRequest threadPool (boolean threadPool ) {
194
- this . threadPool = threadPool ;
172
+ addOrRemoveMetric ( threadPool , Metric . THREAD_POOL . metricName ()) ;
195
173
return this ;
196
174
}
197
175
198
176
/**
199
177
* Should the node file system stats be returned.
200
178
*/
201
179
public boolean fs () {
202
- return this . fs ;
180
+ return Metric . FS . containedIn ( requestedMetrics ) ;
203
181
}
204
182
205
183
/**
206
184
* Should the node file system stats be returned.
207
185
*/
208
186
public NodesStatsRequest fs (boolean fs ) {
209
- this . fs = fs ;
187
+ addOrRemoveMetric ( fs , Metric . FS . metricName ()) ;
210
188
return this ;
211
189
}
212
190
213
191
/**
214
192
* Should the node Transport be returned.
215
193
*/
216
194
public boolean transport () {
217
- return this . transport ;
195
+ return Metric . TRANSPORT . containedIn ( requestedMetrics ) ;
218
196
}
219
197
220
198
/**
221
199
* Should the node Transport be returned.
222
200
*/
223
201
public NodesStatsRequest transport (boolean transport ) {
224
- this . transport = transport ;
202
+ addOrRemoveMetric ( transport , Metric . TRANSPORT . metricName ()) ;
225
203
return this ;
226
204
}
227
205
228
206
/**
229
207
* Should the node HTTP be returned.
230
208
*/
231
209
public boolean http () {
232
- return this . http ;
210
+ return Metric . HTTP . containedIn ( requestedMetrics ) ;
233
211
}
234
212
235
213
/**
236
214
* Should the node HTTP be returned.
237
215
*/
238
216
public NodesStatsRequest http (boolean http ) {
239
- this . http = http ;
217
+ addOrRemoveMetric ( http , Metric . HTTP . metricName ()) ;
240
218
return this ;
241
219
}
242
220
243
221
public boolean breaker () {
244
- return this . breaker ;
222
+ return Metric . BREAKER . containedIn ( requestedMetrics ) ;
245
223
}
246
224
247
225
/**
248
226
* Should the node's circuit breaker stats be returned.
249
227
*/
250
228
public NodesStatsRequest breaker (boolean breaker ) {
251
- this . breaker = breaker ;
229
+ addOrRemoveMetric ( breaker , Metric . BREAKER . metricName ()) ;
252
230
return this ;
253
231
}
254
232
255
233
public boolean script () {
256
- return script ;
234
+ return Metric . SCRIPT . containedIn ( requestedMetrics ) ;
257
235
}
258
236
259
237
public NodesStatsRequest script (boolean script ) {
260
- this . script = script ;
238
+ addOrRemoveMetric ( script , Metric . SCRIPT . metricName ()) ;
261
239
return this ;
262
240
}
263
241
264
242
265
243
public boolean discovery () {
266
- return this . discovery ;
244
+ return Metric . DISCOVERY . containedIn ( requestedMetrics ) ;
267
245
}
268
246
269
247
/**
270
248
* Should the node's discovery stats be returned.
271
249
*/
272
250
public NodesStatsRequest discovery (boolean discovery ) {
273
- this . discovery = discovery ;
251
+ addOrRemoveMetric ( discovery , Metric . DISCOVERY . metricName ()) ;
274
252
return this ;
275
253
}
276
254
277
255
public boolean ingest () {
278
- return ingest ;
256
+ return Metric . INGEST . containedIn ( requestedMetrics ) ;
279
257
}
280
258
281
259
/**
282
260
* Should ingest statistics be returned.
283
261
*/
284
262
public NodesStatsRequest ingest (boolean ingest ) {
285
- this . ingest = ingest ;
263
+ addOrRemoveMetric ( ingest , Metric . INGEST . metricName ()) ;
286
264
return this ;
287
265
}
288
266
289
267
public boolean adaptiveSelection () {
290
- return adaptiveSelection ;
268
+ return Metric . ADAPTIVE_SELECTION . containedIn ( requestedMetrics ) ;
291
269
}
292
270
293
271
/**
294
272
* Should adaptiveSelection statistics be returned.
295
273
*/
296
274
public NodesStatsRequest adaptiveSelection (boolean adaptiveSelection ) {
297
- this . adaptiveSelection = adaptiveSelection ;
275
+ addOrRemoveMetric ( adaptiveSelection , Metric . ADAPTIVE_SELECTION . metricName ()) ;
298
276
return this ;
299
277
}
300
278
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
+
301
292
@ Override
302
293
public void writeTo (StreamOutput out ) throws IOException {
303
294
super .writeTo (out );
304
295
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
+ }
317
349
}
318
350
}
0 commit comments