Skip to content

Commit e73f0ba

Browse files
Avoid making further transport calls if paginationStrategy outputs empty entities (#16444)
Signed-off-by: Harsh Garg <[email protected]> (cherry picked from commit 9a476b6) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent b771f78 commit e73f0ba

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ public void onResponse(ClusterStateResponse clusterStateResponse) {
108108
: paginationStrategy.getRequestedEntities()
109109
);
110110
catShardsResponse.setPageToken(Objects.isNull(paginationStrategy) ? null : paginationStrategy.getResponseToken());
111+
// For paginated queries, if strategy outputs no shards to be returned, avoid fetching IndicesStats.
112+
if (shouldSkipIndicesStatsRequest(paginationStrategy)) {
113+
catShardsResponse.setIndicesStatsResponse(IndicesStatsResponse.getEmptyResponse());
114+
cancellableListener.onResponse(catShardsResponse);
115+
return;
116+
}
111117
IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest();
112118
indicesStatsRequest.setShouldCancelOnTimeout(true);
113119
indicesStatsRequest.all();
@@ -159,4 +165,8 @@ private void validateRequestLimit(
159165
}
160166
}
161167
}
168+
169+
private boolean shouldSkipIndicesStatsRequest(ShardPaginationStrategy paginationStrategy) {
170+
return Objects.nonNull(paginationStrategy) && paginationStrategy.getRequestedEntities().isEmpty();
171+
}
162172
}

server/src/main/java/org/opensearch/action/admin/indices/stats/IndicesStatsResponse.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.opensearch.core.xcontent.XContentBuilder;
4646

4747
import java.io.IOException;
48+
import java.util.Collections;
4849
import java.util.HashMap;
4950
import java.util.List;
5051
import java.util.Map;
@@ -230,4 +231,8 @@ static final class Fields {
230231
public String toString() {
231232
return Strings.toString(MediaTypeRegistry.JSON, this, true, false);
232233
}
234+
235+
public static IndicesStatsResponse getEmptyResponse() {
236+
return new IndicesStatsResponse(new ShardStats[0], 0, 0, 0, Collections.emptyList());
237+
}
233238
}

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,19 @@ public void onResponse(ClusterStateResponse clusterStateResponse) {
205205
groupedListener.onResponse(getSettingsResponse);
206206
groupedListener.onResponse(clusterStateResponse);
207207

208-
sendIndicesStatsRequest(
209-
indicesToBeQueried,
210-
subRequestIndicesOptions,
211-
includeUnloadedSegments,
212-
client,
213-
ActionListener.wrap(groupedListener::onResponse, groupedListener::onFailure)
214-
);
208+
// For paginated queries, if strategy outputs no indices to be returned,
209+
// avoid fetching indices stats.
210+
if (shouldSkipIndicesStatsRequest(paginationStrategy)) {
211+
groupedListener.onResponse(IndicesStatsResponse.getEmptyResponse());
212+
} else {
213+
sendIndicesStatsRequest(
214+
indicesToBeQueried,
215+
subRequestIndicesOptions,
216+
includeUnloadedSegments,
217+
client,
218+
ActionListener.wrap(groupedListener::onResponse, groupedListener::onFailure)
219+
);
220+
}
215221

216222
sendClusterHealthRequest(
217223
indicesToBeQueried,
@@ -1086,4 +1092,8 @@ public Tuple<String, Settings> next() {
10861092
};
10871093
}
10881094

1095+
private boolean shouldSkipIndicesStatsRequest(IndexPaginationStrategy paginationStrategy) {
1096+
return Objects.nonNull(paginationStrategy) && paginationStrategy.getRequestedEntities().isEmpty();
1097+
}
1098+
10891099
}

0 commit comments

Comments
 (0)