Skip to content

Create set-based interface for NodesInfoRequest #53410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
568e79d
Add set-based API to NodesInfoRequest
williamrandolph Mar 6, 2020
6fe9d71
Protect against unknown metric names
williamrandolph Mar 6, 2020
a3035ff
Remove old getters and some magic strings
williamrandolph Mar 6, 2020
4e5083d
Cleanup
williamrandolph Mar 9, 2020
aba731f
Add test coverage
williamrandolph Mar 9, 2020
112679a
Singularize enum name
williamrandolph Mar 10, 2020
9d001d1
Remove TODO comment
williamrandolph Mar 10, 2020
f3baada
Add an addMetrics(String...) method
williamrandolph Mar 10, 2020
4c6c40d
Add TODO for further work
williamrandolph Mar 11, 2020
bffdfe6
Merge branch 'master' into nodes-info-request-refactor-2
williamrandolph Mar 13, 2020
24f252b
Remove redundant convenience method
williamrandolph Mar 13, 2020
c8780fd
Make setter variatic instead of set-based
williamrandolph Mar 16, 2020
d3665f9
Fixup comments and imports
williamrandolph Mar 16, 2020
2f7d1c1
Merge branch 'master' into nodes-info-request-refactor-2
williamrandolph Mar 16, 2020
43e8bbe
Merge branch 'master' into nodes-info-request-refactor-2
williamrandolph Mar 16, 2020
e3b1390
Merge branch 'master' into nodes-info-request-refactor-2
williamrandolph Mar 23, 2020
984d985
Merge branch 'master' into nodes-info-request-refactor-2
williamrandolph Mar 25, 2020
824e571
Streamline set-checking logic
williamrandolph Mar 25, 2020
014385c
Remove unused import
williamrandolph Mar 25, 2020
20cccb9
Merge branch 'master' into nodes-info-request-refactor-2
williamrandolph Mar 25, 2020
73ff776
Merge branch 'master' into nodes-info-request-refactor-2
williamrandolph Mar 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -34,7 +35,7 @@
*/
public class NodesInfoRequest extends BaseNodesRequest<NodesInfoRequest> {

private Set<String> requestedMetrics = Metrics.allMetrics();
private Set<String> requestedMetrics = Metric.allMetrics();

/**
* Create a new NodeInfoRequest from a {@link StreamInput} object.
Expand All @@ -48,16 +49,16 @@ public NodesInfoRequest(StreamInput in) throws IOException {
if (in.getVersion().before(Version.V_7_7_0)){
// prior to version 8.x, a NodesInfoRequest was serialized as a list
// of booleans in a fixed order
addOrRemoveMetric(in.readBoolean(), Metrics.SETTINGS.metricName());
addOrRemoveMetric(in.readBoolean(), Metrics.OS.metricName());
addOrRemoveMetric(in.readBoolean(), Metrics.PROCESS.metricName());
addOrRemoveMetric(in.readBoolean(), Metrics.JVM.metricName());
addOrRemoveMetric(in.readBoolean(), Metrics.THREAD_POOL.metricName());
addOrRemoveMetric(in.readBoolean(), Metrics.TRANSPORT.metricName());
addOrRemoveMetric(in.readBoolean(), Metrics.HTTP.metricName());
addOrRemoveMetric(in.readBoolean(), Metrics.PLUGINS.metricName());
addOrRemoveMetric(in.readBoolean(), Metrics.INGEST.metricName());
addOrRemoveMetric(in.readBoolean(), Metrics.INDICES.metricName());
optionallyAddMetric(in.readBoolean(), Metric.SETTINGS.metricName());
optionallyAddMetric(in.readBoolean(), Metric.OS.metricName());
optionallyAddMetric(in.readBoolean(), Metric.PROCESS.metricName());
optionallyAddMetric(in.readBoolean(), Metric.JVM.metricName());
optionallyAddMetric(in.readBoolean(), Metric.THREAD_POOL.metricName());
optionallyAddMetric(in.readBoolean(), Metric.TRANSPORT.metricName());
optionallyAddMetric(in.readBoolean(), Metric.HTTP.metricName());
optionallyAddMetric(in.readBoolean(), Metric.PLUGINS.metricName());
optionallyAddMetric(in.readBoolean(), Metric.INGEST.metricName());
optionallyAddMetric(in.readBoolean(), Metric.INDICES.metricName());
} else {
requestedMetrics.addAll(Arrays.asList(in.readStringArray()));
}
Expand All @@ -84,174 +85,68 @@ public NodesInfoRequest clear() {
* Sets to return all the data.
*/
public NodesInfoRequest all() {
requestedMetrics.addAll(Metrics.allMetrics());
requestedMetrics.addAll(Metric.allMetrics());
return this;
}

/**
* Should the node settings be returned.
* Get the names of requested metrics
*/
public boolean settings() {
return Metrics.SETTINGS.containedIn(requestedMetrics);
public Set<String> requestedMetrics() {
return Set.copyOf(requestedMetrics);
}

/**
* Should the node settings be returned.
* Add metric
*/
public NodesInfoRequest settings(boolean settings) {
addOrRemoveMetric(settings, Metrics.SETTINGS.metricName());
return this;
}

/**
* Should the node OS be returned.
*/
public boolean os() {
return Metrics.OS.containedIn(requestedMetrics);
}

/**
* Should the node OS be returned.
*/
public NodesInfoRequest os(boolean os) {
addOrRemoveMetric(os, Metrics.OS.metricName());
return this;
}

/**
* Should the node Process be returned.
*/
public boolean process() {
return Metrics.PROCESS.containedIn(requestedMetrics);
}

/**
* Should the node Process be returned.
*/
public NodesInfoRequest process(boolean process) {
addOrRemoveMetric(process, Metrics.PROCESS.metricName());
return this;
}

/**
* Should the node JVM be returned.
*/
public boolean jvm() {
return Metrics.JVM.containedIn(requestedMetrics);
}

/**
* Should the node JVM be returned.
*/
public NodesInfoRequest jvm(boolean jvm) {
addOrRemoveMetric(jvm, Metrics.JVM.metricName());
return this;
}

/**
* Should the node Thread Pool info be returned.
*/
public boolean threadPool() {
return Metrics.THREAD_POOL.containedIn(requestedMetrics);
}

/**
* Should the node Thread Pool info be returned.
*/
public NodesInfoRequest threadPool(boolean threadPool) {
addOrRemoveMetric(threadPool, Metrics.THREAD_POOL.metricName());
return this;
}

/**
* Should the node Transport be returned.
*/
public boolean transport() {
return Metrics.TRANSPORT.containedIn(requestedMetrics);
}

/**
* Should the node Transport be returned.
*/
public NodesInfoRequest transport(boolean transport) {
addOrRemoveMetric(transport, Metrics.TRANSPORT.metricName());
return this;
}

/**
* Should the node HTTP be returned.
*/
public boolean http() {
return Metrics.HTTP.containedIn(requestedMetrics);
}

/**
* Should the node HTTP be returned.
*/
public NodesInfoRequest http(boolean http) {
addOrRemoveMetric(http, Metrics.HTTP.metricName());
public NodesInfoRequest addMetric(String metric) {
if (Metric.allMetrics().contains(metric) == false) {
throw new IllegalStateException("Used an illegal metric: " + metric);
}
requestedMetrics.add(metric);
return this;
}

/**
* Should information about plugins be returned
* @param plugins true if you want info
* @return The request
* Add collection of metrics
*/
public NodesInfoRequest plugins(boolean plugins) {
addOrRemoveMetric(plugins, Metrics.PLUGINS.metricName());
public NodesInfoRequest addMetrics(Collection<String> metrics) {
if (Metric.allMetrics().containsAll(metrics) == false) {
throw new IllegalStateException("Used an illegal metric: " + metrics);
}
requestedMetrics.addAll(metrics);
return this;
}

/**
* @return true if information about plugins is requested
* Add String array of metrics
*/
public boolean plugins() {
return Metrics.PLUGINS.containedIn(requestedMetrics);
}

/**
* Should information about ingest be returned
* @param ingest true if you want info
*/
public NodesInfoRequest ingest(boolean ingest) {
addOrRemoveMetric(ingest, Metrics.INGEST.metricName());
public NodesInfoRequest addMetrics(String... metrics) {
addMetrics(Arrays.stream(metrics).collect(Collectors.toSet()));
return this;
}

/**
* @return true if information about ingest is requested
* Remove metric
*/
public boolean ingest() {
return Metrics.INGEST.containedIn(requestedMetrics);
}

/**
* Should information about indices (currently just indexing buffers) be returned
* @param indices true if you want info
*/
public NodesInfoRequest indices(boolean indices) {
addOrRemoveMetric(indices, Metrics.INDICES.metricName());
public NodesInfoRequest removeMetric(String metric) {
if (Metric.allMetrics().contains(metric) == false) {
throw new IllegalStateException("Used an illegal metric: " + metric);
}
requestedMetrics.remove(metric);
return this;
}

/**
* @return true if information about indices (currently just indexing buffers)
*/
public boolean indices() {
return Metrics.INDICES.containedIn(requestedMetrics);
}

/**
* Helper method for adding and removing metrics.
* @param includeMetric Whether or not to include a metric.
* Helper method for adding and removing metrics. Used when deserializing
* a NodesInfoRequest from an ordered list of booleans.
*
* @param addMetric Whether or not to include a metric.
* @param metricName Name of the metric to include or remove.
*/
private void addOrRemoveMetric(boolean includeMetric, String metricName) {
if (includeMetric) {
private void optionallyAddMetric(boolean addMetric, String metricName) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is now only used for backwards-compatible deserialization, and doesn't need to be able to remove metrics anymore.

if (addMetric) {
requestedMetrics.add(metricName);
} else {
requestedMetrics.remove(metricName);
}
}

Expand All @@ -261,16 +156,16 @@ public void writeTo(StreamOutput out) throws IOException {
if (out.getVersion().before(Version.V_7_7_0)){
// prior to version 8.x, a NodesInfoRequest was serialized as a list
// of booleans in a fixed order
out.writeBoolean(Metrics.SETTINGS.containedIn(requestedMetrics));
out.writeBoolean(Metrics.OS.containedIn(requestedMetrics));
out.writeBoolean(Metrics.PROCESS.containedIn(requestedMetrics));
out.writeBoolean(Metrics.JVM.containedIn(requestedMetrics));
out.writeBoolean(Metrics.THREAD_POOL.containedIn(requestedMetrics));
out.writeBoolean(Metrics.TRANSPORT.containedIn(requestedMetrics));
out.writeBoolean(Metrics.HTTP.containedIn(requestedMetrics));
out.writeBoolean(Metrics.PLUGINS.containedIn(requestedMetrics));
out.writeBoolean(Metrics.INGEST.containedIn(requestedMetrics));
out.writeBoolean(Metrics.INDICES.containedIn(requestedMetrics));
out.writeBoolean(Metric.SETTINGS.containedIn(requestedMetrics));
out.writeBoolean(Metric.OS.containedIn(requestedMetrics));
out.writeBoolean(Metric.PROCESS.containedIn(requestedMetrics));
out.writeBoolean(Metric.JVM.containedIn(requestedMetrics));
out.writeBoolean(Metric.THREAD_POOL.containedIn(requestedMetrics));
out.writeBoolean(Metric.TRANSPORT.containedIn(requestedMetrics));
out.writeBoolean(Metric.HTTP.containedIn(requestedMetrics));
out.writeBoolean(Metric.PLUGINS.containedIn(requestedMetrics));
out.writeBoolean(Metric.INGEST.containedIn(requestedMetrics));
out.writeBoolean(Metric.INDICES.containedIn(requestedMetrics));
} else {
out.writeStringArray(requestedMetrics.toArray(String[]::new));
}
Expand All @@ -281,12 +176,12 @@ public void writeTo(StreamOutput out) throws IOException {
* from the nodes information endpoint. Eventually this list list will be
* pluggable.
*/
enum Metrics {
public enum Metric {
SETTINGS("settings"),
OS("os"),
PROCESS("process"),
JVM("jvm"),
THREAD_POOL("threadPool"),
THREAD_POOL("thread_pool"),
TRANSPORT("transport"),
HTTP("http"),
PLUGINS("plugins"),
Expand All @@ -295,20 +190,20 @@ enum Metrics {

private String metricName;

Metrics(String name) {
Metric(String name) {
this.metricName = name;
}

String metricName() {
public String metricName() {
return this.metricName;
}

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

static Set<String> allMetrics() {
return Arrays.stream(values()).map(Metrics::metricName).collect(Collectors.toSet());
public static Set<String> allMetrics() {
return Arrays.stream(values()).map(Metric::metricName).collect(Collectors.toSet());
}
}
}
Loading