LUCENE-7662: throw CorruptIndexException if index files are missing

This commit is contained in:
Mike McCandless 2017-02-10 20:20:53 -05:00
parent 015382257a
commit be007a6c11
4 changed files with 40 additions and 28 deletions

View File

@ -37,6 +37,10 @@ Improvements
* LUCENE-7489: Better storage of sparse doc-values fields with the default
codec. (Adrien Grand)
* LUCENE-7662: If index files are missing, throw CorruptIndexException instead
of the less descriptive FileNotFound or NoSuchFileException (Mike Drob via
Mike McCandless, Erick Erickson)
Optimizations
* LUCENE-7416: BooleanQuery optimizes queries that have queries that occur both

View File

@ -17,7 +17,10 @@
package org.apache.lucene.index;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
@ -133,6 +136,10 @@ final class SegmentCoreReaders {
pointsReader = null;
}
success = true;
} catch (EOFException | FileNotFoundException e) {
throw new CorruptIndexException("Problem reading index from " + dir, dir.toString(), e);
} catch (NoSuchFileException e) {
throw new CorruptIndexException("Problem reading index.", e.getFile(), e);
} finally {
if (!success) {
decRef();

View File

@ -18,8 +18,10 @@ package org.apache.lucene.index;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -280,8 +282,8 @@ public final class SegmentInfos implements Cloneable, Iterable<SegmentCommitInfo
try (ChecksumIndexInput input = directory.openChecksumInput(segmentFileName, IOContext.READ)) {
try {
return readCommit(directory, input, generation);
} catch (EOFException e) {
throw new CorruptIndexException("Unexpected end of file while reading index.", input, e);
} catch (EOFException | NoSuchFileException | FileNotFoundException e) {
throw new CorruptIndexException("Unexpected file read error while reading index.", input, e);
}
}
}

View File

@ -544,14 +544,9 @@ abstract class BaseIndexFileFormatTestCase extends LuceneTestCase {
conf.setMergeScheduler(new SerialMergeScheduler());
conf.setCodec(getCodec());
iw = new IndexWriter(dir, conf);
} catch (Exception e) {
if (e.getMessage() != null && e.getMessage().startsWith("a random IOException")) {
exceptionStream.println("\nTEST: got expected fake exc:" + e.getMessage());
e.printStackTrace(exceptionStream);
allowAlreadyClosed = true;
} else {
Rethrow.rethrow(e);
}
} catch (IOException e) {
handleFakeIOException(e, exceptionStream);
allowAlreadyClosed = true;
}
if (random().nextInt(10) == 0) {
@ -585,14 +580,9 @@ abstract class BaseIndexFileFormatTestCase extends LuceneTestCase {
conf.setMergeScheduler(new SerialMergeScheduler());
conf.setCodec(getCodec());
iw = new IndexWriter(dir, conf);
} catch (Exception e) {
if (e.getMessage() != null && e.getMessage().startsWith("a random IOException")) {
exceptionStream.println("\nTEST: got expected fake exc:" + e.getMessage());
e.printStackTrace(exceptionStream);
allowAlreadyClosed = true;
} else {
Rethrow.rethrow(e);
}
} catch (IOException e) {
handleFakeIOException(e, exceptionStream);
allowAlreadyClosed = true;
}
}
}
@ -601,16 +591,11 @@ abstract class BaseIndexFileFormatTestCase extends LuceneTestCase {
dir.setRandomIOExceptionRateOnOpen(0.0); // disable exceptions on openInput until next iteration:
// or we make slowExists angry and trip a scarier assert!
iw.close();
} catch (Exception e) {
if (e.getMessage() != null && e.getMessage().startsWith("a random IOException")) {
exceptionStream.println("\nTEST: got expected fake exc:" + e.getMessage());
e.printStackTrace(exceptionStream);
try {
iw.rollback();
} catch (Throwable t) {}
} else {
Rethrow.rethrow(e);
}
} catch (IOException e) {
handleFakeIOException(e, exceptionStream);
try {
iw.rollback();
} catch (Throwable t) {}
}
dir.close();
} catch (Throwable t) {
@ -626,4 +611,18 @@ abstract class BaseIndexFileFormatTestCase extends LuceneTestCase {
System.out.println(exceptionLog.toString("UTF-8"));
}
}
private void handleFakeIOException(IOException e, PrintStream exceptionStream) {
Throwable ex = e;
while (ex != null) {
if (ex.getMessage() != null && ex.getMessage().startsWith("a random IOException")) {
exceptionStream.println("\nTEST: got expected fake exc:" + ex.getMessage());
ex.printStackTrace(exceptionStream);
return;
}
ex = ex.getCause();
}
Rethrow.rethrow(e);
}
}