diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 909708bf164..c9c486616e1 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -86,6 +86,8 @@ New Features * SOLR-12036: Factor out DefaultStreamFactory solrj class. (Christine Poerschke) +* SOLR-12151: Add abstract MultiSolrCloudTestCase class. (Christine Poerschke) + Bug Fixes ---------------------- diff --git a/solr/core/src/test/org/apache/solr/cloud/MultiSolrCloudTestCaseTest.java b/solr/core/src/test/org/apache/solr/cloud/MultiSolrCloudTestCaseTest.java new file mode 100644 index 00000000000..2e382f8e247 --- /dev/null +++ b/solr/core/src/test/org/apache/solr/cloud/MultiSolrCloudTestCaseTest.java @@ -0,0 +1,80 @@ +/* + * 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.cloud; + +import org.junit.BeforeClass; +import org.junit.Test; + +public class MultiSolrCloudTestCaseTest extends MultiSolrCloudTestCase { + + private static int numClouds; + private static int numCollectionsPerCloud; + + private static int numShards; + private static int numReplicas; + private static int maxShardsPerNode; + private static int nodesPerCluster; + + @BeforeClass + public static void setupClusters() throws Exception { + + numClouds = random().nextInt(4); // 0..3 + final String[] clusterIds = new String[numClouds]; + for (int ii=0; ii clusterId2cluster = new HashMap(); + + protected static abstract class DefaultClusterCreateFunction implements Function { + + public DefaultClusterCreateFunction() { + } + + protected abstract int nodesPerCluster(String clusterId); + + @Override + public MiniSolrCloudCluster apply(String clusterId) { + try { + final MiniSolrCloudCluster cluster = new SolrCloudTestCase + .Builder(nodesPerCluster(clusterId), createTempDir()) + .addConfig("conf", configset("cloud-dynamic")) + .build(); + return cluster; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + } + + protected static abstract class DefaultClusterInitFunction implements BiConsumer { + + final private int numShards; + final private int numReplicas; + final private int maxShardsPerNode; + + public DefaultClusterInitFunction(int numShards, int numReplicas, int maxShardsPerNode) { + this.numShards = numShards; + this.numReplicas = numReplicas; + this.maxShardsPerNode = maxShardsPerNode; + } + + protected void doAccept(String collection, MiniSolrCloudCluster cluster) { + try { + CollectionAdminRequest + .createCollection(collection, "conf", numShards, numReplicas) + .setMaxShardsPerNode(maxShardsPerNode) + .processAndWait(cluster.getSolrClient(), SolrCloudTestCase.DEFAULT_TIMEOUT); + + AbstractDistribZkTestBase.waitForRecoveriesToFinish(collection, cluster.getSolrClient().getZkStateReader(), false, true, SolrCloudTestCase.DEFAULT_TIMEOUT); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + } + + protected static void doSetupClusters(final String[] clusterIds, + final Function createFunc, + final BiConsumer initFunc) throws Exception { + + for (final String clusterId : clusterIds) { + assertFalse("duplicate clusterId "+clusterId, clusterId2cluster.containsKey(clusterId)); + MiniSolrCloudCluster cluster = createFunc.apply(clusterId); + initFunc.accept(clusterId, cluster); + clusterId2cluster.put(clusterId, cluster); + } + } + + @AfterClass + public static void shutdownCluster() throws Exception { + for (MiniSolrCloudCluster cluster : clusterId2cluster.values()) { + cluster.shutdown(); + } + } + +} diff --git a/solr/test-framework/src/java/org/apache/solr/cloud/SolrCloudTestCase.java b/solr/test-framework/src/java/org/apache/solr/cloud/SolrCloudTestCase.java index b8bf1f4d857..e0f3d7878a6 100644 --- a/solr/test-framework/src/java/org/apache/solr/cloud/SolrCloudTestCase.java +++ b/solr/test-framework/src/java/org/apache/solr/cloud/SolrCloudTestCase.java @@ -87,7 +87,7 @@ public class SolrCloudTestCase extends SolrTestCaseJ4 { /** * Builder class for a MiniSolrCloudCluster */ - protected static class Builder { + public static class Builder { private final int nodeCount; private final Path baseDir; @@ -187,7 +187,15 @@ public class SolrCloudTestCase extends SolrTestCaseJ4 { * @throws Exception if an error occurs on startup */ public void configure() throws Exception { - cluster = new MiniSolrCloudCluster(nodeCount, baseDir, solrxml, jettyConfig, null, securityJson); + cluster = build(); + } + + /** + * Configure, run and return the {@link MiniSolrCloudCluster} + * @throws Exception if an error occurs on startup + */ + public MiniSolrCloudCluster build() throws Exception { + MiniSolrCloudCluster cluster = new MiniSolrCloudCluster(nodeCount, baseDir, solrxml, jettyConfig, null, securityJson); CloudSolrClient client = cluster.getSolrClient(); for (Config config : configs) { ((ZkClientClusterStateProvider)client.getClusterStateProvider()).uploadConfig(config.path, config.name); @@ -199,6 +207,7 @@ public class SolrCloudTestCase extends SolrTestCaseJ4 { props.setClusterProperty(entry.getKey(), entry.getValue()); } } + return cluster; } }