Search in sources :

Example 6 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class NodeRestUsageIT method testAggregationUsage.

@SuppressWarnings("unchecked")
public void testAggregationUsage() throws IOException {
    // First get the current usage figures
    String path = randomFrom("_nodes/usage", "_nodes/usage/aggregations", "_nodes/usage/_all");
    Response beforeResponse = client().performRequest(new Request("GET", path));
    Map<String, Object> beforeResponseBodyMap = entityAsMap(beforeResponse);
    assertThat(beforeResponseBodyMap, notNullValue());
    int beforeSuccessful = assertSuccess(beforeResponseBodyMap);
    Map<String, Object> beforeNodesMap = (Map<String, Object>) beforeResponseBodyMap.get("nodes");
    assertThat(beforeNodesMap, notNullValue());
    assertThat(beforeNodesMap.size(), equalTo(beforeSuccessful));
    Map<String, Map<String, Long>> beforeCombinedAggsUsage = getTotalUsage(beforeNodesMap);
    // Do some requests to get some rest usage stats
    Request create = new Request("PUT", "/test");
    create.setJsonEntity("{\"mappings\": {\"properties\": { \"str\": {\"type\": \"keyword\"}, " + "\"foo\": {\"type\": \"keyword\"}, \"num\": {\"type\": \"long\"}, \"start\": {\"type\": \"date\"} } }}");
    client().performRequest(create);
    Request searchRequest = new Request("GET", "/test/_search");
    SearchSourceBuilder searchSource = new SearchSourceBuilder().aggregation(AggregationBuilders.terms("str_terms").field("str.keyword")).aggregation(AggregationBuilders.terms("num_terms").field("num")).aggregation(AggregationBuilders.avg("num_avg").field("num"));
    searchRequest.setJsonEntity(Strings.toString(searchSource));
    searchRequest.setJsonEntity(Strings.toString(searchSource));
    client().performRequest(searchRequest);
    searchRequest = new Request("GET", "/test/_search");
    searchSource = new SearchSourceBuilder().aggregation(AggregationBuilders.terms("start").field("start")).aggregation(AggregationBuilders.avg("num1").field("num")).aggregation(AggregationBuilders.avg("num2").field("num")).aggregation(AggregationBuilders.terms("foo").field("foo.keyword"));
    String r = Strings.toString(searchSource);
    searchRequest.setJsonEntity(Strings.toString(searchSource));
    client().performRequest(searchRequest);
    Response response = client().performRequest(new Request("GET", "_nodes/usage"));
    Map<String, Object> responseBodyMap = entityAsMap(response);
    assertThat(responseBodyMap, notNullValue());
    int successful = assertSuccess(responseBodyMap);
    Map<String, Object> nodesMap = (Map<String, Object>) responseBodyMap.get("nodes");
    assertThat(nodesMap, notNullValue());
    assertThat(nodesMap.size(), equalTo(successful));
    Map<String, Map<String, Long>> afterCombinedAggsUsage = getTotalUsage(nodesMap);
    assertDiff(beforeCombinedAggsUsage, afterCombinedAggsUsage, "terms", "numeric", 1L);
    assertDiff(beforeCombinedAggsUsage, afterCombinedAggsUsage, "terms", "date", 1L);
    assertDiff(beforeCombinedAggsUsage, afterCombinedAggsUsage, "terms", "bytes", 2L);
    assertDiff(beforeCombinedAggsUsage, afterCombinedAggsUsage, "avg", "numeric", 3L);
}
Also used : Response(org.opensearch.client.Response) Request(org.opensearch.client.Request) Matchers.containsString(org.hamcrest.Matchers.containsString) HashMap(java.util.HashMap) Map(java.util.Map) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 7 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class NodeRestUsageIT method testWithRestUsage.

@SuppressWarnings("unchecked")
public void testWithRestUsage() throws IOException {
    // First get the current usage figures
    String path = randomFrom("_nodes/usage", "_nodes/usage/rest_actions", "_nodes/usage/_all");
    Response beforeResponse = client().performRequest(new Request("GET", path));
    Map<String, Object> beforeResponseBodyMap = entityAsMap(beforeResponse);
    assertThat(beforeResponseBodyMap, notNullValue());
    int beforeSuccessful = assertSuccess(beforeResponseBodyMap);
    Map<String, Object> beforeNodesMap = (Map<String, Object>) beforeResponseBodyMap.get("nodes");
    assertThat(beforeNodesMap, notNullValue());
    assertThat(beforeNodesMap.size(), equalTo(beforeSuccessful));
    Map<String, Long> beforeCombinedRestUsage = new HashMap<>();
    beforeCombinedRestUsage.put("nodes_usage_action", 0L);
    beforeCombinedRestUsage.put("create_index_action", 0L);
    beforeCombinedRestUsage.put("document_index_action", 0L);
    beforeCombinedRestUsage.put("search_action", 0L);
    beforeCombinedRestUsage.put("refresh_action", 0L);
    beforeCombinedRestUsage.put("cat_indices_action", 0L);
    beforeCombinedRestUsage.put("nodes_info_action", 0L);
    beforeCombinedRestUsage.put("nodes_stats_action", 0L);
    beforeCombinedRestUsage.put("delete_index_action", 0L);
    for (Map.Entry<String, Object> nodeEntry : beforeNodesMap.entrySet()) {
        Map<String, Object> beforeRestActionUsage = (Map<String, Object>) ((Map<String, Object>) nodeEntry.getValue()).get("rest_actions");
        assertThat(beforeRestActionUsage, notNullValue());
        for (Map.Entry<String, Object> restActionEntry : beforeRestActionUsage.entrySet()) {
            Long currentUsage = beforeCombinedRestUsage.get(restActionEntry.getKey());
            if (currentUsage == null) {
                beforeCombinedRestUsage.put(restActionEntry.getKey(), ((Number) restActionEntry.getValue()).longValue());
            } else {
                beforeCombinedRestUsage.put(restActionEntry.getKey(), currentUsage + ((Number) restActionEntry.getValue()).longValue());
            }
        }
    }
    // Do some requests to get some rest usage stats
    client().performRequest(new Request("PUT", "/test"));
    for (int i = 0; i < 3; i++) {
        final Request index = new Request("POST", "/test/_doc/1");
        index.setJsonEntity("{\"foo\": \"bar\"}");
        client().performRequest(index);
    }
    client().performRequest(new Request("GET", "/test/_search"));
    final Request index4 = new Request("POST", "/test/_doc/4");
    index4.setJsonEntity("{\"foo\": \"bar\"}");
    client().performRequest(index4);
    client().performRequest(new Request("POST", "/test/_refresh"));
    client().performRequest(new Request("GET", "/_cat/indices"));
    client().performRequest(new Request("GET", "/_nodes"));
    client().performRequest(new Request("GET", "/test/_search"));
    client().performRequest(new Request("GET", "/_nodes/stats"));
    client().performRequest(new Request("DELETE", "/test"));
    Response response = client().performRequest(new Request("GET", "_nodes/usage"));
    Map<String, Object> responseBodyMap = entityAsMap(response);
    assertThat(responseBodyMap, notNullValue());
    int successful = assertSuccess(responseBodyMap);
    Map<String, Object> nodesMap = (Map<String, Object>) responseBodyMap.get("nodes");
    assertThat(nodesMap, notNullValue());
    assertThat(nodesMap.size(), equalTo(successful));
    Map<String, Long> combinedRestUsage = new HashMap<>();
    for (Map.Entry<String, Object> nodeEntry : nodesMap.entrySet()) {
        Map<String, Object> restActionUsage = (Map<String, Object>) ((Map<String, Object>) nodeEntry.getValue()).get("rest_actions");
        assertThat(restActionUsage, notNullValue());
        for (Map.Entry<String, Object> restActionEntry : restActionUsage.entrySet()) {
            Long currentUsage = combinedRestUsage.get(restActionEntry.getKey());
            if (currentUsage == null) {
                combinedRestUsage.put(restActionEntry.getKey(), ((Number) restActionEntry.getValue()).longValue());
            } else {
                combinedRestUsage.put(restActionEntry.getKey(), currentUsage + ((Number) restActionEntry.getValue()).longValue());
            }
        }
    }
    assertThat(combinedRestUsage.get("nodes_usage_action") - beforeCombinedRestUsage.get("nodes_usage_action"), equalTo(1L));
    assertThat(combinedRestUsage.get("create_index_action") - beforeCombinedRestUsage.get("create_index_action"), equalTo(1L));
    assertThat(combinedRestUsage.get("document_index_action") - beforeCombinedRestUsage.get("document_index_action"), equalTo(4L));
    assertThat(combinedRestUsage.get("search_action") - beforeCombinedRestUsage.get("search_action"), equalTo(2L));
    assertThat(combinedRestUsage.get("refresh_action") - beforeCombinedRestUsage.get("refresh_action"), equalTo(1L));
    assertThat(combinedRestUsage.get("cat_indices_action") - beforeCombinedRestUsage.get("cat_indices_action"), equalTo(1L));
    assertThat(combinedRestUsage.get("nodes_info_action") - beforeCombinedRestUsage.get("nodes_info_action"), equalTo(1L));
    assertThat(combinedRestUsage.get("nodes_stats_action") - beforeCombinedRestUsage.get("nodes_stats_action"), equalTo(1L));
    assertThat(combinedRestUsage.get("delete_index_action") - beforeCombinedRestUsage.get("delete_index_action"), equalTo(1L));
}
Also used : Response(org.opensearch.client.Response) HashMap(java.util.HashMap) Request(org.opensearch.client.Request) Matchers.containsString(org.hamcrest.Matchers.containsString) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class OpenSearchNodesSnifferTests method testSniffNodes.

public void testSniffNodes() throws IOException {
    HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort());
    try (RestClient restClient = RestClient.builder(httpHost).build()) {
        OpenSearchNodesSniffer sniffer = new OpenSearchNodesSniffer(restClient, sniffRequestTimeout, scheme);
        try {
            List<Node> sniffedNodes = sniffer.sniff();
            if (sniffResponse.isFailure) {
                fail("sniffNodes should have failed");
            }
            assertEquals(sniffResponse.result, sniffedNodes);
        } catch (ResponseException e) {
            Response response = e.getResponse();
            if (sniffResponse.isFailure) {
                final String errorPrefix = "method [GET], host [" + httpHost + "], URI [/_nodes/http?timeout=" + sniffRequestTimeout + "ms], status line [HTTP/1.1";
                assertThat(e.getMessage(), startsWith(errorPrefix));
                assertThat(e.getMessage(), containsString(Integer.toString(sniffResponse.nodesInfoResponseCode)));
                assertThat(response.getHost(), equalTo(httpHost));
                assertThat(response.getStatusLine().getStatusCode(), equalTo(sniffResponse.nodesInfoResponseCode));
                assertThat(response.getRequestLine().toString(), equalTo("GET /_nodes/http?timeout=" + sniffRequestTimeout + "ms HTTP/1.1"));
            } else {
                fail("sniffNodes should have succeeded: " + response.getStatusLine());
            }
        }
    }
}
Also used : Response(org.opensearch.client.Response) ResponseException(org.opensearch.client.ResponseException) HttpHost(org.apache.http.HttpHost) Node(org.opensearch.client.Node) RestClient(org.opensearch.client.RestClient) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 9 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class MultiVersionRepositoryAccessIT method createIndex.

private void createIndex(RestHighLevelClient client, String name, int shards) throws IOException {
    final Request putIndexRequest = new Request("PUT", "/" + name);
    putIndexRequest.setJsonEntity("{\n" + "    \"settings\" : {\n" + "        \"index\" : {\n" + "            \"number_of_shards\" : " + shards + ", \n" + "            \"number_of_replicas\" : 0 \n" + "        }\n" + "    }\n" + "}");
    final Response response = client.getLowLevelClient().performRequest(putIndexRequest);
    assertThat(response.getStatusLine().getStatusCode(), is(HttpURLConnection.HTTP_OK));
}
Also used : SnapshotsStatusResponse(org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse) Response(org.opensearch.client.Response) Request(org.opensearch.client.Request) SnapshotsStatusRequest(org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusRequest) DeleteSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.delete.DeleteSnapshotRequest) PutRepositoryRequest(org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest)

Example 10 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class MultiVersionRepositoryAccessIT method createSnapshot.

private static void createSnapshot(RestHighLevelClient client, String repoName, String name, String index) throws IOException {
    final Request createSnapshotRequest = new Request("PUT", "/_snapshot/" + repoName + "/" + name);
    createSnapshotRequest.addParameter("wait_for_completion", "true");
    createSnapshotRequest.setJsonEntity("{ \"indices\" : \"" + index + "\"}");
    final Response response = client.getLowLevelClient().performRequest(createSnapshotRequest);
    assertThat(response.getStatusLine().getStatusCode(), is(HttpURLConnection.HTTP_OK));
}
Also used : SnapshotsStatusResponse(org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse) Response(org.opensearch.client.Response) Request(org.opensearch.client.Request) SnapshotsStatusRequest(org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusRequest) DeleteSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.delete.DeleteSnapshotRequest) PutRepositoryRequest(org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest)

Aggregations

Response (org.opensearch.client.Response)134 Request (org.opensearch.client.Request)113 ResponseException (org.opensearch.client.ResponseException)24 Map (java.util.Map)23 Matchers.containsString (org.hamcrest.Matchers.containsString)20 ArrayList (java.util.ArrayList)17 PutRepositoryRequest (org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest)16 RestClient (org.opensearch.client.RestClient)15 HashMap (java.util.HashMap)14 RequestOptions (org.opensearch.client.RequestOptions)12 ObjectPath (org.opensearch.test.rest.yaml.ObjectPath)12 IndexRequest (org.opensearch.action.index.IndexRequest)11 IOException (java.io.IOException)10 List (java.util.List)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 Version (org.opensearch.Version)8 WriteRequest (org.opensearch.action.support.WriteRequest)8 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)7 IndexResponse (org.opensearch.action.index.IndexResponse)7 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)7