Apache Lucene open-source search software
[SOLR-2983] - fix custom merge policy instantiation issue in SolrIndexConfig
Tommaso Teofilie3056ee81de4
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 48349f6facf..a4bb3c0f3dc 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -471,6 +471,10 @@ DocumentationUpgrading from Solr 3.5----------------------+* SOLR-2983: As a consequence of moving the code which sets a MergePolicy from SolrIndexWriter to SolrIndexConfig,+ (custom) MergePolicies should now have an empty constructor; thus an IndexWriter should not be passed as constructor+ parameter but instead set using the setIndexWriter() method.+* As doGet() methods in SimplePostTool was changed to static, the client applications of thisclass need to be recompiled.diff --git a/solr/core/src/java/org/apache/solr/update/SolrIndexConfig.java b/solr/core/src/java/org/apache/solr/update/SolrIndexConfig.javaindex 6220bb5cc0d..aa2062ee78e 100644--- a/solr/core/src/java/org/apache/solr/update/SolrIndexConfig.java+++ b/solr/core/src/java/org/apache/solr/update/SolrIndexConfig.java@@ -160,14 +160,9 @@ public class SolrIndexConfig {}private MergePolicy buildMergePolicy(IndexSchema schema) {- MergePolicy policy;String mpClassName = mergePolicyInfo == null ? defaultMergePolicyClassName : mergePolicyInfo.className;- try {- policy = (MergePolicy) schema.getResourceLoader().newInstance(mpClassName, null, new Class[]{IndexWriter.class}, new Object[]{this});- } catch (Exception e) {- policy = (MergePolicy) schema.getResourceLoader().newInstance(mpClassName);- }+ MergePolicy policy = (MergePolicy) schema.getResourceLoader().newInstance(mpClassName);if (policy instanceof LogMergePolicy) {LogMergePolicy logMergePolicy = (LogMergePolicy) policy;diff --git a/solr/core/src/test-files/solr/conf/bad-mp-solrconfig.xml b/solr/core/src/test-files/solr/conf/bad-mp-solrconfig.xmlnew file mode 100644index 00000000000..af66ff7e4b3--- /dev/null+++ b/solr/core/src/test-files/solr/conf/bad-mp-solrconfig.xml@@ -0,0 +1,51 @@+<?xml version="1.0" ?>++<!--+ 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.+-->++<config>++ <directoryFactory name="DirectoryFactory" class="${solr.directoryFactory:solr.RAMDirectoryFactory}"/>++ <luceneMatchVersion>${tests.luceneMatchVersion:LUCENE_CURRENT}</luceneMatchVersion>++ <indexDefaults>++ <useCompoundFile>false</useCompoundFile>+ <mergeFactor>10</mergeFactor>+ <ramBufferSizeMB>32</ramBufferSizeMB>+ <maxMergeDocs>2147483647</maxMergeDocs>+ <maxFieldLength>10000</maxFieldLength>+ <writeLockTimeout>1000</writeLockTimeout>+ <mergePolicy class="org.apache.solr.update.DummyMergePolicy"/>++ </indexDefaults>++ <mainIndex>+ <useCompoundFile>false</useCompoundFile>+ <mergeFactor>8</mergeFactor>+ <maxBufferedDocs>10</maxBufferedDocs>+ <maxMergeDocs>2147483647</maxMergeDocs>+ <maxFieldLength>10000</maxFieldLength>+ <mergePolicy class="org.apache.solr.update.DummyMergePolicy"/>+ <unlockOnStartup>true</unlockOnStartup>+ </mainIndex>++ <updateHandler class="solr.DirectUpdateHandler2"/>+ <requestHandler name="standard" class="solr.StandardRequestHandler"></requestHandler>++</config>diff --git a/solr/core/src/test/org/apache/solr/update/DummyMergePolicy.java b/solr/core/src/test/org/apache/solr/update/DummyMergePolicy.javanew file mode 100644index 00000000000..ef0d07504fe--- /dev/null+++ b/solr/core/src/test/org/apache/solr/update/DummyMergePolicy.java@@ -0,0 +1,35 @@+package org.apache.solr.update;++/*+ * 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.+ */++import org.apache.lucene.index.IndexWriter;+import org.apache.lucene.index.LogByteSizeMergePolicy;++/**+ * Dummy implementation of {@link org.apache.lucene.index.MergePolicy} which doesn't have an empty constructor and+ * is expected to fail if used within Solr+ */+class DummyMergePolicy extends LogByteSizeMergePolicy {++ private DummyMergePolicy() {}++ public DummyMergePolicy(IndexWriter writer) {+ super();+ setIndexWriter(writer);+ }+}diff --git a/solr/core/src/test/org/apache/solr/update/SolrIndexConfigTest.java b/solr/core/src/test/org/apache/solr/update/SolrIndexConfigTest.javanew file mode 100644index 00000000000..dd77aedaef9--- /dev/null+++ b/solr/core/src/test/org/apache/solr/update/SolrIndexConfigTest.java@@ -0,0 +1,53 @@+package org.apache.solr.update;++/*+ * 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.+ */++import org.apache.solr.SolrTestCaseJ4;+import org.apache.solr.core.SolrConfig;+import org.apache.solr.schema.IndexSchema;+import org.junit.Test;++/**+ * Testcase for {@link SolrIndexConfig}+ */+public class SolrIndexConfigTest extends SolrTestCaseJ4 {++ @Test+ public void testFailingSolrIndexConfigCreation() {+ try {+ SolrConfig solrConfig = new SolrConfig("bad-mp-solrconfig.xml");+ SolrIndexConfig solrIndexConfig = new SolrIndexConfig(solrConfig, null, null);+ IndexSchema indexSchema = new IndexSchema(solrConfig, "schema.xml", null);+ solrIndexConfig.toIndexWriterConfig(indexSchema);+ fail("a mergePolicy should have an empty constructor in order to be instantiated in Solr thus this should fail ");+ } catch (Exception e) {+ // it failed as expected+ }+ }++ @Test+ public void testTieredMPSolrIndexConfigCreation() throws Exception {+ SolrConfig solrConfig = new SolrConfig("solrconfig-mergepolicy.xml");+ SolrIndexConfig solrIndexConfig = new SolrIndexConfig(solrConfig, null, null);+ assertNotNull(solrIndexConfig);+ assertEquals("org.apache.lucene.index.TieredMergePolicy", solrIndexConfig.defaultMergePolicyClassName);+ IndexSchema indexSchema = new IndexSchema(solrConfig, "schema.xml", null);+ solrIndexConfig.toIndexWriterConfig(indexSchema);+ }++}