Skip to content

Apache Lucene open-source search software

LUCENE-5377: IW.addIndexes(Dir[]) causes silent index corruption

Robert Muird9124f7de77d
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index bd6160323c7..2d2b0794aa3 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -182,6 +182,10 @@ Bug fixes
preventing wrong term positions for fields that use
StringTokenStream. (Michael Busch)
+* LUCENE-5377: IndexWriter.addIndexes(Directory[]) would cause corruption
+ on Lucene 4.6 if any index segments were Lucene 4.0-4.5.
+ (Littlestar, Mike McCandless, Shai Erera, Robert Muir)
+
API Changes
* LUCENE-5339: The facet module was simplified/reworked to make the
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40SegmentInfoFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40SegmentInfoFormat.java
index b40f0daeeb1..a5cb465b946 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40SegmentInfoFormat.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40SegmentInfoFormat.java
@@ -67,11 +67,12 @@ import org.apache.lucene.store.DataOutput; // javadocs
*
* @see SegmentInfos
* @lucene.experimental
- * @deprecated Only for reading old 4.0-4.5 segments
+ * @deprecated Only for reading old 4.0-4.5 segments, and supporting IndexWriter.addIndexes
*/
@Deprecated
public class Lucene40SegmentInfoFormat extends SegmentInfoFormat {
private final SegmentInfoReader reader = new Lucene40SegmentInfoReader();
+ private final SegmentInfoWriter writer = new Lucene40SegmentInfoWriter();
/** Sole constructor. */
public Lucene40SegmentInfoFormat() {
@@ -82,9 +83,11 @@ public class Lucene40SegmentInfoFormat extends SegmentInfoFormat {
return reader;
}
+ // we must unfortunately support write, to allow addIndexes to write a new .si with rewritten filenames:
+ // see LUCENE-5377
@Override
public SegmentInfoWriter getSegmentInfoWriter() {
- throw new UnsupportedOperationException("this codec can only be used for reading");
+ return writer;
}
/** File extension used to store {@link SegmentInfo}. */
diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40SegmentInfoWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40SegmentInfoWriter.java
similarity index 100%
rename from lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40SegmentInfoWriter.java
rename to lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40SegmentInfoWriter.java
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40DocValuesFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40DocValuesFormat.java
index cab6db2ffb2..048f05a3f64 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40DocValuesFormat.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40DocValuesFormat.java
@@ -19,12 +19,18 @@ package org.apache.lucene.codecs.lucene40;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.index.BaseDocValuesFormatTestCase;
+import org.junit.BeforeClass;
/**
* Tests Lucene40DocValuesFormat
*/
public class TestLucene40DocValuesFormat extends BaseDocValuesFormatTestCase {
private final Codec codec = new Lucene40RWCodec();
+
+ @BeforeClass
+ public static void beforeClass() {
+ OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true; // explicitly instantiates ancient codec
+ }
@Override
protected Codec getCodec() {
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40PostingsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40PostingsFormat.java
index 199690f8ef7..a3deacc022e 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40PostingsFormat.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40PostingsFormat.java
@@ -19,6 +19,7 @@ package org.apache.lucene.codecs.lucene40;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.index.BasePostingsFormatTestCase;
+import org.junit.BeforeClass;
/**
* Tests Lucene40PostingsFormat
@@ -26,6 +27,11 @@ import org.apache.lucene.index.BasePostingsFormatTestCase;
public class TestLucene40PostingsFormat extends BasePostingsFormatTestCase {
private final Codec codec = new Lucene40RWCodec();
+ @BeforeClass
+ public static void beforeClass() {
+ OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true; // explicitly instantiates ancient codec
+ }
+
@Override
protected Codec getCodec() {
return codec;
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40PostingsReader.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40PostingsReader.java
index f81223d391f..fd97279391b 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40PostingsReader.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40PostingsReader.java
@@ -34,6 +34,7 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;
+import org.junit.BeforeClass;
public class TestLucene40PostingsReader extends LuceneTestCase {
static final String terms[] = new String[100];
@@ -42,6 +43,11 @@ public class TestLucene40PostingsReader extends LuceneTestCase {
terms[i] = Integer.toString(i+1);
}
}
+
+ @BeforeClass
+ public static void beforeClass() {
+ OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true; // explicitly instantiates ancient codec
+ }
/** tests terms with different probabilities of being in the document.
* depends heavily on term vectors cross-check at checkIndex
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40StoredFieldsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40StoredFieldsFormat.java
index 72427c0249b..2502d895db8 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40StoredFieldsFormat.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40StoredFieldsFormat.java
@@ -19,9 +19,15 @@ package org.apache.lucene.codecs.lucene40;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.index.BaseStoredFieldsFormatTestCase;
+import org.junit.BeforeClass;
public class TestLucene40StoredFieldsFormat extends BaseStoredFieldsFormatTestCase {
+ @BeforeClass
+ public static void beforeClass() {
+ OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true; // explicitly instantiates ancient codec
+ }
+
@Override
protected Codec getCodec() {
return new Lucene40RWCodec();
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40TermVectorsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40TermVectorsFormat.java
index d161e1cb429..e97b3b3e6d9 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40TermVectorsFormat.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestLucene40TermVectorsFormat.java
@@ -19,9 +19,15 @@ package org.apache.lucene.codecs.lucene40;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.index.BaseTermVectorsFormatTestCase;
+import org.junit.BeforeClass;
public class TestLucene40TermVectorsFormat extends BaseTermVectorsFormatTestCase {
+ @BeforeClass
+ public static void beforeClass() {
+ OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true; // explicitly instantiates ancient codec
+ }
+
@Override
protected Codec getCodec() {
return new Lucene40RWCodec();
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestReuseDocsEnum.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestReuseDocsEnum.java
index 848b7e62b3d..3c0cc760257 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestReuseDocsEnum.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene40/TestReuseDocsEnum.java
@@ -37,10 +37,16 @@ import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LineFileDocs;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;
+import org.junit.BeforeClass;
// TODO: really this should be in BaseTestPF or somewhere else? useful test!
public class TestReuseDocsEnum extends LuceneTestCase {
+ @BeforeClass
+ public static void beforeClass() {
+ OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true; // explicitly instantiates ancient codec
+ }
+
public void testReuseDocsEnumNoReuse() throws IOException {
Directory dir = newDirectory();
Codec cp = _TestUtil.alwaysPostingsFormat(new Lucene40RWPostingsFormat());
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene41/TestLucene41StoredFieldsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene41/TestLucene41StoredFieldsFormat.java
index d5b11f6de5f..cf296f50167 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene41/TestLucene41StoredFieldsFormat.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene41/TestLucene41StoredFieldsFormat.java
@@ -19,9 +19,15 @@ package org.apache.lucene.codecs.lucene41;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.index.BaseStoredFieldsFormatTestCase;
+import org.junit.BeforeClass;
public class TestLucene41StoredFieldsFormat extends BaseStoredFieldsFormatTestCase {
+ @BeforeClass
+ public static void beforeClass() {
+ OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true; // explicitly instantiates ancient codec
+ }
+
@Override
protected Codec getCodec() {
return new Lucene41RWCodec();
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene42/TestLucene42DocValuesFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene42/TestLucene42DocValuesFormat.java
index e8885927644..a7796c47b46 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene42/TestLucene42DocValuesFormat.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene42/TestLucene42DocValuesFormat.java
@@ -19,6 +19,7 @@ package org.apache.lucene.codecs.lucene42;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.index.BaseCompressingDocValuesFormatTestCase;
+import org.junit.BeforeClass;
/**
* Tests Lucene42DocValuesFormat
@@ -26,6 +27,11 @@ import org.apache.lucene.index.BaseCompressingDocValuesFormatTestCase;
public class TestLucene42DocValuesFormat extends BaseCompressingDocValuesFormatTestCase {
private final Codec codec = new Lucene42RWCodec();
+ @BeforeClass
+ public static void beforeClass() {
+ OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true; // explicitly instantiates ancient codec
+ }
+
@Override
protected Codec getCodec() {
return codec;
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java b/lucene/core/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java
index daaa72deff9..593632a76b3 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java
@@ -213,6 +213,7 @@ public class TestBackwardsCompatibility extends LuceneTestCase {
@BeforeClass
public static void beforeClass() throws Exception {
+ assertFalse("test infra is broken!", LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE);
List<String> names = new ArrayList<String>(oldNames.length + oldSingleSegmentNames.length);
names.addAll(Arrays.asList(oldNames));
names.addAll(Arrays.asList(oldSingleSegmentNames));
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestNumericDocValuesUpdates.java b/lucene/core/src/test/org/apache/lucene/index/TestNumericDocValuesUpdates.java
index 9de3291267c..69a74586a29 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestNumericDocValuesUpdates.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestNumericDocValuesUpdates.java
@@ -946,9 +946,11 @@ public class TestNumericDocValuesUpdates extends LuceneTestCase {
Codec[] oldCodecs = new Codec[] { new Lucene40RWCodec(), new Lucene41RWCodec(), new Lucene42RWCodec(), new Lucene45RWCodec() };
Directory dir = newDirectory();
+ boolean oldValue = OLD_FORMAT_IMPERSONATION_IS_ACTIVE;
// create a segment with an old Codec
IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()));
conf.setCodec(oldCodecs[random().nextInt(oldCodecs.length)]);
+ OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true;
IndexWriter writer = new IndexWriter(dir, conf);
Document doc = new Document();
doc.add(new StringField("id", "doc", Store.NO));
@@ -966,7 +968,7 @@ public class TestNumericDocValuesUpdates extends LuceneTestCase {
} catch (UnsupportedOperationException e) {
writer.rollback();
} finally {
- OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true;
+ OLD_FORMAT_IMPERSONATION_IS_ACTIVE = oldValue;
}
dir.close();
diff --git a/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java
index 044ca31a632..49f91132e73 100644
--- a/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java
+++ b/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java
@@ -51,6 +51,8 @@ public class TestFileSwitchDirectory extends LuceneTestCase {
FileSwitchDirectory fsd = new FileSwitchDirectory(fileExtensions, primaryDir, secondaryDir, true);
// for now we wire Lucene40Codec because we rely upon its specific impl
+ boolean oldValue = OLD_FORMAT_IMPERSONATION_IS_ACTIVE;
+ OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true;
IndexWriter writer = new IndexWriter(
fsd,
new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).
@@ -82,6 +84,7 @@ public class TestFileSwitchDirectory extends LuceneTestCase {
assertNotNull(files[i]);
}
fsd.close();
+ OLD_FORMAT_IMPERSONATION_IS_ACTIVE = oldValue;
}
private Directory newFSSwitchDirectory(Set<String> primaryExtensions) throws IOException {
diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWCodec.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWCodec.java
index 1c85d27c876..b9119580cf5 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWCodec.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWCodec.java
@@ -41,17 +41,6 @@ public final class Lucene40RWCodec extends Lucene40Codec {
}
};
- private final SegmentInfoFormat infosFormat = new Lucene40SegmentInfoFormat() {
- @Override
- public org.apache.lucene.codecs.SegmentInfoWriter getSegmentInfoWriter() {
- if (!LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE) {
- return super.getSegmentInfoWriter();
- } else {
- return new Lucene40SegmentInfoWriter();
- }
- }
- };
-
private final DocValuesFormat docValues = new Lucene40RWDocValuesFormat();
private final NormsFormat norms = new Lucene40RWNormsFormat();
@@ -70,9 +59,4 @@ public final class Lucene40RWCodec extends Lucene40Codec {
return norms;
}
- @Override
- public SegmentInfoFormat segmentInfoFormat() {
- return infosFormat;
- }
-
}
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.java
index 862a9f459d2..b581b7a60af 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
@@ -334,12 +334,12 @@ public abstract class LuceneTestCase extends Assert {
/**
* When {@code true}, Codecs for old Lucene version will support writing
- * indexes in that format. Defaults to {@code true}, can be disabled by
- * spdecific tests on demand.
+ * indexes in that format. Defaults to {@code false}, can be disabled by
+ * specific tests on demand.
*
* @lucene.internal
*/
- public static boolean OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true;
+ public static boolean OLD_FORMAT_IMPERSONATION_IS_ACTIVE = false;
// -----------------------------------------------------------------
diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java b/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java
index c31de7c17f6..5d61527649f 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java
@@ -155,6 +155,9 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
avoidCodecs.addAll(Arrays.asList(a.value()));
}
+ // set back to default
+ LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE = false;
+
savedCodec = Codec.getDefault();
int randomVal = random.nextInt(10);
if ("Lucene40".equals(TEST_CODEC) || ("random".equals(TEST_CODEC) &&
@@ -163,6 +166,7 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
randomVal == 0 &&
!shouldAvoidCodec("Lucene40"))) {
codec = Codec.forName("Lucene40");
+ LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true;
assert codec instanceof Lucene40RWCodec : "fix your classpath to have tests-framework.jar before lucene-core.jar";
assert (PostingsFormat.forName("Lucene40") instanceof Lucene40RWPostingsFormat) : "fix your classpath to have tests-framework.jar before lucene-core.jar";
} else if ("Lucene41".equals(TEST_CODEC) || ("random".equals(TEST_CODEC) &&
@@ -171,6 +175,7 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
randomVal == 1 &&
!shouldAvoidCodec("Lucene41"))) {
codec = Codec.forName("Lucene41");
+ LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true;
assert codec instanceof Lucene41RWCodec : "fix your classpath to have tests-framework.jar before lucene-core.jar";
} else if ("Lucene42".equals(TEST_CODEC) || ("random".equals(TEST_CODEC) &&
"random".equals(TEST_POSTINGSFORMAT) &&
@@ -178,6 +183,7 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
randomVal == 2 &&
!shouldAvoidCodec("Lucene42"))) {
codec = Codec.forName("Lucene42");
+ LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true;
assert codec instanceof Lucene42RWCodec : "fix your classpath to have tests-framework.jar before lucene-core.jar";
} else if ("Lucene45".equals(TEST_CODEC) || ("random".equals(TEST_CODEC) &&
"random".equals(TEST_POSTINGSFORMAT) &&
@@ -185,6 +191,7 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
randomVal == 5 &&
!shouldAvoidCodec("Lucene45"))) {
codec = Codec.forName("Lucene45");
+ LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true;
assert codec instanceof Lucene45RWCodec : "fix your classpath to have tests-framework.jar before lucene-core.jar";
} else if (("random".equals(TEST_POSTINGSFORMAT) == false) || ("random".equals(TEST_DOCVALUESFORMAT) == false)) {
// the user wired postings or DV: this is messy