-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding RestIndicesListAction along with RestListAction changes
Signed-off-by: Harsh Garg <gkharsh@amazon.com>
- Loading branch information
Harsh Garg
committed
Aug 29, 2024
1 parent
0cfb0e9
commit d665722
Showing
12 changed files
with
1,210 additions
and
940 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1,545 changes: 736 additions & 809 deletions
1,545
server/src/main/java/org/opensearch/rest/action/cat/RestIndicesAction.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
server/src/main/java/org/opensearch/rest/action/list/AbstractListAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.rest.action.list; | ||
|
||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.common.Table; | ||
import org.opensearch.common.io.Streams; | ||
import org.opensearch.common.io.UTF8StreamWriter; | ||
import org.opensearch.core.common.io.stream.BytesStream; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.rest.BaseRestHandler; | ||
import org.opensearch.rest.BytesRestResponse; | ||
import org.opensearch.rest.RestRequest; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import static org.opensearch.rest.action.cat.RestTable.buildHelpWidths; | ||
import static org.opensearch.rest.action.cat.RestTable.pad; | ||
|
||
/** | ||
* Base Transport action class for _list APIs | ||
* | ||
* @opensearch.api | ||
*/ | ||
public abstract class AbstractListAction extends BaseRestHandler { | ||
protected abstract RestChannelConsumer doListRequest(RestRequest request, NodeClient client); | ||
|
||
protected abstract void documentation(StringBuilder sb); | ||
|
||
protected abstract Table getTableWithHeader(RestRequest request); | ||
|
||
@Override | ||
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { | ||
boolean helpWanted = request.paramAsBoolean("help", false); | ||
if (helpWanted) { | ||
return channel -> { | ||
Table table = getTableWithHeader(request); | ||
int[] width = buildHelpWidths(table, request); | ||
BytesStream bytesOutput = Streams.flushOnCloseStream(channel.bytesOutput()); | ||
UTF8StreamWriter out = new UTF8StreamWriter().setOutput(bytesOutput); | ||
for (Table.Cell cell : table.getHeaders()) { | ||
// need to do left-align always, so create new cells | ||
pad(new Table.Cell(cell.value), width[0], request, out); | ||
out.append(" | "); | ||
pad(new Table.Cell(cell.attr.containsKey("alias") ? cell.attr.get("alias") : ""), width[1], request, out); | ||
out.append(" | "); | ||
pad(new Table.Cell(cell.attr.containsKey("desc") ? cell.attr.get("desc") : "not available"), width[2], request, out); | ||
out.append("\n"); | ||
} | ||
out.close(); | ||
channel.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, bytesOutput.bytes())); | ||
}; | ||
} else { | ||
return doListRequest(request, client); | ||
} | ||
} | ||
|
||
static Set<String> RESPONSE_PARAMS = Collections.unmodifiableSet( | ||
new HashSet<>(Arrays.asList("format", "h", "v", "ts", "pri", "bytes", "size", "time", "s", "timeout")) | ||
); | ||
|
||
@Override | ||
protected Set<String> responseParams() { | ||
return RESPONSE_PARAMS; | ||
} | ||
|
||
} |
175 changes: 175 additions & 0 deletions
175
server/src/main/java/org/opensearch/rest/action/list/RestIndicesListAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.rest.action.list; | ||
|
||
import org.opensearch.action.admin.cluster.state.ClusterStateResponse; | ||
import org.opensearch.action.support.GroupedActionListener; | ||
import org.opensearch.action.support.IndicesOptions; | ||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.common.Table; | ||
import org.opensearch.common.unit.TimeValue; | ||
import org.opensearch.core.action.ActionListener; | ||
import org.opensearch.core.action.ActionResponse; | ||
import org.opensearch.core.common.Strings; | ||
import org.opensearch.rest.RestRequest; | ||
import org.opensearch.rest.RestResponse; | ||
import org.opensearch.rest.action.RestResponseListener; | ||
import org.opensearch.rest.action.cat.RestIndicesAction; | ||
import org.opensearch.rest.action.cat.RestTable; | ||
import org.opensearch.rest.pagination.IndexBasedPaginationStrategy; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static java.util.Arrays.asList; | ||
import static java.util.Collections.unmodifiableList; | ||
import static org.opensearch.action.support.clustermanager.ClusterManagerNodeRequest.DEFAULT_CLUSTER_MANAGER_NODE_TIMEOUT; | ||
import static org.opensearch.rest.RestRequest.Method.GET; | ||
|
||
/** | ||
* _list API action to output indices in pages. | ||
* | ||
* @opensearch.api | ||
*/ | ||
public class RestIndicesListAction extends AbstractListAction { | ||
private static final String DEFAULT_LIST_INDICES_PAGE_SIZE_STRING = "1000"; | ||
private static final String PAGINATED_ELEMENT_KEY = "indices"; | ||
|
||
@Override | ||
public List<Route> routes() { | ||
return unmodifiableList(asList(new Route(GET, "/_list/indices"), new Route(GET, "/_list/indices/{index}"))); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "list_indices_action"; | ||
} | ||
|
||
@Override | ||
public boolean allowSystemIndexAccessByDefault() { | ||
return true; | ||
} | ||
|
||
@Override | ||
protected void documentation(StringBuilder sb) { | ||
sb.append("/_list/indices\n"); | ||
sb.append("/_list/indices/{index}\n"); | ||
} | ||
|
||
@Override | ||
public RestChannelConsumer doListRequest(final RestRequest request, final NodeClient client) { | ||
final String[] indices = Strings.splitStringByCommaToArray(request.param("index")); | ||
final boolean local = request.paramAsBoolean("local", false); | ||
TimeValue clusterManagerTimeout = request.paramAsTime("cluster_manager_timeout", DEFAULT_CLUSTER_MANAGER_NODE_TIMEOUT); | ||
final boolean includeUnloadedSegments = request.paramAsBoolean("include_unloaded_segments", false); | ||
final String requestedToken = request.param("next_token"); | ||
final int pageSize = Integer.parseInt(request.param("size", DEFAULT_LIST_INDICES_PAGE_SIZE_STRING)); | ||
final String requestedSortOrder = request.param("sort", "ascending"); | ||
|
||
return channel -> { | ||
final ActionListener<Table> listener = ActionListener.notifyOnce(new RestResponseListener<Table>(channel) { | ||
@Override | ||
public RestResponse buildResponse(final Table table) throws Exception { | ||
return RestTable.buildResponse(table, channel); | ||
} | ||
}); | ||
|
||
// Fetch all the indices from clusterStateRequest for a paginated query. | ||
RestIndicesAction.RestIndicesActionCommonUtils.sendClusterStateRequest( | ||
indices, | ||
IndicesOptions.lenientExpandHidden(), | ||
local, | ||
clusterManagerTimeout, | ||
client, | ||
new ActionListener<ClusterStateResponse>() { | ||
@Override | ||
public void onResponse(final ClusterStateResponse clusterStateResponse) { | ||
try { | ||
IndexBasedPaginationStrategy paginationStrategy = new IndexBasedPaginationStrategy( | ||
requestedToken == null ? null : new IndexBasedPaginationStrategy.IndexStrategyPageToken(requestedToken), | ||
pageSize, | ||
requestedSortOrder, | ||
clusterStateResponse.getState() | ||
); | ||
|
||
final GroupedActionListener<ActionResponse> groupedListener = RestIndicesAction.RestIndicesActionCommonUtils | ||
.createGroupedListener( | ||
request, | ||
4, | ||
listener, | ||
new Table.PaginationMetadata( | ||
true, | ||
PAGINATED_ELEMENT_KEY, | ||
paginationStrategy.getNextToken() == null | ||
? null | ||
: paginationStrategy.getNextToken().generateEncryptedToken() | ||
) | ||
); | ||
groupedListener.onResponse(clusterStateResponse); | ||
|
||
final String[] indicesToBeQueried = paginationStrategy.getElementsFromRequestedToken().toArray(new String[0]); | ||
RestIndicesAction.RestIndicesActionCommonUtils.sendGetSettingsRequest( | ||
indicesToBeQueried, | ||
IndicesOptions.fromRequest(request, IndicesOptions.strictExpand()), | ||
local, | ||
clusterManagerTimeout, | ||
client, | ||
ActionListener.wrap(groupedListener::onResponse, groupedListener::onFailure) | ||
); | ||
RestIndicesAction.RestIndicesActionCommonUtils.sendIndicesStatsRequest( | ||
indicesToBeQueried, | ||
IndicesOptions.lenientExpandHidden(), | ||
includeUnloadedSegments, | ||
client, | ||
ActionListener.wrap(groupedListener::onResponse, groupedListener::onFailure) | ||
); | ||
RestIndicesAction.RestIndicesActionCommonUtils.sendClusterHealthRequest( | ||
indicesToBeQueried, | ||
IndicesOptions.lenientExpandHidden(), | ||
local, | ||
clusterManagerTimeout, | ||
client, | ||
ActionListener.wrap(groupedListener::onResponse, groupedListener::onFailure) | ||
); | ||
} catch (Exception e) { | ||
onFailure(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void onFailure(final Exception e) { | ||
listener.onFailure(e); | ||
} | ||
} | ||
); | ||
}; | ||
|
||
} | ||
|
||
private static final Set<String> RESPONSE_PARAMS; | ||
|
||
static { | ||
final Set<String> responseParams = new HashSet<>(asList("local", "health")); | ||
responseParams.addAll(AbstractListAction.RESPONSE_PARAMS); | ||
RESPONSE_PARAMS = Collections.unmodifiableSet(responseParams); | ||
} | ||
|
||
@Override | ||
protected Set<String> responseParams() { | ||
return RESPONSE_PARAMS; | ||
} | ||
|
||
@Override | ||
protected Table getTableWithHeader(final RestRequest request) { | ||
return RestIndicesAction.RestIndicesActionCommonUtils.getTableWithHeader(request, null); | ||
} | ||
|
||
} |
Oops, something went wrong.