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 * LUCENE-7489: Better storage of sparse doc-values fields with the default
codec. (Adrien Grand) 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 Optimizations
* LUCENE-7416: BooleanQuery optimizes queries that have queries that occur both * LUCENE-7416: BooleanQuery optimizes queries that have queries that occur both

View File

@ -17,7 +17,10 @@
package org.apache.lucene.index; package org.apache.lucene.index;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
@ -133,6 +136,10 @@ final class SegmentCoreReaders {
pointsReader = null; pointsReader = null;
} }
success = true; 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 { } finally {
if (!success) { if (!success) {
decRef(); decRef();

View File

@ -18,8 +18,10 @@ package org.apache.lucene.index;
import java.io.EOFException; import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; 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 (ChecksumIndexInput input = directory.openChecksumInput(segmentFileName, IOContext.READ)) {
try { try {
return readCommit(directory, input, generation); return readCommit(directory, input, generation);
} catch (EOFException e) { } catch (EOFException | NoSuchFileException | FileNotFoundException e) {
throw new CorruptIndexException("Unexpected end of file while reading index.", input, 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.setMergeScheduler(new SerialMergeScheduler());
conf.setCodec(getCodec()); conf.setCodec(getCodec());
iw = new IndexWriter(dir, conf); iw = new IndexWriter(dir, conf);
} catch (Exception e) { } catch (IOException e) {
if (e.getMessage() != null && e.getMessage().startsWith("a random IOException")) { handleFakeIOException(e, exceptionStream);
exceptionStream.println("\nTEST: got expected fake exc:" + e.getMessage()); allowAlreadyClosed = true;
e.printStackTrace(exceptionStream);
allowAlreadyClosed = true;
} else {
Rethrow.rethrow(e);
}
} }
if (random().nextInt(10) == 0) { if (random().nextInt(10) == 0) {
@ -585,14 +580,9 @@ abstract class BaseIndexFileFormatTestCase extends LuceneTestCase {
conf.setMergeScheduler(new SerialMergeScheduler()); conf.setMergeScheduler(new SerialMergeScheduler());
conf.setCodec(getCodec()); conf.setCodec(getCodec());
iw = new IndexWriter(dir, conf); iw = new IndexWriter(dir, conf);
} catch (Exception e) { } catch (IOException e) {
if (e.getMessage() != null && e.getMessage().startsWith("a random IOException")) { handleFakeIOException(e, exceptionStream);
exceptionStream.println("\nTEST: got expected fake exc:" + e.getMessage()); allowAlreadyClosed = true;
e.printStackTrace(exceptionStream);
allowAlreadyClosed = true;
} else {
Rethrow.rethrow(e);
}
} }
} }
} }
@ -601,16 +591,11 @@ abstract class BaseIndexFileFormatTestCase extends LuceneTestCase {
dir.setRandomIOExceptionRateOnOpen(0.0); // disable exceptions on openInput until next iteration: dir.setRandomIOExceptionRateOnOpen(0.0); // disable exceptions on openInput until next iteration:
// or we make slowExists angry and trip a scarier assert! // or we make slowExists angry and trip a scarier assert!
iw.close(); iw.close();
} catch (Exception e) { } catch (IOException e) {
if (e.getMessage() != null && e.getMessage().startsWith("a random IOException")) { handleFakeIOException(e, exceptionStream);
exceptionStream.println("\nTEST: got expected fake exc:" + e.getMessage()); try {
e.printStackTrace(exceptionStream); iw.rollback();
try { } catch (Throwable t) {}
iw.rollback();
} catch (Throwable t) {}
} else {
Rethrow.rethrow(e);
}
} }
dir.close(); dir.close();
} catch (Throwable t) { } catch (Throwable t) {
@ -626,4 +611,18 @@ abstract class BaseIndexFileFormatTestCase extends LuceneTestCase {
System.out.println(exceptionLog.toString("UTF-8")); 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);
}
} }