Apache Lucene open-source search software
LUCENE-7662: throw CorruptIndexException if index files are missing
Mike McCandlessbe007a6c1124
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex b439b46614e..88a1629fffb 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -37,6 +37,10 @@ Improvements* LUCENE-7489: Better storage of sparse doc-values fields with the defaultcodec. (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 bothdiff --git a/lucene/core/src/java/org/apache/lucene/index/SegmentCoreReaders.java b/lucene/core/src/java/org/apache/lucene/index/SegmentCoreReaders.javaindex 21ac4a16e98..270a2d54785 100644--- a/lucene/core/src/java/org/apache/lucene/index/SegmentCoreReaders.java+++ b/lucene/core/src/java/org/apache/lucene/index/SegmentCoreReaders.java@@ -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();diff --git a/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java b/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.javaindex 3e8b1f871bd..aaa1d89f5a3 100644--- a/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java+++ b/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java@@ -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<SegmentCommitInfotry (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);}}}diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/BaseIndexFileFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/BaseIndexFileFormatTestCase.javaindex 7a7abc091c7..3097cdf2c89 100644--- a/lucene/test-framework/src/java/org/apache/lucene/index/BaseIndexFileFormatTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/index/BaseIndexFileFormatTestCase.java@@ -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);+ }}