Skip to content

Commit eaeebbb

Browse files
gargharsh3134Harsh Garg
authored andcommitted
Implementing pagination for _cat/indices API (#14718)
* Implementing pagination for _cat/indices Signed-off-by: Harsh Garg <[email protected]>
1 parent 704e498 commit eaeebbb

19 files changed

+1355
-69
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1616
- [S3 Repository] Change default retry mechanism of s3 clients to Standard Mode ([#15978](https://github.com/opensearch-project/OpenSearch/pull/15978))
1717
- Add new metric REMOTE_STORE to NodeStats API response ([#15611](https://github.com/opensearch-project/OpenSearch/pull/15611))
1818
- New `phone` & `phone-search` analyzer + tokenizer ([#15915](https://github.com/opensearch-project/OpenSearch/pull/15915))
19+
- Add _list/indices API as paginated alternate to _cat/indices ([#14718](https://github.com/opensearch-project/OpenSearch/pull/14718))
1920

2021
### Dependencies
2122
- Bump `org.apache.logging.log4j:log4j-core` from 2.23.1 to 2.24.0 ([#15858](https://github.com/opensearch-project/OpenSearch/pull/15858))

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,9 @@
454454
import org.opensearch.rest.action.ingest.RestGetPipelineAction;
455455
import org.opensearch.rest.action.ingest.RestPutPipelineAction;
456456
import org.opensearch.rest.action.ingest.RestSimulatePipelineAction;
457+
import org.opensearch.rest.action.list.AbstractListAction;
458+
import org.opensearch.rest.action.list.RestIndicesListAction;
459+
import org.opensearch.rest.action.list.RestListAction;
457460
import org.opensearch.rest.action.search.RestClearScrollAction;
458461
import org.opensearch.rest.action.search.RestCountAction;
459462
import org.opensearch.rest.action.search.RestCreatePitAction;
@@ -787,9 +790,14 @@ private ActionFilters setupActionFilters(List<ActionPlugin> actionPlugins) {
787790

788791
public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
789792
List<AbstractCatAction> catActions = new ArrayList<>();
793+
List<AbstractListAction> listActions = new ArrayList<>();
790794
Consumer<RestHandler> registerHandler = handler -> {
791795
if (handler instanceof AbstractCatAction) {
792-
catActions.add((AbstractCatAction) handler);
796+
if (handler instanceof AbstractListAction && ((AbstractListAction) handler).isActionPaginated()) {
797+
listActions.add((AbstractListAction) handler);
798+
} else {
799+
catActions.add((AbstractCatAction) handler);
800+
}
793801
}
794802
restController.registerHandler(handler);
795803
};
@@ -957,6 +965,9 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
957965
}
958966
registerHandler.accept(new RestTemplatesAction());
959967

968+
// LIST API
969+
registerHandler.accept(new RestIndicesListAction());
970+
960971
// Point in time API
961972
registerHandler.accept(new RestCreatePitAction());
962973
registerHandler.accept(new RestDeletePitAction());
@@ -988,6 +999,7 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
988999
}
9891000
}
9901001
registerHandler.accept(new RestCatAction(catActions));
1002+
registerHandler.accept(new RestListAction(listActions));
9911003
registerHandler.accept(new RestDecommissionAction());
9921004
registerHandler.accept(new RestGetDecommissionStateAction());
9931005
registerHandler.accept(new RestRemoteStoreStatsAction());

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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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)