From c62e5f27793b7ae03ce171b3b51c6553cb5004ab Mon Sep 17 00:00:00 2001 From: Harsh Garg Date: Mon, 11 Nov 2024 10:24:59 +0530 Subject: [PATCH] Fixing _list/shards API for closed indices Signed-off-by: Harsh Garg --- .../shards/TransportCatShardsAction.java | 5 +- .../pagination/ShardPaginationStrategy.java | 50 ++++++++++--------- 2 files changed, 30 insertions(+), 25 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 7b36b7a10f4f2..17243a6d5cce2 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 @@ -16,6 +16,7 @@ import org.opensearch.action.pagination.ShardPaginationStrategy; import org.opensearch.action.support.ActionFilters; import org.opensearch.action.support.HandledTransportAction; +import org.opensearch.action.support.IndicesOptions; import org.opensearch.action.support.TimeoutTaskCancellationUtility; import org.opensearch.client.node.NodeClient; import org.opensearch.common.breaker.ResponseLimitBreachedException; @@ -148,7 +149,9 @@ public void onFailure(Exception e) { } private ShardPaginationStrategy getPaginationStrategy(PageParams pageParams, ClusterStateResponse clusterStateResponse) { - return Objects.isNull(pageParams) ? null : new ShardPaginationStrategy(pageParams, clusterStateResponse.getState()); + return Objects.isNull(pageParams) + ? null + : new ShardPaginationStrategy(pageParams, clusterStateResponse.getState(), IndicesOptions.strictExpandOpenAndForbidClosed()); } private void validateRequestLimit( diff --git a/server/src/main/java/org/opensearch/action/pagination/ShardPaginationStrategy.java b/server/src/main/java/org/opensearch/action/pagination/ShardPaginationStrategy.java index 1eb364c883e60..3fb0821d39224 100644 --- a/server/src/main/java/org/opensearch/action/pagination/ShardPaginationStrategy.java +++ b/server/src/main/java/org/opensearch/action/pagination/ShardPaginationStrategy.java @@ -9,6 +9,7 @@ package org.opensearch.action.pagination; import org.opensearch.OpenSearchParseException; +import org.opensearch.action.support.IndicesOptions; import org.opensearch.cluster.ClusterState; import org.opensearch.cluster.metadata.IndexMetadata; import org.opensearch.cluster.routing.IndexRoutingTable; @@ -37,14 +38,23 @@ public class ShardPaginationStrategy implements PaginationStrategy private PageData pageData; public ShardPaginationStrategy(PageParams pageParams, ClusterState clusterState) { + this(pageParams, clusterState, null); + } + + public ShardPaginationStrategy(PageParams pageParams, ClusterState clusterState, IndicesOptions indicesOptions) { ShardStrategyToken shardStrategyToken = getShardStrategyToken(pageParams.getRequestedToken()); // Get list of indices metadata sorted by their creation time and filtered by the last sent index - List filteredIndices = getEligibleIndices( + List filteredIndices = PaginationStrategy.getSortedIndexMetadata( clusterState, - pageParams.getSort(), - Objects.isNull(shardStrategyToken) ? null : shardStrategyToken.lastIndexName, - Objects.isNull(shardStrategyToken) ? null : shardStrategyToken.lastIndexCreationTime + getMetadataFilter( + pageParams.getSort(), + Objects.isNull(shardStrategyToken) ? null : shardStrategyToken.lastIndexName, + Objects.isNull(shardStrategyToken) ? null : shardStrategyToken.lastIndexCreationTime, + indicesOptions + ), + PageParams.PARAM_ASC_SORT_VALUE.equals(pageParams.getSort()) ? ASC_COMPARATOR : DESC_COMPARATOR ); + // Get the list of shards and indices belonging to current page. this.pageData = getPageData( filteredIndices, @@ -54,39 +64,31 @@ public ShardPaginationStrategy(PageParams pageParams, ClusterState clusterState) ); } - private static List getEligibleIndices( - ClusterState clusterState, + private static Predicate getMetadataFilter( String sortOrder, String lastIndexName, - Long lastIndexCreationTime + Long lastIndexCreationTime, + IndicesOptions indicesOptions ) { if (Objects.isNull(lastIndexName) || Objects.isNull(lastIndexCreationTime)) { - return PaginationStrategy.getSortedIndexMetadata( - clusterState, - PageParams.PARAM_ASC_SORT_VALUE.equals(sortOrder) ? ASC_COMPARATOR : DESC_COMPARATOR - ); - } else { - return PaginationStrategy.getSortedIndexMetadata( - clusterState, - getMetadataFilter(sortOrder, lastIndexName, lastIndexCreationTime), - PageParams.PARAM_ASC_SORT_VALUE.equals(sortOrder) ? ASC_COMPARATOR : DESC_COMPARATOR - ); - } - } - - private static Predicate getMetadataFilter(String sortOrder, String lastIndexName, Long lastIndexCreationTime) { - if (Objects.isNull(lastIndexName) || Objects.isNull(lastIndexCreationTime)) { - return indexMetadata -> true; + return indexStateFilter(indicesOptions); } return indexNameFilter(lastIndexName).or( IndexPaginationStrategy.getIndexCreateTimeFilter(sortOrder, lastIndexName, lastIndexCreationTime) - ); + ).and(indexStateFilter(indicesOptions)); } private static Predicate indexNameFilter(String lastIndexName) { return metadata -> metadata.getIndex().getName().equals(lastIndexName); } + private static Predicate indexStateFilter(IndicesOptions indicesOptions) { + if (Objects.isNull(indicesOptions) || !indicesOptions.forbidClosedIndices()) { + return metadata -> true; + } + return metadata -> metadata.getState().equals(IndexMetadata.State.OPEN); + } + /** * Will be used to get the list of shards and respective indices to which they belong, * which are to be displayed in a page.