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 (#16444) (#16456)

(cherry picked from commit 9a476b6)

Signed-off-by: Harsh Garg <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent b771f78 commit 9b72ec4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,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(IndicesStatsResponse.getEmptyResponse());
cancellableListener.onResponse(catShardsResponse);
return;
}
IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest();
indicesStatsRequest.setShouldCancelOnTimeout(true);
indicesStatsRequest.all();
Expand Down Expand Up @@ -159,4 +165,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 @@ -45,6 +45,7 @@
import org.opensearch.core.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -230,4 +231,8 @@ static final class Fields {
public String toString() {
return Strings.toString(MediaTypeRegistry.JSON, this, true, false);
}

public static IndicesStatsResponse getEmptyResponse() {
return new IndicesStatsResponse(new ShardStats[0], 0, 0, 0, Collections.emptyList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,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(IndicesStatsResponse.getEmptyResponse());
} else {
sendIndicesStatsRequest(
indicesToBeQueried,
subRequestIndicesOptions,
includeUnloadedSegments,
client,
ActionListener.wrap(groupedListener::onResponse, groupedListener::onFailure)
);
}

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

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

}

0 comments on commit 9b72ec4

Please sign in to comment.