From 14e3db5f05fd1192263911715948ef2a1bb120e7 Mon Sep 17 00:00:00 2001 From: Pranshu Shukla Date: Thu, 13 Jun 2024 14:54:47 +0530 Subject: [PATCH] Enable feature using flags --- .../cluster/stats/ClusterStatsRequest.java | 19 ++++++++++++ .../cluster/stats/ClusterStatsResponse.java | 27 ++++++++++++++++ .../stats/TransportClusterStatsAction.java | 31 ++++++++++++++----- .../admin/cluster/RestClusterStatsAction.java | 4 +++ 4 files changed, 73 insertions(+), 8 deletions(-) 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 6a99451c596ed..bce66b2882429 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 @@ -47,9 +47,28 @@ @PublicApi(since = "1.0.0") public class ClusterStatsRequest extends BaseNodesRequest { + private boolean includeMappingStats; + private boolean includeAnalysisStats; + public ClusterStatsRequest(StreamInput in) throws IOException { super(in); } + public void setIncludeMappingStats(boolean bool) { + this.includeMappingStats = bool; + } + + public boolean isIncludeMappingStats() { + return includeMappingStats; + } + + public void setIncludeAnalysisStats(boolean bool) { + this.includeAnalysisStats = bool; + } + + public boolean isIncludeAnalysisStats() { + return includeAnalysisStats; + } + /** * Get stats from nodes based on the nodes ids specified. If none are passed, stats 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..c7f209a0b2e88 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,33 @@ 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 e4f483f796f44..282876c32c39c 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 @@ -46,6 +46,7 @@ import org.opensearch.cluster.health.ClusterStateHealth; import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.inject.Inject; +import org.opensearch.common.util.FeatureFlags; import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; import org.opensearch.index.IndexService; @@ -123,14 +124,28 @@ protected ClusterStatsResponse newResponse( + " the cluster state that are too slow for a transport thread" ); ClusterState state = clusterService.state(); - return new ClusterStatsResponse( - System.currentTimeMillis(), - state.metadata().clusterUUID(), - clusterService.getClusterName(), - responses, - failures, - state - ); + + if (FeatureFlags.isEnabled(FeatureFlags.OPTIMIZED_CLUSTER_STATS_SETTING)) { + return new ClusterStatsResponse( + System.currentTimeMillis(), + state.metadata().clusterUUID(), + clusterService.getClusterName(), + responses, + failures, + state, + request + ); + } else { + return new ClusterStatsResponse( + System.currentTimeMillis(), + state.metadata().clusterUUID(), + clusterService.getClusterName(), + responses, + failures, + state + ); + } + } @Override 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 0766e838210fa..061761ac44b01 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 @@ -66,6 +66,10 @@ public String getName() { public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { ClusterStatsRequest clusterStatsRequest = new ClusterStatsRequest().nodesIds(request.paramAsStringArray("nodeId", null)); clusterStatsRequest.timeout(request.param("timeout")); + + clusterStatsRequest.setIncludeMappingStats(request.paramAsBoolean("include_mapping_stats", false)); + clusterStatsRequest.setIncludeAnalysisStats(request.paramAsBoolean("include_analysis_stats", false)); + return channel -> client.admin().cluster().clusterStats(clusterStatsRequest, new NodesResponseRestListener<>(channel)); }