Apache Lucene open-source search software
LUCENE-6213: Add useful exception message when commit contains segments from legacy codecs (forward port from branch_5x)
Ryan Ernstf59604f885fe
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 5df40ce140b..a1c401e216b 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -928,6 +928,9 @@ Other* LUCENE-5915: Remove Pulsing postings format. (Robert Muir)+* LUCENE-6213: Add useful exception message when commit contains segments from legacy codecs.+ (Ryan Ernst)+======================= Lucene 4.10.4 ======================Bug fixesdiff --git a/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java b/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.javaindex 7650fe67600..7fca15acad8 100644--- a/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java+++ b/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java@@ -321,7 +321,9 @@ public class TestBackwardsCompatibility extends LuceneTestCase {"4.10.3-cfs","4.10.3-nocfs","4.10.4-cfs",- "4.10.4-nocfs"+ "4.10.4-nocfs",+ "5x-with-4x-segments-cfs",+ "5x-with-4x-segments-nocfs"};final static String[] oldSingleSegmentNames = {@@ -845,7 +847,7 @@ public class TestBackwardsCompatibility extends LuceneTestCase {mp.setMaxCFSSegmentSizeMB(Double.POSITIVE_INFINITY);// TODO: remove randomnessIndexWriterConfig conf = new IndexWriterConfig(new MockAnalyzer(random()))- .setMaxBufferedDocs(10).setMergePolicy(mp);+ .setMaxBufferedDocs(10).setMergePolicy(NoMergePolicy.INSTANCE);IndexWriter writer = new IndexWriter(dir, conf);for(int i=0;i<35;i++) {@@ -863,7 +865,7 @@ public class TestBackwardsCompatibility extends LuceneTestCase {mp.setNoCFSRatio(doCFS ? 1.0 : 0.0);// TODO: remove randomnessconf = new IndexWriterConfig(new MockAnalyzer(random()))- .setMaxBufferedDocs(10).setMergePolicy(mp);+ .setMaxBufferedDocs(10).setMergePolicy(NoMergePolicy.INSTANCE);writer = new IndexWriter(dir, conf);addNoProxDoc(writer);writer.close();diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/index/unsupported.5x-with-4x-segments-cfs.zip b/lucene/backward-codecs/src/test/org/apache/lucene/index/unsupported.5x-with-4x-segments-cfs.zipnew file mode 100644index 00000000000..96b8cf37a6eBinary files /dev/null and b/lucene/backward-codecs/src/test/org/apache/lucene/index/unsupported.5x-with-4x-segments-cfs.zip differdiff --git a/lucene/backward-codecs/src/test/org/apache/lucene/index/unsupported.5x-with-4x-segments-nocfs.zip b/lucene/backward-codecs/src/test/org/apache/lucene/index/unsupported.5x-with-4x-segments-nocfs.zipnew file mode 100644index 00000000000..de9498db982Binary files /dev/null and b/lucene/backward-codecs/src/test/org/apache/lucene/index/unsupported.5x-with-4x-segments-nocfs.zip differdiff --git a/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java b/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.javaindex a07a2e39fc6..cc207a6bc62 100644--- a/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java+++ b/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java@@ -37,6 +37,7 @@ import org.apache.lucene.codecs.DocValuesFormat;import org.apache.lucene.codecs.FieldInfosFormat;import org.apache.lucene.codecs.LiveDocsFormat;import org.apache.lucene.store.ChecksumIndexInput;+import org.apache.lucene.store.DataInput;import org.apache.lucene.store.DataOutput;import org.apache.lucene.store.Directory;import org.apache.lucene.store.IOContext;@@ -276,7 +277,7 @@ public final class SegmentInfos implements Cloneable, Iterable<SegmentCommitInfobyte id[] = new byte[StringHelper.ID_LENGTH];input.readBytes(id, 0, id.length);CodecUtil.checkIndexHeaderSuffix(input, Long.toString(generation, Character.MAX_RADIX));-+SegmentInfos infos = new SegmentInfos();infos.id = id;infos.generation = generation;@@ -295,10 +296,12 @@ public final class SegmentInfos implements Cloneable, Iterable<SegmentCommitInfoif (hasID == 1) {segmentID = new byte[StringHelper.ID_LENGTH];input.readBytes(segmentID, 0, segmentID.length);+ } else if (hasID == 0) {+ throw new IndexFormatTooOldException(input, "Segment is from Lucene 4.x");} else {throw new CorruptIndexException("invalid hasID byte, got: " + hasID, input);}- Codec codec = Codec.forName(input.readString());+ Codec codec = readCodec(input);SegmentInfo info = codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);info.setCodec(codec);totalDocs += info.maxDoc();@@ -341,15 +344,33 @@ public final class SegmentInfos implements Cloneable, Iterable<SegmentCommitInfoCodecUtil.checkFooter(input);- // LUCENE-6299: check we are in bounds- if (totalDocs > IndexWriter.getActualMaxDocs()) {- throw new CorruptIndexException("Too many documents: an index cannot exceed " + IndexWriter.getActualMaxDocs() + " but readers have total maxDoc=" + totalDocs, input);- }-return infos;}}+ private static final List<String> unsupportedCodecs = Arrays.asList(+ "Lucene3x", "Lucene40", "Lucene41", "Lucene42", "Lucene45", "Lucene46", "Lucene49", "Lucene410"+ );++ private static Codec readCodec(DataInput input) throws IOException {+ final String name = input.readString();+ try {+ return Codec.forName(name);+ } catch (IllegalArgumentException e) {+ // give better error messages if we can, first check if this is a legacy codec+ if (unsupportedCodecs.contains(name)) {+ IOException newExc = new IndexFormatTooOldException(input, "Codec '" + name + "' is too old");+ newExc.initCause(e);+ throw newExc;+ }+ // or maybe it's an old default codec that moved+ if (name.startsWith("Lucene")) {+ throw new IllegalArgumentException("Could not load codec '" + name + "'. Did you forget to add lucene-backward-codecs.jar?", e);+ }+ throw e;+ }+ }+/** Find the latest commit ({@code segments_N file}) and* load all {@link SegmentCommitInfo}s. */public static final SegmentInfos readLatestCommit(Directory directory) throws IOException {