27
27
import java .io .IOException ;
28
28
import java .util .Arrays ;
29
29
import java .util .Set ;
30
+ import java .util .SortedSet ;
31
+ import java .util .TreeSet ;
30
32
import java .util .stream .Collectors ;
31
33
32
34
/**
33
35
* A request to get node (cluster) level information.
34
36
*/
35
37
public class NodesInfoRequest extends BaseNodesRequest <NodesInfoRequest > {
36
38
37
- private Set <String > requestedMetrics = Metrics .allMetrics ();
39
+ private Set <String > requestedMetrics = Metric .allMetrics ();
38
40
39
41
/**
40
42
* Create a new NodeInfoRequest from a {@link StreamInput} object.
@@ -48,16 +50,16 @@ public NodesInfoRequest(StreamInput in) throws IOException {
48
50
if (in .getVersion ().before (Version .V_7_7_0 )){
49
51
// prior to version 8.x, a NodesInfoRequest was serialized as a list
50
52
// 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 ());
53
+ optionallyAddMetric (in .readBoolean (), Metric .SETTINGS .metricName ());
54
+ optionallyAddMetric (in .readBoolean (), Metric .OS .metricName ());
55
+ optionallyAddMetric (in .readBoolean (), Metric .PROCESS .metricName ());
56
+ optionallyAddMetric (in .readBoolean (), Metric .JVM .metricName ());
57
+ optionallyAddMetric (in .readBoolean (), Metric .THREAD_POOL .metricName ());
58
+ optionallyAddMetric (in .readBoolean (), Metric .TRANSPORT .metricName ());
59
+ optionallyAddMetric (in .readBoolean (), Metric .HTTP .metricName ());
60
+ optionallyAddMetric (in .readBoolean (), Metric .PLUGINS .metricName ());
61
+ optionallyAddMetric (in .readBoolean (), Metric .INGEST .metricName ());
62
+ optionallyAddMetric (in .readBoolean (), Metric .INDICES .metricName ());
61
63
} else {
62
64
requestedMetrics .addAll (Arrays .asList (in .readStringArray ()));
63
65
}
@@ -84,174 +86,63 @@ public NodesInfoRequest clear() {
84
86
* Sets to return all the data.
85
87
*/
86
88
public NodesInfoRequest all () {
87
- requestedMetrics .addAll (Metrics .allMetrics ());
89
+ requestedMetrics .addAll (Metric .allMetrics ());
88
90
return this ;
89
91
}
90
92
91
93
/**
92
- * Should the node settings be returned.
94
+ * Get the names of requested metrics
93
95
*/
94
- public boolean settings () {
95
- return Metrics . SETTINGS . containedIn (requestedMetrics );
96
+ public Set < String > requestedMetrics () {
97
+ return Set . copyOf (requestedMetrics );
96
98
}
97
99
98
100
/**
99
- * Should the node settings be returned.
101
+ * Add metric
100
102
*/
101
- public NodesInfoRequest settings (boolean settings ) {
102
- addOrRemoveMetric (settings , Metrics .SETTINGS .metricName ());
103
- return this ;
104
- }
105
-
106
- /**
107
- * Should the node OS be returned.
108
- */
109
- public boolean os () {
110
- return Metrics .OS .containedIn (requestedMetrics );
111
- }
112
-
113
- /**
114
- * Should the node OS be returned.
115
- */
116
- public NodesInfoRequest os (boolean os ) {
117
- addOrRemoveMetric (os , Metrics .OS .metricName ());
118
- return this ;
119
- }
120
-
121
- /**
122
- * Should the node Process be returned.
123
- */
124
- public boolean process () {
125
- return Metrics .PROCESS .containedIn (requestedMetrics );
126
- }
127
-
128
- /**
129
- * Should the node Process be returned.
130
- */
131
- public NodesInfoRequest process (boolean process ) {
132
- addOrRemoveMetric (process , Metrics .PROCESS .metricName ());
133
- return this ;
134
- }
135
-
136
- /**
137
- * Should the node JVM be returned.
138
- */
139
- public boolean jvm () {
140
- return Metrics .JVM .containedIn (requestedMetrics );
141
- }
142
-
143
- /**
144
- * Should the node JVM be returned.
145
- */
146
- public NodesInfoRequest jvm (boolean jvm ) {
147
- addOrRemoveMetric (jvm , Metrics .JVM .metricName ());
148
- return this ;
149
- }
150
-
151
- /**
152
- * Should the node Thread Pool info be returned.
153
- */
154
- public boolean threadPool () {
155
- return Metrics .THREAD_POOL .containedIn (requestedMetrics );
156
- }
157
-
158
- /**
159
- * Should the node Thread Pool info be returned.
160
- */
161
- public NodesInfoRequest threadPool (boolean threadPool ) {
162
- addOrRemoveMetric (threadPool , Metrics .THREAD_POOL .metricName ());
163
- return this ;
164
- }
165
-
166
- /**
167
- * Should the node Transport be returned.
168
- */
169
- public boolean transport () {
170
- return Metrics .TRANSPORT .containedIn (requestedMetrics );
171
- }
172
-
173
- /**
174
- * Should the node Transport be returned.
175
- */
176
- public NodesInfoRequest transport (boolean transport ) {
177
- addOrRemoveMetric (transport , Metrics .TRANSPORT .metricName ());
178
- return this ;
179
- }
180
-
181
- /**
182
- * Should the node HTTP be returned.
183
- */
184
- public boolean http () {
185
- return Metrics .HTTP .containedIn (requestedMetrics );
186
- }
187
-
188
- /**
189
- * Should the node HTTP be returned.
190
- */
191
- public NodesInfoRequest http (boolean http ) {
192
- addOrRemoveMetric (http , Metrics .HTTP .metricName ());
193
- return this ;
194
- }
195
-
196
- /**
197
- * Should information about plugins be returned
198
- * @param plugins true if you want info
199
- * @return The request
200
- */
201
- public NodesInfoRequest plugins (boolean plugins ) {
202
- addOrRemoveMetric (plugins , Metrics .PLUGINS .metricName ());
103
+ public NodesInfoRequest addMetric (String metric ) {
104
+ if (Metric .allMetrics ().contains (metric ) == false ) {
105
+ throw new IllegalStateException ("Used an illegal metric: " + metric );
106
+ }
107
+ requestedMetrics .add (metric );
203
108
return this ;
204
109
}
205
110
206
111
/**
207
- * @return true if information about plugins is requested
208
- */
209
- public boolean plugins () {
210
- return Metrics .PLUGINS .containedIn (requestedMetrics );
211
- }
212
-
213
- /**
214
- * Should information about ingest be returned
215
- * @param ingest true if you want info
112
+ * Add multiple metrics
216
113
*/
217
- public NodesInfoRequest ingest (boolean ingest ) {
218
- addOrRemoveMetric (ingest , Metrics .INGEST .metricName ());
114
+ public NodesInfoRequest addMetrics (String ... metrics ) {
115
+ SortedSet <String > metricsSet = new TreeSet <>(Set .of (metrics ));
116
+ if (Metric .allMetrics ().containsAll (metricsSet ) == false ) {
117
+ metricsSet .removeAll (Metric .allMetrics ());
118
+ String plural = metricsSet .size () == 1 ? "" : "s" ;
119
+ throw new IllegalStateException ("Used illegal metric" + plural + ": " + metricsSet );
120
+ }
121
+ requestedMetrics .addAll (metricsSet );
219
122
return this ;
220
123
}
221
124
222
125
/**
223
- * @return true if information about ingest is requested
126
+ * Remove metric
224
127
*/
225
- public boolean ingest () {
226
- return Metrics .INGEST .containedIn (requestedMetrics );
227
- }
228
-
229
- /**
230
- * Should information about indices (currently just indexing buffers) be returned
231
- * @param indices true if you want info
232
- */
233
- public NodesInfoRequest indices (boolean indices ) {
234
- addOrRemoveMetric (indices , Metrics .INDICES .metricName ());
128
+ public NodesInfoRequest removeMetric (String metric ) {
129
+ if (Metric .allMetrics ().contains (metric ) == false ) {
130
+ throw new IllegalStateException ("Used an illegal metric: " + metric );
131
+ }
132
+ requestedMetrics .remove (metric );
235
133
return this ;
236
134
}
237
135
238
136
/**
239
- * @return true if information about indices (currently just indexing buffers)
240
- */
241
- public boolean 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.
137
+ * Helper method for adding and removing metrics. Used when deserializing
138
+ * a NodesInfoRequest from an ordered list of booleans.
139
+ *
140
+ * @param addMetric Whether or not to include a metric.
248
141
* @param metricName Name of the metric to include or remove.
249
142
*/
250
- private void addOrRemoveMetric (boolean includeMetric , String metricName ) {
251
- if (includeMetric ) {
143
+ private void optionallyAddMetric (boolean addMetric , String metricName ) {
144
+ if (addMetric ) {
252
145
requestedMetrics .add (metricName );
253
- } else {
254
- requestedMetrics .remove (metricName );
255
146
}
256
147
}
257
148
@@ -261,16 +152,16 @@ public void writeTo(StreamOutput out) throws IOException {
261
152
if (out .getVersion ().before (Version .V_7_7_0 )){
262
153
// prior to version 8.x, a NodesInfoRequest was serialized as a list
263
154
// 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 ));
155
+ out .writeBoolean (Metric .SETTINGS .containedIn (requestedMetrics ));
156
+ out .writeBoolean (Metric .OS .containedIn (requestedMetrics ));
157
+ out .writeBoolean (Metric .PROCESS .containedIn (requestedMetrics ));
158
+ out .writeBoolean (Metric .JVM .containedIn (requestedMetrics ));
159
+ out .writeBoolean (Metric .THREAD_POOL .containedIn (requestedMetrics ));
160
+ out .writeBoolean (Metric .TRANSPORT .containedIn (requestedMetrics ));
161
+ out .writeBoolean (Metric .HTTP .containedIn (requestedMetrics ));
162
+ out .writeBoolean (Metric .PLUGINS .containedIn (requestedMetrics ));
163
+ out .writeBoolean (Metric .INGEST .containedIn (requestedMetrics ));
164
+ out .writeBoolean (Metric .INDICES .containedIn (requestedMetrics ));
274
165
} else {
275
166
out .writeStringArray (requestedMetrics .toArray (String []::new ));
276
167
}
@@ -281,7 +172,7 @@ public void writeTo(StreamOutput out) throws IOException {
281
172
* from the nodes information endpoint. Eventually this list list will be
282
173
* pluggable.
283
174
*/
284
- enum Metrics {
175
+ public enum Metric {
285
176
SETTINGS ("settings" ),
286
177
OS ("os" ),
287
178
PROCESS ("process" ),
@@ -295,20 +186,20 @@ enum Metrics {
295
186
296
187
private String metricName ;
297
188
298
- Metrics (String name ) {
189
+ Metric (String name ) {
299
190
this .metricName = name ;
300
191
}
301
192
302
- String metricName () {
193
+ public String metricName () {
303
194
return this .metricName ;
304
195
}
305
196
306
197
boolean containedIn (Set <String > metricNames ) {
307
198
return metricNames .contains (this .metricName ());
308
199
}
309
200
310
- static Set <String > allMetrics () {
311
- return Arrays .stream (values ()).map (Metrics ::metricName ).collect (Collectors .toSet ());
201
+ public static Set <String > allMetrics () {
202
+ return Arrays .stream (values ()).map (Metric ::metricName ).collect (Collectors .toSet ());
312
203
}
313
204
}
314
205
}
0 commit comments