Skip to content

Commit

Permalink
Avoid making further transport calls if paginationStrategy outputs em…
Browse files Browse the repository at this point in the history
…pty entities

Signed-off-by: Harsh Garg <[email protected]>
  • Loading branch information
Harsh Garg committed Oct 23, 2024
1 parent 0419e5d commit 523acf6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 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;

Check warning on line 124 in server/src/main/java/org/opensearch/action/admin/cluster/shards/TransportCatShardsAction.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/action/admin/cluster/shards/TransportCatShardsAction.java#L122-L124

Added lines #L122 - L124 were not covered by tests
}
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,14 @@ 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 static final ClusterHealthResponse EMPTY_CLUSTER_HEALTH_RESPONSE = new ClusterHealthResponse();

private final ResponseLimitSettings responseLimitSettings;

Expand Down Expand Up @@ -212,6 +221,14 @@ public void onResponse(ClusterStateResponse clusterStateResponse) {
groupedListener.onResponse(getSettingsResponse);
groupedListener.onResponse(clusterStateResponse);

// For paginated queries, if strategy outputs no indices to be returned,
// avoid fetching health and stats.
if (shouldSkipNextRequests(paginationStrategy)) {
groupedListener.onResponse(EMPTY_CLUSTER_HEALTH_RESPONSE);
groupedListener.onResponse(EMPTY_INDICES_STATS_RESPONSE);
return;

Check warning on line 229 in server/src/main/java/org/opensearch/rest/action/cat/RestIndicesAction.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/rest/action/cat/RestIndicesAction.java#L227-L229

Added lines #L227 - L229 were not covered by tests
}

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

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

}

0 comments on commit 523acf6

Please sign in to comment.