shashank created CAMEL-25907:
--------------------------------
Summary: camel-elasticsearch, camel-opensearch - a producer closes
the client of the component, so all endpoints of the component fail every
request after one route stops
Key: CAMEL-25907
URL: https://issues.apache.org/jira/browse/CAMEL-25907
Project: Camel
Issue Type: Bug
Components: camel-elasticsearch, camel-opensearch
Reporter: shashank
Assignee: shashank
{{ElasticsearchProducer}} and {{OpensearchProducer}} (main 74ee085f7d64) are
handed the {{RestClient}} of the component ({{client}} option,
{{@Metadata(autowired = true)}}, so a single {{RestClient}} bean in the
registry is used without any configuration) through the endpoint, and close it
as if it were their own:
{code:java}
protected void doStop() throws Exception {
if (client != null) {
LOG.info("Disconnecting from Elasticsearch cluster: {}",
configuration.getClusterName());
client.close();
...
{code}
(OpenSearch: {{if (client != null && openSearchClient == null)}}.) With
{{disconnect=true}} the cleanup after each request closes the transport and the
client as well.
So once one route with such a producer is stopped or removed (route reload, JMX
/ CLI, a route policy that stops routes, a {{ProducerTemplate}} that is
stopped), the shared client is closed, and every other endpoint of the
component fails every request from then on: {{Request execution cancelled}}
(Elasticsearch) or {{Connection pool shut down}} (OpenSearch). The route that
was stopped fails the same way after it is started again, since its new
producer gets the same closed client. Only a restart of the application helps.
With a client that is a Spring bean, Camel also closes it under the rest of the
application. A producer that created its own client from {{hostAddresses}} does
not clear it either, so the same producer instance stopped and started again
keeps using its closed client.
CAMEL-13763 reported the first case for the XML route reload of Camel 2 in
2019; it was closed as Won't Fix because that reload feature was removed, with
the note that the start/stop logic could be looked at. Routes are reloaded and
restarted in Camel 4 in many ways. The sibling
{{camel-elasticsearch-rest-client}} closes only a client it created
({{createdRestClient}}), as camel-clickhouse and camel-kudu do.
*Fix*: both producers record whether they created the client; {{doStop}} and
the {{disconnect=true}} cleanup close only that client and clear it and the
sniffer, so the next start creates a new one. The client of the component is
left to the application (upgrade note in the 4.24 guide, as Camel no longer
closes it at shutdown either).
*Tests*: new {{ElasticsearchProducerClientLifecycleTest}} and
{{OpensearchProducerClientLifecycleTest}} (4 each, a JDK {{HttpServer}} answers
the ping as a node does). {{stoppingARouteKeepsTheClientOfTheComponent}}
(client autowired from the registry, routes {{a}} and {{b}}, {{a}} stopped,
request on {{b}}) and {{disconnectKeepsTheClientOfTheComponent}} fail on main
with a {{CamelExecutionException}} caused by {{Request execution cancelled}} /
{{Connection pool shut down}}; {{stoppedProducerClosesTheClientItCreated}}
fails on main at the ping after the producer was started again.
{{restartedRouteCreatesANewClient}} is a control that passes on main (a route
restart creates a new producer). Unit tests with the fix: camel-elasticsearch
23 and camel-opensearch 23 run, 0 failures.
*How it was found*: variant analysis of CAMEL-25851, CAMEL-25852 and
CAMEL-25883 (a client, channel or handle of the component that one endpoint
closes under the others), then the JUnit tests above.
*Affected*: main, camel-4.23.x, camel-4.22.x and camel-4.18.x (the same
unconditional close in {{doStop}} of both producers; on 4.18.x OpenSearch has
no {{openSearchClient}} option; read, not run).
_Filed with Claude Code on behalf of allthingssecurity._
--
This message was sent by Atlassian Jira
(v8.20.10#820010)