SOLR-12565: Add SolrJ snippet to 'Using ZooKeeper to manage config' ref-guide page

This commit is contained in:
Jason Gerlowski 2018-10-11 14:21:49 -04:00
parent d48f22c1ad
commit 42ac07d11b
2 changed files with 104 additions and 2 deletions

View File

@ -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, <<config-sets.adoc#config-sets,Config Sets>> can also be uploaded to ZooKeeper independent of collection creation using either Solr's <<solr-control-script-reference.adoc#solr-control-script-reference,Solr Control Script>> or the {solr-javadocs}/solr-solrj/org/apache/solr/client/solrj/impl/CloudSolrClient.html[CloudSolrClient.uploadConfig] java method.
In production situations, <<config-sets.adoc#config-sets,Config Sets>> can also be uploaded to ZooKeeper independent of collection creation using either Solr's <<solr-control-script-reference.adoc#solr-control-script-reference,Solr Control Script>> 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 <name for configset> -d <path to directory with configset>
----
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 <<setting-up-an-external-zookeeper-ensemble#updating-solr-s-include-files,instructions>>) you can omit `-z <zk host string>` from the above command.
NOTE: If you have defined `ZK_HOST` in `solr.in.sh`/`solr.in.cmd` (see <<setting-up-an-external-zookeeper-ensemble#updating-solr-s-include-files,instructions>>) you can omit `-z <zk host string>` from the above command.

View File

@ -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<String> 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<String> 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));
}
}
}