From 7f09be8ac6c9a8f69142d6abac7a9cc7b6845def Mon Sep 17 00:00:00 2001 From: Ryan Tu Date: Wed, 24 Nov 2021 10:58:21 +0800 Subject: [PATCH] Validate stale connection in HTTPClient pool. --- .../settings/ClickHouseConnectionSettings.java | 1 + .../clickhouse/settings/ClickHouseProperties.java | 12 ++++++++++++ .../clickhouse/util/ClickHouseHttpClientBuilder.java | 1 + 3 files changed, 14 insertions(+) diff --git a/clickhouse-jdbc/src/main/java/ru/yandex/clickhouse/settings/ClickHouseConnectionSettings.java b/clickhouse-jdbc/src/main/java/ru/yandex/clickhouse/settings/ClickHouseConnectionSettings.java index 1b59f6d66..09129e534 100644 --- a/clickhouse-jdbc/src/main/java/ru/yandex/clickhouse/settings/ClickHouseConnectionSettings.java +++ b/clickhouse-jdbc/src/main/java/ru/yandex/clickhouse/settings/ClickHouseConnectionSettings.java @@ -22,6 +22,7 @@ public enum ClickHouseConnectionSettings implements DriverPropertyCreator { /** * for ConnectionManager */ + VALIDATE_AFTER_INACTIVITY_MILLIS("validateAfterInactivityMillis", 3 * 1000, "period of inactivity in milliseconds after which persistent connections must be re-validated, this check helps detect connections that have become stale (half-closed) while kept inactive in the pool. "), TIME_TO_LIVE_MILLIS("timeToLiveMillis", 60 * 1000, ""), DEFAULT_MAX_PER_ROUTE("defaultMaxPerRoute", 500, ""), MAX_TOTAL("maxTotal", 10000, ""), diff --git a/clickhouse-jdbc/src/main/java/ru/yandex/clickhouse/settings/ClickHouseProperties.java b/clickhouse-jdbc/src/main/java/ru/yandex/clickhouse/settings/ClickHouseProperties.java index 9e8d831d7..d54ffdd55 100644 --- a/clickhouse-jdbc/src/main/java/ru/yandex/clickhouse/settings/ClickHouseProperties.java +++ b/clickhouse-jdbc/src/main/java/ru/yandex/clickhouse/settings/ClickHouseProperties.java @@ -17,6 +17,7 @@ public class ClickHouseProperties { private int socketTimeout; private int connectionTimeout; private int timeToLiveMillis; + private int validateAfterInactivityMillis; private int defaultMaxPerRoute; private int maxTotal; private int maxRetries; @@ -110,6 +111,7 @@ public ClickHouseProperties(Properties info) { this.socketTimeout = (Integer)getSetting(info, ClickHouseConnectionSettings.SOCKET_TIMEOUT); this.connectionTimeout = (Integer)getSetting(info, ClickHouseConnectionSettings.CONNECTION_TIMEOUT); this.timeToLiveMillis = (Integer)getSetting(info, ClickHouseConnectionSettings.TIME_TO_LIVE_MILLIS); + this.validateAfterInactivityMillis = (Integer)getSetting(info, ClickHouseConnectionSettings.VALIDATE_AFTER_INACTIVITY_MILLIS); this.defaultMaxPerRoute = (Integer)getSetting(info, ClickHouseConnectionSettings.DEFAULT_MAX_PER_ROUTE); this.maxTotal = (Integer)getSetting(info, ClickHouseConnectionSettings.MAX_TOTAL); this.maxRetries = (Integer)getSetting(info, ClickHouseConnectionSettings.MAX_RETRIES); @@ -177,6 +179,7 @@ public Properties asProperties() { ret.put(ClickHouseConnectionSettings.SOCKET_TIMEOUT.getKey(), String.valueOf(socketTimeout)); ret.put(ClickHouseConnectionSettings.CONNECTION_TIMEOUT.getKey(), String.valueOf(connectionTimeout)); ret.put(ClickHouseConnectionSettings.TIME_TO_LIVE_MILLIS.getKey(), String.valueOf(timeToLiveMillis)); + ret.put(ClickHouseConnectionSettings.VALIDATE_AFTER_INACTIVITY_MILLIS.getKey(), String.valueOf(validateAfterInactivityMillis)); ret.put(ClickHouseConnectionSettings.DEFAULT_MAX_PER_ROUTE.getKey(), String.valueOf(defaultMaxPerRoute)); ret.put(ClickHouseConnectionSettings.MAX_TOTAL.getKey(), String.valueOf(maxTotal)); ret.put(ClickHouseConnectionSettings.MAX_RETRIES.getKey(), String.valueOf(maxRetries)); @@ -247,6 +250,7 @@ public ClickHouseProperties(ClickHouseProperties properties) { setSocketTimeout(properties.socketTimeout); setConnectionTimeout(properties.connectionTimeout); setTimeToLiveMillis(properties.timeToLiveMillis); + setValidateAfterInactivityMillis(properties.validateAfterInactivityMillis); setDefaultMaxPerRoute(properties.defaultMaxPerRoute); setMaxTotal(properties.maxTotal); setMaxRetries(properties.maxRetries); @@ -566,6 +570,14 @@ public void setTimeToLiveMillis(int timeToLiveMillis) { this.timeToLiveMillis = timeToLiveMillis; } + public int getValidateAfterInactivityMillis() { + return validateAfterInactivityMillis; + } + + public void setValidateAfterInactivityMillis(int validateAfterInactivityMillis) { + this.validateAfterInactivityMillis = validateAfterInactivityMillis; + } + public int getDefaultMaxPerRoute() { return defaultMaxPerRoute; } diff --git a/clickhouse-jdbc/src/main/java/ru/yandex/clickhouse/util/ClickHouseHttpClientBuilder.java b/clickhouse-jdbc/src/main/java/ru/yandex/clickhouse/util/ClickHouseHttpClientBuilder.java index a7fd7956d..782d07896 100644 --- a/clickhouse-jdbc/src/main/java/ru/yandex/clickhouse/util/ClickHouseHttpClientBuilder.java +++ b/clickhouse-jdbc/src/main/java/ru/yandex/clickhouse/util/ClickHouseHttpClientBuilder.java @@ -151,6 +151,7 @@ private PoolingHttpClientConnectionManager getConnectionManager() TimeUnit.MILLISECONDS ); + connectionManager.setValidateAfterInactivity(properties.getValidateAfterInactivityMillis()); connectionManager.setDefaultMaxPerRoute(properties.getDefaultMaxPerRoute()); connectionManager.setMaxTotal(properties.getMaxTotal()); connectionManager.setDefaultConnectionConfig(getConnectionConfig());