Skip to content

[ML] Inference API add configurable connection pool TTL #127585

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

Merged
merged 4 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,17 @@ public Collection<?> createComponents(PluginServices services) {
var inferenceServices = new ArrayList<>(inferenceServiceExtensions);
inferenceServices.add(this::getInferenceServiceFactories);

var inferenceServiceSettings = new ElasticInferenceServiceSettings(settings);
inferenceServiceSettings.init(services.clusterService());

// Create a separate instance of HTTPClientManager with its own SSL configuration (`xpack.inference.elastic.http.ssl.*`).
var elasticInferenceServiceHttpClientManager = HttpClientManager.create(
settings,
services.threadPool(),
services.clusterService(),
throttlerManager,
getSslService()
getSslService(),
inferenceServiceSettings.getConnectionTtl()
);

var elasticInferenceServiceRequestSenderFactory = new HttpRequestSender.Factory(
Expand All @@ -292,9 +296,6 @@ public Collection<?> createComponents(PluginServices services) {
);
elasicInferenceServiceFactory.set(elasticInferenceServiceRequestSenderFactory);

var inferenceServiceSettings = new ElasticInferenceServiceSettings(settings);
inferenceServiceSettings.init(services.clusterService());

var authorizationHandler = new ElasticInferenceServiceAuthorizationRequestHandler(
inferenceServiceSettings.getElasticInferenceServiceUrl(),
services.threadPool()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.Closeable;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.elasticsearch.core.Strings.format;
import static org.elasticsearch.xpack.inference.services.elastic.ElasticInferenceServiceSettings.ELASTIC_INFERENCE_SERVICE_SSL_CONFIGURATION_PREFIX;
Expand Down Expand Up @@ -112,14 +113,15 @@ public static HttpClientManager create(
ThreadPool threadPool,
ClusterService clusterService,
ThrottlerManager throttlerManager,
SSLService sslService
SSLService sslService,
TimeValue connectionTtl
) {
// Set the sslStrategy to ensure an encrypted connection, as Elastic Inference Service requires it.
SSLIOSessionStrategy sslioSessionStrategy = sslService.sslIOSessionStrategy(
sslService.getSSLConfiguration(ELASTIC_INFERENCE_SERVICE_SSL_CONFIGURATION_PREFIX)
);

PoolingNHttpClientConnectionManager connectionManager = createConnectionManager(sslioSessionStrategy);
PoolingNHttpClientConnectionManager connectionManager = createConnectionManager(sslioSessionStrategy, connectionTtl);
return new HttpClientManager(settings, connectionManager, threadPool, clusterService, throttlerManager);
}

Expand All @@ -146,7 +148,7 @@ public static HttpClientManager create(
this.addSettingsUpdateConsumers(clusterService);
}

private static PoolingNHttpClientConnectionManager createConnectionManager(SSLIOSessionStrategy sslStrategy) {
private static PoolingNHttpClientConnectionManager createConnectionManager(SSLIOSessionStrategy sslStrategy, TimeValue connectionTtl) {
ConnectingIOReactor ioReactor;
try {
var configBuilder = IOReactorConfig.custom().setSoKeepAlive(true);
Expand All @@ -162,7 +164,15 @@ private static PoolingNHttpClientConnectionManager createConnectionManager(SSLIO
.register("https", sslStrategy)
.build();

return new PoolingNHttpClientConnectionManager(ioReactor, registry);
return new PoolingNHttpClientConnectionManager(
ioReactor,
null,
registry,
null,
null,
Math.toIntExact(connectionTtl.getMillis()),
TimeUnit.MILLISECONDS
);
}

private static PoolingNHttpClientConnectionManager createConnectionManager() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,33 @@ public class ElasticInferenceServiceSettings {
Setting.Property.NodeScope
);

/**
* Total time to live (TTL) defines maximum life span of persistent connections regardless of their
* expiration setting. No persistent connection will be re-used past its TTL value.
* Using a TTL of -1 will disable the expiration of persistent connections (the idle connection evictor will still apply).
*/
public static final Setting<TimeValue> CONNECTION_TTL_SETTING = Setting.timeSetting(
"xpack.inference.elastic.http.connection_ttl",
TimeValue.timeValueSeconds(60),
Setting.Property.NodeScope
);

@Deprecated
private final String eisGatewayUrl;

private final String elasticInferenceServiceUrl;
private final boolean periodicAuthorizationEnabled;
private volatile TimeValue authRequestInterval;
private volatile TimeValue maxAuthorizationRequestJitter;
private final TimeValue connectionTtl;

public ElasticInferenceServiceSettings(Settings settings) {
eisGatewayUrl = EIS_GATEWAY_URL.get(settings);
elasticInferenceServiceUrl = ELASTIC_INFERENCE_SERVICE_URL.get(settings);
periodicAuthorizationEnabled = PERIODIC_AUTHORIZATION_ENABLED.get(settings);
authRequestInterval = AUTHORIZATION_REQUEST_INTERVAL.get(settings);
maxAuthorizationRequestJitter = MAX_AUTHORIZATION_REQUEST_JITTER.get(settings);
connectionTtl = CONNECTION_TTL_SETTING.get(settings);
}

/**
Expand Down Expand Up @@ -115,6 +128,10 @@ public TimeValue getMaxAuthorizationRequestJitter() {
return maxAuthorizationRequestJitter;
}

public TimeValue getConnectionTtl() {
return connectionTtl;
}

public static List<Setting<?>> getSettingsDefinitions() {
ArrayList<Setting<?>> settings = new ArrayList<>();
settings.add(EIS_GATEWAY_URL);
Expand All @@ -124,6 +141,7 @@ public static List<Setting<?>> getSettingsDefinitions() {
settings.add(PERIODIC_AUTHORIZATION_ENABLED);
settings.add(AUTHORIZATION_REQUEST_INTERVAL);
settings.add(MAX_AUTHORIZATION_REQUEST_JITTER);
settings.add(CONNECTION_TTL_SETTING);
return settings;
}

Expand Down