Apache Lucene open-source search software
SOLR-4951: Better randomization of MergePolicy in Solr tests
Chris M. Hostetterc78b4f80bb8b
diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.javaindex b173fef07b8..985c1d205e7 100644--- a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java@@ -793,15 +793,8 @@ public abstract class LuceneTestCase extends Assert {}}- if (rarely(r)) {- c.setMergePolicy(new MockRandomMergePolicy(r));- } else if (r.nextBoolean()) {- c.setMergePolicy(newTieredMergePolicy());- } else if (r.nextInt(5) == 0) {- c.setMergePolicy(newAlcoholicMergePolicy());- } else {- c.setMergePolicy(newLogMergePolicy());- }+ c.setMergePolicy(newMergePolicy(r));+if (rarely(r)) {c.setMergedSegmentWarmer(new SimpleMergedSegmentWarmer(c.getInfoStream()));}@@ -810,6 +803,21 @@ public abstract class LuceneTestCase extends Assert {return c;}+ public static MergePolicy newMergePolicy(Random r) {+ if (rarely(r)) {+ return new MockRandomMergePolicy(r);+ } else if (r.nextBoolean()) {+ return newTieredMergePolicy(r);+ } else if (r.nextInt(5) == 0) {+ return newAlcoholicMergePolicy(r, classEnvRule.timeZone);+ }+ return newLogMergePolicy(r);+ }++ public static MergePolicy newMergePolicy() {+ return newMergePolicy(random());+ }+public static LogMergePolicy newLogMergePolicy() {return newLogMergePolicy(random());}diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex ad62d9edd33..1466018e90d 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -100,6 +100,8 @@ Other Changes* SOLR-5056: Improve type safety of ConfigSolr class. (Alan Woodward)+* SOLR-4951: Better randomization of MergePolicy in Solr tests (hossman)+================== 4.4.0 ==================Versions of Major Componentsdiff --git a/solr/core/src/test-files/solr/collection1/conf/solrconfig-indexconfig.xml b/solr/core/src/test-files/solr/collection1/conf/solrconfig-indexconfig.xmlindex 834032d5adf..066f8632e96 100644--- a/solr/core/src/test-files/solr/collection1/conf/solrconfig-indexconfig.xml+++ b/solr/core/src/test-files/solr/collection1/conf/solrconfig-indexconfig.xml@@ -25,5 +25,6 @@<useCompoundFile>${useCompoundFile:false}</useCompoundFile><maxIndexingThreads>123</maxIndexingThreads><infoStream>true</infoStream>+ <mergePolicy class="org.apache.solr.util.RandomMergePolicy" /></indexConfig></config>diff --git a/solr/core/src/test-files/solr/collection1/conf/solrconfig.snippet.randomindexconfig.xml b/solr/core/src/test-files/solr/collection1/conf/solrconfig.snippet.randomindexconfig.xmlindex ce21fdf242b..845753825bd 100644--- a/solr/core/src/test-files/solr/collection1/conf/solrconfig.snippet.randomindexconfig.xml+++ b/solr/core/src/test-files/solr/collection1/conf/solrconfig.snippet.randomindexconfig.xml@@ -23,4 +23,5 @@ A solrconfig.xml snippet containing indexConfig settings for randomized testing.--><indexConfig><useCompoundFile>${useCompoundFile:false}</useCompoundFile>+ <mergePolicy class="org.apache.solr.util.RandomMergePolicy" /></indexConfig>diff --git a/solr/core/src/test/org/apache/solr/core/TestSolrIndexConfig.java b/solr/core/src/test/org/apache/solr/core/TestSolrIndexConfig.javaindex e32e610feaf..fe08f53c364 100644--- a/solr/core/src/test/org/apache/solr/core/TestSolrIndexConfig.java+++ b/solr/core/src/test/org/apache/solr/core/TestSolrIndexConfig.java@@ -17,8 +17,14 @@ package org.apache.solr.core;* limitations under the License.*/+import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;+import org.apache.lucene.index.LiveIndexWriterConfig;+import org.apache.solr.SolrTestCaseJ4;+import org.apache.solr.core.SolrCore;+import org.apache.solr.util.RefCounted;+import org.apache.solr.util.RandomMergePolicy;import org.apache.solr.update.LoggingInfoStream;import org.junit.BeforeClass;@@ -28,11 +34,31 @@ public class TestSolrIndexConfig extends SolrTestCaseJ4 {public static void beforeClass() throws Exception {initCore("solrconfig-indexconfig.xml","schema.xml");}++ public void testLiveWriter() throws Exception {+ SolrCore core = h.getCore();+ RefCounted<IndexWriter> iw = core.getUpdateHandler().getSolrCoreState().getIndexWriter(core);+ try {+ checkIndexWriterConfig(iw.get().getConfig());+ } finally {+ if (null != iw) iw.decref();+ }+ }+- public void testIndexConfig() throws Exception {+ public void testIndexConfigParsing() throws Exception {IndexWriterConfig iwc = solrConfig.indexConfig.toIndexWriterConfig(h.getCore().getLatestSchema());+ checkIndexWriterConfig(iwc);+ }++ private void checkIndexWriterConfig(LiveIndexWriterConfig iwc) {+assertEquals(123, iwc.getMaxThreadStates());assertTrue(iwc.getInfoStream() instanceof LoggingInfoStream);+ assertTrue(iwc.getMergePolicy().getClass().toString(),+ iwc.getMergePolicy() instanceof RandomMergePolicy);+}+}diff --git a/solr/core/src/test/org/apache/solr/util/TestRandomMergePolicy.java b/solr/core/src/test/org/apache/solr/util/TestRandomMergePolicy.javanew file mode 100644index 00000000000..549dd1420bc--- /dev/null+++ b/solr/core/src/test/org/apache/solr/util/TestRandomMergePolicy.java@@ -0,0 +1,73 @@+/*+ * 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.util;++import org.apache.lucene.index.MergePolicy;+import org.apache.lucene.util.LuceneTestCase;++import java.lang.reflect.Method;+import java.lang.reflect.Modifier;+import java.lang.reflect.InvocationTargetException;++/**+ * A "test the test" sanity check using reflection to ensure that+ * {@linke RandomMergePolicy} is working as expected+ */+public class TestRandomMergePolicy extends LuceneTestCase {++ /**+ * Ensure every MP method is overridden by RMP+ * (future proof ourselves against new methods being added to MP)+ */+ public void testMethodOverride() {+ Class rmp = RandomMergePolicy.class;+ for (Method meth : rmp.getMethods()) {+ if (// ignore things like hashCode, equals, etc...+ meth.getDeclaringClass().equals(Object.class)+ // can't do anything about it regardless of what class declares it+ || Modifier.isFinal(meth.getModifiers())) {+ continue;+ }+ assertEquals("method not overridden by RandomMergePolicy: " ++ meth.toGenericString(),+ rmp, meth.getDeclaringClass());+ }+ }++ /**+ * Ensure any "getter" methods return the same value as+ * the wrapped MP+ * (future proof ourselves against new final getter/setter pairs being+ * added to MP w/o dealing with them in the RMP Constructor)+ */+ public void testGetters() throws IllegalAccessException, InvocationTargetException {+ final int iters = atLeast(20);+ for (int i = 0; i < iters; i++) {+ RandomMergePolicy rmp = new RandomMergePolicy();+ Class mp = MergePolicy.class;+ for (Method meth : mp.getDeclaredMethods()) {+ if (meth.getName().startsWith("get") &&+ (0 == meth.getParameterTypes().length)) {++ assertEquals("MergePolicy getter gave diff results for RandomMergePolicy and the policy it wrapped: " + meth.toGenericString(),+ meth.invoke(rmp), meth.invoke(rmp.inner));+ }+ }+ }+ }+}diff --git a/solr/test-framework/src/java/org/apache/solr/util/RandomMergePolicy.java b/solr/test-framework/src/java/org/apache/solr/util/RandomMergePolicy.javanew file mode 100644index 00000000000..a58a80c520e--- /dev/null+++ b/solr/test-framework/src/java/org/apache/solr/util/RandomMergePolicy.java@@ -0,0 +1,91 @@+/*+ * 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.util;++import org.apache.lucene.index.*;+import org.apache.lucene.index.MergePolicy.MergeSpecification;+import org.apache.lucene.util.LuceneTestCase;++import java.util.Map;+import java.io.IOException;++/**+ * A {@link MergePolicy} with a no-arg constructor that proxies to a+ * wrapped instance retrieved from {@link LuceneTestCase#newMergePolicy}.+ * Solr tests utilizing the Lucene randomized test framework can refer+ * to this class in solrconfig.xml to get a fully randomized merge policy.+ */+public final class RandomMergePolicy extends MergePolicy {++ /**+ * Not private so tests can inspect it,+ * Not final so it can be set on clone+ */+ MergePolicy inner;++ public RandomMergePolicy() {+ this(LuceneTestCase.newMergePolicy());+ }+ private RandomMergePolicy(MergePolicy inner) {+ super(inner.getNoCFSRatio(),+ (long) (inner.getMaxCFSSegmentSizeMB() * 1024 * 1024));+ this.inner = inner;+ }++ public RandomMergePolicy clone() {+ RandomMergePolicy clone = (RandomMergePolicy) super.clone();+ clone.inner = this.inner.clone();+ return clone;+ }++ public void close() {+ inner.close();+ }++ public MergeSpecification findForcedDeletesMerges(SegmentInfos segmentInfos)+ throws IOException {++ return inner.findForcedDeletesMerges(segmentInfos);+ }+ public MergeSpecification findForcedMerges(SegmentInfos segmentInfos,+ int maxSegmentCount,+ Map<SegmentInfoPerCommit,Boolean> segmentsToMerge)+ throws IOException {++ return inner.findForcedMerges(segmentInfos, maxSegmentCount, segmentsToMerge);+ }++ public MergeSpecification findMerges(MergeTrigger mergeTrigger,+ SegmentInfos segmentInfos)+ throws IOException {++ return inner.findMerges(mergeTrigger, segmentInfos);+ }++ public void setIndexWriter(IndexWriter writer) {+ inner.setIndexWriter(writer);+ }++ public boolean useCompoundFile(SegmentInfos infos,+ SegmentInfoPerCommit mergedInfo)+ throws IOException {++ return inner.useCompoundFile(infos, mergedInfo);+ }++}