Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing pagination for _cat/indices API #14718

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected RestChannelConsumer doCatRequest(final RestRequest request, final Node
}

@Override
protected void documentation(StringBuilder sb) {
public void documentation(StringBuilder sb) {
sb.append(documentation());
}

Expand Down
13 changes: 12 additions & 1 deletion server/src/main/java/org/opensearch/action/ActionModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@
import org.opensearch.rest.action.ingest.RestGetPipelineAction;
import org.opensearch.rest.action.ingest.RestPutPipelineAction;
import org.opensearch.rest.action.ingest.RestSimulatePipelineAction;
import org.opensearch.rest.action.list.RestIndicesListAction;
import org.opensearch.rest.action.list.RestListAction;
import org.opensearch.rest.action.search.RestClearScrollAction;
import org.opensearch.rest.action.search.RestCountAction;
import org.opensearch.rest.action.search.RestCreatePitAction;
Expand Down Expand Up @@ -799,9 +801,14 @@ private ActionFilters setupActionFilters(List<ActionPlugin> actionPlugins) {

public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
List<AbstractCatAction> catActions = new ArrayList<>();
List<AbstractCatAction> listActions = new ArrayList<>();
Consumer<RestHandler> registerHandler = handler -> {
if (handler instanceof AbstractCatAction) {
catActions.add((AbstractCatAction) handler);
if (((AbstractCatAction) handler).isActionPaginated()) {
listActions.add((AbstractCatAction) handler);
} else {
catActions.add((AbstractCatAction) handler);
}
}
restController.registerHandler(handler);
};
Expand Down Expand Up @@ -977,6 +984,9 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
}
registerHandler.accept(new RestTemplatesAction());

// LIST API
registerHandler.accept(new RestIndicesListAction());

// Point in time API
registerHandler.accept(new RestCreatePitAction());
registerHandler.accept(new RestDeletePitAction());
Expand Down Expand Up @@ -1008,6 +1018,7 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
}
}
registerHandler.accept(new RestCatAction(catActions));
registerHandler.accept(new RestListAction(listActions));
registerHandler.accept(new RestDecommissionAction());
registerHandler.accept(new RestGetDecommissionStateAction());
registerHandler.accept(new RestRemoteStoreStatsAction());
Expand Down
15 changes: 15 additions & 0 deletions server/src/main/java/org/opensearch/common/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import org.opensearch.common.time.DateFormatter;
import org.opensearch.core.common.Strings;
import org.opensearch.rest.pagination.PaginatedQueryResponse;

import java.time.Instant;
import java.time.ZoneOffset;
Expand All @@ -59,9 +60,19 @@ public class Table {
private List<Cell> currentCells;
private boolean inHeaders = false;
private boolean withTime = false;
/**
* paginatedQueryResponse if null will imply the Table response is not paginated.
*/
private PaginatedQueryResponse paginatedQueryResponse;
public static final String EPOCH = "epoch";
public static final String TIMESTAMP = "timestamp";

public Table() {}

public Table(@Nullable PaginatedQueryResponse paginatedQueryResponse) {
this.paginatedQueryResponse = paginatedQueryResponse;
}

public Table startHeaders() {
inHeaders = true;
currentCells = new ArrayList<>();
Expand Down Expand Up @@ -230,6 +241,10 @@ public Map<String, String> getAliasMap() {
return headerAliasMap;
}

public PaginatedQueryResponse getPaginatedQueryResponse() {
return paginatedQueryResponse;
}

/**
* Cell in a table
*
Expand Down
12 changes: 12 additions & 0 deletions server/src/main/java/org/opensearch/rest/RestRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.http.HttpChannel;
import org.opensearch.http.HttpRequest;
import org.opensearch.rest.pagination.PaginatedQueryRequest;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -67,6 +68,9 @@

import static org.opensearch.common.unit.TimeValue.parseTimeValue;
import static org.opensearch.core.common.unit.ByteSizeValue.parseBytesSizeValue;
import static org.opensearch.rest.pagination.PaginatedQueryRequest.PAGINATED_QUERY_PARAM_NEXT_TOKEN_KEY;
import static org.opensearch.rest.pagination.PaginatedQueryRequest.PAGINATED_QUERY_PARAM_SIZE_KEY;
import static org.opensearch.rest.pagination.PaginatedQueryRequest.PAGINATED_QUERY_PARAM_SORT_KEY;

/**
* REST Request
Expand Down Expand Up @@ -591,6 +595,14 @@ public static MediaType parseContentType(List<String> header) {
throw new IllegalArgumentException("empty Content-Type header");
}

public PaginatedQueryRequest parsePaginatedQueryParams(String defaultSortOrder, int defaultPageSize) {
return new PaginatedQueryRequest(
param(PAGINATED_QUERY_PARAM_NEXT_TOKEN_KEY),
param(PAGINATED_QUERY_PARAM_SORT_KEY, defaultSortOrder),
paramAsInt(PAGINATED_QUERY_PARAM_SIZE_KEY, defaultPageSize)
);
}

/**
* Thrown if there is an error in the content type header.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.pagination.PaginatedQueryRequest;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import static org.opensearch.rest.action.cat.RestTable.buildHelpWidths;
Expand All @@ -57,9 +59,11 @@
*/
public abstract class AbstractCatAction extends BaseRestHandler {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we want to change AbstractCatAction at all? we can keep the changes in AbstractListAction?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added AbstractListAction which is now extending AbstractCatAction as discussed, please see if it resolves the concern now.


protected PaginatedQueryRequest paginatedQueryRequest;

protected abstract RestChannelConsumer doCatRequest(RestRequest request, NodeClient client);

protected abstract void documentation(StringBuilder sb);
public abstract void documentation(StringBuilder sb);

protected abstract Table getTableWithHeader(RestRequest request);

Expand All @@ -85,6 +89,10 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
channel.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, bytesOutput.bytes()));
};
} else {
if (isActionPaginated()) {
this.paginatedQueryRequest = validateAndGetPaginationMetadata(request);
assert Objects.nonNull(paginatedQueryRequest) : "paginatedQueryRequest can not be null for paginated queries";
}
return doCatRequest(request, client);
}
}
Expand All @@ -98,4 +106,23 @@ protected Set<String> responseParams() {
return RESPONSE_PARAMS;
}

/**
*
* @return boolean denoting whether the RestAction will output paginated responses or not.
* Is kept false by default, every paginated action to override and return true.
*/
public boolean isActionPaginated() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to add AbstractListAction instead of adding in AbstractCatAction as list are separate APIs anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in the revision.

return false;
}

/**
*
* @return Metadata that can be extracted out from the rest request. Each paginated action to override and provide
* its own implementation. Query params supported by the action specific to pagination along with the respective validations,
* should be added here.
*/
protected PaginatedQueryRequest validateAndGetPaginationMetadata(RestRequest restRequest) {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public RestResponse buildResponse(GetAliasesResponse response) throws Exception
}

@Override
protected void documentation(StringBuilder sb) {
public void documentation(StringBuilder sb) {
sb.append("/_cat/aliases\n");
sb.append("/_cat/aliases/{alias}\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public String getName() {
}

@Override
protected void documentation(StringBuilder sb) {
public void documentation(StringBuilder sb) {
sb.append("/_cat/allocation\n");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public String getName() {
}

@Override
protected void documentation(StringBuilder sb) {
public void documentation(StringBuilder sb) {
sb.append("/_cat/recovery\n");
sb.append("/_cat/recovery/{index}\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public String getName() {
}

@Override
protected void documentation(StringBuilder sb) {
public void documentation(StringBuilder sb) {
sb.append("/_cat/segment_replication\n");
sb.append("/_cat/segment_replication/{index}\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public String getName() {
}

@Override
protected void documentation(StringBuilder sb) {
public void documentation(StringBuilder sb) {
sb.append("/_cat/cluster_manager\n");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public String getName() {
}

@Override
protected void documentation(StringBuilder sb) {
public void documentation(StringBuilder sb) {
sb.append("/_cat/count\n");
sb.append("/_cat/count/{index}\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public RestResponse buildResponse(NodesStatsResponse nodeStatses) throws Excepti
}

@Override
protected void documentation(StringBuilder sb) {
public void documentation(StringBuilder sb) {
sb.append("/_cat/fielddata\n");
sb.append("/_cat/fielddata/{fields}\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public boolean allowSystemIndexAccessByDefault() {
}

@Override
protected void documentation(StringBuilder sb) {
public void documentation(StringBuilder sb) {
sb.append("/_cat/health\n");
}

Expand Down
Loading
Loading