Skip to content

Commit b11960e

Browse files
Create set-based interface for NodesInfoRequest (#53410) (#54223)
This commit begins the work of removing the "hard-coded" metric getters and setters from the NodesInfoRequest classes. We start by providing new flexible getters and setters. We then update the test classes to remove the old getters, and then remove those getters.
1 parent 53e2fec commit b11960e

File tree

11 files changed

+225
-305
lines changed

11 files changed

+225
-305
lines changed

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

Lines changed: 60 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@
2626

2727
import java.io.IOException;
2828
import java.util.Arrays;
29+
import java.util.HashSet;
2930
import java.util.Set;
31+
import java.util.SortedSet;
32+
import java.util.TreeSet;
3033
import java.util.stream.Collectors;
3134

3235
/**
3336
* A request to get node (cluster) level information.
3437
*/
3538
public class NodesInfoRequest extends BaseNodesRequest<NodesInfoRequest> {
3639

37-
private Set<String> requestedMetrics = Metrics.allMetrics();
40+
private Set<String> requestedMetrics = Metric.allMetrics();
3841

3942
/**
4043
* Create a new NodeInfoRequest from a {@link StreamInput} object.
@@ -48,16 +51,16 @@ public NodesInfoRequest(StreamInput in) throws IOException {
4851
if (in.getVersion().before(Version.V_7_7_0)){
4952
// prior to version 8.x, a NodesInfoRequest was serialized as a list
5053
// 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());
54+
optionallyAddMetric(in.readBoolean(), Metric.SETTINGS.metricName());
55+
optionallyAddMetric(in.readBoolean(), Metric.OS.metricName());
56+
optionallyAddMetric(in.readBoolean(), Metric.PROCESS.metricName());
57+
optionallyAddMetric(in.readBoolean(), Metric.JVM.metricName());
58+
optionallyAddMetric(in.readBoolean(), Metric.THREAD_POOL.metricName());
59+
optionallyAddMetric(in.readBoolean(), Metric.TRANSPORT.metricName());
60+
optionallyAddMetric(in.readBoolean(), Metric.HTTP.metricName());
61+
optionallyAddMetric(in.readBoolean(), Metric.PLUGINS.metricName());
62+
optionallyAddMetric(in.readBoolean(), Metric.INGEST.metricName());
63+
optionallyAddMetric(in.readBoolean(), Metric.INDICES.metricName());
6164
} else {
6265
requestedMetrics.addAll(Arrays.asList(in.readStringArray()));
6366
}
@@ -84,174 +87,63 @@ public NodesInfoRequest clear() {
8487
* Sets to return all the data.
8588
*/
8689
public NodesInfoRequest all() {
87-
requestedMetrics.addAll(Metrics.allMetrics());
90+
requestedMetrics.addAll(Metric.allMetrics());
8891
return this;
8992
}
9093

9194
/**
92-
* Should the node settings be returned.
95+
* Get the names of requested metrics
9396
*/
94-
public boolean settings() {
95-
return Metrics.SETTINGS.containedIn(requestedMetrics);
97+
public Set<String> requestedMetrics() {
98+
return new HashSet<>(requestedMetrics);
9699
}
97100

98101
/**
99-
* Should the node settings be returned.
102+
* Add metric
100103
*/
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());
104+
public NodesInfoRequest addMetric(String metric) {
105+
if (Metric.allMetrics().contains(metric) == false) {
106+
throw new IllegalStateException("Used an illegal metric: " + metric);
107+
}
108+
requestedMetrics.add(metric);
203109
return this;
204110
}
205111

206112
/**
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
113+
* Add multiple metrics
216114
*/
217-
public NodesInfoRequest ingest(boolean ingest) {
218-
addOrRemoveMetric(ingest, Metrics.INGEST.metricName());
115+
public NodesInfoRequest addMetrics(String... metrics) {
116+
SortedSet<String> metricsSet = new TreeSet<>(Arrays.asList(metrics));
117+
if (Metric.allMetrics().containsAll(metricsSet) == false) {
118+
metricsSet.removeAll(Metric.allMetrics());
119+
String plural = metricsSet.size() == 1 ? "" : "s";
120+
throw new IllegalStateException("Used illegal metric" + plural + ": " + metricsSet);
121+
}
122+
requestedMetrics.addAll(metricsSet);
219123
return this;
220124
}
221125

222126
/**
223-
* @return true if information about ingest is requested
127+
* Remove metric
224128
*/
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());
129+
public NodesInfoRequest removeMetric(String metric) {
130+
if (Metric.allMetrics().contains(metric) == false) {
131+
throw new IllegalStateException("Used an illegal metric: " + metric);
132+
}
133+
requestedMetrics.remove(metric);
235134
return this;
236135
}
237136

238137
/**
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.
138+
* Helper method for adding and removing metrics. Used when deserializing
139+
* a NodesInfoRequest from an ordered list of booleans.
140+
*
141+
* @param addMetric Whether or not to include a metric.
248142
* @param metricName Name of the metric to include or remove.
249143
*/
250-
private void addOrRemoveMetric(boolean includeMetric, String metricName) {
251-
if (includeMetric) {
144+
private void optionallyAddMetric(boolean addMetric, String metricName) {
145+
if (addMetric) {
252146
requestedMetrics.add(metricName);
253-
} else {
254-
requestedMetrics.remove(metricName);
255147
}
256148
}
257149

@@ -261,16 +153,16 @@ public void writeTo(StreamOutput out) throws IOException {
261153
if (out.getVersion().before(Version.V_7_7_0)){
262154
// prior to version 8.x, a NodesInfoRequest was serialized as a list
263155
// 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));
156+
out.writeBoolean(Metric.SETTINGS.containedIn(requestedMetrics));
157+
out.writeBoolean(Metric.OS.containedIn(requestedMetrics));
158+
out.writeBoolean(Metric.PROCESS.containedIn(requestedMetrics));
159+
out.writeBoolean(Metric.JVM.containedIn(requestedMetrics));
160+
out.writeBoolean(Metric.THREAD_POOL.containedIn(requestedMetrics));
161+
out.writeBoolean(Metric.TRANSPORT.containedIn(requestedMetrics));
162+
out.writeBoolean(Metric.HTTP.containedIn(requestedMetrics));
163+
out.writeBoolean(Metric.PLUGINS.containedIn(requestedMetrics));
164+
out.writeBoolean(Metric.INGEST.containedIn(requestedMetrics));
165+
out.writeBoolean(Metric.INDICES.containedIn(requestedMetrics));
274166
} else {
275167
out.writeStringArray(requestedMetrics.toArray(new String[0]));
276168
}
@@ -281,7 +173,7 @@ public void writeTo(StreamOutput out) throws IOException {
281173
* from the nodes information endpoint. Eventually this list list will be
282174
* pluggable.
283175
*/
284-
enum Metrics {
176+
public enum Metric {
285177
SETTINGS("settings"),
286178
OS("os"),
287179
PROCESS("process"),
@@ -295,20 +187,20 @@ enum Metrics {
295187

296188
private String metricName;
297189

298-
Metrics(String name) {
190+
Metric(String name) {
299191
this.metricName = name;
300192
}
301193

302-
String metricName() {
194+
public String metricName() {
303195
return this.metricName;
304196
}
305197

306198
boolean containedIn(Set<String> metricNames) {
307199
return metricNames.contains(this.metricName());
308200
}
309201

310-
static Set<String> allMetrics() {
311-
return Arrays.stream(values()).map(Metrics::metricName).collect(Collectors.toSet());
202+
public static Set<String> allMetrics() {
203+
return Arrays.stream(values()).map(Metric::metricName).collect(Collectors.toSet());
312204
}
313205
}
314206
}

0 commit comments

Comments
 (0)