Skip to content

added OpenSearchClient as parameter to pass custom instance#24320

Open
renjth-81 wants to merge 2 commits into
apache:mainfrom
renjth-81:opensearch-custom-openSearchClient
Open

added OpenSearchClient as parameter to pass custom instance#24320
renjth-81 wants to merge 2 commits into
apache:mainfrom
renjth-81:opensearch-custom-openSearchClient

Conversation

@renjth-81

@renjth-81 renjth-81 commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Description

Added OpenSearchClient as a parameter to allow custom OpenSearchClient instance to be passed.

Target

  • [ ✓] I checked that the commit is targeting the correct branch (Camel 4 uses the main branch)

Tracking

  • If this is a large change, bug fix, or code improvement, I checked there is a JIRA issue filed for the change (usually before you start working on it).

Apache Camel coding standards and style

  • [ ✓] I checked that each commit in the pull request has a meaningful subject line and body.
  • [ ✓] I have run mvn clean install -DskipTests locally from root folder and I have committed all auto-generated changes.

AI-assisted contributions

  • [ x] If this PR includes AI-generated code, commits have proper co-authorship attribution (e.g., Co-authored-by trailers) and the PR description identifies the AI tool used.
@github-actions

Copy link
Copy Markdown
Contributor

🌟 Thank you for your contribution to the Apache Camel project! 🌟
🤖 CI automation will test this PR automatically.

🐫 Apache Camel Committers, please review the following items:

  • First-time contributors require MANUAL approval for the GitHub Actions to run
  • You can use the command /component-test (camel-)component-name1 (camel-)component-name2.. to request a test from the test bot although they are normally detected and executed by CI.
  • You can label PRs using skip-tests and test-dependents to fine-tune the checks executed by this PR.
  • Build and test logs are available in the summary page. Only Apache Camel committers have access to the summary.

⚠️ Be careful when sharing logs. Review their contents before sharing them publicly.

@davsclaus

Copy link
Copy Markdown
Contributor

you need to also rebuild camel-catalog, and some code in component and endpoint dsl

@davsclaus

Copy link
Copy Markdown
Contributor

There are uncommitted changes
HEAD detached at pull/24320/merge
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/opensearch.json
modified: dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/OpensearchComponentBuilderFactory.java
modified: dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/OpensearchEndpointBuilderFactory.java

@oscerd oscerd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(); then this.openSearchClient = createDefaultOpenSearchClient();
  • createDefaultOpenSearchClient() builds new OpenSearchClient(new RestClientTransport(client, ...)), but only calls startClient() when configuration.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 OpenSearchClient instance 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-XXXXX issue with the commit/PR title prefixed accordingly. Could you create one (component camel-opensearch) and update the title? The added missing files commit 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.

@davsclaus

Copy link
Copy Markdown
Contributor

creating a new client should ideally not be done in constructors but in doInit or doStart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment