lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r635869 - in /lucene/java/branches/lucene_2_3: ./ src/java/org/apache/lucene/index/ src/test/org/apache/lucene/index/ src/test/org/apache/lucene/util/
Date Tue, 11 Mar 2008 10:32:34 GMT
Author: mikemccand
Date: Tue Mar 11 03:32:20 2008
New Revision: 635869

URL: http://svn.apache.org/viewvc?rev=635869&view=rev
Log:
LUCENE-1198 (porting to 2.3): don't corrupt the index if an exception happens inside DocumentsWriter.init

Modified:
    lucene/java/branches/lucene_2_3/CHANGES.txt
    lucene/java/branches/lucene_2_3/src/java/org/apache/lucene/index/DocumentsWriter.java
    lucene/java/branches/lucene_2_3/src/java/org/apache/lucene/index/IndexWriter.java
    lucene/java/branches/lucene_2_3/src/test/org/apache/lucene/index/TestIndexWriter.java
    lucene/java/branches/lucene_2_3/src/test/org/apache/lucene/util/_TestUtil.java

Modified: lucene/java/branches/lucene_2_3/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/branches/lucene_2_3/CHANGES.txt?rev=635869&r1=635868&r2=635869&view=diff
==============================================================================
--- lucene/java/branches/lucene_2_3/CHANGES.txt (original)
+++ lucene/java/branches/lucene_2_3/CHANGES.txt Tue Mar 11 03:32:20 2008
@@ -10,6 +10,9 @@
  2. LUCENE-1197: Fixed issue whereby IndexWriter would flush by RAM
     too early when TermVectors were in use.  (Mike McCandless)
 
+ 3. LUCENE-1198: Don't corrupt index if an exception happens inside
+    DocumentsWriter.init (Mike McCandless)
+
 ======================= Release 2.3.1 2008-02-22 =======================
 
 Bug fixes

Modified: lucene/java/branches/lucene_2_3/src/java/org/apache/lucene/index/DocumentsWriter.java
URL: http://svn.apache.org/viewvc/lucene/java/branches/lucene_2_3/src/java/org/apache/lucene/index/DocumentsWriter.java?rev=635869&r1=635868&r2=635869&view=diff
==============================================================================
--- lucene/java/branches/lucene_2_3/src/java/org/apache/lucene/index/DocumentsWriter.java
(original)
+++ lucene/java/branches/lucene_2_3/src/java/org/apache/lucene/index/DocumentsWriter.java
Tue Mar 11 03:32:20 2008
@@ -722,6 +722,7 @@
     void init(Document doc, int docID) throws IOException, AbortException {
 
       assert !isIdle;
+      assert writer.testPoint("DocumentsWriter.ThreadState.init start");
 
       this.docID = docID;
       docBoost = doc.getBoost();
@@ -2371,18 +2372,6 @@
     if (segment == null)
       segment = writer.newSegmentName();
 
-    numDocsInRAM++;
-
-    // We must at this point commit to flushing to ensure we
-    // always get N docs when we flush by doc count, even if
-    // > 1 thread is adding documents:
-    if (!flushPending && maxBufferedDocs != IndexWriter.DISABLE_AUTO_FLUSH
-        && numDocsInRAM >= maxBufferedDocs) {
-      flushPending = true;
-      state.doFlushAfter = true;
-    } else
-      state.doFlushAfter = false;
-
     state.isIdle = false;
 
     try {
@@ -2391,11 +2380,21 @@
         state.init(doc, nextDocID);
         if (delTerm != null) {
           addDeleteTerm(delTerm, state.docID);
-          if (!state.doFlushAfter)
-            state.doFlushAfter = timeToFlushDeletes();
+          state.doFlushAfter = timeToFlushDeletes();
         }
-        // Only increment nextDocID on successful init
+        // Only increment nextDocID & numDocsInRAM on successful init
         nextDocID++;
+        numDocsInRAM++;
+
+        // We must at this point commit to flushing to ensure we
+        // always get N docs when we flush by doc count, even if
+        // > 1 thread is adding documents:
+        if (!flushPending && maxBufferedDocs != IndexWriter.DISABLE_AUTO_FLUSH
+            && numDocsInRAM >= maxBufferedDocs) {
+          flushPending = true;
+          state.doFlushAfter = true;
+        }
+
         success = true;
       } finally {
         if (!success) {

Modified: lucene/java/branches/lucene_2_3/src/java/org/apache/lucene/index/IndexWriter.java
URL: http://svn.apache.org/viewvc/lucene/java/branches/lucene_2_3/src/java/org/apache/lucene/index/IndexWriter.java?rev=635869&r1=635868&r2=635869&view=diff
==============================================================================
--- lucene/java/branches/lucene_2_3/src/java/org/apache/lucene/index/IndexWriter.java (original)
+++ lucene/java/branches/lucene_2_3/src/java/org/apache/lucene/index/IndexWriter.java Tue
Mar 11 03:32:20 2008
@@ -3434,4 +3434,10 @@
 
     return buffer.toString();
   }
+
+  // Used only by assert for testing.  Current points:
+  //  "DocumentsWriter.ThreadState.init start"
+  boolean testPoint(String name) {
+    return true;
+  }
 }

Modified: lucene/java/branches/lucene_2_3/src/test/org/apache/lucene/index/TestIndexWriter.java
URL: http://svn.apache.org/viewvc/lucene/java/branches/lucene_2_3/src/test/org/apache/lucene/index/TestIndexWriter.java?rev=635869&r1=635868&r2=635869&view=diff
==============================================================================
--- lucene/java/branches/lucene_2_3/src/test/org/apache/lucene/index/TestIndexWriter.java
(original)
+++ lucene/java/branches/lucene_2_3/src/test/org/apache/lucene/index/TestIndexWriter.java
Tue Mar 11 03:32:20 2008
@@ -2847,4 +2847,39 @@
     writer.addDocument(doc);
     writer.close();
   }
+
+  // LUCENE-1198
+  public class MockIndexWriter extends IndexWriter {
+
+    public MockIndexWriter(Directory dir, boolean autoCommit, Analyzer a, boolean create)
throws IOException {
+      super(dir, autoCommit, a, create);
+    }
+
+    boolean doFail;
+
+    boolean testPoint(String name) {
+      if (doFail && name.equals("DocumentsWriter.ThreadState.init start"))
+        throw new RuntimeException("intentionally failing");
+      return true;
+    }
+  }
+
+  public void testExceptionDocumentsWriterInit() throws IOException {
+    MockRAMDirectory dir = new MockRAMDirectory();
+    MockIndexWriter w = new MockIndexWriter(dir, false, new WhitespaceAnalyzer(), true);
+    Document doc = new Document();
+    doc.add(new Field("field", "a field", Field.Store.YES,
+                      Field.Index.TOKENIZED));
+    w.addDocument(doc);
+    w.doFail = true;
+    try {
+      w.addDocument(doc);
+      fail("did not hit exception");
+    } catch (RuntimeException re) {
+      // expected
+    }
+    w.close();
+    _TestUtil.checkIndex(dir);
+    dir.close();
+  }
 }

Modified: lucene/java/branches/lucene_2_3/src/test/org/apache/lucene/util/_TestUtil.java
URL: http://svn.apache.org/viewvc/lucene/java/branches/lucene_2_3/src/test/org/apache/lucene/util/_TestUtil.java?rev=635869&r1=635868&r2=635869&view=diff
==============================================================================
--- lucene/java/branches/lucene_2_3/src/test/org/apache/lucene/util/_TestUtil.java (original)
+++ lucene/java/branches/lucene_2_3/src/test/org/apache/lucene/util/_TestUtil.java Tue Mar
11 03:32:20 2008
@@ -22,6 +22,10 @@
 import org.apache.lucene.index.IndexWriter;
 import org.apache.lucene.index.MergeScheduler;
 import org.apache.lucene.index.ConcurrentMergeScheduler;
+import org.apache.lucene.index.CheckIndex;
+import org.apache.lucene.store.Directory;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
 
 public class _TestUtil {
 
@@ -49,4 +53,16 @@
     if (ms instanceof ConcurrentMergeScheduler)
       ((ConcurrentMergeScheduler) ms).sync();
   }
+
+  public static boolean checkIndex(Directory dir) throws IOException {
+    ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
+    CheckIndex.out = new PrintStream(bos);
+    if (!CheckIndex.check(dir, false)) {
+      System.out.println("CheckIndex failed");
+      System.out.println(bos.toString());
+      return false;
+    } else
+      return true;
+  }
+
 }



Mime
View raw message