Skip to content

Commit 6d6227a

Browse files
Create set-based interface for NodesInfoRequest (#53410)
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 0a98904 commit 6d6227a

File tree

11 files changed

+224
-305
lines changed

11 files changed

+224
-305
lines changed

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

Lines changed: 59 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@
2727
import java.io.IOException;
2828
import java.util.Arrays;
2929
import java.util.Set;
30+
import java.util.SortedSet;
31+
import java.util.TreeSet;
3032
import java.util.stream.Collectors;
3133

3234
/**
3335
* A request to get node (cluster) level information.
3436
*/
3537
public class NodesInfoRequest extends BaseNodesRequest<NodesInfoRequest> {
3638

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

3941
/**
4042
* Create a new NodeInfoRequest from a {@link StreamInput} object.
@@ -48,16 +50,16 @@ public NodesInfoRequest(StreamInput in) throws IOException {
4850
if (in.getVersion().before(Version.V_7_7_0)){
4951
// prior to version 8.x, a NodesInfoRequest was serialized as a list
5052
// 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());
6163
} else {
6264
requestedMetrics.addAll(Arrays.asList(in.readStringArray()));
6365
}
@@ -84,174 +86,63 @@ public NodesInfoRequest clear() {
8486
* Sets to return all the data.
8587
*/
8688
public NodesInfoRequest all() {
87-
requestedMetrics.addAll(Metrics.allMetrics());
89+
requestedMetrics.addAll(Metric.allMetrics());
8890
return this;
8991
}
9092

9193
/**
92-
* Should the node settings be returned.
94+
* Get the names of requested metrics
9395
*/
94-
public boolean settings() {
95-
return Metrics.SETTINGS.containedIn(requestedMetrics);
96+
public Set<String> requestedMetrics() {
97+
return Set.copyOf(requestedMetrics);
9698
}
9799

98100
/**
99-
* Should the node settings be returned.
101+
* Add metric
100102
*/
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);
203108
return this;
204109
}
205110

206111
/**
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
216113
*/
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);
219122
return this;
220123
}
221124

222125
/**
223-
* @return true if information about ingest is requested
126+
* Remove metric
224127
*/
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);
235133
return this;
236134
}
237135

238136
/**
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.
248141
* @param metricName Name of the metric to include or remove.
249142
*/
250-
private void addOrRemoveMetric(boolean includeMetric, String metricName) {
251-
if (includeMetric) {
143+
private void optionallyAddMetric(boolean addMetric, String metricName) {
144+
if (addMetric) {
252145
requestedMetrics.add(metricName);
253-
} else {
254-
requestedMetrics.remove(metricName);
255146
}
256147
}
257148

@@ -261,16 +152,16 @@ public void writeTo(StreamOutput out) throws IOException {
261152
if (out.getVersion().before(Version.V_7_7_0)){
262153
// prior to version 8.x, a NodesInfoRequest was serialized as a list
263154
// 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));
274165
} else {
275166
out.writeStringArray(requestedMetrics.toArray(String[]::new));
276167
}
@@ -281,7 +172,7 @@ public void writeTo(StreamOutput out) throws IOException {
281172
* from the nodes information endpoint. Eventually this list list will be
282173
* pluggable.
283174
*/
284-
enum Metrics {
175+
public enum Metric {
285176
SETTINGS("settings"),
286177
OS("os"),
287178
PROCESS("process"),
@@ -295,20 +186,20 @@ enum Metrics {
295186

296187
private String metricName;
297188

298-
Metrics(String name) {
189+
Metric(String name) {
299190
this.metricName = name;
300191
}
301192

302-
String metricName() {
193+
public String metricName() {
303194
return this.metricName;
304195
}
305196

306197
boolean containedIn(Set<String> metricNames) {
307198
return metricNames.contains(this.metricName());
308199
}
309200

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());
312203
}
313204
}
314205
}

0 commit comments

Comments
 (0)