Repository navigation
Description
Java API client version
9.0.4
Java version
21
Elasticsearch Version
9.0.4
Problem description
Hello,
We are trying to diagnose an issue which seems to have only started to come up with the new transport. This looks similar to #1003, but it's with the httpclient5 transport.
We have an application which makes various periodic queries, some more involved than others, and phases of intense indexing work. This app has been using Elasticsearch since 2.x era, and has used every client along the way: TransportClient, HLRC, "new" Java client with elasticsearch-rest-client, and now the client with HTTPClient5 transport. The structure and query patterns of the app have not changed in any notable way over this period.
This past weekend, the app was deployed for the first time with the HTTPClient5 transport to two different machines, one with 10 nodes on a low-cpu-constrained client environment without TLS or users, and another with a cpu-constrained environment on a single node (same machine) with TLS and users. The nodes all run on the dockerized version of ES 9.0.4.
Both environments went through an index migration from our side, using the bulk ingester class, and then resumed the normal activities of occasional queries and occasional indexing requests.
The constrained environment has now twice this weekend gotten into a state where queries cannot be run, and there are log entries that I/O reactor has been shut down every time the client tries to operate, and nothing brings it back except restarts / reinitializations of the client code.
Some of our code is more funky regarding the CompleteableFuture patterns, but even simple code like this can bring it out once it's in this state:
final String finalIndexPattern = "myIndexv*";
final int INDEX_OPERATION_DEFAULT_TIMEOUT_SEC = 45;
List<IndicesRecord> r = client.cat().indices(t -> t.index(finalIndexPattern)).get(INDEX_OPERATION_DEFAULT_TIMEOUT_SEC, TimeUnit.SECONDS).indices();An example of the trace:
org.apache.hc.core5.reactor.IOReactorShutdownException: I/O reactor has been shut down
...
Caused by: java.util.concurrent.ExecutionException: org.apache.hc.core5.reactor.IOReactorShutdownException: I/O reactor has been shut down
at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:396)
at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2096)
at <snip - private code>
... 106 more
Caused by: org.apache.hc.core5.reactor.IOReactorShutdownException: I/O reactor has been shut down
at org.apache.hc.core5.reactor.AbstractIOReactorBase.connect(AbstractIOReactorBase.java:50)
at org.apache.hc.client5.http.impl.nio.MultihomeIOSessionRequester$2.executeNext(MultihomeIOSessionRequester.java:139)
at org.apache.hc.client5.http.impl.nio.MultihomeIOSessionRequester$2.run(MultihomeIOSessionRequester.java:188)
at org.apache.hc.client5.http.impl.nio.MultihomeIOSessionRequester.connect(MultihomeIOSessionRequester.java:192)
at org.apache.hc.client5.http.impl.nio.DefaultAsyncClientConnectionOperator.connect(DefaultAsyncClientConnectionOperator.java:115)
at org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager.connect(PoolingAsyncClientConnectionManager.java:453)
at org.apache.hc.client5.http.impl.async.InternalHttpAsyncExecRuntime.connectEndpoint(InternalHttpAsyncExecRuntime.java:226)
at org.apache.hc.client5.http.impl.async.AsyncConnectExec.doProceedToNextHop(AsyncConnectExec.java:220)
at org.apache.hc.client5.http.impl.async.AsyncConnectExec.proceedToNextHop(AsyncConnectExec.java:195)
at org.apache.hc.client5.http.impl.async.AsyncConnectExec.access$000(AsyncConnectExec.java:90)
at org.apache.hc.client5.http.impl.async.AsyncConnectExec$1.completed(AsyncConnectExec.java:162)
at org.apache.hc.client5.http.impl.async.AsyncConnectExec$1.completed(AsyncConnectExec.java:151)
at org.apache.hc.client5.http.impl.async.InternalHttpAsyncExecRuntime$1.completed(InternalHttpAsyncExecRuntime.java:128)
at org.apache.hc.client5.http.impl.async.InternalHttpAsyncExecRuntime$1.completed(InternalHttpAsyncExecRuntime.java:120)
at org.apache.hc.core5.concurrent.BasicFuture.completed(BasicFuture.java:148)
at org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager$3$1.leaseCompleted(PoolingAsyncClientConnectionManager.java:336)
at org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager$3$1.completed(PoolingAsyncClientConnectionManager.java:321)
at org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager$3$1.completed(PoolingAsyncClientConnectionManager.java:282)
at org.apache.hc.core5.concurrent.BasicFuture.completed(BasicFuture.java:148)
at org.apache.hc.core5.pool.StrictConnPool.fireCallbacks(StrictConnPool.java:401)
at org.apache.hc.core5.pool.StrictConnPool.lease(StrictConnPool.java:219)
at org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager$3.<init>(PoolingAsyncClientConnectionManager.java:279)
at org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager.lease(PoolingAsyncClientConnectionManager.java:274)
at org.apache.hc.client5.http.impl.async.InternalHttpAsyncExecRuntime.acquireEndpoint(InternalHttpAsyncExecRuntime.java:115)
at org.apache.hc.client5.http.impl.async.AsyncConnectExec.execute(AsyncConnectExec.java:150)
at org.apache.hc.client5.http.impl.async.AsyncExecChainElement.execute(AsyncExecChainElement.java:54)
at org.apache.hc.client5.http.impl.async.AsyncProtocolExec.internalExecute(AsyncProtocolExec.java:207)
at org.apache.hc.client5.http.impl.async.AsyncProtocolExec.execute(AsyncProtocolExec.java:172)
at org.apache.hc.client5.http.impl.async.AsyncExecChainElement.execute(AsyncExecChainElement.java:54)
at org.apache.hc.client5.http.impl.async.AsyncHttpRequestRetryExec.internalExecute(AsyncHttpRequestRetryExec.java:113)
at org.apache.hc.client5.http.impl.async.AsyncHttpRequestRetryExec.execute(AsyncHttpRequestRetryExec.java:211)
at org.apache.hc.client5.http.impl.async.AsyncExecChainElement.execute(AsyncExecChainElement.java:54)
at org.apache.hc.client5.http.impl.async.AsyncRedirectExec.internalExecute(AsyncRedirectExec.java:111)
at org.apache.hc.client5.http.impl.async.AsyncRedirectExec.execute(AsyncRedirectExec.java:278)
at org.apache.hc.client5.http.impl.async.AsyncExecChainElement.execute(AsyncExecChainElement.java:54)
at org.apache.hc.client5.http.impl.async.InternalAbstractHttpAsyncClient.executeImmediate(InternalAbstractHttpAsyncClient.java:386)
at org.apache.hc.client5.http.impl.async.InternalAbstractHttpAsyncClient.lambda$doExecute$0(InternalAbstractHttpAsyncClient.java:242)
at org.apache.hc.core5.http.nio.support.BasicRequestProducer.sendRequest(BasicRequestProducer.java:93)
at org.apache.hc.client5.http.impl.async.InternalAbstractHttpAsyncClient.doExecute(InternalAbstractHttpAsyncClient.java:209)
at org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient.execute(CloseableHttpAsyncClient.java:96)
at org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient.execute(CloseableHttpAsyncClient.java:106)
at co.elastic.clients.transport.rest5_client.low_level.Rest5Client.lambda$performRequestAsync$1(Rest5Client.java:407)
at co.elastic.clients.transport.rest5_client.low_level.Cancellable$RequestCancellable.runIfNotCancelled(Cancellable.java:98)
at co.elastic.clients.transport.rest5_client.low_level.Rest5Client.performRequestAsync(Rest5Client.java:404)
at co.elastic.clients.transport.rest5_client.low_level.Rest5Client.performRequestAsync(Rest5Client.java:391)
at co.elastic.clients.transport.rest5_client.Rest5ClientHttpClient.performRequestAsync(Rest5ClientHttpClient.java:114)
at co.elastic.clients.transport.ElasticsearchTransportBase.performRequestAsync(ElasticsearchTransportBase.java:190)
at co.elastic.clients.elasticsearch.cat.ElasticsearchCatAsyncClient.indices(ElasticsearchCatAsyncClient.java:538)
at co.elastic.clients.elasticsearch.cat.ElasticsearchCatAsyncClient.indices(ElasticsearchCatAsyncClient.java:576)
... 107 moreThe original exception, before all requests show the reactor error, is that the connection is closed:
Caused by: java.util.concurrent.ExecutionException: org.apache.hc.core5.http.RequestNotExecutedException: Connection is closed
at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:396)
at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2096)
at <snip - private code>
... 106 more
Caused by: org.apache.hc.core5.http.RequestNotExecutedException: Connection is closed
at org.apache.hc.core5.http.nio.command.CommandSupport.cancelCommands(CommandSupport.java:68)
at org.apache.hc.core5.http.impl.nio.AbstractHttp1StreamDuplexer.onDisconnect(AbstractHttp1StreamDuplexer.java:415)
at org.apache.hc.core5.http.impl.nio.AbstractHttp1IOEventHandler.disconnected(AbstractHttp1IOEventHandler.java:95)
at org.apache.hc.core5.http.impl.nio.ClientHttp1IOEventHandler.disconnected(ClientHttp1IOEventHandler.java:41)
at org.apache.hc.core5.reactor.ssl.SSLIOSession$1.disconnected(SSLIOSession.java:253)
at org.apache.hc.core5.reactor.InternalDataChannel.disconnected(InternalDataChannel.java:205)
at org.apache.hc.core5.reactor.SingleCoreIOReactor.processClosedSessions(SingleCoreIOReactor.java:229)
at org.apache.hc.core5.reactor.SingleCoreIOReactor.doExecute(SingleCoreIOReactor.java:131)
at org.apache.hc.core5.reactor.AbstractSingleCoreIOReactor.execute(AbstractSingleCoreIOReactor.java:92)
at org.apache.hc.core5.reactor.IOReactorWorker.run(IOReactorWorker.java:44)
... 1 moreI'm not surprised about the occasional close with the heavy environment, but like I've said, nothing has really changed except for the update to 9.x and the httpclient. We were on elasticsearch-java 8.18.0 with elasticsearch-rest-client 9.0.3 immediately before this deployment.
What other information would be preferable? I am running more tests with the CPU constraint relaxed a bit more, to see if this is bringing out the situation, as a data point to help distinguish the other different factors between systems (single node, same host, tls, user auth). My next plan after this is to switch back to the elasticsearch-rest-client 9.0.4 and revert the client initialization changes to see if it pops up again with this environment under load.
Thanks for your time!
Activity
marcreichman-pfi commentedon Aug 4, 2025
It seems like this method should probably be called during client building.
Information gleaned mostly from this thread.
I am going to also experiment with
.setHttpClientConfigCallbackto try to usesetIoReactorExceptionCallback(..). I believe the default code should set this, however.l-trotta commentedon Aug 11, 2025
Hello @marcreichman-pfi and thank you for the detailed report. I believe this is caused by the missing
SafeResponseConsumerwhich was set by default in the old RestClient and would avoid reactor shutdowns in case of exceptions. When writing the updated Rest5Client I couldn't manage to trigger an IoReactor shutdown, so I naively assumed graceful shutdowns to be handled at the library level... seems I was wrong ^^" sorry for the mess. I'll add aSafeResponseConsumerimplementation to Rest5Client in the next patch, I'll update you when it will be out!l-trotta commentedon Aug 13, 2025
@marcreichman-pfi will be out in 9.1.1 and 9.0.5, available now (might have to wait a bit to have them show in maven central though). please let me know if it fixes the issue :)
marcreichman-pfi commentedon Aug 13, 2025
Thank you @l-trotta ! Appreciate your quick turnaround.
marcreichman-pfi commentedon Aug 22, 2025
@l-trotta just FYI I have not seen this once in 9.0.5+
marcreichman-pfi commentedon Nov 19, 2025
@l-trotta I'm not sure if this warrants a reopen but I did see this again today using 9.0.8, on a system which was very bogged down during an index version migration. Heavy use of bulk ingester, several queries going on as well. I have logs, what do you need?
l-trotta commentedon Nov 19, 2025
hey @marcreichman-pfi, trace logs as the ones in the PR description should be enough!
marcreichman-pfi commentedon Nov 19, 2025
Here's a larger version, we were coming in from many threads with bulk ingester, so I have lots to pick from.
trace.log
this is a sample of our client init code. Most notably that error log did not fire.
l-trotta commentedon Nov 20, 2025
unfortunately the logs only show what's happening after the IO reactor was shut down, we'd need to know the original reason to investigate... weird that it didn't trigger the callback if that's the case! if you could get logs with the start of the exceptions, maybe there could be something happening before. In the meantime I'll investigate if it's possible for the IO reactor to shut down without throwing exception.
marcreichman-pfi commentedon Nov 20, 2025
Hi @l-trotta, that is actually the start of all exceptions. It's possible that the initial condition hit the logs out of order, though, since this is a bulk ingester situation with a feeding queue/thread pool to 8 concurrent requests. I do see a connection is closed exception at
2025-11-19 11:12:28,774which is after the initial message (forBulk operation 3499). I also see some info from a different query outside of the bulk helper at2025-11-19 11:20:30,141with a different connection is closed.So there are various
connection is closedscattered around there, besides just the IO reactor being shut down. I am not sure how to trap any earlier state as there are no error messages associated. Is there a higher verbosity for ES client or HC5 I should use? I don't encounter this often, as you can see from the rate of my messages to this ticket.marcreichman-pfi commentedon Nov 20, 2025
aha - I did find this about 10-12m before the other exceptions started, from a different log:
Maybe that sheds some light on why the handler didn't fire.
l-trotta commentedon Nov 20, 2025
considering the customized use of Rest5Client I'm suspecting something: how is the actual ElasticsearchClient created? The default builders use the
SafeResponseConsumer, which was the solution when the problem first appeared, but if the creation of the client + rest client is customized then it's possible that theSafeResponseConsumeris not set, so there's nothing preventing the OOME from shutting down the IO reactor.marcreichman-pfi commentedon Nov 25, 2025
@l-trotta So sorry for the delayed response.
Here is the entire code (sanitized) of client creation:
I had made the adjustment to use the safe response consumer ourselves after some of the previous messages months ago (related to #1054). This code has not been changed since then. We use our own consumer factory override to tolerate larger responses than the default.
1 remaining item
marcreichman-pfi commentedon Dec 2, 2025
@l-trotta Thanks, I will try to reproduce although it's uncommon. I don't get DEBUG levels from the apache http5 client. I'll see about digging into how to set this up. We use
log4j-slf4j2-impl, so it may just be that i'd need to add a logger fororg.apache.hc.client5to a dedicated file. I'll see what I can do and if I can reproduce.marcreichman-pfi commentedon Dec 2, 2025
@l-trotta OK - it took a few tries but I made it happen. It definitely correlates with an OOM a bit before the shutdown time. So with that I can probably make it happen with a smaller heap in my app.
Just to review the usage pattern, this is the client as written above, and a "full reindex" of data from our database, which is basically a queue-driven thread pool which feeds a bulk ingester. The bulk ingester setting is as follows:
Generally when this runs it tends to crush the nodes a bit, and slides a window from the bulk ingester concurrent reqs back to the thread pool (usually 32) and up the indexing queue (usually 10000) to block the calling producer for the queue. We use this all the time and have been since the days of ES 2.x and the transport client, through the HLRC, to the new client. We do this on many systems, this one seems to have a harder time with the memory load since it's a slower machine. I'm not sure if something in the http5/9.x entity code is more memory consuming than the previous approaches.
Here is the original OOM - maybe the thread matters for later?:
Here are some of the fresh failures:
Finally, I'm attaching the entire httpclient5 debug file as you requested. It comes from before the heap issue (~14:44), the next bulk index starting (~15:01), and the problems starting to show with the reactor (~15:03). There were previous bulk index operations before and usual timers for things like checking nodes are up, so the log is quite busy and I'm gzipping it. I can see
IMMEDIATEabout ~100-125ms before the reactor shutdown errors. I hope this helps to make sense of things! I did this with only some apache logs in DEBUG because otherwise it was a flood and slowed it down too much. I can make this happen again if you need me to try other options/settings/log levelses_apache.log.gz
marcreichman-pfi commentedon Dec 3, 2025
I'm running another test using
setIOSessionListenerto see if I can provide more useful info, with a slightly smaller heap to maybe cause it to generate again. If so I'll give another set of logs with the additional io session events.A couple thoughts:
Errorof OOM means that typical exception blocks may not catch it and it seems like it's getting all the way out to the IOReactor itself and the java Thread. I wonder if it's worth catching something earlier just so better handling can be applied. But I also don't see how anything relating to the safe consumer could be involved here since it's on the production side.entity.getContent()and set the atomicreference. I wonder if synchronization is needed here, or some other approach to ensure thread safety, again if it is needed. I don't understand the whole flow of this particular async path so this is speculation.marcreichman-pfi commentedon Dec 3, 2025
@l-trotta Here is my latest update - I'll stop after this so you have a chance to look into things.
Here's the OOM on
elasticsearch-rest-client-2-thread-8(9:33:35). As far as I can tell this was the JVM echoing to stdout (as is captured by this log) and the thread died.About 30 seconds before, these started using my new IO session listener logs and the logs I had before. They end for this thread
elasticsearch-rest-client-2-thread-8and for this... channel?c-0000000023right when the OOM happens.This was 159ms before the OOM, and is the last mention of any kind of thread
elasticsearch-rest-client-2-thread-8orc-0000000023.In this particular case, it took a bit longer from the OOM to the reactor shutdown.
The reactor shutdown traces are the same as above, so I'll leave them out, but they start in the same way, from a regular query, and then immediately followed by the same ones on bulk operations from our listener. In this case though, the first IO reactor failure was
10:29:15,445which is much longer than the usual 10 minutes or so. I suspect the extra debug logging added enough i/o overhead to stretch this out a bit.Just before the reactor failure messages started coming in, there was this. This came from the same thread as the original I/O reactor shut down message (
__ejb-thread-pool10).Within 200-225ms or so of this, we start getting other closes, and a shutdown:
If you'd like I can share this debug/trace as a whole. I have sanitized it and compressed into a tar.gz. It is 15.2MB. Let me know if you'd like it and if you'd prefer it to be sent a different way than a github issue attachment?
For now I'll leave you with all this. Thanks for your time!
l-trotta commentedon Dec 4, 2025
@marcreichman-pfi Thanks again for all the provided information. It's weird because I can reproduce everything that is happening on your side, except for shutting down the IO reactor. Though I did realize something, and this may sound silly that I only noticed this today, but if the OOM error is being thrown by the request producer, then of course the SafeResponseConsumer won't handle that, since it only works for responses! I need to figure out first why this safety mechanism was only dedicated to responses, and see if it makes sense to add it to requests handling too... in addition to finding out why I can't get it to shut down locally.
marcreichman-pfi commentedon Dec 4, 2025
Thanks @l-trotta - I did happen to notice that too in this comment. I think if you can produce OOM in the entity producing side (low heap, large bulks with concurrent requests) you should be able to get into this situation pretty easily.
I think if the MultiBufferEntity code (or the entity producer calling it) can detect this, it may be able to not let the OOM get all the way out to the bottom of the JDK thread. I feel that catching
OutOfMemoryError, orErrorat all, is bad practice, so I'm not sure what the best course of action is. It also makes me wonder if there is any avenue for more aggressive cleanup, or reduction in copies, in any of that entity producing path, to help alleviate the heap pressure.For now, I should be able to run this particular system with a slightly larger heap, and/or less aggressive bulk ingester settings, to work around it now that I understand the scenario moreso. Hopefully you figure out a solution satisfactory to you and the overall project. Thanks again for your time and attention!
l-trotta commentedon Dec 19, 2025
Hello @marcreichman-pfi, I did some research on why we don't have the same safety mechanism for requests, and it's simply because for responses the users' application has no control over the size of payloads, so it's more difficult to avoid reactor deaths in case of large payloads in responses, while for requests, it's better for users to regulate the application's throughput then to rely on safety mechanisms. So the client won't implement a
SafeRequestConsumer, but! I do have good news.Knowing that the same application with the same resources that used to work with an 8.x client version suddenly goes out of memory with 9.x and rest5 made me suspect something wrong was happening with the rest5 client, and I think I might have found the cause, if I'm right this fix will cut memory consumption by half (which should solve your issue). I'll do more testing and will update soon! Thank you for your patience.
marcreichman-pfi commentedon Dec 22, 2025
@l-trotta Thanks for your persistence! It does seem like the newer client is "heavier" so I'm curious what you find.
l-trotta commentedon Jan 9, 2026
Hey @marcreichman-pfi, versions 9.0.9, 9.1.10 and 9.2.4 are out with the fix!
marcreichman-pfi commentedon Jan 13, 2026
@l-trotta Thank you! I will give it a try shortly. Looks like a good fix.
marcreichman-pfi commentedon Jan 15, 2026
@l-trotta I tried to beat this up pretty well with my original heap size. I have not been able to blow it up in my usual pattern. I understand that if something throws certain types of Error/Exception on the entity preparation side we may still have this scenario occur, but knowing of it is half the battle.. :)
Thanks again for your continued investigation into this issue! I think we can close it, hopefully for good this time. 😄
l-trotta commentedon Jan 15, 2026
@marcreichman-pfi good to hear! thank you for helping me through this :)