Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch Rest Client - ConnectionClosedException when calling performRequestAsync

I'm having a problem on HttpAsyncRequestExecutor.

I'm using elasticsearch Java Rest Client and I'm always getting a ConnectionClosedException (see below) when I call the performRequestAsync:

    // variables (all with valid format):
    // endpoint is just a List<String> with "14655/_search"

    // params is just a Map<String, String> with
    //   "pretty", "true"
    //   "search_type", "query_then_fetch"

    // entity is just a HttpEntity entity with the Json body request

    final int numRequests = endpoints.size();
    final CountDownLatch latch = new CountDownLatch(numRequests);
    try (Timer.Context ctx = this.requestTimer.time()) {
        for (final String endpoint : endpoints) {
            // ERROR hapens here:
            restClient.performRequestAsync("GET", endpoint, params, entity,
                    new ResponseListener() {

                        @Override
                        public void onSuccess(final Response response) {
                            if (response != null) {
                                responses.add(response);
                                latch.countDown();
                            }
                        }

                        @Override
                        public void onFailure(final Exception exception) {
                            latch.countDown();

                            logger.error("could not get search results for....",exception);
                            exception.printStackTrace();
                        }
                    });
        }
    }

Exception here:

org.apache.http.ConnectionClosedException: Connection closed
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.endOfInput(HttpAsyncRequestExecutor.java:341)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:263)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:116)
at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:164)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:339)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:317)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:278)
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:106)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:590)
at java.lang.Thread.run(Thread.java:745)

I don't know what is the real cause for the connection close. The exact same request works well in kopf and returns valid search results. Plus, I don't call any restClient.close() or anything similar.

Any ideas where the problem might be? Is the end of the input the cause to get a closed connection state (according to org.apache.http.nio.protocol.HttpAsyncRequestExecutor.endOfInput(conn))? If yes, what input is that?

Thanks

UPDATE:

I suspect the problem is related to Tomcat's HttpClient, because this code works correctly in an integration test (that is, returns results)... But it does not work (get the same ConnectionClosedException) when I make a REST request through a tomcat deployed "interface"

Any lights on this?

like image 575
JonyD Avatar asked Jun 25 '26 15:06

JonyD


1 Answers

The problem was the port was wrong. For REST requests the port should be 9200 (and not 9300 like it was configured). More info on elasticsearch ports.

I wish elasticsearch could make a more explicit error log or a hint like "are you connecting with the right port?" for when one tries to access the 9300 port with anything other than the built-in clients.

like image 128
JonyD Avatar answered Jun 27 '26 05:06

JonyD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!