From 42ac07d11b9735df6dace64bf751ce528c0d01c8 Mon Sep 17 00:00:00 2001 From: Jason Gerlowski Date: Thu, 11 Oct 2018 14:21:49 -0400 Subject: [PATCH] SOLR-12565: Add SolrJ snippet to 'Using ZooKeeper to manage config' ref-guide page --- ...okeeper-to-manage-configuration-files.adoc | 12 ++- .../ref_guide_examples/ZkConfigFilesTest.java | 94 +++++++++++++++++++ 2 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/ZkConfigFilesTest.java diff --git a/solr/solr-ref-guide/src/using-zookeeper-to-manage-configuration-files.adoc b/solr/solr-ref-guide/src/using-zookeeper-to-manage-configuration-files.adoc index b1b3b5a77e1..546ee99b197 100644 --- a/solr/solr-ref-guide/src/using-zookeeper-to-manage-configuration-files.adoc +++ b/solr/solr-ref-guide/src/using-zookeeper-to-manage-configuration-files.adoc @@ -1,4 +1,6 @@ = Using ZooKeeper to Manage Configuration Files +:solr-root-path: ../../ +:example-source-dir: {solr-root-path}solrj/src/test/org/apache/solr/client/ref_guide_examples/ // 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 @@ -51,7 +53,7 @@ IMPORTANT: It's a good idea to keep these files under version control. == Uploading Configuration Files using bin/solr or SolrJ -In production situations, <> can also be uploaded to ZooKeeper independent of collection creation using either Solr's <> or the {solr-javadocs}/solr-solrj/org/apache/solr/client/solrj/impl/CloudSolrClient.html[CloudSolrClient.uploadConfig] java method. +In production situations, <> can also be uploaded to ZooKeeper independent of collection creation using either Solr's <> or SolrJ. The below command can be used to upload a new configset using the bin/solr script. @@ -60,6 +62,12 @@ The below command can be used to upload a new configset using the bin/solr scrip bin/solr zk upconfig -n -d ---- +The following code shows how this can also be achieved using SolrJ: +[source,java,indent=0] +---- +include::{example-source-dir}ZkConfigFilesTest.java[tag=zk-configset-upload] +---- + It is strongly recommended that the configurations be kept in a version control system, Git, SVN or similar. == Managing Your SolrCloud Configuration Files @@ -85,4 +93,4 @@ If you for example would like to keep your `solr.xml` in ZooKeeper to avoid havi bin/solr zk cp file:local/file/path/to/solr.xml zk:/solr.xml -z localhost:2181 ---- -NOTE: If you have defined `ZK_HOST` in `solr.in.sh`/`solr.in.cmd` (see <>) you can omit `-z ` from the above command. \ No newline at end of file +NOTE: If you have defined `ZK_HOST` in `solr.in.sh`/`solr.in.cmd` (see <>) you can omit `-z ` from the above command. diff --git a/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/ZkConfigFilesTest.java b/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/ZkConfigFilesTest.java new file mode 100644 index 00000000000..dcb11118872 --- /dev/null +++ b/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/ZkConfigFilesTest.java @@ -0,0 +1,94 @@ +/* + * 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.ref_guide_examples; + +import java.io.File; +import java.nio.file.Paths; +import java.util.List; + +import org.apache.solr.cloud.SolrCloudTestCase; +import org.apache.solr.common.cloud.SolrZkClient; +import org.apache.solr.common.cloud.ZkConfigManager; +import org.apache.solr.util.ExternalPaths; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Examples showing how to manipulate configsets in ZK. + * + * Snippets surrounded by "tag" and "end" comments are extracted and used in the Solr Reference Guide. + */ +public class ZkConfigFilesTest extends SolrCloudTestCase { + + private static final int ZK_TIMEOUT_MILLIS = 10000; + + @BeforeClass + public static void setUpCluster() throws Exception { + configureCluster(1) + .configure(); + } + + @Before + public void clearConfigsBefore() throws Exception { + clearConfigs(); + } + + @After + public void clearConfigsAfter() throws Exception { + clearConfigs(); + } + + private void clearConfigs() throws Exception { + ZkConfigManager manager = new ZkConfigManager(cluster.getZkClient()); + List configs = manager.listConfigs(); + for (String config : configs) { + manager.deleteConfigDir(config); + } + } + + @Test + public void testCanUploadConfigToZk() throws Exception { + final String zkConnectionString = cluster.getZkClient().getZkServerAddress(); + final String localConfigSetDirectory = new File(ExternalPaths.TECHPRODUCTS_CONFIGSET).getAbsolutePath(); + + assertConfigsContainOnly(); + + // tag::zk-configset-upload[] + try (SolrZkClient zkClient = new SolrZkClient(zkConnectionString, ZK_TIMEOUT_MILLIS)) { + ZkConfigManager manager = new ZkConfigManager(zkClient); + manager.uploadConfigDir(Paths.get(localConfigSetDirectory), "nameForConfigset"); + } + // end::zk-configset-upload[] + + assertConfigsContainOnly("nameForConfigset"); + } + + private void assertConfigsContainOnly(String... expectedConfigs) throws Exception { + final int expectedSize = expectedConfigs.length; + + ZkConfigManager manager = new ZkConfigManager(cluster.getZkClient()); + List actualConfigs = manager.listConfigs(); + + assertEquals(expectedSize, actualConfigs.size()); + for (String expectedConfig : expectedConfigs) { + assertTrue("Expected ZK to contain " + expectedConfig + ", but it didn't. Actual configs: ", actualConfigs.contains(expectedConfig)); + } + } +}