dsmiley commented on code in PR #4260:
URL: https://github.com/apache/solr/pull/4260#discussion_r3034399564
##########
solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java:
##########
@@ -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:
Review Comment:
from this point forward, it's superfluous. The examples ought to speak for
themselves.
##########
solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudClientConnectionString.java:
##########
@@ -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) {
+
+ public CloudClientConnectionString {
+ if (quorumItems == null || quorumItems.isEmpty()) {
+ throw new IllegalArgumentException("No valid hosts/urls found");
+ }
+ }
+
+ public static boolean isValidHttpUrl(String s) {
Review Comment:
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.
##########
solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudClientConnectionString.java:
##########
@@ -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) {
Review Comment:
And IMO it'd be more at home as an inner class of CloudSolrClient since it's
only used for it.
##########
solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/SolrClientCache.java:
##########
@@ -77,29 +77,33 @@ public void setDefaultZKHost(String zkHost) {
}
}
- public synchronized CloudSolrClient getCloudSolrClient(String zkHost) {
+ public synchronized CloudSolrClient getCloudSolrClient(String
connectionString) {
ensureOpen();
- Objects.requireNonNull(zkHost, "ZooKeeper host cannot be null!");
- if (solrClients.containsKey(zkHost)) {
- return (CloudSolrClient) solrClients.get(zkHost);
+ Objects.requireNonNull(connectionString, "Connection string cannot be
null!");
+ if (solrClients.containsKey(connectionString)) {
+ return (CloudSolrClient) solrClients.get(connectionString);
}
// Can only use ZK ACLs if there is a default ZK Host, and the given ZK
host contains that
// default.
// Basically the ZK ACLs are assumed to be only used for the default ZK
host,
// thus we should only provide the ACLs to that Zookeeper instance.
- String zkHostNoChroot = zkHost.split("/")[0];
- boolean canUseACLs =
-
Optional.ofNullable(defaultZkHost.get()).map(zkHostNoChroot::equals).orElse(false);
+ boolean canUseACLs = false;
+ CloudClientConnectionString cloudClientConnectionString =
+ CloudClientConnectionString.parse(connectionString);
+ if (cloudClientConnectionString.isZk()) {
+ String zkHostNoChroot = connectionString.split("/")[0];
Review Comment:
`cloudClientConnectionString` should contain the chroot if there is one.
And you _just_ parsed it.
FWIW the name `cloudClientConnectionString` is terrible... it is in fact
**not** a string but a record
##########
solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudClientConnectionString.java:
##########
@@ -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) {
Review Comment:
Lets drop the suffix String here :-)
##########
solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudClientConnectionString.java:
##########
@@ -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) {
+
+ public CloudClientConnectionString {
+ if (quorumItems == null || quorumItems.isEmpty()) {
+ throw new IllegalArgumentException("No valid hosts/urls found");
+ }
+ }
+
+ public static boolean isValidHttpUrl(String s) {
+ 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();
Review Comment:
what test failed due to spaces within the string? IMO that'd be erroneous.
##########
solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java:
##########
@@ -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:
+ *
+ * <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
Review Comment:
LOL drop the `@since`
##########
solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudClientConnectionStringTest.java:
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.util.Collection;
+import java.util.List;
+import java.util.Set;
+import org.apache.solr.SolrTestCase;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class CloudClientConnectionStringTest extends SolrTestCase {
+
+ private static <T> boolean collectionEqual(Collection<T> coll, Collection<T>
coll2) {
+ return coll.size() == coll2.size()
+ && Set.copyOf(coll).containsAll(coll2)
+ && Set.copyOf(coll2).containsAll(coll);
+ }
+
+ @Test
+ public void testBuildQuorumForZk() {
+ CloudClientConnectionString parsed =
+
CloudClientConnectionString.parse("zookeeper1:2181,zookeeper2:2181,zookeeper3:2181/solr");
+ Assert.assertTrue(parsed.isZk());
+ List<String> expectedQuorum = List.of("zookeeper1:2181",
"zookeeper2:2181", "zookeeper3:2181");
+ Assert.assertTrue(collectionEqual(expectedQuorum, parsed.quorumItems()));
+ Assert.assertEquals("/solr", parsed.zkChroot());
+ }
+
+ @Test
+ public void testBuildQuorumForZkIpV4() {
+ CloudClientConnectionString parsed =
+ CloudClientConnectionString.parse(
+ "192.194.10.10:2181,192.194.10.11:2181,192.194.10.12:2181/solr");
+ Assert.assertTrue(parsed.isZk());
+ List<String> expectedQuorum =
+ List.of("192.194.10.10:2181", "192.194.10.11:2181",
"192.194.10.12:2181");
+ Assert.assertTrue(collectionEqual(expectedQuorum, parsed.quorumItems()));
+ Assert.assertEquals("/solr", parsed.zkChroot());
+ }
+
+ @Test
+ public void testBuildQuorumForZkIpV6() {
+ CloudClientConnectionString parsed =
+ CloudClientConnectionString.parse(
+ "[2001:db8:85a3::8a2e:370:7334]:2181,"
+ + "[2001:db8:85a3::8a2e:370:7335]:2181,"
+ + "[2001:db8:85a3::8a2e:370:7336]:2181/solr");
+ Assert.assertTrue(parsed.isZk());
+ List<String> expectedQuorum =
+ List.of(
+ "[2001:db8:85a3::8a2e:370:7334]:2181",
+ "[2001:db8:85a3::8a2e:370:7335]:2181",
+ "[2001:db8:85a3::8a2e:370:7336]:2181");
+ Assert.assertTrue(collectionEqual(expectedQuorum, parsed.quorumItems()));
+ Assert.assertEquals("/solr", parsed.zkChroot());
+ }
+
+ @Test
+ public void testBuildQuorumForZkNoChroot() {
+ CloudClientConnectionString parsed =
+
CloudClientConnectionString.parse("zookeeper1:2181,zookeeper2:2181,zookeeper3:2181");
+ Assert.assertTrue(parsed.isZk());
+ List<String> expectedQuorum = List.of("zookeeper1:2181",
"zookeeper2:2181", "zookeeper3:2181");
+ Assert.assertTrue(collectionEqual(expectedQuorum, parsed.quorumItems()));
+ Assert.assertNull(parsed.zkChroot());
+ }
+
+ @Test
+ public void testBuildQuorumForZkWithSpaces() {
+ CloudClientConnectionString parsed =
+ CloudClientConnectionString.parse(
+ " zookeeper1:2181 , zookeeper2:2181 , zookeeper3:2181 /solr
");
Review Comment:
I can't imagine someone doing this. IMO connection strings should already
be free of whitespace.
##########
solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudClientConnectionStringTest.java:
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.util.Collection;
+import java.util.List;
+import java.util.Set;
+import org.apache.solr.SolrTestCase;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class CloudClientConnectionStringTest extends SolrTestCase {
+
+ private static <T> boolean collectionEqual(Collection<T> coll, Collection<T>
coll2) {
+ return coll.size() == coll2.size()
+ && Set.copyOf(coll).containsAll(coll2)
+ && Set.copyOf(coll2).containsAll(coll);
+ }
+
+ @Test
+ public void testBuildQuorumForZk() {
+ CloudClientConnectionString parsed =
+
CloudClientConnectionString.parse("zookeeper1:2181,zookeeper2:2181,zookeeper3:2181/solr");
+ Assert.assertTrue(parsed.isZk());
+ List<String> expectedQuorum = List.of("zookeeper1:2181",
"zookeeper2:2181", "zookeeper3:2181");
+ Assert.assertTrue(collectionEqual(expectedQuorum, parsed.quorumItems()));
+ Assert.assertEquals("/solr", parsed.zkChroot());
+ }
+
+ @Test
+ public void testBuildQuorumForZkIpV4() {
+ CloudClientConnectionString parsed =
+ CloudClientConnectionString.parse(
+ "192.194.10.10:2181,192.194.10.11:2181,192.194.10.12:2181/solr");
+ Assert.assertTrue(parsed.isZk());
+ List<String> expectedQuorum =
+ List.of("192.194.10.10:2181", "192.194.10.11:2181",
"192.194.10.12:2181");
+ Assert.assertTrue(collectionEqual(expectedQuorum, parsed.quorumItems()));
+ Assert.assertEquals("/solr", parsed.zkChroot());
+ }
+
+ @Test
+ public void testBuildQuorumForZkIpV6() {
+ CloudClientConnectionString parsed =
+ CloudClientConnectionString.parse(
+ "[2001:db8:85a3::8a2e:370:7334]:2181,"
+ + "[2001:db8:85a3::8a2e:370:7335]:2181,"
+ + "[2001:db8:85a3::8a2e:370:7336]:2181/solr");
+ Assert.assertTrue(parsed.isZk());
+ List<String> expectedQuorum =
+ List.of(
+ "[2001:db8:85a3::8a2e:370:7334]:2181",
+ "[2001:db8:85a3::8a2e:370:7335]:2181",
+ "[2001:db8:85a3::8a2e:370:7336]:2181");
+ Assert.assertTrue(collectionEqual(expectedQuorum, parsed.quorumItems()));
+ Assert.assertEquals("/solr", parsed.zkChroot());
+ }
+
+ @Test
+ public void testBuildQuorumForZkNoChroot() {
+ CloudClientConnectionString parsed =
+
CloudClientConnectionString.parse("zookeeper1:2181,zookeeper2:2181,zookeeper3:2181");
+ Assert.assertTrue(parsed.isZk());
+ List<String> expectedQuorum = List.of("zookeeper1:2181",
"zookeeper2:2181", "zookeeper3:2181");
+ Assert.assertTrue(collectionEqual(expectedQuorum, parsed.quorumItems()));
+ Assert.assertNull(parsed.zkChroot());
+ }
+
+ @Test
+ public void testBuildQuorumForZkWithSpaces() {
+ CloudClientConnectionString parsed =
+ CloudClientConnectionString.parse(
+ " zookeeper1:2181 , zookeeper2:2181 , zookeeper3:2181 /solr
");
Review Comment:
By adding a test like this, you signal to everyone who looks at it -- this
can happen. But really, nobody has been doing that.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]