From c100bf845e3703e4f6e73af03c82138016ca975f Mon Sep 17 00:00:00 2001 From: Andrey Burov Date: Tue, 17 Dec 2024 17:37:22 +0300 Subject: [PATCH] Use credentials for health checking in ClickHouseNodes --- .../com/clickhouse/client/ClickHouseNode.java | 15 ++++++- .../clickhouse/client/ClickHouseNodes.java | 42 ++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseNode.java b/clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseNode.java index 59275aa18..7edf39031 100644 --- a/clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseNode.java +++ b/clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseNode.java @@ -762,6 +762,19 @@ public static ClickHouseNode of(String uri) { * @return non-null node object */ public static ClickHouseNode of(String uri, Map options) { + return of(uri, options, null); + } + + /** + * Creates a node object using given URI. Same as + * {@code of(uri, ClickHouseProtocol.ANY)}. + * + * @param uri non-empty URI + * @param options default options + * @param defaultCredentials will be used if not specified in uri (optional) + * @return non-null node object + */ + public static ClickHouseNode of(String uri, Map options, ClickHouseCredentials defaultCredentials) { URI normalizedUri = normalize(uri, null); Map params = new LinkedHashMap<>(); @@ -791,7 +804,7 @@ public static ClickHouseNode of(String uri, Map options) { && scheme.equalsIgnoreCase("https")) { params.put(ClickHouseClientOption.SSL.getKey(), "true"); } - ClickHouseCredentials credentials = extract(normalizedUri.getRawUserInfo(), params, null); + ClickHouseCredentials credentials = extract(normalizedUri.getRawUserInfo(), params, defaultCredentials); return new ClickHouseNode(normalizedUri.getHost(), protocol, port, credentials, params, tags); } diff --git a/clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseNodes.java b/clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseNodes.java index 4e1679003..cac5bc490 100644 --- a/clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseNodes.java +++ b/clickhouse-client/src/main/java/com/clickhouse/client/ClickHouseNodes.java @@ -54,6 +54,19 @@ public class ClickHouseNodes implements ClickHouseNodeManager { * @return non-null list of nodes */ static ClickHouseNodes create(String endpoints, Map defaultOptions) { + return create(endpoints, defaultOptions, null); + } + + /** + * Creates list of managed {@link ClickHouseNode} for load balancing and + * fail-over. + * + * @param endpoints non-empty URIs separated by comma + * @param defaultOptions default options + * @param credentials will be used for health checking (optional) + * @return non-null list of nodes + */ + static ClickHouseNodes create(String endpoints, Map defaultOptions, ClickHouseCredentials credentials) { int index = endpoints.indexOf(ClickHouseNode.SCHEME_DELIMITER); String defaultProtocol = ((ClickHouseProtocol) ClickHouseDefaults.PROTOCOL .getEffectiveDefaultValue()).name(); @@ -145,7 +158,7 @@ static ClickHouseNodes create(String endpoints, Map defaultOptions) { } ClickHouseNode defaultNode = ClickHouseNode.of(defaultProtocol + "://localhost" + defaultParams, - defaultOptions); + defaultOptions, credentials); List nodes = new LinkedList<>(); for (String uri : list) { nodes.add(ClickHouseNode.of(uri, defaultNode)); @@ -268,6 +281,18 @@ public static ClickHouseNodes of(String endpoints) { return of(endpoints, Collections.emptyMap()); } + /** + * Gets or creates list of managed {@link ClickHouseNode} for load balancing + * and fail-over. + * + * @param endpoints non-empty URIs separated by comma + * @param credentials will be used for health checking (optional) + * @return non-null list of nodes + */ + public static ClickHouseNodes of(String endpoints, ClickHouseCredentials credentials) { + return of(endpoints, Collections.emptyMap(), credentials); + } + /** * Gets or creates list of managed {@link ClickHouseNode} for load balancing * and fail-over. @@ -277,8 +302,21 @@ public static ClickHouseNodes of(String endpoints) { * @return non-null list of nodes */ public static ClickHouseNodes of(String endpoints, Map options) { + return of(endpoints, options, null); + } + + /** + * Gets or creates list of managed {@link ClickHouseNode} for load balancing + * and fail-over. + * + * @param endpoints non-empty URIs separated by comma + * @param options default options + * @param credentials will be used for health checking (optional) + * @return non-null list of nodes + */ + public static ClickHouseNodes of(String endpoints, Map options, ClickHouseCredentials credentials) { return cache.computeIfAbsent(buildCacheKey(ClickHouseChecker.nonEmpty(endpoints, "Endpoints"), options), - k -> create(endpoints, options)); + k -> create(endpoints, options, credentials)); } /**