From 5d8aaccd9a77fecb1a9c5842f7a8113e3eebb623 Mon Sep 17 00:00:00 2001 From: Swetha Guptha Date: Fri, 23 Aug 2024 10:58:29 +0530 Subject: [PATCH] Add support to _cluster/stats API for enabling/disabling mapping_stats and analysis_stats at request level. Signed-off-by: Swetha Guptha --- CHANGELOG.md | 1 + .../admin/cluster/stats/ClusterStatsIT.java | 43 +++++++++++++ .../cluster/stats/ClusterStatsRequest.java | 20 +++++++ .../stats/ClusterStatsRequestBuilder.java | 10 ++++ .../cluster/stats/ClusterStatsResponse.java | 29 +++++++++ .../stats/TransportClusterStatsAction.java | 3 +- .../admin/cluster/RestClusterStatsAction.java | 2 + .../stats/ClusterStatsRequestBuilderTest.java | 60 +++++++++++++++++++ 8 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 server/src/test/java/org/opensearch/action/admin/cluster/stats/ClusterStatsRequestBuilderTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index cd02af4f625b9..7a689de0055b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased 2.x] ### Added +- Add support to cluster stats API for enabling/disabling mapping_stats and analysis_stats at request level. - [Offline Nodes] Adds offline-tasks library containing various interfaces to be used for Offline Background Tasks. ([#13574](https://github.com/opensearch-project/OpenSearch/pull/13574)) - Fix for hasInitiatedFetching to fix allocation explain and manual reroute APIs (([#14972](https://github.com/opensearch-project/OpenSearch/pull/14972)) - [Workload Management] Add queryGroupId to Task ([14708](https://github.com/opensearch-project/OpenSearch/pull/14708)) diff --git a/server/src/internalClusterTest/java/org/opensearch/action/admin/cluster/stats/ClusterStatsIT.java b/server/src/internalClusterTest/java/org/opensearch/action/admin/cluster/stats/ClusterStatsIT.java index f23cdbb50b37a..44153b4e6b433 100644 --- a/server/src/internalClusterTest/java/org/opensearch/action/admin/cluster/stats/ClusterStatsIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/action/admin/cluster/stats/ClusterStatsIT.java @@ -384,6 +384,49 @@ public void testFieldTypes() { } } + public void testIndicesStatsAnalysisAndMappingStatsInclusion() { + internalCluster().startNode(); + ensureGreen(); + + client().admin().indices().prepareCreate("test1").setMapping("{\"properties\":{\"foo\":{\"type\": \"keyword\"}}}").get(); + client().admin() + .indices() + .prepareCreate("test2") + .setMapping( + "{\"properties\":{\"foo\":{\"type\": \"keyword\"},\"bar\":{\"properties\":{\"baz\":{\"type\":\"keyword\"}," + + "\"eggplant\":{\"type\":\"integer\"}}}}}" + ) + .get(); + + ClusterStatsResponse response = client().admin() + .cluster() + .prepareClusterStats() + .useAggregatedNodeLevelResponses(randomBoolean()) + .includeMappingStats(false) + .includeAnalysisStats(false) + .get(); + assertNull(response.getIndicesStats().getMappings()); + assertNull(response.getIndicesStats().getAnalysis()); + + response = client().admin().cluster().prepareClusterStats().useAggregatedNodeLevelResponses(randomBoolean()).get(); + assertNotNull(response.getIndicesStats().getAnalysis()); + assertThat(response.getIndicesStats().getMappings().getFieldTypeStats().size(), equalTo(3)); + Set stats = response.getIndicesStats().getMappings().getFieldTypeStats(); + for (IndexFeatureStats stat : stats) { + switch (stat.getName()) { + case "integer": + assertThat(stat.getCount(), greaterThanOrEqualTo(1)); + break; + case "keyword": + assertThat(stat.getCount(), greaterThanOrEqualTo(3)); + break; + case "object": + assertThat(stat.getCount(), greaterThanOrEqualTo(1)); + break; + } + } + } + public void testNodeRolesWithMasterLegacySettings() throws ExecutionException, InterruptedException { int total = 1; Settings legacyMasterSettings = Settings.builder() diff --git a/server/src/main/java/org/opensearch/action/admin/cluster/stats/ClusterStatsRequest.java b/server/src/main/java/org/opensearch/action/admin/cluster/stats/ClusterStatsRequest.java index bd75b2210e474..147b393246c12 100644 --- a/server/src/main/java/org/opensearch/action/admin/cluster/stats/ClusterStatsRequest.java +++ b/server/src/main/java/org/opensearch/action/admin/cluster/stats/ClusterStatsRequest.java @@ -48,6 +48,10 @@ @PublicApi(since = "1.0.0") public class ClusterStatsRequest extends BaseNodesRequest { + private boolean includeMappingStats = true; + + private boolean includeAnalysisStats = true; + public ClusterStatsRequest(StreamInput in) throws IOException { super(in); if (in.getVersion().onOrAfter(Version.V_2_16_0)) { @@ -73,6 +77,22 @@ public void useAggregatedNodeLevelResponses(boolean useAggregatedNodeLevelRespon this.useAggregatedNodeLevelResponses = useAggregatedNodeLevelResponses; } + public void setIncludeMappingStats(boolean setIncludeMappingStats) { + this.includeMappingStats = setIncludeMappingStats; + } + + public boolean isIncludeMappingStats() { + return includeMappingStats; + } + + public void setIncludeAnalysisStats(boolean setIncludeAnalysisStats) { + this.includeAnalysisStats = setIncludeAnalysisStats; + } + + public boolean isIncludeAnalysisStats() { + return includeAnalysisStats; + } + @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); diff --git a/server/src/main/java/org/opensearch/action/admin/cluster/stats/ClusterStatsRequestBuilder.java b/server/src/main/java/org/opensearch/action/admin/cluster/stats/ClusterStatsRequestBuilder.java index 4d0932bd3927d..2f8f62e611043 100644 --- a/server/src/main/java/org/opensearch/action/admin/cluster/stats/ClusterStatsRequestBuilder.java +++ b/server/src/main/java/org/opensearch/action/admin/cluster/stats/ClusterStatsRequestBuilder.java @@ -55,4 +55,14 @@ public final ClusterStatsRequestBuilder useAggregatedNodeLevelResponses(boolean request.useAggregatedNodeLevelResponses(useAggregatedNodeLevelResponses); return this; } + + public ClusterStatsRequestBuilder includeMappingStats(boolean value) { + request.setIncludeMappingStats(value); + return this; + } + + public ClusterStatsRequestBuilder includeAnalysisStats(boolean value) { + request.setIncludeAnalysisStats(value); + return this; + } } diff --git a/server/src/main/java/org/opensearch/action/admin/cluster/stats/ClusterStatsResponse.java b/server/src/main/java/org/opensearch/action/admin/cluster/stats/ClusterStatsResponse.java index cc002b689a2a5..cc2acea1a46a9 100644 --- a/server/src/main/java/org/opensearch/action/admin/cluster/stats/ClusterStatsResponse.java +++ b/server/src/main/java/org/opensearch/action/admin/cluster/stats/ClusterStatsResponse.java @@ -105,6 +105,35 @@ public ClusterStatsResponse( this.status = status; } + public ClusterStatsResponse( + long timestamp, + String clusterUUID, + ClusterName clusterName, + List nodes, + List failures, + ClusterState state, + ClusterStatsRequest request + ) { + super(clusterName, nodes, failures); + this.clusterUUID = clusterUUID; + this.timestamp = timestamp; + nodesStats = new ClusterStatsNodes(nodes); + indicesStats = new ClusterStatsIndices( + nodes, + request.isIncludeMappingStats() ? MappingStats.of(state) : null, + request.isIncludeAnalysisStats() ? AnalysisStats.of(state) : null + ); + ClusterHealthStatus status = null; + for (ClusterStatsNodeResponse response : nodes) { + // only the cluster-manager node populates the status + if (response.clusterStatus() != null) { + status = response.clusterStatus(); + break; + } + } + this.status = status; + } + public String getClusterUUID() { return this.clusterUUID; } diff --git a/server/src/main/java/org/opensearch/action/admin/cluster/stats/TransportClusterStatsAction.java b/server/src/main/java/org/opensearch/action/admin/cluster/stats/TransportClusterStatsAction.java index be7d41a7ba75e..9a76beb154882 100644 --- a/server/src/main/java/org/opensearch/action/admin/cluster/stats/TransportClusterStatsAction.java +++ b/server/src/main/java/org/opensearch/action/admin/cluster/stats/TransportClusterStatsAction.java @@ -129,7 +129,8 @@ protected ClusterStatsResponse newResponse( clusterService.getClusterName(), responses, failures, - state + state, + request ); } diff --git a/server/src/main/java/org/opensearch/rest/action/admin/cluster/RestClusterStatsAction.java b/server/src/main/java/org/opensearch/rest/action/admin/cluster/RestClusterStatsAction.java index d4426a004af8e..e2909a8a9e750 100644 --- a/server/src/main/java/org/opensearch/rest/action/admin/cluster/RestClusterStatsAction.java +++ b/server/src/main/java/org/opensearch/rest/action/admin/cluster/RestClusterStatsAction.java @@ -68,6 +68,8 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC clusterStatsRequest.timeout(request.param("timeout")); clusterStatsRequest.setIncludeDiscoveryNodes(false); clusterStatsRequest.useAggregatedNodeLevelResponses(true); + clusterStatsRequest.setIncludeMappingStats(request.paramAsBoolean("include_mapping_stats", true)); + clusterStatsRequest.setIncludeAnalysisStats(request.paramAsBoolean("include_analysis_stats", true)); return channel -> client.admin().cluster().clusterStats(clusterStatsRequest, new NodesResponseRestListener<>(channel)); } diff --git a/server/src/test/java/org/opensearch/action/admin/cluster/stats/ClusterStatsRequestBuilderTest.java b/server/src/test/java/org/opensearch/action/admin/cluster/stats/ClusterStatsRequestBuilderTest.java new file mode 100644 index 0000000000000..7c4f978462776 --- /dev/null +++ b/server/src/test/java/org/opensearch/action/admin/cluster/stats/ClusterStatsRequestBuilderTest.java @@ -0,0 +1,60 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.action.admin.cluster.stats; + +import org.opensearch.test.OpenSearchTestCase; +import org.opensearch.test.client.NoOpClient; +import org.junit.After; +import org.junit.Before; + +public class ClusterStatsRequestBuilderTest extends OpenSearchTestCase { + + private NoOpClient testClient; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + this.testClient = new NoOpClient(getTestName()); + } + + @Override + @After + public void tearDown() throws Exception { + this.testClient.close(); + super.tearDown(); + } + + public void testUseAggregatedNodeLevelResponses() { + ClusterStatsRequestBuilder clusterStatsRequestBuilder = new ClusterStatsRequestBuilder( + this.testClient, + ClusterStatsAction.INSTANCE + ); + clusterStatsRequestBuilder.useAggregatedNodeLevelResponses(false); + assertFalse(clusterStatsRequestBuilder.request().useAggregatedNodeLevelResponses()); + } + + public void testIncludeMappingStats() { + ClusterStatsRequestBuilder clusterStatsRequestBuilder = new ClusterStatsRequestBuilder( + this.testClient, + ClusterStatsAction.INSTANCE + ); + clusterStatsRequestBuilder.includeMappingStats(false); + assertFalse(clusterStatsRequestBuilder.request().isIncludeMappingStats()); + } + + public void testIncludeAnalysisStats() { + ClusterStatsRequestBuilder clusterStatsRequestBuilder = new ClusterStatsRequestBuilder( + this.testClient, + ClusterStatsAction.INSTANCE + ); + clusterStatsRequestBuilder.includeAnalysisStats(false); + assertFalse(clusterStatsRequestBuilder.request().isIncludeAnalysisStats()); + } +}