-
Notifications
You must be signed in to change notification settings - Fork 817
[SOLR-18130][WIP] Unified connection string CloudSolrClient.Builder implementation #4260
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.solr.client.solrj.impl; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.List; | ||
| import org.apache.solr.common.util.StrUtils; | ||
|
|
||
| /** Universal connection string parser logic. */ | ||
| public record CloudClientConnectionString(boolean isZk, List<String> quorumItems, String zkChroot) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets drop the suffix String here :-)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And IMO it'd be more at home as an inner class of CloudSolrClient since it's only used for it. |
||
|
|
||
| public CloudClientConnectionString { | ||
| if (quorumItems == null || quorumItems.isEmpty()) { | ||
| throw new IllegalArgumentException("No valid hosts/urls found"); | ||
| } | ||
| } | ||
|
|
||
| public static boolean isValidHttpUrl(String s) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps it's a matter of taste / preference but if I were coding this, I simply wouldn't bother with this validation. The user will get an error eventually anyway if they screw up so badly as to malform their URL. |
||
| if (s == null || s.isBlank()) return false; | ||
|
|
||
| try { | ||
| URI uri = new URI(s); | ||
|
|
||
| return ("http".equalsIgnoreCase(uri.getScheme()) || "https".equalsIgnoreCase(uri.getScheme())) | ||
| && uri.getHost() != null; | ||
|
|
||
| } catch (Exception e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public static CloudClientConnectionString parse(String connectionString) { | ||
| if (connectionString == null || connectionString.trim().isEmpty()) { | ||
| throw new IllegalArgumentException("Connection string must not be null or empty"); | ||
| } | ||
| connectionString = connectionString.trim(); | ||
| if (connectionString.contains("://")) { | ||
| return parseHttpQuorum(connectionString); | ||
| } | ||
| return parseZkQuorum(connectionString); | ||
| } | ||
|
|
||
| private static CloudClientConnectionString parseZkQuorum(String connectionString) { | ||
| String zkChroot = null; | ||
| String zkHosts = connectionString; | ||
| int slashIndex = connectionString.indexOf('/'); | ||
| if (slashIndex != -1) { | ||
| zkHosts = connectionString.substring(0, slashIndex); | ||
| zkChroot = connectionString.substring(slashIndex); | ||
| } | ||
| List<String> quorumItems = StrUtils.split(zkHosts, ',').stream().map(String::trim).toList(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what test failed due to spaces within the string? IMO that'd be erroneous. |
||
| for (String host : quorumItems) { | ||
| if (host == null || host.isBlank()) { | ||
| throw new IllegalArgumentException("Empty host in Zookeeper connection string"); | ||
| } | ||
| } | ||
| return new CloudClientConnectionString(true, quorumItems, zkChroot); | ||
| } | ||
|
|
||
| private static CloudClientConnectionString parseHttpQuorum(String connectionString) { | ||
| List<String> httpUrls = | ||
| StrUtils.split(connectionString, ',').stream().map(String::trim).toList(); | ||
| for (String part : httpUrls) { | ||
| if (!isValidHttpUrl(part)) { | ||
| throw new IllegalArgumentException("Malformed HTTP(S) URL: " + part); | ||
| } | ||
| } | ||
| return new CloudClientConnectionString(false, httpUrls, null); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -241,6 +241,44 @@ public Builder(ClusterStateProvider stateProvider) { | |
| this.stateProvider = stateProvider; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a client builder based on a connection string of 2 possible formats: | ||
| * | ||
| * <ul> | ||
| * <li>ZooKeeper connection string (optionally with chroot), e.g. {@code | ||
| * zk1:2181,zk2:2181,zk3:2181/solr} | ||
| * <li>Comma-separated list of Solr node base URLs (HTTP or HTTPS), e.g. {@code | ||
| * http://solr1:8983/solr,http://solr2:8983/solr} | ||
| * </ul> | ||
| * | ||
| * <p>Usage examples: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from this point forward, it's superfluous. The examples ought to speak for themselves. |
||
| * | ||
| * <pre>{@code | ||
| * // ZooKeeper with chroot | ||
| * new CloudSolrClient.Builder("zk1:2181,zk2:2181,zk3:2181/solr"); | ||
| * | ||
| * // ZooKeeper without chroot | ||
| * new CloudSolrClient.Builder("zk1:2181,zk2:2181,zk3:2181"); | ||
| * | ||
| * // Direct HTTPS connections | ||
| * new CloudSolrClient.Builder("https://solr1:8983/solr,https://solr2:8983/solr"); | ||
| * }</pre> | ||
| * | ||
| * @param connectionString a string specifying either ZooKeeper connection string or HTTP(S) | ||
| * Solr URLs | ||
| * @throws IllegalArgumentException if string is null, empty, or malformed | ||
| * @since 11.0.0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LOL drop the |
||
| */ | ||
| public Builder(String connectionString) { | ||
| CloudClientConnectionString connStr = CloudClientConnectionString.parse(connectionString); | ||
| if (connStr.isZk()) { | ||
| this.zkHosts = connStr.quorumItems(); | ||
| this.zkChroot = connStr.zkChroot(); | ||
| } else { | ||
| this.solrUrls = connStr.quorumItems(); | ||
| } | ||
| } | ||
|
|
||
| /** Whether to use the default ZK ACLs when building a ZK Client. */ | ||
| public Builder canUseZkACLs(boolean canUseZkACLs) { | ||
| this.canUseZkACLs = canUseZkACLs; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the chroot should be a field of cloudClientConnectionString
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is no chroot - is zkHosts joined via comma.
I can replace it by
String zkHostNoChroot = connectionString.substring(0,connectionString.length()-cloudClientConnectionString.zkChroot().length());Or
String zkHostNoChroot = connectionString.substring(0, connectionString.length() - cloudClientConnectionString.zkChroot().length());But IMO, currently implementation seems more pretty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cloudClientConnectionStringshould contain the chroot if there is one. And you just parsed it.FWIW the name
cloudClientConnectionStringis terrible... it is in fact not a string but a record