From 2099d929666eb6a68f28536ef84c91cd27e42570 Mon Sep 17 00:00:00 2001 From: Harsh Garg Date: Wed, 23 Oct 2024 15:33:39 +0530 Subject: [PATCH] Moving empty IndicesStats creation to Response class Signed-off-by: Harsh Garg --- .../admin/cluster/shards/TransportCatShardsAction.java | 2 +- .../action/admin/indices/stats/IndicesStatsResponse.java | 5 +++++ .../org/opensearch/rest/action/cat/RestIndicesAction.java | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/opensearch/action/admin/cluster/shards/TransportCatShardsAction.java b/server/src/main/java/org/opensearch/action/admin/cluster/shards/TransportCatShardsAction.java index 1ac4d6068725b..abe1b94a668cf 100644 --- a/server/src/main/java/org/opensearch/action/admin/cluster/shards/TransportCatShardsAction.java +++ b/server/src/main/java/org/opensearch/action/admin/cluster/shards/TransportCatShardsAction.java @@ -119,7 +119,7 @@ public void onResponse(ClusterStateResponse clusterStateResponse) { 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); + catShardsResponse.setIndicesStatsResponse(IndicesStatsResponse.getEmptyResponse()); cancellableListener.onResponse(catShardsResponse); return; } diff --git a/server/src/main/java/org/opensearch/action/admin/indices/stats/IndicesStatsResponse.java b/server/src/main/java/org/opensearch/action/admin/indices/stats/IndicesStatsResponse.java index ae989573b39ea..a5409e076730d 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/stats/IndicesStatsResponse.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/stats/IndicesStatsResponse.java @@ -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; @@ -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()); + } } diff --git a/server/src/main/java/org/opensearch/rest/action/cat/RestIndicesAction.java b/server/src/main/java/org/opensearch/rest/action/cat/RestIndicesAction.java index 805ea9130aff1..067498c756d46 100644 --- a/server/src/main/java/org/opensearch/rest/action/cat/RestIndicesAction.java +++ b/server/src/main/java/org/opensearch/rest/action/cat/RestIndicesAction.java @@ -223,7 +223,7 @@ public void onResponse(ClusterStateResponse clusterStateResponse) { // For paginated queries, if strategy outputs no indices to be returned, // avoid fetching indices stats. if (shouldSkipIndicesStatsRequest(paginationStrategy)) { - groupedListener.onResponse(EMPTY_INDICES_STATS_RESPONSE); + groupedListener.onResponse(IndicesStatsResponse.getEmptyResponse()); } else { sendIndicesStatsRequest( indicesToBeQueried,