Skip to content

Commit 958e9d1

Browse files
Refactor nodes stats request builders to match requests (#54363) (#54604)
* Refactor nodes stats request builders to match requests (#54363) * Remove hard-coded setters from NodesInfoRequestBuilder * Remove hard-coded setters from NodesStatsRequest * Use static imports to reduce clutter * Remove uses of old info APIs
1 parent fd729a6 commit 958e9d1

File tree

21 files changed

+111
-215
lines changed

21 files changed

+111
-215
lines changed

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/IngestRestartIT.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.function.Consumer;
4040
import java.util.function.Function;
4141

42+
import static org.elasticsearch.action.admin.cluster.node.stats.NodesStatsRequest.Metric.INGEST;
4243
import static org.hamcrest.Matchers.equalTo;
4344
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
4445

@@ -99,7 +100,9 @@ public void testFailureInConditionalProcessor() {
99100
);
100101
assertTrue(e.getMessage().contains("this script always fails"));
101102

102-
NodesStatsResponse r = client().admin().cluster().prepareNodesStats(internalCluster().getNodeNames()).setIngest(true).get();
103+
NodesStatsResponse r = client().admin().cluster().prepareNodesStats(internalCluster().getNodeNames())
104+
.addMetric(INGEST.metricName())
105+
.get();
103106
int nodeCount = r.getNodes().size();
104107
for (int k = 0; k < nodeCount; k++) {
105108
List<IngestStats.ProcessorStat> stats = r.getNodes().get(k).getIngestStats().getProcessorStats().get(pipelineId);

modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4TransportMultiPortIntegrationIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.Arrays;
4141
import java.util.Locale;
4242

43+
import static org.elasticsearch.action.admin.cluster.node.info.NodesInfoRequest.Metric.TRANSPORT;
4344
import static org.hamcrest.Matchers.allOf;
4445
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
4546
import static org.hamcrest.Matchers.hasKey;
@@ -88,7 +89,7 @@ public void testThatTransportClientCanConnect() throws Exception {
8889

8990
@Network
9091
public void testThatInfosAreExposed() throws Exception {
91-
NodesInfoResponse response = client().admin().cluster().prepareNodesInfo().clear().setTransport(true).get();
92+
NodesInfoResponse response = client().admin().cluster().prepareNodesInfo().clear().addMetric(TRANSPORT.metricName()).get();
9293
for (NodeInfo nodeInfo : response.getNodes()) {
9394
assertThat(nodeInfo.getTransport().getProfileAddresses().keySet(), hasSize(1));
9495
assertThat(nodeInfo.getTransport().getProfileAddresses(), hasKey("client1"));

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

Lines changed: 12 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.elasticsearch.action.support.nodes.NodesOperationRequestBuilder;
2323
import org.elasticsearch.client.ElasticsearchClient;
2424

25-
// TODO: This class's interface should match that of NodesInfoRequest
2625
public class NodesInfoRequestBuilder extends NodesOperationRequestBuilder<NodesInfoRequest, NodesInfoResponse, NodesInfoRequestBuilder> {
2726

2827
public NodesInfoRequestBuilder(ElasticsearchClient client, NodesInfoAction action) {
@@ -46,90 +45,24 @@ public NodesInfoRequestBuilder all() {
4645
}
4746

4847
/**
49-
* Should the node settings be returned.
48+
* Add a single metric to the request.
49+
*
50+
* @param metric Name of metric as a string.
51+
* @return This, for request chaining.
5052
*/
51-
public NodesInfoRequestBuilder setSettings(boolean settings) {
52-
addOrRemoveMetric(settings, NodesInfoRequest.Metric.SETTINGS);
53+
public NodesInfoRequestBuilder addMetric(String metric) {
54+
request.addMetric(metric);
5355
return this;
5456
}
5557

5658
/**
57-
* Should the node OS info be returned.
59+
* Add an array of metrics to the request.
60+
*
61+
* @param metrics Metric names as strings.
62+
* @return This, for request chaining.
5863
*/
59-
public NodesInfoRequestBuilder setOs(boolean os) {
60-
addOrRemoveMetric(os, NodesInfoRequest.Metric.OS);
64+
public NodesInfoRequestBuilder addMetrics(String... metrics) {
65+
request.addMetrics(metrics);
6166
return this;
6267
}
63-
64-
/**
65-
* Should the node OS process be returned.
66-
*/
67-
public NodesInfoRequestBuilder setProcess(boolean process) {
68-
addOrRemoveMetric(process, NodesInfoRequest.Metric.PROCESS);
69-
return this;
70-
}
71-
72-
/**
73-
* Should the node JVM info be returned.
74-
*/
75-
public NodesInfoRequestBuilder setJvm(boolean jvm) {
76-
addOrRemoveMetric(jvm, NodesInfoRequest.Metric.JVM);
77-
return this;
78-
}
79-
80-
/**
81-
* Should the node thread pool info be returned.
82-
*/
83-
public NodesInfoRequestBuilder setThreadPool(boolean threadPool) {
84-
addOrRemoveMetric(threadPool, NodesInfoRequest.Metric.THREAD_POOL);
85-
return this;
86-
}
87-
88-
/**
89-
* Should the node Transport info be returned.
90-
*/
91-
public NodesInfoRequestBuilder setTransport(boolean transport) {
92-
addOrRemoveMetric(transport, NodesInfoRequest.Metric.TRANSPORT);
93-
return this;
94-
}
95-
96-
/**
97-
* Should the node HTTP info be returned.
98-
*/
99-
public NodesInfoRequestBuilder setHttp(boolean http) {
100-
addOrRemoveMetric(http, NodesInfoRequest.Metric.HTTP);
101-
return this;
102-
}
103-
104-
/**
105-
* Should the node plugins info be returned.
106-
*/
107-
public NodesInfoRequestBuilder setPlugins(boolean plugins) {
108-
addOrRemoveMetric(plugins, NodesInfoRequest.Metric.PLUGINS);
109-
return this;
110-
}
111-
112-
/**
113-
* Should the node ingest info be returned.
114-
*/
115-
public NodesInfoRequestBuilder setIngest(boolean ingest) {
116-
addOrRemoveMetric(ingest, NodesInfoRequest.Metric.INGEST);
117-
return this;
118-
}
119-
120-
/**
121-
* Should the node indices info be returned.
122-
*/
123-
public NodesInfoRequestBuilder setIndices(boolean indices) {
124-
addOrRemoveMetric(indices, NodesInfoRequest.Metric.INDICES);
125-
return this;
126-
}
127-
128-
private void addOrRemoveMetric(boolean includeMetric, NodesInfoRequest.Metric metric) {
129-
if (includeMetric) {
130-
request.addMetric(metric.metricName());
131-
} else {
132-
request.removeMetric(metric.metricName());
133-
}
134-
}
13568
}

server/src/main/java/org/elasticsearch/action/admin/cluster/node/stats/NodesStatsRequestBuilder.java

Lines changed: 18 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -47,125 +47,40 @@ public NodesStatsRequestBuilder clear() {
4747
}
4848

4949
/**
50-
* Should the node indices stats be returned.
51-
*/
52-
public NodesStatsRequestBuilder setIndices(boolean indices) {
53-
request.indices(indices);
54-
return this;
55-
}
56-
57-
public NodesStatsRequestBuilder setBreaker(boolean breaker) {
58-
addOrRemoveMetric(breaker, NodesStatsRequest.Metric.BREAKER);
59-
return this;
60-
}
61-
62-
public NodesStatsRequestBuilder setScript(boolean script) {
63-
addOrRemoveMetric(script, NodesStatsRequest.Metric.SCRIPT);
64-
return this;
65-
}
66-
67-
/**
68-
* Should the node indices stats be returned.
69-
*/
70-
public NodesStatsRequestBuilder setIndices(CommonStatsFlags indices) {
71-
request.indices(indices);
72-
return this;
73-
}
74-
75-
/**
76-
* Should the node OS stats be returned.
77-
*/
78-
public NodesStatsRequestBuilder setOs(boolean os) {
79-
addOrRemoveMetric(os, NodesStatsRequest.Metric.OS);
80-
return this;
81-
}
82-
83-
/**
84-
* Should the node OS stats be returned.
85-
*/
86-
public NodesStatsRequestBuilder setProcess(boolean process) {
87-
addOrRemoveMetric(process, NodesStatsRequest.Metric.PROCESS);
88-
return this;
89-
}
90-
91-
/**
92-
* Should the node JVM stats be returned.
93-
*/
94-
public NodesStatsRequestBuilder setJvm(boolean jvm) {
95-
addOrRemoveMetric(jvm, NodesStatsRequest.Metric.JVM);
96-
return this;
97-
}
98-
99-
/**
100-
* Should the node thread pool stats be returned.
101-
*/
102-
public NodesStatsRequestBuilder setThreadPool(boolean threadPool) {
103-
addOrRemoveMetric(threadPool, NodesStatsRequest.Metric.THREAD_POOL);
104-
return this;
105-
}
106-
107-
/**
108-
* Should the node file system stats be returned.
50+
* Add a single metric to the request.
51+
*
52+
* @param metric Name of metric as a string.
53+
* @return This, for request chaining.
10954
*/
110-
public NodesStatsRequestBuilder setFs(boolean fs) {
111-
addOrRemoveMetric(fs, NodesStatsRequest.Metric.FS);
55+
public NodesStatsRequestBuilder addMetric(String metric) {
56+
request.addMetric(metric);
11257
return this;
11358
}
11459

11560
/**
116-
* Should the node Transport stats be returned.
61+
* Add an array of metrics to the request.
62+
*
63+
* @param metrics Metric names as strings.
64+
* @return This, for request chaining.
11765
*/
118-
public NodesStatsRequestBuilder setTransport(boolean transport) {
119-
addOrRemoveMetric(transport, NodesStatsRequest.Metric.TRANSPORT);
66+
public NodesStatsRequestBuilder addMetrics(String... metrics) {
67+
request.addMetrics(metrics);
12068
return this;
12169
}
12270

12371
/**
124-
* Should the node HTTP stats be returned.
125-
*/
126-
public NodesStatsRequestBuilder setHttp(boolean http) {
127-
addOrRemoveMetric(http, NodesStatsRequest.Metric.HTTP);
128-
return this;
129-
}
130-
131-
/**
132-
* Should the discovery stats be returned.
133-
*/
134-
public NodesStatsRequestBuilder setDiscovery(boolean discovery) {
135-
addOrRemoveMetric(discovery, NodesStatsRequest.Metric.DISCOVERY);
136-
return this;
137-
}
138-
139-
/**
140-
* Should ingest statistics be returned.
72+
* Should the node indices stats be returned.
14173
*/
142-
public NodesStatsRequestBuilder setIngest(boolean ingest) {
143-
addOrRemoveMetric(ingest, NodesStatsRequest.Metric.INGEST);
144-
return this;
145-
}
146-
147-
public NodesStatsRequestBuilder setAdaptiveSelection(boolean adaptiveSelection) {
148-
addOrRemoveMetric(adaptiveSelection, NodesStatsRequest.Metric.ADAPTIVE_SELECTION);
74+
public NodesStatsRequestBuilder setIndices(boolean indices) {
75+
request.indices(indices);
14976
return this;
15077
}
15178

15279
/**
153-
* Should script context cache statistics be returned
80+
* Should the node indices stats be returned.
15481
*/
155-
public NodesStatsRequestBuilder setScriptCache(boolean scriptCache) {
156-
addOrRemoveMetric(scriptCache, NodesStatsRequest.Metric.SCRIPT_CACHE);
82+
public NodesStatsRequestBuilder setIndices(CommonStatsFlags indices) {
83+
request.indices(indices);
15784
return this;
15885
}
159-
160-
/**
161-
* Helper method for adding metrics to a request
162-
*/
163-
private void addOrRemoveMetric(boolean includeMetric, NodesStatsRequest.Metric metric) {
164-
if (includeMetric) {
165-
request.addMetric(metric.metricName());
166-
} else {
167-
request.removeMetric(metric.metricName());
168-
}
169-
}
170-
17186
}

server/src/test/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.Map;
4242
import java.util.concurrent.ExecutionException;
4343

44+
import static org.elasticsearch.action.admin.cluster.node.stats.NodesStatsRequest.Metric.OS;
4445
import static org.hamcrest.Matchers.equalTo;
4546
import static org.hamcrest.Matchers.is;
4647

@@ -193,7 +194,7 @@ public void testValuesSmokeScreen() throws IOException, ExecutionException, Inte
193194
assertThat(msg, response.nodesStats.getProcess().getMinOpenFileDescriptors(), Matchers.greaterThanOrEqualTo(-1L));
194195
assertThat(msg, response.nodesStats.getProcess().getMaxOpenFileDescriptors(), Matchers.greaterThanOrEqualTo(-1L));
195196

196-
NodesStatsResponse nodesStatsResponse = client().admin().cluster().prepareNodesStats().setOs(true).get();
197+
NodesStatsResponse nodesStatsResponse = client().admin().cluster().prepareNodesStats().addMetric(OS.metricName()).get();
197198
long total = 0;
198199
long free = 0;
199200
long used = 0;

server/src/test/java/org/elasticsearch/cluster/coordination/ZenDiscoveryIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.util.concurrent.TimeUnit;
4848
import java.util.concurrent.TimeoutException;
4949

50+
import static org.elasticsearch.action.admin.cluster.node.stats.NodesStatsRequest.Metric.DISCOVERY;
5051
import static org.hamcrest.Matchers.containsString;
5152
import static org.hamcrest.Matchers.equalTo;
5253
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
@@ -165,7 +166,7 @@ public void testDiscoveryStats() throws Exception {
165166
equalTo(0))); // see https://github.com/elastic/elasticsearch/issues/24388
166167

167168
logger.info("--> request node discovery stats");
168-
NodesStatsResponse statsResponse = client().admin().cluster().prepareNodesStats().clear().setDiscovery(true).get();
169+
NodesStatsResponse statsResponse = client().admin().cluster().prepareNodesStats().clear().addMetric(DISCOVERY.metricName()).get();
169170
assertThat(statsResponse.getNodes().size(), equalTo(1));
170171

171172
DiscoveryStats stats = statsResponse.getNodes().get(0).getDiscoveryStats();

server/src/test/java/org/elasticsearch/cluster/settings/SettingsFilteringIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Collection;
3434
import java.util.List;
3535

36+
import static org.elasticsearch.action.admin.cluster.node.info.NodesInfoRequest.Metric.SETTINGS;
3637
import static org.elasticsearch.test.ESIntegTestCase.Scope.SUITE;
3738
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
3839
import static org.hamcrest.Matchers.equalTo;
@@ -89,7 +90,7 @@ public void testSettingsFiltering() {
8990
}
9091

9192
public void testNodeInfoIsFiltered() {
92-
NodesInfoResponse nodeInfos = client().admin().cluster().prepareNodesInfo().clear().setSettings(true).get();
93+
NodesInfoResponse nodeInfos = client().admin().cluster().prepareNodesInfo().clear().addMetric(SETTINGS.metricName()).get();
9394
for(NodeInfo info : nodeInfos.getNodes()) {
9495
Settings settings = info.getSettings();
9596
assertNotNull(settings);

server/src/test/java/org/elasticsearch/index/shard/IndexShardIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomAsciiLettersOfLength;
111111
import static java.util.Collections.emptyMap;
112112
import static java.util.Collections.emptySet;
113+
import static org.elasticsearch.action.admin.cluster.node.stats.NodesStatsRequest.Metric.BREAKER;
113114
import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE;
114115
import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.NONE;
115116
import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS;
@@ -583,7 +584,7 @@ private void checkAccountingBreaker() {
583584
CircuitBreaker acctBreaker = breakerService.getBreaker(CircuitBreaker.ACCOUNTING);
584585
long usedMem = acctBreaker.getUsed();
585586
assertThat(usedMem, greaterThan(0L));
586-
NodesStatsResponse response = client().admin().cluster().prepareNodesStats().setIndices(true).setBreaker(true).get();
587+
NodesStatsResponse response = client().admin().cluster().prepareNodesStats().setIndices(true).addMetric(BREAKER.metricName()).get();
587588
NodeStats stats = response.getNodes().get(0);
588589
assertNotNull(stats);
589590
SegmentsStats segmentsStats = stats.getIndices().getSegments();

server/src/test/java/org/elasticsearch/index/shard/RemoveCorruptedShardDataCommandIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
import java.util.stream.Stream;
9090
import java.util.stream.StreamSupport;
9191

92+
import static org.elasticsearch.action.admin.cluster.node.stats.NodesStatsRequest.Metric.FS;
9293
import static org.elasticsearch.common.util.CollectionUtils.iterableAsArrayList;
9394
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
9495
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
@@ -599,7 +600,7 @@ private Path getPathToShardData(String indexName, String dirSuffix) {
599600
}
600601

601602
public static Path getPathToShardData(String nodeId, ShardId shardId, String shardPathSubdirectory) {
602-
final NodesStatsResponse nodeStatsResponse = client().admin().cluster().prepareNodesStats(nodeId).setFs(true).get();
603+
final NodesStatsResponse nodeStatsResponse = client().admin().cluster().prepareNodesStats(nodeId).addMetric(FS.metricName()).get();
603604
final Set<Path> paths = StreamSupport.stream(nodeStatsResponse.getNodes().get(0).getFs().spliterator(), false)
604605
.map(nodePath -> PathUtils.get(nodePath.getPath())
605606
.resolve(NodeEnvironment.INDICES_FOLDER)

0 commit comments

Comments
 (0)