Apache Solr open-source search software
fix a few cases where we fail to close IO if we hit exc during init
Michael McCandless74076d2ce89d
diff --git a/lucene/src/java/org/apache/lucene/index/DocFieldProcessor.java b/lucene/src/java/org/apache/lucene/index/DocFieldProcessor.javaindex a45d52c2aa7..12e1524f9af 100644--- a/lucene/src/java/org/apache/lucene/index/DocFieldProcessor.java+++ b/lucene/src/java/org/apache/lucene/index/DocFieldProcessor.java@@ -112,12 +112,8 @@ final class DocFieldProcessor extends DocConsumer {field = next;}}- try {- IOUtils.closeWhileHandlingException(perDocConsumers.values());- // TODO add abort to PerDocConsumer!- } catch (IOException e) {- // ignore on abort!- }+ IOUtils.closeWhileHandlingException(perDocConsumers.values());+ // TODO add abort to PerDocConsumer!try {fieldsWriter.abort();diff --git a/lucene/src/java/org/apache/lucene/index/codecs/lucene40/Lucene40PostingsWriter.java b/lucene/src/java/org/apache/lucene/index/codecs/lucene40/Lucene40PostingsWriter.javaindex 7a4df561321..104b6389620 100644--- a/lucene/src/java/org/apache/lucene/index/codecs/lucene40/Lucene40PostingsWriter.java+++ b/lucene/src/java/org/apache/lucene/index/codecs/lucene40/Lucene40PostingsWriter.java@@ -26,8 +26,8 @@ import java.util.List;import org.apache.lucene.index.CorruptIndexException;import org.apache.lucene.index.DocsEnum;-import org.apache.lucene.index.FieldInfo;import org.apache.lucene.index.FieldInfo.IndexOptions;+import org.apache.lucene.index.FieldInfo;import org.apache.lucene.index.IndexFileNames;import org.apache.lucene.index.SegmentWriteState;import org.apache.lucene.index.codecs.PostingsWriterBase;@@ -36,6 +36,7 @@ import org.apache.lucene.store.IndexOutput;import org.apache.lucene.store.RAMOutputStream;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.CodecUtil;+import org.apache.lucene.util.IOUtils;/** @lucene.experimental */public final class Lucene40PostingsWriter extends PostingsWriterBase {@@ -92,14 +93,22 @@ public final class Lucene40PostingsWriter extends PostingsWriterBase {// this.segment = state.segmentName;String fileName = IndexFileNames.segmentFileName(state.segmentName, state.segmentSuffix, Lucene40PostingsFormat.FREQ_EXTENSION);freqOut = state.directory.createOutput(fileName, state.context);- if (state.fieldInfos.hasProx()) {- // At least one field does not omit TF, so create the- // prox file- fileName = IndexFileNames.segmentFileName(state.segmentName, state.segmentSuffix, Lucene40PostingsFormat.PROX_EXTENSION);- proxOut = state.directory.createOutput(fileName, state.context);- } else {- // Every field omits TF so we will write no prox file- proxOut = null;+ boolean success = false;+ try {+ if (state.fieldInfos.hasProx()) {+ // At least one field does not omit TF, so create the+ // prox file+ fileName = IndexFileNames.segmentFileName(state.segmentName, state.segmentSuffix, Lucene40PostingsFormat.PROX_EXTENSION);+ proxOut = state.directory.createOutput(fileName, state.context);+ } else {+ // Every field omits TF so we will write no prox file+ proxOut = null;+ }+ success = true;+ } finally {+ if (!success) {+ IOUtils.closeWhileHandlingException(freqOut);+ }}totalNumDocs = state.numDocs;diff --git a/lucene/src/java/org/apache/lucene/index/codecs/sep/SepPostingsWriter.java b/lucene/src/java/org/apache/lucene/index/codecs/sep/SepPostingsWriter.javaindex 4ae97ed8310..e48e2d985a4 100644--- a/lucene/src/java/org/apache/lucene/index/codecs/sep/SepPostingsWriter.java+++ b/lucene/src/java/org/apache/lucene/index/codecs/sep/SepPostingsWriter.java@@ -151,7 +151,6 @@ public final class SepPostingsWriter extends PostingsWriterBase {if (!success) {IOUtils.closeWhileHandlingException(docOut, skipOut, freqOut, posOut, payloadOut);}-}}diff --git a/lucene/src/java/org/apache/lucene/util/IOUtils.java b/lucene/src/java/org/apache/lucene/util/IOUtils.javaindex 8508c1803bc..3c1fbc71df6 100644--- a/lucene/src/java/org/apache/lucene/util/IOUtils.java+++ b/lucene/src/java/org/apache/lucene/util/IOUtils.java@@ -190,7 +190,7 @@ public final class IOUtils {* @param objects* objects to call <tt>close()</tt> on*/- public static void closeWhileHandlingException(Closeable... objects) throws IOException {+ public static void closeWhileHandlingException(Closeable... objects) {for (Closeable object : objects) {try {if (object != null) {@@ -204,7 +204,7 @@ public final class IOUtils {/*** @see #closeWhileHandlingException(Closeable...)*/- public static void closeWhileHandlingException(Iterable<? extends Closeable> objects) throws IOException {+ public static void closeWhileHandlingException(Iterable<? extends Closeable> objects) {for (Closeable object : objects) {try {if (object != null) {diff --git a/lucene/src/test-framework/java/org/apache/lucene/index/codecs/preflexrw/PreFlexFieldsWriter.java b/lucene/src/test-framework/java/org/apache/lucene/index/codecs/preflexrw/PreFlexFieldsWriter.javaindex ca3c45e5a4d..aa0b27aea4f 100644--- a/lucene/src/test-framework/java/org/apache/lucene/index/codecs/preflexrw/PreFlexFieldsWriter.java+++ b/lucene/src/test-framework/java/org/apache/lucene/index/codecs/preflexrw/PreFlexFieldsWriter.java@@ -50,15 +50,31 @@ class PreFlexFieldsWriter extends FieldsConsumer {state.fieldInfos,state.termIndexInterval);- final String freqFile = IndexFileNames.segmentFileName(state.segmentName, "", Lucene3xPostingsFormat.FREQ_EXTENSION);- freqOut = state.directory.createOutput(freqFile, state.context);- totalNumDocs = state.numDocs;-- if (state.fieldInfos.hasProx()) {- final String proxFile = IndexFileNames.segmentFileName(state.segmentName, "", Lucene3xPostingsFormat.PROX_EXTENSION);- proxOut = state.directory.createOutput(proxFile, state.context);- } else {- proxOut = null;+ boolean success = false;+ try {+ final String freqFile = IndexFileNames.segmentFileName(state.segmentName, "", Lucene3xPostingsFormat.FREQ_EXTENSION);+ freqOut = state.directory.createOutput(freqFile, state.context);+ totalNumDocs = state.numDocs;+ success = true;+ } finally {+ if (!success) {+ IOUtils.closeWhileHandlingException(termsOut);+ }+ }++ success = false;+ try {+ if (state.fieldInfos.hasProx()) {+ final String proxFile = IndexFileNames.segmentFileName(state.segmentName, "", Lucene3xPostingsFormat.PROX_EXTENSION);+ proxOut = state.directory.createOutput(proxFile, state.context);+ } else {+ proxOut = null;+ }+ success = true;+ } finally {+ if (!success) {+ IOUtils.closeWhileHandlingException(termsOut, freqOut);+ }}skipListWriter = new DefaultSkipListWriter(termsOut.skipInterval,diff --git a/lucene/src/test-framework/java/org/apache/lucene/index/codecs/preflexrw/TermInfosWriter.java b/lucene/src/test-framework/java/org/apache/lucene/index/codecs/preflexrw/TermInfosWriter.javaindex 8ca9be54ce1..ba2c5eafe17 100644--- a/lucene/src/test-framework/java/org/apache/lucene/index/codecs/preflexrw/TermInfosWriter.java+++ b/lucene/src/test-framework/java/org/apache/lucene/index/codecs/preflexrw/TermInfosWriter.java@@ -92,17 +92,12 @@ final class TermInfosWriter implements Closeable {initialize(directory, segment, fis, interval, false);boolean success = false;try {- other = new TermInfosWriter(directory, segment, fis, interval, true);- other.other = this;+ other = new TermInfosWriter(directory, segment, fis, interval, true);+ other.other = this;success = true;} finally {if (!success) {- try {- IOUtils.closeWhileHandlingException(output);- } catch (IOException e) {- // cannot happen since we suppress exceptions- throw new RuntimeException(e);- }+ IOUtils.closeWhileHandlingException(output);try {directory.deleteFile(IndexFileNames.segmentFileName(segment, "",@@ -129,21 +124,16 @@ final class TermInfosWriter implements Closeable {: Lucene3xPostingsFormat.TERMS_EXTENSION)), IOContext.DEFAULT);boolean success = false;try {- output.writeInt(FORMAT_CURRENT); // write format- output.writeLong(0); // leave space for size- output.writeInt(indexInterval); // write indexInterval- output.writeInt(skipInterval); // write skipInterval- output.writeInt(maxSkipLevels); // write maxSkipLevels- assert initUTF16Results();+ output.writeInt(FORMAT_CURRENT); // write format+ output.writeLong(0); // leave space for size+ output.writeInt(indexInterval); // write indexInterval+ output.writeInt(skipInterval); // write skipInterval+ output.writeInt(maxSkipLevels); // write maxSkipLevels+ assert initUTF16Results();success = true;} finally {if (!success) {- try {- IOUtils.closeWhileHandlingException(output);- } catch (IOException e) {- // cannot happen since we suppress exceptions- throw new RuntimeException(e);- }+ IOUtils.closeWhileHandlingException(output);try {directory.deleteFile(IndexFileNames.segmentFileName(segment, "",diff --git a/lucene/src/test/org/apache/lucene/index/TestTransactions.java b/lucene/src/test/org/apache/lucene/index/TestTransactions.javaindex 5f8482005da..acda37b6bfd 100644--- a/lucene/src/test/org/apache/lucene/index/TestTransactions.java+++ b/lucene/src/test/org/apache/lucene/index/TestTransactions.java@@ -37,8 +37,9 @@ public class TestTransactions extends LuceneTestCase {private class RandomFailure extends MockDirectoryWrapper.Failure {@Overridepublic void eval(MockDirectoryWrapper dir) throws IOException {- if (TestTransactions.doFail && random.nextInt() % 10 <= 3)+ if (TestTransactions.doFail && random.nextInt() % 10 <= 3) {throw new IOException("now failing randomly but on purpose");+ }}}