Skip to content

Commit

Permalink
Add support to _cluster/stats API for enabling/disabling mapping_stat…
Browse files Browse the repository at this point in the history
…s and analysis_stats at request level.

Signed-off-by: Swetha Guptha <[email protected]>
  • Loading branch information
Swetha Guptha committed Aug 27, 2024
1 parent ed65482 commit 5d8aacc
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IndexFeatureStats> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
@PublicApi(since = "1.0.0")
public class ClusterStatsRequest extends BaseNodesRequest<ClusterStatsRequest> {

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)) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,35 @@ public ClusterStatsResponse(
this.status = status;
}

public ClusterStatsResponse(
long timestamp,
String clusterUUID,
ClusterName clusterName,
List<ClusterStatsNodeResponse> nodes,
List<FailedNodeException> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ protected ClusterStatsResponse newResponse(
clusterService.getClusterName(),
responses,
failures,
state
state,
request
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 5d8aacc

Please sign in to comment.