Skip to content

Commit 4c3ceae

Browse files
authored
[ML] Set Connect Timeout to 5s (#123272)
Reduced connection timeout from infinite to a system configurable setting that defaults to 5s. Increased EIS auth token timeout from 30s to 1m.
1 parent c7bcdd3 commit 4c3ceae

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

docs/changelog/123272.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 123272
2+
summary: Set Connect Timeout to 5s
3+
area: Machine Learning
4+
type: bug
5+
issues: []

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/external/http/HttpClient.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package org.elasticsearch.xpack.inference.external.http;
99

1010
import org.apache.http.HttpResponse;
11+
import org.apache.http.client.config.RequestConfig;
1112
import org.apache.http.client.protocol.HttpClientContext;
1213
import org.apache.http.concurrent.FutureCallback;
1314
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
@@ -56,14 +57,18 @@ public static HttpClient create(
5657
PoolingNHttpClientConnectionManager connectionManager,
5758
ThrottlerManager throttlerManager
5859
) {
59-
CloseableHttpAsyncClient client = createAsyncClient(Objects.requireNonNull(connectionManager));
60+
var client = createAsyncClient(Objects.requireNonNull(connectionManager), Objects.requireNonNull(settings));
6061

6162
return new HttpClient(settings, client, threadPool, throttlerManager);
6263
}
6364

64-
private static CloseableHttpAsyncClient createAsyncClient(PoolingNHttpClientConnectionManager connectionManager) {
65-
HttpAsyncClientBuilder clientBuilder = HttpAsyncClientBuilder.create();
66-
clientBuilder.setConnectionManager(connectionManager);
65+
private static CloseableHttpAsyncClient createAsyncClient(
66+
PoolingNHttpClientConnectionManager connectionManager,
67+
HttpSettings settings
68+
) {
69+
var requestConfig = RequestConfig.custom().setConnectTimeout(settings.connectionTimeout()).build();
70+
71+
var clientBuilder = HttpAsyncClientBuilder.create().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig);
6772
// The apache client will be shared across all connections because it can be expensive to create it
6873
// so we don't want to support cookies to avoid accidental authentication for unauthorized users
6974
clientBuilder.disableCookieManagement();

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/external/http/HttpSettings.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.common.settings.Settings;
1313
import org.elasticsearch.common.unit.ByteSizeUnit;
1414
import org.elasticsearch.common.unit.ByteSizeValue;
15+
import org.elasticsearch.core.TimeValue;
1516

1617
import java.util.List;
1718
import java.util.Objects;
@@ -27,12 +28,21 @@ public class HttpSettings {
2728
Setting.Property.Dynamic
2829
);
2930

31+
// The time we wait for a connection to establish
32+
public static final Setting<TimeValue> CONNECTION_TIMEOUT = Setting.timeSetting(
33+
"xpack.inference.http.connect_timeout",
34+
TimeValue.timeValueSeconds(5),
35+
Setting.Property.NodeScope
36+
);
37+
3038
private volatile ByteSizeValue maxResponseSize;
39+
private final int connectionTimeout;
3140

3241
public HttpSettings(Settings settings, ClusterService clusterService) {
3342
Objects.requireNonNull(clusterService);
3443
Objects.requireNonNull(settings);
3544
maxResponseSize = MAX_HTTP_RESPONSE_SIZE.get(settings);
45+
connectionTimeout = Math.toIntExact(CONNECTION_TIMEOUT.get(settings).getMillis());
3646

3747
clusterService.getClusterSettings().addSettingsUpdateConsumer(MAX_HTTP_RESPONSE_SIZE, this::setMaxResponseSize);
3848
}
@@ -41,11 +51,15 @@ public ByteSizeValue getMaxResponseSize() {
4151
return maxResponseSize;
4252
}
4353

54+
public int connectionTimeout() {
55+
return connectionTimeout;
56+
}
57+
4458
private void setMaxResponseSize(ByteSizeValue maxResponseSize) {
4559
this.maxResponseSize = maxResponseSize;
4660
}
4761

4862
public static List<Setting<?>> getSettingsDefinitions() {
49-
return List.of(MAX_HTTP_RESPONSE_SIZE);
63+
return List.of(MAX_HTTP_RESPONSE_SIZE, CONNECTION_TIMEOUT);
5064
}
5165
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/elastic/authorization/ElasticInferenceServiceAuthorizationHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.concurrent.CountDownLatch;
3030
import java.util.concurrent.TimeUnit;
3131

32-
import static org.elasticsearch.xpack.core.inference.action.InferenceAction.Request.DEFAULT_TIMEOUT;
3332
import static org.elasticsearch.xpack.inference.services.elastic.ElasticInferenceService.ELASTIC_INFERENCE_SERVICE_IDENTIFIER;
3433

3534
/**
@@ -39,6 +38,7 @@ public class ElasticInferenceServiceAuthorizationHandler {
3938

4039
private static final String FAILED_TO_RETRIEVE_MESSAGE =
4140
"Failed to retrieve the authorization information from the Elastic Inference Service.";
41+
private static final TimeValue DEFAULT_AUTH_TIMEOUT = TimeValue.timeValueMinutes(1);
4242
private static final ResponseHandler AUTH_RESPONSE_HANDLER = createAuthResponseHandler();
4343

4444
private static ResponseHandler createAuthResponseHandler() {
@@ -110,7 +110,7 @@ public void getAuthorization(ActionListener<ElasticInferenceServiceAuthorization
110110

111111
var request = new ElasticInferenceServiceAuthorizationRequest(baseUrl, getCurrentTraceInfo());
112112

113-
sender.sendWithoutQueuing(logger, request, AUTH_RESPONSE_HANDLER, DEFAULT_TIMEOUT, newListener);
113+
sender.sendWithoutQueuing(logger, request, AUTH_RESPONSE_HANDLER, DEFAULT_AUTH_TIMEOUT, newListener);
114114
} catch (Exception e) {
115115
logger.warn(Strings.format("Retrieving the authorization information encountered an exception: %s", e));
116116
requestCompleteLatch.countDown();

0 commit comments

Comments
 (0)