lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r636467 - in /lucene/java/trunk/src: java/org/apache/lucene/index/SegmentInfos.java test/org/apache/lucene/index/TestIndexWriter.java test/org/apache/lucene/store/MockRAMDirectory.java
Date Wed, 12 Mar 2008 19:23:25 GMT
Author: mikemccand
Date: Wed Mar 12 12:23:17 2008
New Revision: 636467

URL: http://svn.apache.org/viewvc?rev=636467&view=rev
Log:
LUCENE-1214: preseve original exception in SegmentInfos write & commit

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/index/SegmentInfos.java
    lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexWriter.java
    lucene/java/trunk/src/test/org/apache/lucene/store/MockRAMDirectory.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/index/SegmentInfos.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/SegmentInfos.java?rev=636467&r1=636466&r2=636467&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/SegmentInfos.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/SegmentInfos.java Wed Mar 12 12:23:17
2008
@@ -300,13 +300,29 @@
     } finally {
       boolean success2 = false;
       try {
-        output.close();
-        success2 = true;
+        if (!success) {
+          // We hit an exception above; try to close the file
+          // but suppress any exception:
+          try {
+            output.close();
+            success2 = true;
+          } catch (Throwable t) {
+            // Suppress so we keep throwing the original exception
+          }
+        } else {
+          output.close();
+          success2 = true;
+        }
       } finally {
-        if (!success || !success2)
-          // Try not to leave a truncated segments_N file in
-          // the index:
-          directory.deleteFile(segmentFileName);
+        if (!success || !success2) {
+          try {
+            // Try not to leave a truncated segments_N file in
+            // the index:
+            directory.deleteFile(segmentFileName);
+          } catch (Throwable t) {
+            // Suppress so we keep throwing the original exception
+          }
+        }
       }
     }
 
@@ -738,7 +754,11 @@
         final String segmentFileName = IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS,
                                                                              "",
                                                                              generation);
-        dir.deleteFile(segmentFileName);
+        try {
+          dir.deleteFile(segmentFileName);
+        } catch (Throwable t) {
+          // Suppress so we keep throwing the original exception
+        }
       }
     }
 
@@ -758,8 +778,13 @@
       dir.sync(fileName);
       success = true;
     } finally {
-      if (!success)
-        dir.deleteFile(fileName);
+      if (!success) {
+        try {
+          dir.deleteFile(fileName);
+        } catch (Throwable t) {
+          // Suppress so we keep throwing the original exception
+        }
+      }
     }
   }
 }

Modified: lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexWriter.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexWriter.java?rev=636467&r1=636466&r2=636467&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexWriter.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexWriter.java Wed Mar 12 12:23:17
2008
@@ -3268,4 +3268,54 @@
 
     dir.close();
   }
+
+  private static class FailOnlyInCommit extends MockRAMDirectory.Failure {
+
+    boolean fail1, fail2;
+
+    public void eval(MockRAMDirectory dir)  throws IOException {
+      StackTraceElement[] trace = new Exception().getStackTrace();
+      boolean isCommit = false;
+      boolean isDelete = false;
+      for (int i = 0; i < trace.length; i++) {
+        if ("org.apache.lucene.index.SegmentInfos".equals(trace[i].getClassName()) &&
"commit".equals(trace[i].getMethodName()))
+          isCommit = true;
+        if ("org.apache.lucene.store.MockRAMDirectory".equals(trace[i].getClassName()) &&
"deleteFile".equals(trace[i].getMethodName()))
+          isDelete = true;
+      }
+
+      if (isCommit) {
+        if (!isDelete) {
+          fail1 = true;
+          throw new RuntimeException("now fail first");
+        } else {
+          fail2 = true;
+          throw new IOException("now fail during delete");
+        }
+      }
+    }
+  }
+
+  // LUCENE-1214
+  public void testExceptionsDuringCommit() throws Throwable {
+    MockRAMDirectory dir = new MockRAMDirectory();
+    FailOnlyInCommit failure = new FailOnlyInCommit();
+    IndexWriter w = new IndexWriter(dir, false, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED);
+    Document doc = new Document();
+    doc.add(new Field("field", "a field", Field.Store.YES,
+                      Field.Index.TOKENIZED));
+    w.addDocument(doc);
+    dir.failOn(failure);
+    try {
+      w.close();
+      fail();
+    } catch (IOException ioe) {
+      fail("expected only RuntimeException");
+    } catch (RuntimeException re) {
+      // Expected
+    }
+    assertTrue(failure.fail1 && failure.fail2);
+    w.abort();
+    dir.close();
+  }
 }

Modified: lucene/java/trunk/src/test/org/apache/lucene/store/MockRAMDirectory.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/store/MockRAMDirectory.java?rev=636467&r1=636466&r2=636467&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/store/MockRAMDirectory.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/store/MockRAMDirectory.java Wed Mar 12 12:23:17
2008
@@ -186,6 +186,9 @@
   }
 
   private synchronized void deleteFile(String name, boolean forced) throws IOException {
+
+    maybeThrowDeterministicException();
+
     if (crashed && !forced)
       throw new IOException("cannot delete after crash");
 



Mime
View raw message