Skip to content

Commit 523acf6

Browse files
author
Harsh Garg
committed
Avoid making further transport calls if paginationStrategy outputs empty entities
Signed-off-by: Harsh Garg <[email protected]>
1 parent 0419e5d commit 523acf6

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

server/src/main/java/org/opensearch/action/admin/cluster/shards/TransportCatShardsAction.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.opensearch.action.admin.cluster.state.ClusterStateResponse;
1313
import org.opensearch.action.admin.indices.stats.IndicesStatsRequest;
1414
import org.opensearch.action.admin.indices.stats.IndicesStatsResponse;
15+
import org.opensearch.action.admin.indices.stats.ShardStats;
1516
import org.opensearch.action.pagination.PageParams;
1617
import org.opensearch.action.pagination.ShardPaginationStrategy;
1718
import org.opensearch.action.support.ActionFilters;
@@ -27,6 +28,7 @@
2728
import org.opensearch.tasks.Task;
2829
import org.opensearch.transport.TransportService;
2930

31+
import java.util.Collections;
3032
import java.util.Objects;
3133

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

4143
private final NodeClient client;
4244
private final ResponseLimitSettings responseLimitSettings;
45+
private static final IndicesStatsResponse EMPTY_INDICES_STATS_RESPONSE = new IndicesStatsResponse(
46+
new ShardStats[0],
47+
0,
48+
0,
49+
0,
50+
Collections.emptyList()
51+
);
4352

4453
@Inject
4554
public TransportCatShardsAction(
@@ -108,6 +117,12 @@ public void onResponse(ClusterStateResponse clusterStateResponse) {
108117
: paginationStrategy.getRequestedEntities()
109118
);
110119
catShardsResponse.setPageToken(Objects.isNull(paginationStrategy) ? null : paginationStrategy.getResponseToken());
120+
// For paginated queries, if strategy outputs no shards to be returned, avoid fetching IndicesStats.
121+
if (shouldSkipIndicesStatsRequest(paginationStrategy)) {
122+
catShardsResponse.setIndicesStatsResponse(EMPTY_INDICES_STATS_RESPONSE);
123+
cancellableListener.onResponse(catShardsResponse);
124+
return;
125+
}
111126
IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest();
112127
indicesStatsRequest.setShouldCancelOnTimeout(true);
113128
indicesStatsRequest.all();
@@ -159,4 +174,8 @@ private void validateRequestLimit(
159174
}
160175
}
161176
}
177+
178+
private boolean shouldSkipIndicesStatsRequest(ShardPaginationStrategy paginationStrategy) {
179+
return Objects.nonNull(paginationStrategy) && paginationStrategy.getRequestedEntities().isEmpty();
180+
}
162181
}

server/src/main/java/org/opensearch/rest/action/cat/RestIndicesAction.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.opensearch.action.admin.indices.stats.IndexStats;
4444
import org.opensearch.action.admin.indices.stats.IndicesStatsRequest;
4545
import org.opensearch.action.admin.indices.stats.IndicesStatsResponse;
46+
import org.opensearch.action.admin.indices.stats.ShardStats;
4647
import org.opensearch.action.pagination.IndexPaginationStrategy;
4748
import org.opensearch.action.pagination.PageToken;
4849
import org.opensearch.action.support.GroupedActionListener;
@@ -104,6 +105,14 @@ public class RestIndicesAction extends AbstractListAction {
104105
"Parameter [master_timeout] is deprecated and will be removed in 3.0. To support inclusive language, please use [cluster_manager_timeout] instead.";
105106
private static final String DUPLICATE_PARAMETER_ERROR_MESSAGE =
106107
"Please only use one of the request parameters [master_timeout, cluster_manager_timeout].";
108+
private static final IndicesStatsResponse EMPTY_INDICES_STATS_RESPONSE = new IndicesStatsResponse(
109+
new ShardStats[0],
110+
0,
111+
0,
112+
0,
113+
Collections.emptyList()
114+
);
115+
private static final ClusterHealthResponse EMPTY_CLUSTER_HEALTH_RESPONSE = new ClusterHealthResponse();
107116

108117
private final ResponseLimitSettings responseLimitSettings;
109118

@@ -212,6 +221,14 @@ public void onResponse(ClusterStateResponse clusterStateResponse) {
212221
groupedListener.onResponse(getSettingsResponse);
213222
groupedListener.onResponse(clusterStateResponse);
214223

224+
// For paginated queries, if strategy outputs no indices to be returned,
225+
// avoid fetching health and stats.
226+
if (shouldSkipNextRequests(paginationStrategy)) {
227+
groupedListener.onResponse(EMPTY_CLUSTER_HEALTH_RESPONSE);
228+
groupedListener.onResponse(EMPTY_INDICES_STATS_RESPONSE);
229+
return;
230+
}
231+
215232
sendIndicesStatsRequest(
216233
indicesToBeQueried,
217234
subRequestIndicesOptions,
@@ -1093,4 +1110,8 @@ public Tuple<String, Settings> next() {
10931110
};
10941111
}
10951112

1113+
private boolean shouldSkipNextRequests(IndexPaginationStrategy paginationStrategy) {
1114+
return Objects.nonNull(paginationStrategy) && paginationStrategy.getRequestedEntities().isEmpty();
1115+
}
1116+
10961117
}

0 commit comments

Comments
 (0)