From 2551b9d917c2ceb23cf16567f16870ceef725ba7 Mon Sep 17 00:00:00 2001 From: Vilius Pranckaitis Date: Sat, 28 Jul 2018 14:40:36 +1000 Subject: [PATCH] SOLR-12595: CloudSolrClient.Builder accepts ZK connection strings --- .../client/solrj/impl/CloudSolrClient.java | 22 +++++++++++++++++-- .../impl/CloudSolrClientBuilderTest.java | 19 ++++++++-------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java index 1eee687515af..ce958ba6242b 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java @@ -1411,6 +1411,25 @@ public Builder(List zkHosts, Optional zkChroot) { if (zkChroot.isPresent()) this.zkChroot = zkChroot.get(); } + /** + * Provide a ZK connection string which will be used when configuring {@link CloudSolrClient} instances. + * + * Usage example: + * + *
+     *   final SolrClient client = new CloudSolrClient.Builder("zk1:2181,zk2:2181/solr").build();
+     * 
+ * + * @param zkHost a String containing zookeeper hosts and possibly chroot (e.g. "zk1:2181,zk2:2181/solr") + */ + public Builder(String zkHost) { + this(new ZkClientClusterStateProvider(zkHost)); + } + + private Builder(ClusterStateProvider stateProvider) { + this.stateProvider = stateProvider; + } + /** * Provide a ZooKeeper client endpoint to be used when configuring {@link CloudSolrClient} instances. * @@ -1566,8 +1585,7 @@ public CloudSolrClient build() { if (stateProvider == null) { if (!zkHosts.isEmpty()) { stateProvider = new ZkClientClusterStateProvider(zkHosts, zkChroot); - } - else if (!this.solrUrls.isEmpty()) { + } else if (!this.solrUrls.isEmpty()) { try { stateProvider = new HttpClusterStateProvider(solrUrls, httpClient); } catch (Exception e) { diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientBuilderTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientBuilderTest.java index ad6660c684fa..28202336e2a1 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientBuilderTest.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/CloudSolrClientBuilderTest.java @@ -31,6 +31,9 @@ public class CloudSolrClientBuilderTest extends LuceneTestCase { private static final String ANY_CHROOT = "/ANY_CHROOT"; private static final String ANY_ZK_HOST = "ANY_ZK_HOST"; private static final String ANY_OTHER_ZK_HOST = "ANY_OTHER_ZK_HOST"; + private static final String ANY_ZK_CONNECTION_STRING = + String.format("%s,%s%s", ANY_ZK_HOST, ANY_OTHER_ZK_HOST, ANY_CHROOT); + @Test(expected = IllegalArgumentException.class) public void testNoZkHostSpecified() { @@ -52,9 +55,9 @@ public void testSingleZkHostSpecified() throws IOException { @Test public void testSeveralZkHostsSpecifiedSingly() throws IOException { final List zkHostList = new ArrayList<>(); - zkHostList.add(ANY_ZK_HOST); zkHostList.add(ANY_OTHER_ZK_HOST); - try (CloudSolrClient createdClient = new Builder(zkHostList, Optional.of(ANY_CHROOT)) - .build()) { + zkHostList.add(ANY_ZK_HOST); + zkHostList.add(ANY_OTHER_ZK_HOST); + try (CloudSolrClient createdClient = new Builder(zkHostList, Optional.of(ANY_CHROOT)).build()) { final String clientZkHost = createdClient.getZkHost(); assertTrue(clientZkHost.contains(ANY_ZK_HOST)); @@ -63,22 +66,20 @@ public void testSeveralZkHostsSpecifiedSingly() throws IOException { } @Test - public void testSeveralZkHostsSpecifiedTogether() throws IOException { - final ArrayList zkHosts = new ArrayList(); - zkHosts.add(ANY_ZK_HOST); - zkHosts.add(ANY_OTHER_ZK_HOST); - try(CloudSolrClient createdClient = new Builder(zkHosts, Optional.of(ANY_CHROOT)).build()) { + public void testZkConnectionString() throws IOException { + try(CloudSolrClient createdClient = new Builder(ANY_ZK_CONNECTION_STRING).build()) { final String clientZkHost = createdClient.getZkHost(); assertTrue(clientZkHost.contains(ANY_ZK_HOST)); assertTrue(clientZkHost.contains(ANY_OTHER_ZK_HOST)); + assertTrue(clientZkHost.contains(ANY_CHROOT)); } } @Test public void testByDefaultConfiguresClientToSendUpdatesOnlyToShardLeaders() throws IOException { try(CloudSolrClient createdClient = new Builder(Collections.singletonList(ANY_ZK_HOST), Optional.of(ANY_CHROOT)).build()) { - assertTrue(createdClient.isUpdatesToLeaders() == true); + assertTrue(createdClient.isUpdatesToLeaders()); } }