LUCENE-6213: Add useful exception message when commit contains segments from legacy codecs (forward port from branch_5x)

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1676648 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan Ernst 2015-04-28 23:46:20 +00:00
parent 701b7ddf3a
commit f59604f885
5 changed files with 36 additions and 10 deletions

View File

@ -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 fixes

View File

@ -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 randomness
IndexWriterConfig 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 randomness
conf = new IndexWriterConfig(new MockAnalyzer(random()))
.setMaxBufferedDocs(10).setMergePolicy(mp);
.setMaxBufferedDocs(10).setMergePolicy(NoMergePolicy.INSTANCE);
writer = new IndexWriter(dir, conf);
addNoProxDoc(writer);
writer.close();

View File

@ -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<SegmentCommitInfo
byte 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<SegmentCommitInfo
if (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<SegmentCommitInfo
CodecUtil.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 {