12
12
import org .opensearch .action .admin .cluster .state .ClusterStateResponse ;
13
13
import org .opensearch .action .admin .indices .stats .IndicesStatsRequest ;
14
14
import org .opensearch .action .admin .indices .stats .IndicesStatsResponse ;
15
+ import org .opensearch .action .admin .indices .stats .ShardStats ;
15
16
import org .opensearch .action .pagination .PageParams ;
16
17
import org .opensearch .action .pagination .ShardPaginationStrategy ;
17
18
import org .opensearch .action .support .ActionFilters ;
27
28
import org .opensearch .tasks .Task ;
28
29
import org .opensearch .transport .TransportService ;
29
30
31
+ import java .util .Collections ;
30
32
import java .util .Objects ;
31
33
32
34
import static org .opensearch .common .breaker .ResponseLimitSettings .LimitEntity .SHARDS ;
@@ -40,6 +42,13 @@ public class TransportCatShardsAction extends HandledTransportAction<CatShardsRe
40
42
41
43
private final NodeClient client ;
42
44
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
+ );
43
52
44
53
@ Inject
45
54
public TransportCatShardsAction (
@@ -108,6 +117,12 @@ public void onResponse(ClusterStateResponse clusterStateResponse) {
108
117
: paginationStrategy .getRequestedEntities ()
109
118
);
110
119
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
+ }
111
126
IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest ();
112
127
indicesStatsRequest .setShouldCancelOnTimeout (true );
113
128
indicesStatsRequest .all ();
@@ -159,4 +174,8 @@ private void validateRequestLimit(
159
174
}
160
175
}
161
176
}
177
+
178
+ private boolean shouldSkipIndicesStatsRequest (ShardPaginationStrategy paginationStrategy ) {
179
+ return Objects .nonNull (paginationStrategy ) && paginationStrategy .getRequestedEntities ().isEmpty ();
180
+ }
162
181
}
0 commit comments