Skip to content

Commit

Permalink
Use credentials for health checking in ClickHouseNodes
Browse files Browse the repository at this point in the history
  • Loading branch information
burov4j committed Dec 17, 2024
1 parent 20babb5 commit c100bf8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> params = new LinkedHashMap<>();
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -145,7 +158,7 @@ static ClickHouseNodes create(String endpoints, Map<?, ?> defaultOptions) {
}

ClickHouseNode defaultNode = ClickHouseNode.of(defaultProtocol + "://localhost" + defaultParams,
defaultOptions);
defaultOptions, credentials);
List<ClickHouseNode> nodes = new LinkedList<>();
for (String uri : list) {
nodes.add(ClickHouseNode.of(uri, defaultNode));
Expand Down Expand Up @@ -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.
Expand All @@ -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));
}

/**
Expand Down

0 comments on commit c100bf8

Please sign in to comment.