From 57cc2cdcfcff164f8ca2cf2ae388a32e47f0ee11 Mon Sep 17 00:00:00 2001 From: Bharathwaj G Date: Fri, 2 Sep 2022 11:21:13 +0530 Subject: [PATCH] Adding rest tests Signed-off-by: Bharathwaj G --- CHANGELOG.md | 1 + .../opensearch/client/RequestConverters.java | 2 +- .../client/{Pit1IT.java => PitIT.java} | 4 +- .../rest-api-spec/api/get_all_pits.json | 19 +++++++ .../rest-api-spec/test/pit/10_basic.yml | 12 +++++ .../action/search/GetAllPitNodesResponse.java | 2 +- .../java/org/opensearch/client/Client.java | 4 ++ .../client/support/AbstractClient.java | 8 +++ .../action/search/RestGetAllPitsAction.java | 53 ++++++++----------- .../search/pit/RestGetAllPitsActionTests.java | 45 ++++++++++++++++ 10 files changed, 114 insertions(+), 36 deletions(-) rename client/rest-high-level/src/test/java/org/opensearch/client/{Pit1IT.java => PitIT.java} (97%) create mode 100644 rest-api-spec/src/main/resources/rest-api-spec/api/get_all_pits.json create mode 100644 server/src/test/java/org/opensearch/search/pit/RestGetAllPitsActionTests.java diff --git a/CHANGELOG.md b/CHANGELOG.md index f89e7eba0698c..f517092c02d04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Added - Github workflow for changelog verification ([#4085](https://github.com/opensearch-project/OpenSearch/pull/4085)) - Point in time rest layer changes for create and delete PIT API ([#4064](https://github.com/opensearch-project/OpenSearch/pull/4064)) +- Point in time rest layer changes for list PIT and PIT segments API ([#4388](https://github.com/opensearch-project/OpenSearch/pull/4388)) - Added @dreamer-89 as an Opensearch maintainer ([#4342](https://github.com/opensearch-project/OpenSearch/pull/4342)) - Added release notes for 1.3.5 ([#4343](https://github.com/opensearch-project/OpenSearch/pull/4343)) - Added release notes for 2.2.1 ([#4344](https://github.com/opensearch-project/OpenSearch/pull/4344)) diff --git a/client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java b/client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java index 41391ed6f1c07..91c339cc92c1b 100644 --- a/client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java +++ b/client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java @@ -499,7 +499,7 @@ static Request deleteAllPits() { } static Request getAllPits() { - return new Request(HttpGet.METHOD_NAME, "/_search/point_in_time/all"); + return new Request(HttpGet.METHOD_NAME, "/_search/point_in_time/_all"); } static Request multiSearch(MultiSearchRequest multiSearchRequest) throws IOException { diff --git a/client/rest-high-level/src/test/java/org/opensearch/client/Pit1IT.java b/client/rest-high-level/src/test/java/org/opensearch/client/PitIT.java similarity index 97% rename from client/rest-high-level/src/test/java/org/opensearch/client/Pit1IT.java rename to client/rest-high-level/src/test/java/org/opensearch/client/PitIT.java index 240696a6cd65b..69bec31d632e9 100644 --- a/client/rest-high-level/src/test/java/org/opensearch/client/Pit1IT.java +++ b/client/rest-high-level/src/test/java/org/opensearch/client/PitIT.java @@ -30,7 +30,7 @@ /** * Tests point in time API with rest high level client */ -public class Pit1IT extends OpenSearchRestHighLevelClientTestCase { +public class PitIT extends OpenSearchRestHighLevelClientTestCase { @Before public void indexDocuments() throws IOException { @@ -124,7 +124,7 @@ public void onFailure(Exception e) { highLevelClient().getAllPitsAsync(RequestOptions.DEFAULT, getPitsListener); highLevelClient().deleteAllPitsAsync(RequestOptions.DEFAULT, deletePitListener); // validate no pits case - //highLevelClient().getAllPitsAsync(RequestOptions.DEFAULT, getPitsListener); + highLevelClient().getAllPitsAsync(RequestOptions.DEFAULT, getPitsListener); highLevelClient().deleteAllPitsAsync(RequestOptions.DEFAULT, deletePitListener); } } diff --git a/rest-api-spec/src/main/resources/rest-api-spec/api/get_all_pits.json b/rest-api-spec/src/main/resources/rest-api-spec/api/get_all_pits.json new file mode 100644 index 0000000000000..544a8cb11b002 --- /dev/null +++ b/rest-api-spec/src/main/resources/rest-api-spec/api/get_all_pits.json @@ -0,0 +1,19 @@ +{ + "get_all_pits":{ + "documentation":{ + "url":"https://opensearch.org/docs/latest/opensearch/rest-api/point_in_time/", + "description":"Lists all active point in time searches." + }, + "stability":"stable", + "url":{ + "paths":[ + { + "path":"/_search/point_in_time/_all", + "methods":[ + "GET" + ] + } + ] + } + } +} diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/pit/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/pit/10_basic.yml index 2023bcc8f5c87..cd0c5b9126a9d 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/pit/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/pit/10_basic.yml @@ -79,6 +79,12 @@ - match: {hits.total: 3 } - length: {hits.hits: 1 } + - do: + get_all_pits: {} + + - match: {pits.0.pit_id: $pit_id} + - match: {pits.0.keep_alive: 82800000 } + - do: delete_pit: body: @@ -119,6 +125,12 @@ - set: {pit_id: pit_id} - match: { _shards.failed: 0} + - do: + get_all_pits: {} + + - match: {pits.0.pit_id: $pit_id} + - match: {pits.0.keep_alive: 82800000 } + - do: delete_all_pits: {} diff --git a/server/src/main/java/org/opensearch/action/search/GetAllPitNodesResponse.java b/server/src/main/java/org/opensearch/action/search/GetAllPitNodesResponse.java index be5421f019c8b..9543c5ae01297 100644 --- a/server/src/main/java/org/opensearch/action/search/GetAllPitNodesResponse.java +++ b/server/src/main/java/org/opensearch/action/search/GetAllPitNodesResponse.java @@ -65,7 +65,7 @@ public GetAllPitNodesResponse(List listPitInfos, GetAllPitNodesResp pitInfos.addAll(listPitInfos); } - public GetAllPitNodesResponse( + public GetAllPitNodesResponse( List listPitInfos, ClusterName clusterName, List getAllPitNodeResponse, diff --git a/server/src/main/java/org/opensearch/client/Client.java b/server/src/main/java/org/opensearch/client/Client.java index 94043d5c3c89f..d996cbfbe6e89 100644 --- a/server/src/main/java/org/opensearch/client/Client.java +++ b/server/src/main/java/org/opensearch/client/Client.java @@ -64,6 +64,8 @@ import org.opensearch.action.search.CreatePitResponse; import org.opensearch.action.search.DeletePitRequest; import org.opensearch.action.search.DeletePitResponse; +import org.opensearch.action.search.GetAllPitNodesRequest; +import org.opensearch.action.search.GetAllPitNodesResponse; import org.opensearch.action.search.MultiSearchRequest; import org.opensearch.action.search.MultiSearchRequestBuilder; import org.opensearch.action.search.MultiSearchResponse; @@ -341,6 +343,8 @@ public interface Client extends OpenSearchClient, Releasable { */ void deletePits(DeletePitRequest deletePITRequest, ActionListener listener); + void getAllPits(GetAllPitNodesRequest getAllPitNodesRequest, ActionListener listener); + /** * Get information of segments of one or more PITs */ diff --git a/server/src/main/java/org/opensearch/client/support/AbstractClient.java b/server/src/main/java/org/opensearch/client/support/AbstractClient.java index bc80a2ba92bf8..b20d5432d17b7 100644 --- a/server/src/main/java/org/opensearch/client/support/AbstractClient.java +++ b/server/src/main/java/org/opensearch/client/support/AbstractClient.java @@ -335,6 +335,9 @@ import org.opensearch.action.search.DeletePitAction; import org.opensearch.action.search.DeletePitRequest; import org.opensearch.action.search.DeletePitResponse; +import org.opensearch.action.search.GetAllPitNodesRequest; +import org.opensearch.action.search.GetAllPitNodesResponse; +import org.opensearch.action.search.GetAllPitsAction; import org.opensearch.action.search.MultiSearchAction; import org.opensearch.action.search.MultiSearchRequest; import org.opensearch.action.search.MultiSearchRequestBuilder; @@ -595,6 +598,11 @@ public void deletePits(final DeletePitRequest deletePITRequest, final ActionList execute(DeletePitAction.INSTANCE, deletePITRequest, listener); } + @Override + public void getAllPits(final GetAllPitNodesRequest getAllPitNodesRequest, final ActionListener listener) { + execute(GetAllPitsAction.INSTANCE, getAllPitNodesRequest, listener); + } + @Override public void pitSegments(final PitSegmentsRequest request, final ActionListener listener) { execute(PitSegmentsAction.INSTANCE, request, listener); diff --git a/server/src/main/java/org/opensearch/rest/action/search/RestGetAllPitsAction.java b/server/src/main/java/org/opensearch/rest/action/search/RestGetAllPitsAction.java index 3ef3a20ffc9a7..687ecddc1021d 100644 --- a/server/src/main/java/org/opensearch/rest/action/search/RestGetAllPitsAction.java +++ b/server/src/main/java/org/opensearch/rest/action/search/RestGetAllPitsAction.java @@ -9,10 +9,8 @@ package org.opensearch.rest.action.search; -import org.opensearch.action.admin.cluster.state.ClusterStateRequest; import org.opensearch.action.search.GetAllPitNodesRequest; import org.opensearch.action.search.GetAllPitNodesResponse; -import org.opensearch.action.search.GetAllPitsAction; import org.opensearch.client.node.NodeClient; import org.opensearch.common.xcontent.XContentBuilder; import org.opensearch.rest.BaseRestHandler; @@ -40,45 +38,36 @@ public String getName() { @Override protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { - final ClusterStateRequest clusterStateRequest = new ClusterStateRequest(); - clusterStateRequest.local(false); - boolean includeAll = request.paramAsBoolean("include_all", false); - clusterStateRequest.clusterManagerNodeTimeout( - request.paramAsTime("cluster_manager_timeout", clusterStateRequest.clusterManagerNodeTimeout()) - ); - clusterStateRequest.clear().nodes(true).routingTable(true).indices("*"); GetAllPitNodesRequest getAllPITNodesRequest = new GetAllPitNodesRequest(); - return channel -> client.execute( - GetAllPitsAction.INSTANCE, - getAllPITNodesRequest, - new RestResponseListener(channel) { - @Override - public RestResponse buildResponse(final GetAllPitNodesResponse getAllPITNodesResponse) throws Exception { - try (XContentBuilder builder = channel.newBuilder()) { - builder.startObject(); - if (getAllPITNodesResponse.hasFailures()) { - builder.startArray("failures"); - for (int idx = 0; idx < getAllPITNodesResponse.failures().size(); idx++) { - builder.field( - getAllPITNodesResponse.failures().get(idx).nodeId(), - getAllPITNodesResponse.failures().get(idx).getDetailedMessage() - ); - } - builder.endArray(); + return channel -> client.getAllPits(getAllPITNodesRequest, new RestResponseListener(channel) { + @Override + public RestResponse buildResponse(final GetAllPitNodesResponse getAllPITNodesResponse) throws Exception { + try (XContentBuilder builder = channel.newBuilder()) { + builder.startObject(); + if (getAllPITNodesResponse.hasFailures()) { + builder.startArray("failures"); + for (int idx = 0; idx < getAllPITNodesResponse.failures().size(); idx++) { + builder.field( + getAllPITNodesResponse.failures().get(idx).nodeId(), + getAllPITNodesResponse.failures().get(idx).getDetailedMessage() + ); } - builder.field("pits", getAllPITNodesResponse.getPitInfos()); - builder.endObject(); - if (getAllPITNodesResponse.getPitInfos().isEmpty()) return new BytesRestResponse(RestStatus.NOT_FOUND, builder); - return new BytesRestResponse(RestStatus.OK, builder); + builder.endArray(); } + builder.field("pits", getAllPITNodesResponse.getPitInfos()); + builder.endObject(); + if (getAllPITNodesResponse.getPitInfos().isEmpty()) { + return new BytesRestResponse(RestStatus.NOT_FOUND, builder); + } + return new BytesRestResponse(RestStatus.OK, builder); } } - ); + }); } @Override public List routes() { - return unmodifiableList(Collections.singletonList(new Route(GET, "/_search/point_in_time/all"))); + return unmodifiableList(Collections.singletonList(new Route(GET, "/_search/point_in_time/_all"))); } } diff --git a/server/src/test/java/org/opensearch/search/pit/RestGetAllPitsActionTests.java b/server/src/test/java/org/opensearch/search/pit/RestGetAllPitsActionTests.java new file mode 100644 index 0000000000000..9d6c5af1f2715 --- /dev/null +++ b/server/src/test/java/org/opensearch/search/pit/RestGetAllPitsActionTests.java @@ -0,0 +1,45 @@ +/* + * 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.search.pit; + +import org.apache.lucene.util.SetOnce; +import org.opensearch.action.ActionListener; +import org.opensearch.action.search.GetAllPitNodesRequest; +import org.opensearch.action.search.GetAllPitNodesResponse; +import org.opensearch.client.node.NodeClient; +import org.opensearch.rest.RestRequest; +import org.opensearch.rest.action.search.RestGetAllPitsAction; +import org.opensearch.test.OpenSearchTestCase; +import org.opensearch.test.client.NoOpNodeClient; +import org.opensearch.test.rest.FakeRestChannel; +import org.opensearch.test.rest.FakeRestRequest; + +import static org.hamcrest.Matchers.equalTo; + +/** + * Tests for rest get all PITs action + */ +public class RestGetAllPitsActionTests extends OpenSearchTestCase { + + public void testGetAllPits() throws Exception { + SetOnce pitCalled = new SetOnce<>(); + try (NodeClient nodeClient = new NoOpNodeClient(this.getTestName()) { + @Override + public void getAllPits(GetAllPitNodesRequest request, ActionListener listener) { + pitCalled.set(true); + } + }) { + RestGetAllPitsAction action = new RestGetAllPitsAction(); + RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withPath("/_all").build(); + FakeRestChannel channel = new FakeRestChannel(request, false, 0); + action.handleRequest(request, channel, nodeClient); + assertThat(pitCalled.get(), equalTo(true)); + } + } +}