Skip to content

Commit

Permalink
Adding params for enabling and disabling mapping_stats and analysis_s…
Browse files Browse the repository at this point in the history
…tats with flags
  • Loading branch information
Pranshu-S committed Jun 19, 2024
1 parent 93d507a commit 2ec8310
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,12 @@ public void testClusterStatusWhenStateNotRecovered() throws Exception {
public void testFieldTypes() {
internalCluster().startNode();
ensureGreen();
ClusterStatsResponse response = client().admin().cluster().prepareClusterStats().get();
ClusterStatsResponse response = client().admin()
.cluster()
.prepareClusterStats()
.includeAnalysisStats(true)
.includeMappingStats(true)
.get();
assertThat(response.getStatus(), Matchers.equalTo(ClusterHealthStatus.GREEN));
assertTrue(response.getIndicesStats().getMappings().getFieldTypeStats().isEmpty());

Expand All @@ -301,7 +306,7 @@ public void testFieldTypes() {
+ "\"eggplant\":{\"type\":\"integer\"}}}}}"
)
.get();
response = client().admin().cluster().prepareClusterStats().get();
response = client().admin().cluster().prepareClusterStats().includeMappingStats(true).includeAnalysisStats(true).get();
assertThat(response.getIndicesStats().getMappings().getFieldTypeStats().size(), equalTo(3));
Set<IndexFeatureStats> stats = response.getIndicesStats().getMappings().getFieldTypeStats();
for (IndexFeatureStats stat : stats) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
registerHandler.accept(new RestNodesUsageAction());
registerHandler.accept(new RestNodesHotThreadsAction());
registerHandler.accept(new RestClusterAllocationExplainAction());
registerHandler.accept(new RestClusterStatsAction());
registerHandler.accept(new RestClusterStatsAction(settings, clusterSettings));
registerHandler.accept(new RestClusterStateAction(settingsFilter));
registerHandler.accept(new RestClusterHealthAction());
registerHandler.accept(new RestClusterUpdateSettingsAction());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,29 @@
@PublicApi(since = "1.0.0")
public class ClusterStatsRequest extends BaseNodesRequest<ClusterStatsRequest> {

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
* based on all nodes will be returned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,14 @@ public class ClusterStatsRequestBuilder extends NodesOperationRequestBuilder<
public ClusterStatsRequestBuilder(OpenSearchClient client, ClusterStatsAction action) {
super(client, action, new ClusterStatsRequest());
}

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 @@ -149,6 +149,7 @@
import org.opensearch.ratelimitting.admissioncontrol.settings.IoBasedAdmissionControllerSettings;
import org.opensearch.repositories.fs.FsRepository;
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.rest.action.admin.cluster.RestClusterStatsAction;
import org.opensearch.script.ScriptService;
import org.opensearch.search.SearchModule;
import org.opensearch.search.SearchService;
Expand Down Expand Up @@ -749,7 +750,9 @@ public void apply(Settings value, Settings current, Settings previous) {
RemoteStoreSettings.CLUSTER_REMOTE_STORE_PATH_HASH_ALGORITHM_SETTING,
RemoteStoreSettings.CLUSTER_REMOTE_MAX_TRANSLOG_READERS,
RemoteStoreSettings.CLUSTER_REMOTE_STORE_TRANSLOG_METADATA,
SearchService.CLUSTER_ALLOW_DERIVED_FIELD_SETTING
SearchService.CLUSTER_ALLOW_DERIVED_FIELD_SETTING,

RestClusterStatsAction.CLUSTER_STATS_LEVEL_SETTING
)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@

import org.opensearch.action.admin.cluster.stats.ClusterStatsRequest;
import org.opensearch.client.node.NodeClient;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Settings;
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.action.RestActions.NodesResponseRestListener;
Expand All @@ -52,6 +55,40 @@
*/
public class RestClusterStatsAction extends BaseRestHandler {

/**
* Setting up the response in the form of levels - for experimental testings -
* level 1 excludes Mapping and Analysis Stats.
*
* This will determine the default behavior of the cluster stats call.
*/
public static final String CLUSTER_STATS_RESPONSE_LEVEL = "opensearch.experimental.optimization.cluster_stats.response_level";

public static final Setting<Integer> CLUSTER_STATS_LEVEL_SETTING = Setting.intSetting(
CLUSTER_STATS_RESPONSE_LEVEL,
2,
1,
Setting.Property.NodeScope,
Setting.Property.Dynamic
);

public volatile boolean defaultMappingAndAnalysisStats;

public RestClusterStatsAction(Settings settings, ClusterSettings clusterSettings) {
updateMappingAndAnalysisStatsFromLevel(CLUSTER_STATS_LEVEL_SETTING.get(settings));
clusterSettings.addSettingsUpdateConsumer(CLUSTER_STATS_LEVEL_SETTING, this::updateMappingAndAnalysisStatsFromLevel);
}

private void updateMappingAndAnalysisStatsFromLevel(int level) {
switch (level) {
case 1:
this.defaultMappingAndAnalysisStats = false;
break;
case 2:
this.defaultMappingAndAnalysisStats = true;
break;
}
}

@Override
public List<Route> routes() {
return unmodifiableList(asList(new Route(GET, "/_cluster/stats"), new Route(GET, "/_cluster/stats/nodes/{nodeId}")));
Expand All @@ -66,6 +103,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", defaultMappingAndAnalysisStats));
clusterStatsRequest.setIncludeAnalysisStats(request.paramAsBoolean("include_analysis_stats", defaultMappingAndAnalysisStats));

return channel -> client.admin().cluster().clusterStats(clusterStatsRequest, new NodesResponseRestListener<>(channel));
}

Expand Down

0 comments on commit 2ec8310

Please sign in to comment.