From 2ef69d2ec33f54a532940c7a89bc7b8d07449912 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Fri, 5 Oct 2012 14:39:55 +0000 Subject: [PATCH] LUCENE-4456: clean up more exception handling git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1394551 13f79535-47bb-0310-9956-ffa450edef68 --- .../lucene/codecs/sep/SepPostingsReader.java | 26 ++----------------- .../SimpleTextFieldInfosReader.java | 11 ++++++-- .../SimpleTextFieldInfosWriter.java | 9 ++++++- .../simpletext/SimpleTextPerDocProducer.java | 8 +++++- .../SimpleTextStoredFieldsReader.java | 4 ++- .../SimpleTextTermVectorsReader.java | 4 ++- .../lucene/codecs/PerDocProducerBase.java | 4 ++- .../lucene40/Lucene40DocValuesConsumer.java | 12 ++++++--- .../lucene40/Lucene40FieldInfosReader.java | 13 +++++++--- .../lucene40/Lucene40FieldInfosWriter.java | 9 ++++++- .../lucene40/Lucene40StoredFieldsReader.java | 4 ++- .../lucene40/Lucene40TermVectorsReader.java | 4 ++- .../java/org/apache/lucene/util/IOUtils.java | 2 +- 13 files changed, 69 insertions(+), 41 deletions(-) diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/sep/SepPostingsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/sep/SepPostingsReader.java index c76de76b175..53f902b7e0a 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/sep/SepPostingsReader.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/sep/SepPostingsReader.java @@ -37,6 +37,7 @@ import org.apache.lucene.store.IndexInput; import org.apache.lucene.util.ArrayUtil; import org.apache.lucene.util.Bits; import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.IOUtils; /** Concrete class that reads the current doc/freq/skip * postings format. @@ -101,30 +102,7 @@ public class SepPostingsReader extends PostingsReaderBase { @Override public void close() throws IOException { - try { - if (freqIn != null) - freqIn.close(); - } finally { - try { - if (docIn != null) - docIn.close(); - } finally { - try { - if (skipIn != null) - skipIn.close(); - } finally { - try { - if (posIn != null) { - posIn.close(); - } - } finally { - if (payloadIn != null) { - payloadIn.close(); - } - } - } - } - } + IOUtils.close(freqIn, docIn, skipIn, posIn, payloadIn); } private static final class SepTermState extends BlockTermState { diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosReader.java index 87891f455e8..7a232cc1e64 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosReader.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosReader.java @@ -52,6 +52,7 @@ public class SimpleTextFieldInfosReader extends FieldInfosReader { IndexInput input = directory.openInput(fileName, iocontext); BytesRef scratch = new BytesRef(); + boolean success = false; try { SimpleTextUtil.readLine(input, scratch); @@ -127,9 +128,15 @@ public class SimpleTextFieldInfosReader extends FieldInfosReader { throw new CorruptIndexException("did not read all bytes from file \"" + fileName + "\": read " + input.getFilePointer() + " vs size " + input.length() + " (resource: " + input + ")"); } - return new FieldInfos(infos); + FieldInfos fieldInfos = new FieldInfos(infos); + success = true; + return fieldInfos; } finally { - input.close(); + if (success) { + input.close(); + } else { + IOUtils.closeWhileHandlingException(input); + } } } diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosWriter.java index d8e40727d55..b5693c9328f 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosWriter.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextFieldInfosWriter.java @@ -29,6 +29,7 @@ import org.apache.lucene.store.Directory; import org.apache.lucene.store.IOContext; import org.apache.lucene.store.IndexOutput; import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.IOUtils; /** * writes plaintext field infos files @@ -62,6 +63,7 @@ public class SimpleTextFieldInfosWriter extends FieldInfosWriter { final String fileName = IndexFileNames.segmentFileName(segmentName, "", FIELD_INFOS_EXTENSION); IndexOutput out = directory.createOutput(fileName, context); BytesRef scratch = new BytesRef(); + boolean success = false; try { SimpleTextUtil.write(out, NUMFIELDS); SimpleTextUtil.write(out, Integer.toString(infos.size()), scratch); @@ -125,8 +127,13 @@ public class SimpleTextFieldInfosWriter extends FieldInfosWriter { } } } + success = true; } finally { - out.close(); + if (success) { + out.close(); + } else { + IOUtils.closeWhileHandlingException(out); + } } } diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextPerDocProducer.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextPerDocProducer.java index 639bc54339c..f831efd64ec 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextPerDocProducer.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextPerDocProducer.java @@ -96,10 +96,16 @@ public class SimpleTextPerDocProducer extends PerDocProducerBase { @Override public void close() throws IOException { + boolean success = false; try { super.close(); + success = true; } finally { - IOUtils.close(input); + if (success) { + IOUtils.close(input); + } else { + IOUtils.closeWhileHandlingException(input); + } } } diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextStoredFieldsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextStoredFieldsReader.java index 4ca78b22a04..9966af986fe 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextStoredFieldsReader.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextStoredFieldsReader.java @@ -60,7 +60,9 @@ public class SimpleTextStoredFieldsReader extends StoredFieldsReader { success = true; } finally { if (!success) { - close(); + try { + close(); + } catch (Throwable t) {} // ensure we throw our original exception } } readIndex(); diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java index 3b80d3dc964..f62f677942f 100644 --- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java +++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextTermVectorsReader.java @@ -66,7 +66,9 @@ public class SimpleTextTermVectorsReader extends TermVectorsReader { success = true; } finally { if (!success) { - close(); + try { + close(); + } catch (Throwable t) {} // ensure we throw our original exception } } readIndex(); diff --git a/lucene/core/src/java/org/apache/lucene/codecs/PerDocProducerBase.java b/lucene/core/src/java/org/apache/lucene/codecs/PerDocProducerBase.java index 50ec84ee12e..18742790c6f 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/PerDocProducerBase.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/PerDocProducerBase.java @@ -85,7 +85,9 @@ public abstract class PerDocProducerBase extends PerDocProducer { } finally { if (!success) { // if we fail we must close all opened resources if there are any - closeInternal(values.values()); + try { + closeInternal(values.values()); + } catch (Throwable t) {} // keep our original exception } } return values; diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesConsumer.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesConsumer.java index 0ac516ef713..8d58e673334 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesConsumer.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40DocValuesConsumer.java @@ -70,11 +70,17 @@ public class Lucene40DocValuesConsumer extends DocValuesWriterBase { public void abort() { try { close(); - } catch (IOException ignored) { - } - IOUtils.deleteFilesIgnoringExceptions(mainDirectory, IndexFileNames.segmentFileName( + } catch (Throwable t) { + // ignore + } finally { + // TODO: why the inconsistency here? we do this, but not SimpleText (which says IFD + // will do it). + // TODO: check that IFD really does this always, even if codec abort() throws a + // RuntimeException (e.g. ThreadInterruptedException) + IOUtils.deleteFilesIgnoringExceptions(mainDirectory, IndexFileNames.segmentFileName( segmentName, segmentSuffix, IndexFileNames.COMPOUND_FILE_EXTENSION), IndexFileNames.segmentFileName(segmentName, segmentSuffix, IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION)); + } } } diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosReader.java index 9ec9e549e5f..7146bdcca3f 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosReader.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosReader.java @@ -32,6 +32,7 @@ import org.apache.lucene.index.DocValues; import org.apache.lucene.store.Directory; import org.apache.lucene.store.IOContext; import org.apache.lucene.store.IndexInput; +import org.apache.lucene.util.IOUtils; /** * Lucene 4.0 FieldInfos reader. @@ -50,6 +51,7 @@ public class Lucene40FieldInfosReader extends FieldInfosReader { final String fileName = IndexFileNames.segmentFileName(segmentName, "", Lucene40FieldInfosWriter.FIELD_INFOS_EXTENSION); IndexInput input = directory.openInput(fileName, iocontext); + boolean success = false; try { CodecUtil.checkHeader(input, Lucene40FieldInfosWriter.CODEC_NAME, Lucene40FieldInfosWriter.FORMAT_START, @@ -97,10 +99,15 @@ public class Lucene40FieldInfosReader extends FieldInfosReader { if (input.getFilePointer() != input.length()) { throw new CorruptIndexException("did not read all bytes from file \"" + fileName + "\": read " + input.getFilePointer() + " vs size " + input.length() + " (resource: " + input + ")"); } - - return new FieldInfos(infos); + FieldInfos fieldInfos = new FieldInfos(infos); + success = true; + return fieldInfos; } finally { - input.close(); + if (success) { + input.close(); + } else { + IOUtils.closeWhileHandlingException(input); + } } } diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosWriter.java index 84d8b98e056..7d0e6eae02e 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosWriter.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40FieldInfosWriter.java @@ -28,6 +28,7 @@ import org.apache.lucene.index.IndexFileNames; import org.apache.lucene.store.Directory; import org.apache.lucene.store.IOContext; import org.apache.lucene.store.IndexOutput; +import org.apache.lucene.util.IOUtils; /** * Lucene 4.0 FieldInfos writer. @@ -60,6 +61,7 @@ public class Lucene40FieldInfosWriter extends FieldInfosWriter { public void write(Directory directory, String segmentName, FieldInfos infos, IOContext context) throws IOException { final String fileName = IndexFileNames.segmentFileName(segmentName, "", FIELD_INFOS_EXTENSION); IndexOutput output = directory.createOutput(fileName, context); + boolean success = false; try { CodecUtil.writeHeader(output, CODEC_NAME, FORMAT_CURRENT); output.writeVInt(infos.size()); @@ -92,8 +94,13 @@ public class Lucene40FieldInfosWriter extends FieldInfosWriter { output.writeByte(val); output.writeStringStringMap(fi.attributes()); } + success = true; } finally { - output.close(); + if (success) { + output.close(); + } else { + IOUtils.closeWhileHandlingException(output); + } } } diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40StoredFieldsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40StoredFieldsReader.java index bcdcdae83be..8d309ec8111 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40StoredFieldsReader.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40StoredFieldsReader.java @@ -102,7 +102,9 @@ public final class Lucene40StoredFieldsReader extends StoredFieldsReader impleme // of things that were opened so that we don't have to // wait for a GC to do so. if (!success) { - close(); + try { + close(); + } catch (Throwable t) {} // ensure we throw our original exception } } } diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40TermVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40TermVectorsReader.java index 27bed1ff185..00f8e0bdb67 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40TermVectorsReader.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene40/Lucene40TermVectorsReader.java @@ -139,7 +139,9 @@ public class Lucene40TermVectorsReader extends TermVectorsReader implements Clos // of things that were opened so that we don't have to // wait for a GC to do so. if (!success) { - close(); + try { + close(); + } catch (Throwable t) {} // ensure we throw our original exception } } } diff --git a/lucene/core/src/java/org/apache/lucene/util/IOUtils.java b/lucene/core/src/java/org/apache/lucene/util/IOUtils.java index 0e76c6007b9..83522dcb87e 100644 --- a/lucene/core/src/java/org/apache/lucene/util/IOUtils.java +++ b/lucene/core/src/java/org/apache/lucene/util/IOUtils.java @@ -335,7 +335,7 @@ public final class IOUtils { for (String name : files) { try { dir.deleteFile(name); - } catch (IOException ignored) { + } catch (Throwable ignored) { // ignore } }