Skip to content

Commit ff5df05

Browse files
gargharsh3134ruai0511
authored andcommitted
Implementing pagination for _cat/indices API (opensearch-project#14718)
* Implementing pagination for _cat/indices Signed-off-by: Harsh Garg <[email protected]>
1 parent 7e0df0b commit ff5df05

19 files changed

+1355
-69
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1414
- Add successfulSearchShardIndices in searchRequestContext ([#15967](https://github.com/opensearch-project/OpenSearch/pull/15967))
1515
- Remove identity-related feature flagged code from the RestController ([#15430](https://github.com/opensearch-project/OpenSearch/pull/15430))
1616
- Add support for msearch API to pass search pipeline name - ([#15923](https://github.com/opensearch-project/OpenSearch/pull/15923))
17+
- Add _list/indices API as paginated alternate to _cat/indices ([#14718](https://github.com/opensearch-project/OpenSearch/pull/14718))
1718

1819
### Dependencies
1920
- Bump `com.azure:azure-identity` from 1.13.0 to 1.13.2 ([#15578](https://github.com/opensearch-project/OpenSearch/pull/15578))

server/src/main/java/org/opensearch/action/ActionModule.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,9 @@
464464
import org.opensearch.rest.action.ingest.RestGetPipelineAction;
465465
import org.opensearch.rest.action.ingest.RestPutPipelineAction;
466466
import org.opensearch.rest.action.ingest.RestSimulatePipelineAction;
467+
import org.opensearch.rest.action.list.AbstractListAction;
468+
import org.opensearch.rest.action.list.RestIndicesListAction;
469+
import org.opensearch.rest.action.list.RestListAction;
467470
import org.opensearch.rest.action.search.RestClearScrollAction;
468471
import org.opensearch.rest.action.search.RestCountAction;
469472
import org.opensearch.rest.action.search.RestCreatePitAction;
@@ -806,9 +809,14 @@ private ActionFilters setupActionFilters(List<ActionPlugin> actionPlugins) {
806809

807810
public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
808811
List<AbstractCatAction> catActions = new ArrayList<>();
812+
List<AbstractListAction> listActions = new ArrayList<>();
809813
Consumer<RestHandler> registerHandler = handler -> {
810814
if (handler instanceof AbstractCatAction) {
811-
catActions.add((AbstractCatAction) handler);
815+
if (handler instanceof AbstractListAction && ((AbstractListAction) handler).isActionPaginated()) {
816+
listActions.add((AbstractListAction) handler);
817+
} else {
818+
catActions.add((AbstractCatAction) handler);
819+
}
812820
}
813821
restController.registerHandler(handler);
814822
};
@@ -985,6 +993,9 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
985993
}
986994
registerHandler.accept(new RestTemplatesAction());
987995

996+
// LIST API
997+
registerHandler.accept(new RestIndicesListAction());
998+
988999
// Point in time API
9891000
registerHandler.accept(new RestCreatePitAction());
9901001
registerHandler.accept(new RestDeletePitAction());
@@ -1016,6 +1027,7 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
10161027
}
10171028
}
10181029
registerHandler.accept(new RestCatAction(catActions));
1030+
registerHandler.accept(new RestListAction(listActions));
10191031
registerHandler.accept(new RestDecommissionAction());
10201032
registerHandler.accept(new RestGetDecommissionStateAction());
10211033
registerHandler.accept(new RestRemoteStoreStatsAction());

server/src/main/java/org/opensearch/common/Table.java

+15
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import org.opensearch.common.time.DateFormatter;
3636
import org.opensearch.core.common.Strings;
37+
import org.opensearch.rest.pagination.PageToken;
3738

3839
import java.time.Instant;
3940
import java.time.ZoneOffset;
@@ -59,9 +60,19 @@ public class Table {
5960
private List<Cell> currentCells;
6061
private boolean inHeaders = false;
6162
private boolean withTime = false;
63+
/**
64+
* paginatedQueryResponse if null will imply the Table response is not paginated.
65+
*/
66+
private PageToken pageToken;
6267
public static final String EPOCH = "epoch";
6368
public static final String TIMESTAMP = "timestamp";
6469

70+
public Table() {}
71+
72+
public Table(@Nullable PageToken pageToken) {
73+
this.pageToken = pageToken;
74+
}
75+
6576
public Table startHeaders() {
6677
inHeaders = true;
6778
currentCells = new ArrayList<>();
@@ -230,6 +241,10 @@ public Map<String, String> getAliasMap() {
230241
return headerAliasMap;
231242
}
232243

244+
public PageToken getPageToken() {
245+
return pageToken;
246+
}
247+
233248
/**
234249
* Cell in a table
235250
*

server/src/main/java/org/opensearch/rest/RestHandler.java

+7
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ default boolean allowSystemIndexAccessByDefault() {
125125
return false;
126126
}
127127

128+
/**
129+
* Denotes whether the RestHandler will output paginated responses or not.
130+
*/
131+
default boolean isActionPaginated() {
132+
return false;
133+
}
134+
128135
static RestHandler wrapper(RestHandler delegate) {
129136
return new Wrapper(delegate);
130137
}

server/src/main/java/org/opensearch/rest/RestRequest.java

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.opensearch.core.xcontent.XContentParser;
5252
import org.opensearch.http.HttpChannel;
5353
import org.opensearch.http.HttpRequest;
54+
import org.opensearch.rest.pagination.PageParams;
5455

5556
import java.io.IOException;
5657
import java.io.InputStream;
@@ -67,6 +68,9 @@
6768

6869
import static org.opensearch.common.unit.TimeValue.parseTimeValue;
6970
import static org.opensearch.core.common.unit.ByteSizeValue.parseBytesSizeValue;
71+
import static org.opensearch.rest.pagination.PageParams.PARAM_NEXT_TOKEN;
72+
import static org.opensearch.rest.pagination.PageParams.PARAM_SIZE;
73+
import static org.opensearch.rest.pagination.PageParams.PARAM_SORT;
7074

7175
/**
7276
* REST Request
@@ -591,6 +595,10 @@ public static MediaType parseContentType(List<String> header) {
591595
throw new IllegalArgumentException("empty Content-Type header");
592596
}
593597

598+
public PageParams parsePaginatedQueryParams(String defaultSortOrder, int defaultPageSize) {
599+
return new PageParams(param(PARAM_NEXT_TOKEN), param(PARAM_SORT, defaultSortOrder), paramAsInt(PARAM_SIZE, defaultPageSize));
600+
}
601+
594602
/**
595603
* Thrown if there is an error in the content type header.
596604
*

0 commit comments

Comments
 (0)