Skip to content

Commit

Permalink
Adding rest tests
Browse files Browse the repository at this point in the history
Signed-off-by: Bharathwaj G <bharath78910@gmail.com>
  • Loading branch information
bharath-techie committed Sep 2, 2022
1 parent 18c5be0 commit 57cc2cd
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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"
]
}
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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: {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public GetAllPitNodesResponse(List<ListPitInfo> listPitInfos, GetAllPitNodesResp
pitInfos.addAll(listPitInfos);
}

public <E> GetAllPitNodesResponse(
public GetAllPitNodesResponse(
List<ListPitInfo> listPitInfos,
ClusterName clusterName,
List<GetAllPitNodeResponse> getAllPitNodeResponse,
Expand Down
4 changes: 4 additions & 0 deletions server/src/main/java/org/opensearch/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -341,6 +343,8 @@ public interface Client extends OpenSearchClient, Releasable {
*/
void deletePits(DeletePitRequest deletePITRequest, ActionListener<DeletePitResponse> listener);

void getAllPits(GetAllPitNodesRequest getAllPitNodesRequest, ActionListener<GetAllPitNodesResponse> listener);

/**
* Get information of segments of one or more PITs
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<GetAllPitNodesResponse> listener) {
execute(GetAllPitsAction.INSTANCE, getAllPitNodesRequest, listener);
}

@Override
public void pitSegments(final PitSegmentsRequest request, final ActionListener<IndicesSegmentResponse> listener) {
execute(PitSegmentsAction.INSTANCE, request, listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<GetAllPitNodesResponse>(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<GetAllPitNodesResponse>(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<Route> 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")));
}

}
Original file line number Diff line number Diff line change
@@ -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<Boolean> pitCalled = new SetOnce<>();
try (NodeClient nodeClient = new NoOpNodeClient(this.getTestName()) {
@Override
public void getAllPits(GetAllPitNodesRequest request, ActionListener<GetAllPitNodesResponse> 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));
}
}
}

0 comments on commit 57cc2cd

Please sign in to comment.