Skip to content

Commit c62e5f2

Browse files
author
Harsh Garg
committed
Fixing _list/shards API for closed indices
Signed-off-by: Harsh Garg <[email protected]>
1 parent 9f790ee commit c62e5f2

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.opensearch.action.pagination.ShardPaginationStrategy;
1717
import org.opensearch.action.support.ActionFilters;
1818
import org.opensearch.action.support.HandledTransportAction;
19+
import org.opensearch.action.support.IndicesOptions;
1920
import org.opensearch.action.support.TimeoutTaskCancellationUtility;
2021
import org.opensearch.client.node.NodeClient;
2122
import org.opensearch.common.breaker.ResponseLimitBreachedException;
@@ -148,7 +149,9 @@ public void onFailure(Exception e) {
148149
}
149150

150151
private ShardPaginationStrategy getPaginationStrategy(PageParams pageParams, ClusterStateResponse clusterStateResponse) {
151-
return Objects.isNull(pageParams) ? null : new ShardPaginationStrategy(pageParams, clusterStateResponse.getState());
152+
return Objects.isNull(pageParams)
153+
? null
154+
: new ShardPaginationStrategy(pageParams, clusterStateResponse.getState(), IndicesOptions.strictExpandOpenAndForbidClosed());
152155
}
153156

154157
private void validateRequestLimit(

server/src/main/java/org/opensearch/action/pagination/ShardPaginationStrategy.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package org.opensearch.action.pagination;
1010

1111
import org.opensearch.OpenSearchParseException;
12+
import org.opensearch.action.support.IndicesOptions;
1213
import org.opensearch.cluster.ClusterState;
1314
import org.opensearch.cluster.metadata.IndexMetadata;
1415
import org.opensearch.cluster.routing.IndexRoutingTable;
@@ -37,14 +38,23 @@ public class ShardPaginationStrategy implements PaginationStrategy<ShardRouting>
3738
private PageData pageData;
3839

3940
public ShardPaginationStrategy(PageParams pageParams, ClusterState clusterState) {
41+
this(pageParams, clusterState, null);
42+
}
43+
44+
public ShardPaginationStrategy(PageParams pageParams, ClusterState clusterState, IndicesOptions indicesOptions) {
4045
ShardStrategyToken shardStrategyToken = getShardStrategyToken(pageParams.getRequestedToken());
4146
// Get list of indices metadata sorted by their creation time and filtered by the last sent index
42-
List<IndexMetadata> filteredIndices = getEligibleIndices(
47+
List<IndexMetadata> filteredIndices = PaginationStrategy.getSortedIndexMetadata(
4348
clusterState,
44-
pageParams.getSort(),
45-
Objects.isNull(shardStrategyToken) ? null : shardStrategyToken.lastIndexName,
46-
Objects.isNull(shardStrategyToken) ? null : shardStrategyToken.lastIndexCreationTime
49+
getMetadataFilter(
50+
pageParams.getSort(),
51+
Objects.isNull(shardStrategyToken) ? null : shardStrategyToken.lastIndexName,
52+
Objects.isNull(shardStrategyToken) ? null : shardStrategyToken.lastIndexCreationTime,
53+
indicesOptions
54+
),
55+
PageParams.PARAM_ASC_SORT_VALUE.equals(pageParams.getSort()) ? ASC_COMPARATOR : DESC_COMPARATOR
4756
);
57+
4858
// Get the list of shards and indices belonging to current page.
4959
this.pageData = getPageData(
5060
filteredIndices,
@@ -54,39 +64,31 @@ public ShardPaginationStrategy(PageParams pageParams, ClusterState clusterState)
5464
);
5565
}
5666

57-
private static List<IndexMetadata> getEligibleIndices(
58-
ClusterState clusterState,
67+
private static Predicate<IndexMetadata> getMetadataFilter(
5968
String sortOrder,
6069
String lastIndexName,
61-
Long lastIndexCreationTime
70+
Long lastIndexCreationTime,
71+
IndicesOptions indicesOptions
6272
) {
6373
if (Objects.isNull(lastIndexName) || Objects.isNull(lastIndexCreationTime)) {
64-
return PaginationStrategy.getSortedIndexMetadata(
65-
clusterState,
66-
PageParams.PARAM_ASC_SORT_VALUE.equals(sortOrder) ? ASC_COMPARATOR : DESC_COMPARATOR
67-
);
68-
} else {
69-
return PaginationStrategy.getSortedIndexMetadata(
70-
clusterState,
71-
getMetadataFilter(sortOrder, lastIndexName, lastIndexCreationTime),
72-
PageParams.PARAM_ASC_SORT_VALUE.equals(sortOrder) ? ASC_COMPARATOR : DESC_COMPARATOR
73-
);
74-
}
75-
}
76-
77-
private static Predicate<IndexMetadata> getMetadataFilter(String sortOrder, String lastIndexName, Long lastIndexCreationTime) {
78-
if (Objects.isNull(lastIndexName) || Objects.isNull(lastIndexCreationTime)) {
79-
return indexMetadata -> true;
74+
return indexStateFilter(indicesOptions);
8075
}
8176
return indexNameFilter(lastIndexName).or(
8277
IndexPaginationStrategy.getIndexCreateTimeFilter(sortOrder, lastIndexName, lastIndexCreationTime)
83-
);
78+
).and(indexStateFilter(indicesOptions));
8479
}
8580

8681
private static Predicate<IndexMetadata> indexNameFilter(String lastIndexName) {
8782
return metadata -> metadata.getIndex().getName().equals(lastIndexName);
8883
}
8984

85+
private static Predicate<IndexMetadata> indexStateFilter(IndicesOptions indicesOptions) {
86+
if (Objects.isNull(indicesOptions) || !indicesOptions.forbidClosedIndices()) {
87+
return metadata -> true;
88+
}
89+
return metadata -> metadata.getState().equals(IndexMetadata.State.OPEN);
90+
}
91+
9092
/**
9193
* Will be used to get the list of shards and respective indices to which they belong,
9294
* which are to be displayed in a page.

0 commit comments

Comments
 (0)