added OpenSearchClient as parameter to pass custom instance#24320
added OpenSearchClient as parameter to pass custom instance#24320renjth-81 wants to merge 2 commits into
Conversation
|
🌟 Thank you for your contribution to the Apache Camel project! 🌟 🐫 Apache Camel Committers, please review the following items:
|
|
you need to also rebuild camel-catalog, and some code in component and endpoint dsl |
|
There are uncommitted changes |
oscerd
left a comment
There was a problem hiding this comment.
Thanks for the contribution, @renjth-81 — being able to pass a custom OpenSearchClient is a useful addition, and the generated catalog/DSL files are all correctly regenerated. However, there's a blocking correctness problem in the current implementation that CI is also catching, so I'm requesting changes.
Blocker: the cached transport captures a null RestClient → NPE on every exchange
OpensearchProducer now builds and caches openSearchClient in its constructor:
- the constructor sets
this.client = endpoint.getClient();thenthis.openSearchClient = createDefaultOpenSearchClient(); createDefaultOpenSearchClient()buildsnew OpenSearchClient(new RestClientTransport(client, ...)), but only callsstartClient()whenconfiguration.isDisconnect() && client == null.
In the standard configuration (a RestClient built from hostAddresses, no autowired client), endpoint.getClient() is null at construction time — the component creates the endpoint with a null client by default (OpensearchComponent.java:91), and the real RestClient is created lazily later in doStart() → startClient() (OpensearchProducer.java, the double-checked-locking block around lines 452–467). disconnect defaults to false (OpensearchConfiguration.java:60), so createDefaultOpenSearchClient() does not start the client and ends up wrapping a RestClientTransport around a null RestClient.
process() then does OpenSearchTransport transport = openSearchClient._transport(); and reuses that cached transport, so every exchange dereferences the null RestClient:
java.lang.NullPointerException: Cannot invoke
"org.opensearch.client.RestClient.performRequestAsync(...)" because "this.restClient" is null
This is exactly what CI is failing on — build (17, false) and build (25, false) are both red, with OpensearchPingIT.testPing and all OpensearchBulkIT.* failing with this NPE. The user-supplied-client path (endpoint.getOpenSearchClient() != null) works, but the default path — which all of the component's own integration tests exercise — is broken.
Suggested direction
Resolve the transport lazily rather than in the constructor: keep using the user-supplied OpenSearchClient when one is provided, but for the default case build the transport in process() / after doStart() once client is non-null (as the original code did). Please also keep the disconnect=true semantics in mind — the old process() rebuilt the transport per call and cleanup() closes and nulls the client after each exchange (OpensearchProducer.java, around lines 437–445), so a single cached transport would also break disconnect mode after the first exchange.
Other items
- Tests: please add a test proving a custom
OpenSearchClientinstance is actually honored at runtime — the new option currently has no coverage, and the existing integration tests regressed. - Jira: apache/camel changes should be tracked by a
CAMEL-XXXXXissue with the commit/PR title prefixed accordingly. Could you create one (componentcamel-opensearch) and update the title? Theadded missing filescommit subject could also be made more descriptive.
For context: this PR currently shows as approved, but that review was on the very first commit (62b5af1), before the generated-files commit and before CI ran — so it predates the failures above.
Thanks again for working on this — happy to re-review once the transport init is made lazy and CI is green.
Reviewed with Claude Code on behalf of Andrea Cosentino (@oscerd). This review was generated by an AI agent and may contain inaccuracies; please verify all suggestions before applying.
|
creating a new client should ideally not be done in constructors but in doInit or doStart |
Description
Added OpenSearchClient as a parameter to allow custom OpenSearchClient instance to be passed.
Target
mainbranch)Tracking
Apache Camel coding standards and style
mvn clean install -DskipTestslocally from root folder and I have committed all auto-generated changes.AI-assisted contributions
Co-authored-bytrailers) and the PR description identifies the AI tool used.