Skip to content

Commit

Permalink
Avoid making further stats calls if paginationStrategy outputs empty …
Browse files Browse the repository at this point in the history
…entities

Signed-off-by: Harsh Garg <[email protected]>
  • Loading branch information
Harsh Garg committed Oct 23, 2024
1 parent 0419e5d commit b328dbc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.opensearch.action.admin.cluster.state.ClusterStateResponse;
import org.opensearch.action.admin.indices.stats.IndicesStatsRequest;
import org.opensearch.action.admin.indices.stats.IndicesStatsResponse;
import org.opensearch.action.admin.indices.stats.ShardStats;
import org.opensearch.action.pagination.PageParams;
import org.opensearch.action.pagination.ShardPaginationStrategy;
import org.opensearch.action.support.ActionFilters;
Expand All @@ -27,6 +28,7 @@
import org.opensearch.tasks.Task;
import org.opensearch.transport.TransportService;

import java.util.Collections;
import java.util.Objects;

import static org.opensearch.common.breaker.ResponseLimitSettings.LimitEntity.SHARDS;
Expand All @@ -40,6 +42,13 @@ public class TransportCatShardsAction extends HandledTransportAction<CatShardsRe

private final NodeClient client;
private final ResponseLimitSettings responseLimitSettings;
private static final IndicesStatsResponse EMPTY_INDICES_STATS_RESPONSE = new IndicesStatsResponse(
new ShardStats[0],
0,
0,
0,
Collections.emptyList()
);

@Inject
public TransportCatShardsAction(
Expand Down Expand Up @@ -108,6 +117,12 @@ public void onResponse(ClusterStateResponse clusterStateResponse) {
: paginationStrategy.getRequestedEntities()
);
catShardsResponse.setPageToken(Objects.isNull(paginationStrategy) ? null : paginationStrategy.getResponseToken());
// For paginated queries, if strategy outputs no shards to be returned, avoid fetching IndicesStats.
if (shouldSkipIndicesStatsRequest(paginationStrategy)) {
catShardsResponse.setIndicesStatsResponse(EMPTY_INDICES_STATS_RESPONSE);
cancellableListener.onResponse(catShardsResponse);
return;
}
IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest();
indicesStatsRequest.setShouldCancelOnTimeout(true);
indicesStatsRequest.all();
Expand Down Expand Up @@ -159,4 +174,8 @@ private void validateRequestLimit(
}
}
}

private boolean shouldSkipIndicesStatsRequest(ShardPaginationStrategy paginationStrategy) {
return Objects.nonNull(paginationStrategy) && paginationStrategy.getRequestedEntities().isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.opensearch.action.admin.indices.stats.IndexStats;
import org.opensearch.action.admin.indices.stats.IndicesStatsRequest;
import org.opensearch.action.admin.indices.stats.IndicesStatsResponse;
import org.opensearch.action.admin.indices.stats.ShardStats;
import org.opensearch.action.pagination.IndexPaginationStrategy;
import org.opensearch.action.pagination.PageToken;
import org.opensearch.action.support.GroupedActionListener;
Expand Down Expand Up @@ -104,6 +105,13 @@ public class RestIndicesAction extends AbstractListAction {
"Parameter [master_timeout] is deprecated and will be removed in 3.0. To support inclusive language, please use [cluster_manager_timeout] instead.";
private static final String DUPLICATE_PARAMETER_ERROR_MESSAGE =
"Please only use one of the request parameters [master_timeout, cluster_manager_timeout].";
private static final IndicesStatsResponse EMPTY_INDICES_STATS_RESPONSE = new IndicesStatsResponse(
new ShardStats[0],
0,
0,
0,
Collections.emptyList()
);

private final ResponseLimitSettings responseLimitSettings;

Expand Down Expand Up @@ -212,13 +220,19 @@ public void onResponse(ClusterStateResponse clusterStateResponse) {
groupedListener.onResponse(getSettingsResponse);
groupedListener.onResponse(clusterStateResponse);

sendIndicesStatsRequest(
indicesToBeQueried,
subRequestIndicesOptions,
includeUnloadedSegments,
client,
ActionListener.wrap(groupedListener::onResponse, groupedListener::onFailure)
);
// For paginated queries, if strategy outputs no indices to be returned,
// avoid fetching indices stats.
if (shouldSkipIndicesStatsRequest(paginationStrategy)) {
groupedListener.onResponse(EMPTY_INDICES_STATS_RESPONSE);
} else {
sendIndicesStatsRequest(
indicesToBeQueried,
subRequestIndicesOptions,
includeUnloadedSegments,
client,
ActionListener.wrap(groupedListener::onResponse, groupedListener::onFailure)
);
}

sendClusterHealthRequest(
indicesToBeQueried,
Expand Down Expand Up @@ -1093,4 +1107,8 @@ public Tuple<String, Settings> next() {
};
}

private boolean shouldSkipIndicesStatsRequest(IndexPaginationStrategy paginationStrategy) {
return Objects.nonNull(paginationStrategy) && paginationStrategy.getRequestedEntities().isEmpty();
}

}

0 comments on commit b328dbc

Please sign in to comment.