Skip to content

Fix default connectTimeout of transportBuilder #1633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Fixed
- Fixed ScoreCombination's `parameters` structure ([#1594](https://github.com/opensearch-project/opensearch-java/pull/1594))
- Fixed default connectTimeout of transportBuilder ([#1633](https://github.com/opensearch-project/opensearch-java/pull/1633))

## [3.0.0] - 05/16/2025
### ⚠️ Breaking Changes ⚠️
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ public static ApacheHttpClient5TransportBuilder builder(HttpHost... hosts) {
private CloseableHttpAsyncClient createHttpClient() {
// default timeouts are all infinite
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
.setConnectTimeout(Timeout.ofMilliseconds(DEFAULT_CONNECT_TIMEOUT_MILLIS))
Copy link
Contributor

Choose a reason for hiding this comment

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

Let’s remove DEFAULT_CONNECT_TIMEOUT_MILLIS from this class since we don’t need it here now.

.setResponseTimeout(Timeout.ofMilliseconds(DEFAULT_RESPONSE_TIMEOUT_MILLIS));

if (requestConfigCallback != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.opensearch.client.transport.httpclient5;

import com.fasterxml.jackson.databind.JsonNode;
import org.apache.hc.client5.http.ConnectTimeoutException;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.util.Timeout;
import org.junit.Test;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch.core.SearchRequest;

import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

public class ApacheHttpClient5TransportBuilderTest {

@Test
public void timeOutTest() {

int expectTime = 10;
String expectMessage = expectTime + " MILLISECONDS";

ApacheHttpClient5Transport apacheHttpClient5Transport = ApacheHttpClient5TransportBuilder.builder(new HttpHost("10.255.255.1", 9200))
.setHttpClientConfigCallback(httpClientBuilder -> {

ConnectionConfig connectionConfig = ConnectionConfig.custom()
.setConnectTimeout(Timeout.ofMilliseconds(expectTime))
.build();

PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder
.create()
.setDefaultConnectionConfig(connectionConfig)
Copy link
Collaborator

Choose a reason for hiding this comment

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

@001mertiya could you clarify please what exactly you are trying to achieve here? The RequestConfig and ConnectionConfig are different configuration settings. If you are not satisfied with the RequestConfigsettings, use requestConfigCallback to set them to any value, including null

Copy link
Contributor Author

@playLeo playLeo Jun 27, 2025

Choose a reason for hiding this comment

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

@reta
The connectTimeout setting has been moved from RequestConfig to ConnectionConfig, and since the setting in RequestConfig has been deprecated, I think it should be removed. Also, the purpose of this test was to verify that the connectTimeout works correctly when a connection is established using the ConnectionConfig setting.

Copy link
Collaborator

@reta reta Jun 27, 2025

Choose a reason for hiding this comment

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

Thanks @playLeo , if it was deprecated and is not working anymore, we should make sure our API does switch to ConnectionConfig from RequestConfig

Copy link
Contributor Author

@playLeo playLeo Jun 29, 2025

Choose a reason for hiding this comment

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

here http5client commit link!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you, my point is: we have class RequestConfigCallback [1] that historically was used to configure timeouts in question. Now, since has timeout related methods deprecated, we could (and probably should) reflect that by introducing ConnectionConfigCallback to allow configuring these timeouts.

[1] https://github.com/opensearch-project/opensearch-java/blob/main/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5TransportBuilder.java#L376

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the detailed explanation. I understand. I have a question before I start working. It seems that you can configure RequestConfig and ConnectionConfig either by using HttpClientConfigCallback or by using RequestConfigCallback and ConnectionConfigCallback. Doesn’t having both ways to set these configurations create potential confusion? What is the benefit of using ConnectionConfigCallback?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks @playLeo , since the timeout properties from RequestConfig have been moved (and potentially removed), the ConnectionConfig is the way to configure those (we would need to document that).

Copy link
Contributor Author

@playLeo playLeo Jul 3, 2025

Choose a reason for hiding this comment

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

we could (and probably should) reflect that by introducing ConnectionConfigCallback to allow configuring these timeouts.

I think I did not explain it clearly. If we create a ConnectionConfigCallback we would then have two ways to set the ConnectionConfig one through the existing HttpClientConfigCallback and one through the new ConnectionConfigCallback so I am wondering if the ConnectionConfigCallback is really necessary.

I also know that for RequestConfig there are two ways to configure it using HttpClientConfigCallback and RequestConfigCallback but I am wondering if the RequestConfigCallback is really needed in that case too.

If there are two ways to configure this, wouldn’t that be confusing? Are you saying it’s fine as long as it’s documented properly?

Copy link
Collaborator

Choose a reason for hiding this comment

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

If there are two ways to configure this, wouldn’t that be confusing? Are you saying it’s fine as long as it’s documented properly?

Yeah, so the ConnectionConfigCallback is only useful when the caller does not want to deal with connection manager (since it is part of it). In this case, providing just ConnectionConfigCallback and not HttpClientConfigCallback would work just fine. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please take a look at the updated

.build();

return httpClientBuilder.setConnectionManager(connectionManager);
}).build();

OpenSearchClient osClient = new OpenSearchClient(apacheHttpClient5Transport);

ConnectTimeoutException exception = assertThrows(ConnectTimeoutException.class, () -> osClient.search(SearchRequest.of(sb -> sb.index("index")), JsonNode.class));

assertTrue("Expected timeout value not found in message", exception.getMessage().contains(expectMessage));
}
}
Loading