Search in sources :

Example 1 with ResponseListener

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

the class SearchRestCancellationIT method verifyCancellationDuringQueryPhase.

void verifyCancellationDuringQueryPhase(String searchAction, Request searchRequest) throws Exception {
    Map<String, String> nodeIdToName = readNodesInfo();
    List<ScriptedBlockPlugin> plugins = initBlockFactory();
    indexTestData();
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<Exception> error = new AtomicReference<>();
    Cancellable cancellable = getRestClient().performRequestAsync(searchRequest, new ResponseListener() {

        @Override
        public void onSuccess(Response response) {
            latch.countDown();
        }

        @Override
        public void onFailure(Exception exception) {
            error.set(exception);
            latch.countDown();
        }
    });
    awaitForBlock(plugins);
    cancellable.cancel();
    ensureSearchTaskIsCancelled(searchAction, nodeIdToName::get);
    disableBlocks(plugins);
    latch.await();
    assertThat(error.get(), instanceOf(CancellationException.class));
}
Also used : Cancellable(org.opensearch.client.Cancellable) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResponseListener(org.opensearch.client.ResponseListener) CountDownLatch(java.util.concurrent.CountDownLatch) CancellationException(java.util.concurrent.CancellationException) NodesInfoResponse(org.opensearch.action.admin.cluster.node.info.NodesInfoResponse) Response(org.opensearch.client.Response) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) CancellationException(java.util.concurrent.CancellationException)

Example 2 with ResponseListener

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

the class RemoteScrollableHitSource method execute.

private <T> void execute(Request request, BiFunction<XContentParser, XContentType, T> parser, RejectAwareActionListener<? super T> listener) {
    // Preserve the thread context so headers survive after the call
    java.util.function.Supplier<ThreadContext.StoredContext> contextSupplier = threadPool.getThreadContext().newRestorableContext(true);
    try {
        client.performRequestAsync(request, new ResponseListener() {

            @Override
            public void onSuccess(org.opensearch.client.Response response) {
                // Restore the thread context to get the precious headers
                try (ThreadContext.StoredContext ctx = contextSupplier.get()) {
                    // eliminates compiler warning
                    assert ctx != null;
                    T parsedResponse;
                    try {
                        HttpEntity responseEntity = response.getEntity();
                        InputStream content = responseEntity.getContent();
                        XContentType xContentType = null;
                        if (responseEntity.getContentType() != null) {
                            final String mimeType = ContentType.parse(responseEntity.getContentType().getValue()).getMimeType();
                            xContentType = XContentType.fromMediaType(mimeType);
                        }
                        if (xContentType == null) {
                            try {
                                logger.debug("Response didn't include Content-Type: " + bodyMessage(response.getEntity()));
                                throw new OpenSearchException("Response didn't include supported Content-Type, remote is likely not an OpenSearch instance");
                            } catch (IOException e) {
                                OpenSearchException ee = new OpenSearchException("Error extracting body from response");
                                ee.addSuppressed(e);
                                throw ee;
                            }
                        }
                        // EMPTY is safe here because we don't call namedObject
                        try (XContentParser xContentParser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, content)) {
                            parsedResponse = parser.apply(xContentParser, xContentType);
                        } catch (XContentParseException e) {
                            /* Because we're streaming the response we can't get a copy of it here. The best we can do is hint that it
                                 * is totally wrong and we're probably not talking to Elasticsearch. */
                            throw new OpenSearchException("Error parsing the response, remote is likely not an OpenSearch instance", e);
                        }
                    } catch (IOException e) {
                        throw new OpenSearchException("Error deserializing response, remote is likely not an OpenSearch instance", e);
                    }
                    listener.onResponse(parsedResponse);
                }
            }

            @Override
            public void onFailure(Exception e) {
                try (ThreadContext.StoredContext ctx = contextSupplier.get()) {
                    // eliminates compiler warning
                    assert ctx != null;
                    if (e instanceof ResponseException) {
                        ResponseException re = (ResponseException) e;
                        int statusCode = re.getResponse().getStatusLine().getStatusCode();
                        e = wrapExceptionToPreserveStatus(statusCode, re.getResponse().getEntity(), re);
                        if (RestStatus.TOO_MANY_REQUESTS.getStatus() == statusCode) {
                            listener.onRejection(e);
                            return;
                        }
                    } else if (e instanceof ContentTooLongException) {
                        e = new IllegalArgumentException("Remote responded with a chunk that was too large. Use a smaller batch size.", e);
                    }
                    listener.onFailure(e);
                }
            }
        });
    } catch (Exception e) {
        listener.onFailure(e);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) ResponseException(org.opensearch.client.ResponseException) InputStream(java.io.InputStream) ContentTooLongException(org.apache.http.ContentTooLongException) ResponseListener(org.opensearch.client.ResponseListener) IOException(java.io.IOException) ContentTooLongException(org.apache.http.ContentTooLongException) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) OpenSearchException(org.opensearch.OpenSearchException) IOException(java.io.IOException) ResponseException(org.opensearch.client.ResponseException) XContentParseException(org.opensearch.common.xcontent.XContentParseException) XContentType(org.opensearch.common.xcontent.XContentType) OpenSearchException(org.opensearch.OpenSearchException) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentParseException(org.opensearch.common.xcontent.XContentParseException)

Example 3 with ResponseListener

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

the class WaitForRefreshAndCloseIT method start.

private ActionFuture<String> start(Request request) {
    PlainActionFuture<String> future = new PlainActionFuture<>();
    request.addParameter("refresh", "wait_for");
    request.addParameter("error_trace", "");
    client().performRequestAsync(request, new ResponseListener() {

        @Override
        public void onSuccess(Response response) {
            try {
                future.onResponse(EntityUtils.toString(response.getEntity()));
            } catch (IOException e) {
                future.onFailure(e);
            }
        }

        @Override
        public void onFailure(Exception exception) {
            future.onFailure(exception);
        }
    });
    return future;
}
Also used : Response(org.opensearch.client.Response) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Matchers.containsString(org.hamcrest.Matchers.containsString) ResponseListener(org.opensearch.client.ResponseListener) IOException(java.io.IOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ResponseException(org.opensearch.client.ResponseException)

Example 4 with ResponseListener

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

the class RestClientDocumentation method usage.

// end::rest-client-options-singleton
@SuppressWarnings("unused")
public void usage() throws IOException, InterruptedException {
    // tag::rest-client-init
    RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http"), new HttpHost("localhost", 9201, "http")).build();
    // end::rest-client-init
    // tag::rest-client-close
    restClient.close();
    // end::rest-client-close
    {
        // tag::rest-client-init-default-headers
        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
        Header[] defaultHeaders = new Header[] { new BasicHeader("header", "value") };
        // <1>
        builder.setDefaultHeaders(defaultHeaders);
    // end::rest-client-init-default-headers
    }
    {
        // tag::rest-client-init-node-selector
        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
        // <1>
        builder.setNodeSelector(NodeSelector.SKIP_DEDICATED_MASTERS);
    // end::rest-client-init-node-selector
    }
    {
        // tag::rest-client-init-allocation-aware-selector
        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
        builder.setNodeSelector(new // <1>
        NodeSelector() {

            @Override
            public void select(Iterable<Node> nodes) {
                /*
                     * Prefer any node that belongs to rack_one. If none is around
                     * we will go to another rack till it's time to try and revive
                     * some of the nodes that belong to rack_one.
                     */
                boolean foundOne = false;
                for (Node node : nodes) {
                    String rackId = node.getAttributes().get("rack_id").get(0);
                    if ("rack_one".equals(rackId)) {
                        foundOne = true;
                        break;
                    }
                }
                if (foundOne) {
                    Iterator<Node> nodesIt = nodes.iterator();
                    while (nodesIt.hasNext()) {
                        Node node = nodesIt.next();
                        String rackId = node.getAttributes().get("rack_id").get(0);
                        if ("rack_one".equals(rackId) == false) {
                            nodesIt.remove();
                        }
                    }
                }
            }
        });
    // end::rest-client-init-allocation-aware-selector
    }
    {
        // tag::rest-client-init-failure-listener
        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
        builder.setFailureListener(new RestClient.FailureListener() {

            @Override
            public void onFailure(Node node) {
            // <1>
            }
        });
    // end::rest-client-init-failure-listener
    }
    {
        // tag::rest-client-init-request-config-callback
        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
        builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {

            @Override
            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
                // <1>
                return requestConfigBuilder.setSocketTimeout(10000);
            }
        });
    // end::rest-client-init-request-config-callback
    }
    {
        // tag::rest-client-init-client-config-callback
        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
        builder.setHttpClientConfigCallback(new HttpClientConfigCallback() {

            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                return httpClientBuilder.setProxy(// <1>
                new HttpHost("proxy", 9000, "http"));
            }
        });
    // end::rest-client-init-client-config-callback
    }
    {
        // tag::rest-client-sync
        Request request = new Request(// <1>
        "GET", // <2>
        "/");
        Response response = restClient.performRequest(request);
    // end::rest-client-sync
    }
    {
        // tag::rest-client-async
        Request request = new Request(// <1>
        "GET", // <2>
        "/");
        Cancellable cancellable = restClient.performRequestAsync(request, new ResponseListener() {

            @Override
            public void onSuccess(Response response) {
            // <3>
            }

            @Override
            public void onFailure(Exception exception) {
            // <4>
            }
        });
    // end::rest-client-async
    }
    {
        Request request = new Request("GET", "/");
        // tag::rest-client-parameters
        request.addParameter("pretty", "true");
        // end::rest-client-parameters
        // tag::rest-client-body
        request.setEntity(new NStringEntity("{\"json\":\"text\"}", ContentType.APPLICATION_JSON));
        // end::rest-client-body
        // tag::rest-client-body-shorter
        request.setJsonEntity("{\"json\":\"text\"}");
        // end::rest-client-body-shorter
        // tag::rest-client-options-set-singleton
        request.setOptions(COMMON_OPTIONS);
        // end::rest-client-options-set-singleton
        {
            // tag::rest-client-options-customize-header
            RequestOptions.Builder options = COMMON_OPTIONS.toBuilder();
            options.addHeader("cats", "knock things off of other things");
            request.setOptions(options);
        // end::rest-client-options-customize-header
        }
    }
    {
        HttpEntity[] documents = new HttpEntity[10];
        // tag::rest-client-async-example
        final CountDownLatch latch = new CountDownLatch(documents.length);
        for (int i = 0; i < documents.length; i++) {
            Request request = new Request("PUT", "/posts/doc/" + i);
            // let's assume that the documents are stored in an HttpEntity array
            request.setEntity(documents[i]);
            restClient.performRequestAsync(request, new ResponseListener() {

                @Override
                public void onSuccess(Response response) {
                    // <1>
                    latch.countDown();
                }

                @Override
                public void onFailure(Exception exception) {
                    // <2>
                    latch.countDown();
                }
            });
        }
        latch.await();
    // end::rest-client-async-example
    }
    {
        // tag::rest-client-async-cancel
        Request request = new Request("GET", "/posts/_search");
        Cancellable cancellable = restClient.performRequestAsync(request, new ResponseListener() {

            @Override
            public void onSuccess(Response response) {
            // <1>
            }

            @Override
            public void onFailure(Exception exception) {
            // <2>
            }
        });
        cancellable.cancel();
    // end::rest-client-async-cancel
    }
    {
        // tag::rest-client-response2
        Response response = restClient.performRequest(new Request("GET", "/"));
        // <1>
        RequestLine requestLine = response.getRequestLine();
        // <2>
        HttpHost host = response.getHost();
        // <3>
        int statusCode = response.getStatusLine().getStatusCode();
        // <4>
        Header[] headers = response.getHeaders();
        // <5>
        String responseBody = EntityUtils.toString(response.getEntity());
    // end::rest-client-response2
    }
}
Also used : RequestOptions(org.opensearch.client.RequestOptions) Cancellable(org.opensearch.client.Cancellable) Node(org.opensearch.client.Node) RestClientBuilder(org.opensearch.client.RestClientBuilder) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) RestClientBuilder(org.opensearch.client.RestClientBuilder) NStringEntity(org.apache.http.nio.entity.NStringEntity) HttpHost(org.apache.http.HttpHost) RequestConfig(org.apache.http.client.config.RequestConfig) RestClient(org.opensearch.client.RestClient) Request(org.opensearch.client.Request) ResponseListener(org.opensearch.client.ResponseListener) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) Response(org.opensearch.client.Response) RequestLine(org.apache.http.RequestLine) HttpClientConfigCallback(org.opensearch.client.RestClientBuilder.HttpClientConfigCallback) NodeSelector(org.opensearch.client.NodeSelector) BasicHeader(org.apache.http.message.BasicHeader)

Example 5 with ResponseListener

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

the class SearchRestCancellationIT method verifyCancellationDuringFetchPhase.

void verifyCancellationDuringFetchPhase(String searchAction, Request searchRequest) throws Exception {
    Map<String, String> nodeIdToName = readNodesInfo();
    List<ScriptedBlockPlugin> plugins = initBlockFactory();
    indexTestData();
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<Exception> error = new AtomicReference<>();
    Cancellable cancellable = getRestClient().performRequestAsync(searchRequest, new ResponseListener() {

        @Override
        public void onSuccess(Response response) {
            latch.countDown();
        }

        @Override
        public void onFailure(Exception exception) {
            error.set(exception);
            latch.countDown();
        }
    });
    awaitForBlock(plugins);
    cancellable.cancel();
    ensureSearchTaskIsCancelled(searchAction, nodeIdToName::get);
    disableBlocks(plugins);
    latch.await();
    assertThat(error.get(), instanceOf(CancellationException.class));
}
Also used : Cancellable(org.opensearch.client.Cancellable) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResponseListener(org.opensearch.client.ResponseListener) CountDownLatch(java.util.concurrent.CountDownLatch) CancellationException(java.util.concurrent.CancellationException) NodesInfoResponse(org.opensearch.action.admin.cluster.node.info.NodesInfoResponse) Response(org.opensearch.client.Response) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) CancellationException(java.util.concurrent.CancellationException)

Aggregations

ResponseListener (org.opensearch.client.ResponseListener)5 Response (org.opensearch.client.Response)4 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Cancellable (org.opensearch.client.Cancellable)3 CancellationException (java.util.concurrent.CancellationException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 NodesInfoResponse (org.opensearch.action.admin.cluster.node.info.NodesInfoResponse)2 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)2 ResponseException (org.opensearch.client.ResponseException)2 InputStream (java.io.InputStream)1 ExecutionException (java.util.concurrent.ExecutionException)1 ContentTooLongException (org.apache.http.ContentTooLongException)1 HttpEntity (org.apache.http.HttpEntity)1 HttpHost (org.apache.http.HttpHost)1 RequestLine (org.apache.http.RequestLine)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 HttpAsyncClientBuilder (org.apache.http.impl.nio.client.HttpAsyncClientBuilder)1 BasicHeader (org.apache.http.message.BasicHeader)1 NStringEntity (org.apache.http.nio.entity.NStringEntity)1