-
Notifications
You must be signed in to change notification settings - Fork 25.2k
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
williamrandolph
merged 21 commits into
elastic:master
from
williamrandolph:nodes-info-request-refactor-2
Mar 25, 2020
Merged
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 6fe9d71
Protect against unknown metric names
williamrandolph a3035ff
Remove old getters and some magic strings
williamrandolph 4e5083d
Cleanup
williamrandolph aba731f
Add test coverage
williamrandolph 112679a
Singularize enum name
williamrandolph 9d001d1
Remove TODO comment
williamrandolph f3baada
Add an addMetrics(String...) method
williamrandolph 4c6c40d
Add TODO for further work
williamrandolph bffdfe6
Merge branch 'master' into nodes-info-request-refactor-2
williamrandolph 24f252b
Remove redundant convenience method
williamrandolph c8780fd
Make setter variatic instead of set-based
williamrandolph d3665f9
Fixup comments and imports
williamrandolph 2f7d1c1
Merge branch 'master' into nodes-info-request-refactor-2
williamrandolph 43e8bbe
Merge branch 'master' into nodes-info-request-refactor-2
williamrandolph e3b1390
Merge branch 'master' into nodes-info-request-refactor-2
williamrandolph 984d985
Merge branch 'master' into nodes-info-request-refactor-2
williamrandolph 824e571
Streamline set-checking logic
williamrandolph 014385c
Remove unused import
williamrandolph 20cccb9
Merge branch 'master' into nodes-info-request-refactor-2
williamrandolph 73ff776
Merge branch 'master' into nodes-info-request-refactor-2
williamrandolph File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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. | ||
|
@@ -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())); | ||
} | ||
|
@@ -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) { | ||
williamrandolph marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) { | ||
williamrandolph marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
|
||
|
@@ -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)); | ||
} | ||
|
@@ -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"), | ||
williamrandolph marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TRANSPORT("transport"), | ||
HTTP("http"), | ||
PLUGINS("plugins"), | ||
|
@@ -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()); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.