Apache Solr open-source search software
LUCENE-5481: Don't run unnecessary merges in IndexWriter.forceMerge.
Adrien Grand86f7bbaf11cf
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 04e88433691..e00e4eaf02a 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -106,6 +106,9 @@ Bug fixes* LUCENE-5466: query is always null in countDocsWithClass() of SimpleNaiveBayesClassifier.(Koji Sekiguchi)+* LUCENE-5481: IndexWriter.forceMerge used to run a merge even if there was a+ single segment in the index. (Adrien Grand, Mike McCandless)+Test Framework* LUCENE-5449: Rename _TestUtil and _TestHelper to remove the leading _.diff --git a/lucene/core/src/java/org/apache/lucene/index/LogMergePolicy.java b/lucene/core/src/java/org/apache/lucene/index/LogMergePolicy.javaindex 54ebe69dbf8..1b6b30893a1 100644--- a/lucene/core/src/java/org/apache/lucene/index/LogMergePolicy.java+++ b/lucene/core/src/java/org/apache/lucene/index/LogMergePolicy.java@@ -196,7 +196,7 @@ public abstract class LogMergePolicy extends MergePolicy {}return numToMerge <= maxNumSegments &&- (numToMerge != 1 || !segmentIsOriginal || isMerged(mergeInfo));+ (numToMerge != 1 || !segmentIsOriginal || isMerged(infos, mergeInfo));}/**@@ -221,7 +221,7 @@ public abstract class LogMergePolicy extends MergePolicy {}// need to skip that segment + add a merge for the 'right' segments,// unless there is only 1 which is merged.- if (last - start - 1 > 1 || (start != last - 1 && !isMerged(infos.info(start + 1)))) {+ if (last - start - 1 > 1 || (start != last - 1 && !isMerged(infos, infos.info(start + 1)))) {// there is more than 1 segment to the right of// this one, or a mergeable single segment.spec.add(new OneMerge(segments.subList(start + 1, last)));@@ -237,7 +237,7 @@ public abstract class LogMergePolicy extends MergePolicy {// Add any left-over segments, unless there is just 1// already fully merged- if (last > 0 && (++start + 1 < last || !isMerged(infos.info(start)))) {+ if (last > 0 && (++start + 1 < last || !isMerged(infos, infos.info(start)))) {spec.add(new OneMerge(segments.subList(start, last)));}@@ -267,7 +267,7 @@ public abstract class LogMergePolicy extends MergePolicy {// Since we must merge down to 1 segment, the// choice is simple:- if (last > 1 || !isMerged(infos.info(0))) {+ if (last > 1 || !isMerged(infos, infos.info(0))) {spec.add(new OneMerge(segments.subList(0, last)));}} else if (last > maxNumSegments) {@@ -352,7 +352,7 @@ public abstract class LogMergePolicy extends MergePolicy {}// There is only one segment already, and it is merged- if (maxNumSegments == 1 && last == 1 && isMerged(infos.info(0))) {+ if (maxNumSegments == 1 && last == 1 && isMerged(infos, infos.info(0))) {if (verbose()) {message("already 1 seg; skip");}diff --git a/lucene/core/src/java/org/apache/lucene/index/MergePolicy.java b/lucene/core/src/java/org/apache/lucene/index/MergePolicy.javaindex 3b32277836b..540307d2fe4 100644--- a/lucene/core/src/java/org/apache/lucene/index/MergePolicy.java+++ b/lucene/core/src/java/org/apache/lucene/index/MergePolicy.java@@ -520,13 +520,13 @@ public abstract class MergePolicy implements java.io.Closeable, Cloneable {/** Returns true if this single info is already fully merged (has no* pending deletes, is in the same dir as the* writer, and matches the current compound file setting */- protected final boolean isMerged(SegmentCommitInfo info) {+ protected final boolean isMerged(SegmentInfos infos, SegmentCommitInfo info) throws IOException {IndexWriter w = writer.get();assert w != null;boolean hasDeletions = w.numDeletedDocs(info) > 0;return !hasDeletions &&info.info.dir == w.getDirectory() &&- ((noCFSRatio > 0.0 && noCFSRatio < 1.0) || maxCFSSegmentSize < Long.MAX_VALUE);+ useCompoundFile(infos, info) == info.info.getUseCompoundFile();}/** Returns current {@code noCFSRatio}.diff --git a/lucene/core/src/java/org/apache/lucene/index/TieredMergePolicy.java b/lucene/core/src/java/org/apache/lucene/index/TieredMergePolicy.javaindex 6941b82f6e5..45b8d7e6a77 100644--- a/lucene/core/src/java/org/apache/lucene/index/TieredMergePolicy.java+++ b/lucene/core/src/java/org/apache/lucene/index/TieredMergePolicy.java@@ -518,7 +518,7 @@ public class TieredMergePolicy extends MergePolicy {}if ((maxSegmentCount > 1 && eligible.size() <= maxSegmentCount) ||- (maxSegmentCount == 1 && eligible.size() == 1 && (!segmentIsOriginal || isMerged(eligible.get(0))))) {+ (maxSegmentCount == 1 && eligible.size() == 1 && (!segmentIsOriginal || isMerged(infos, eligible.get(0))))) {if (verbose()) {message("already merged");}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestLogMergePolicy.java b/lucene/core/src/test/org/apache/lucene/index/TestLogMergePolicy.javanew file mode 100644index 00000000000..0be5bb91fa3--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/index/TestLogMergePolicy.java@@ -0,0 +1,26 @@+package org.apache.lucene.index;++/*+ * 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.+ */++public class TestLogMergePolicy extends BaseMergePolicyTestCase {++ public MergePolicy mergePolicy() {+ return newLogMergePolicy(random());+ }++}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestTieredMergePolicy.java b/lucene/core/src/test/org/apache/lucene/index/TestTieredMergePolicy.javaindex 155e8fea114..b417bc9d8ca 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestTieredMergePolicy.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestTieredMergePolicy.java@@ -21,11 +21,13 @@ import org.apache.lucene.analysis.MockAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.store.Directory;-import org.apache.lucene.util.LuceneTestCase;-import org.apache.lucene.util.TestUtil;import org.apache.lucene.util.TestUtil;-public class TestTieredMergePolicy extends LuceneTestCase {+public class TestTieredMergePolicy extends BaseMergePolicyTestCase {++ public MergePolicy mergePolicy() {+ return newTieredMergePolicy();+ }public void testForceMergeDeletes() throws Exception {Directory dir = newDirectory();diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/BaseMergePolicyTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/BaseMergePolicyTestCase.javanew file mode 100644index 00000000000..7c6358dc07b--- /dev/null+++ b/lucene/test-framework/src/java/org/apache/lucene/index/BaseMergePolicyTestCase.java@@ -0,0 +1,69 @@+package org.apache.lucene.index;++/*+ * 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 java.io.IOException;+import java.util.concurrent.atomic.AtomicBoolean;++import org.apache.lucene.analysis.MockAnalyzer;+import org.apache.lucene.document.Document;+import org.apache.lucene.store.Directory;+import org.apache.lucene.util.LuceneTestCase;+import org.apache.lucene.util.TestUtil;++/**+ * Base test case for {@link MergePolicy}.+ */+public abstract class BaseMergePolicyTestCase extends LuceneTestCase {++ /** Create a new {@link MergePolicy} instance. */+ protected abstract MergePolicy mergePolicy();++ public void testForceMergeNotNeeded() throws IOException {+ Directory dir = newDirectory();+ final AtomicBoolean mayMerge = new AtomicBoolean(true);+ final MergeScheduler mergeScheduler = new SerialMergeScheduler() {+ @Override+ synchronized public void merge(IndexWriter writer) throws IOException {+ if (!mayMerge.get() && writer.getNextMerge() != null) {+ throw new AssertionError();+ }+ super.merge(writer);+ }+ };+ IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setMergeScheduler(mergeScheduler).setMergePolicy(mergePolicy()));+ writer.getConfig().getMergePolicy().setNoCFSRatio(random().nextBoolean() ? 0 : 1);+ final int numSegments = TestUtil.nextInt(random(), 2, 20);+ for (int i = 0; i < numSegments; ++i) {+ final int numDocs = TestUtil.nextInt(random(), 1, 5);+ for (int j = 0; j < numDocs; ++j) {+ writer.addDocument(new Document());+ }+ writer.getReader().close();+ }+ for (int i = 5; i >= 0; --i) {+ final int segmentCount = writer.getSegmentCount();+ final int maxNumSegments = i == 0 ? 1 : TestUtil.nextInt(random(), 1, 10);+ mayMerge.set(segmentCount > maxNumSegments);+ writer.forceMerge(maxNumSegments);+ }+ writer.close();+ dir.close();+ }++}