You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sh...@apache.org on 2011/05/25 12:46:24 UTC

svn commit: r1127470 - in /lucene/dev/trunk/lucene/src: java/org/apache/lucene/index/ test-framework/org/apache/lucene/store/ test-framework/org/apache/lucene/util/ test/org/apache/lucene/index/

Author: shaie
Date: Wed May 25 10:46:23 2011
New Revision: 1127470

URL: http://svn.apache.org/viewvc?rev=1127470&view=rev
Log:
LUCENE-3139: add more verbosing to LTC in case it fails to remove temp directories

Modified:
    lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java
    lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java
    lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java
    lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/util/_TestUtil.java
    lucene/dev/trunk/lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java

Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java?rev=1127470&r1=1127469&r2=1127470&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java Wed May 25 10:46:23 2011
@@ -59,7 +59,7 @@ public class PersistentSnapshotDeletionP
 
   /**
    * Reads the snapshots information from the given {@link Directory}. This
-   * method does can be used if the snapshots information is needed, however you
+   * method can be used if the snapshots information is needed, however you
    * cannot instantiate the deletion policy (because e.g., some other process
    * keeps a lock on the snapshots directory).
    */
@@ -122,11 +122,19 @@ public class PersistentSnapshotDeletionP
       writer.commit();
     }
 
-    // Initializes the snapshots information. This code should basically run
-    // only if mode != CREATE, but if it is, it's no harm as we only open the
-    // reader once and immediately close it.
-    for (Entry<String, String> e : readSnapshotsInfo(dir).entrySet()) {
-      registerSnapshotInfo(e.getKey(), e.getValue(), null);
+    try {
+      // Initializes the snapshots information. This code should basically run
+      // only if mode != CREATE, but if it is, it's no harm as we only open the
+      // reader once and immediately close it.
+      for (Entry<String, String> e : readSnapshotsInfo(dir).entrySet()) {
+        registerSnapshotInfo(e.getKey(), e.getValue(), null);
+      }
+    } catch (RuntimeException e) {
+      writer.close(); // don't leave any open file handles
+      throw e;
+    } catch (IOException e) {
+      writer.close(); // don't leave any open file handles
+      throw e;
     }
   }
 

Modified: lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java?rev=1127470&r1=1127469&r2=1127470&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java (original)
+++ lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java Wed May 25 10:46:23 2011
@@ -45,20 +45,23 @@ public class MockIndexOutputWrapper exte
 
   @Override
   public void close() throws IOException {
-    dir.maybeThrowDeterministicException();
-    delegate.close();
-    if (dir.trackDiskUsage) {
-      // Now compute actual disk usage & track the maxUsedSize
-      // in the MockDirectoryWrapper:
-      long size = dir.getRecomputedActualSizeInBytes();
-      if (size > dir.maxUsedSize) {
-        dir.maxUsedSize = size;
+    try {
+      dir.maybeThrowDeterministicException();
+    } finally {
+      delegate.close();
+      if (dir.trackDiskUsage) {
+        // Now compute actual disk usage & track the maxUsedSize
+        // in the MockDirectoryWrapper:
+        long size = dir.getRecomputedActualSizeInBytes();
+        if (size > dir.maxUsedSize) {
+          dir.maxUsedSize = size;
+        }
+      }
+      synchronized(dir) {
+        dir.openFileHandles.remove(this);
+        dir.openFilesForWrite.remove(name);
       }
     }
-    synchronized(dir) {
-      dir.openFileHandles.remove(this);
-      dir.openFilesForWrite.remove(name);
-    }
   }
 
   @Override

Modified: lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java?rev=1127470&r1=1127469&r2=1127470&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java (original)
+++ lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java Wed May 25 10:46:23 2011
@@ -28,6 +28,7 @@ import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.util.*;
+import java.util.Map.Entry;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
@@ -132,7 +133,7 @@ public abstract class LuceneTestCase ext
   }
   
   /** set of directories we created, in afterclass we try to clean these up */
-  static final Set<String> tempDirs = Collections.synchronizedSet(new HashSet<String>());
+  private static final Map<File, StackTraceElement[]> tempDirs = Collections.synchronizedMap(new HashMap<File, StackTraceElement[]>());
 
   // by default we randomly pick a different codec for
   // each test case (non-J4 tests) and each test class (J4
@@ -180,7 +181,7 @@ public abstract class LuceneTestCase ext
     SETUP,   // test has called setUp()
     RANTEST, // test is running
     TEARDOWN // test has called tearDown()
-  };
+  }
   
   /**
    * Some tests expect the directory to contain a single segment, and want to do tests on that segment's reader.
@@ -454,11 +455,20 @@ public abstract class LuceneTestCase ext
     }
     // clear out any temp directories if we can
     if (!testsFailed) {
-      for (String path : tempDirs) {
+      for (Entry<File, StackTraceElement[]> entry : tempDirs.entrySet()) {
         try {
-          _TestUtil.rmDir(new File(path));
+          _TestUtil.rmDir(entry.getKey());
         } catch (IOException e) {
           e.printStackTrace();
+          System.err.println("path " + entry.getKey() + " allocated from");
+          // first two STE's are Java's
+          StackTraceElement[] elements = entry.getValue();
+          for (int i = 2; i < elements.length; i++) {
+            StackTraceElement ste = elements[i];            
+            // print only our code's stack information
+            if (ste.getClassName().indexOf("org.apache.lucene") == -1) break; 
+            System.err.println("\t" + ste);
+          }
         }
       }
     }
@@ -1112,6 +1122,11 @@ public abstract class LuceneTestCase ext
     return d;
   }
 
+  /** Registers a temp file that will be deleted when tests are done. */
+  public static void registerTempFile(File tmpFile) {
+    tempDirs.put(tmpFile.getAbsoluteFile(), Thread.currentThread().getStackTrace());
+  }
+  
   static Directory newDirectoryImpl(Random random, String clazzName) {
     if (clazzName.equals("random"))
       clazzName = randomDirectory(random);
@@ -1124,7 +1139,7 @@ public abstract class LuceneTestCase ext
         final File tmpFile = File.createTempFile("test", "tmp", TEMP_DIR);
         tmpFile.delete();
         tmpFile.mkdir();
-        tempDirs.add(tmpFile.getAbsolutePath());
+        registerTempFile(tmpFile);
         return newFSDirectoryImpl(clazz.asSubclass(FSDirectory.class), tmpFile, null);
       }
 

Modified: lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/util/_TestUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/util/_TestUtil.java?rev=1127470&r1=1127469&r2=1127470&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/util/_TestUtil.java (original)
+++ lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/util/_TestUtil.java Wed May 25 10:46:23 2011
@@ -54,8 +54,8 @@ public class _TestUtil {
   /** Returns temp dir, containing String arg in its name;
    *  does not create the directory. */
   public static File getTempDir(String desc) {
-    File f = new File(LuceneTestCase.TEMP_DIR, desc + "." + new Random().nextLong());
-    LuceneTestCase.tempDirs.add(f.getAbsolutePath());
+    File f = new File(LuceneTestCase.TEMP_DIR, desc + "." + LuceneTestCase.random.nextLong());
+    LuceneTestCase.registerTempFile(f);
     return f;
   }
 
@@ -91,7 +91,7 @@ public class _TestUtil {
     rmDir(destDir);
     
     destDir.mkdir();
-    LuceneTestCase.tempDirs.add(destDir.getAbsolutePath());
+    LuceneTestCase.registerTempFile(destDir);
     
     while (entries.hasMoreElements()) {
       ZipEntry entry = entries.nextElement();

Modified: lucene/dev/trunk/lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java?rev=1127470&r1=1127469&r2=1127470&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java (original)
+++ lucene/dev/trunk/lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java Wed May 25 10:46:23 2011
@@ -30,9 +30,13 @@ import org.junit.Before;
 import org.junit.Test;
 
 public class TestPersistentSnapshotDeletionPolicy extends TestSnapshotDeletionPolicy {
+
   // Keep it a class member so that getDeletionPolicy can use it
   private Directory snapshotDir;
   
+  // so we can close it if called by SDP tests
+  private PersistentSnapshotDeletionPolicy psdp;
+  
   @Before
   @Override
   public void setUp() throws Exception {
@@ -43,15 +47,17 @@ public class TestPersistentSnapshotDelet
   @After
   @Override
   public void tearDown() throws Exception {
+    if (psdp != null) psdp.close();
     snapshotDir.close();
     super.tearDown();
   }
   
   @Override
   protected SnapshotDeletionPolicy getDeletionPolicy() throws IOException {
+    if (psdp != null) psdp.close();
     snapshotDir.close();
     snapshotDir = newDirectory();
-    return new PersistentSnapshotDeletionPolicy(
+    return psdp = new PersistentSnapshotDeletionPolicy(
         new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.CREATE,
         TEST_VERSION_CURRENT);
   }
@@ -173,6 +179,8 @@ public class TestPersistentSnapshotDelet
      fail("should not have reached here - the snapshots directory should be locked!");
     } catch (LockObtainFailedException e) {
       // expected
+    } finally {
+      psdp.close();
     }
     
     // Reading the snapshots info should succeed though