You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2015/05/29 19:42:08 UTC

svn commit: r1682520 - in /lucene/dev/branches/lucene6508/lucene/core/src: java/org/apache/lucene/store/ test/org/apache/lucene/index/ test/org/apache/lucene/store/

Author: rmuir
Date: Fri May 29 17:42:07 2015
New Revision: 1682520

URL: http://svn.apache.org/r1682520
Log:
LUCENE-6508: fix tests and cleanup

Modified:
    lucene/dev/branches/lucene6508/lucene/core/src/java/org/apache/lucene/store/NativeFSLockFactory.java
    lucene/dev/branches/lucene6508/lucene/core/src/java/org/apache/lucene/store/SimpleFSLockFactory.java
    lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java
    lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterConfig.java
    lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java
    lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java

Modified: lucene/dev/branches/lucene6508/lucene/core/src/java/org/apache/lucene/store/NativeFSLockFactory.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6508/lucene/core/src/java/org/apache/lucene/store/NativeFSLockFactory.java?rev=1682520&r1=1682519&r2=1682520&view=diff
==============================================================================
--- lucene/dev/branches/lucene6508/lucene/core/src/java/org/apache/lucene/store/NativeFSLockFactory.java (original)
+++ lucene/dev/branches/lucene6508/lucene/core/src/java/org/apache/lucene/store/NativeFSLockFactory.java Fri May 29 17:42:07 2015
@@ -106,21 +106,20 @@ public final class NativeFSLockFactory e
     // used as a best-effort check, to see if the underlying file has changed
     final FileTime creationTime = Files.readAttributes(realPath, BasicFileAttributes.class).creationTime();
     
-    boolean obtained = false;
     if (LOCK_HELD.add(realPath.toString())) {
       FileChannel channel = null;
+      FileLock lock = null;
       try {
         channel = FileChannel.open(realPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
-        FileLock lock = channel.tryLock();
+        lock = channel.tryLock();
         if (lock != null) {
-          obtained = true;
-          return new NativeFSLock(lock, realPath, creationTime);
+          return new NativeFSLock(lock, channel, realPath, creationTime);
         } else {
           throw new IOException("Lock held by another program: " + realPath);
         }
       } finally {
-        if (obtained == false) { // not successful - clear up and move out
-          IOUtils.closeWhileHandlingException(channel); // nocommit: addSuppressed
+        if (lock == null) { // not successful - clear up and move out
+          IOUtils.close(channel); // nocommit: addSuppressed
           clearLockHeld(realPath);  // clear LOCK_HELD last 
         }
       }
@@ -135,21 +134,26 @@ public final class NativeFSLockFactory e
       throw new AlreadyClosedException("Lock path was cleared but never marked as held: " + path);
     }
   }
-    
+
+  // TODO: kind of bogus we even pass channel:
+  // FileLock has an accessor, but mockfs doesnt yet mock the locks, too scary atm.
+
   static final class NativeFSLock extends Lock {
     private final FileLock lock;
+    private final FileChannel channel;
     private final Path path;
     private final FileTime creationTime;
-    private boolean closed;
+    private volatile boolean closed;
     
-    NativeFSLock(FileLock lock, Path path, FileTime creationTime) {
+    NativeFSLock(FileLock lock, FileChannel channel, Path path, FileTime creationTime) {
       this.lock = lock;
+      this.channel = channel;
       this.path = path;
       this.creationTime = creationTime;
     }
 
     @Override
-    public synchronized void ensureValid() throws IOException {
+    public void ensureValid() throws IOException {
       if (closed) {
         throw new AlreadyClosedException("Lock instance already released: " + this);
       }
@@ -163,7 +167,7 @@ public final class NativeFSLockFactory e
       }
       // try to validate the underlying file descriptor.
       // this will throw IOException if something is wrong.
-      long size = lock.channel().size();
+      long size = channel.size();
       if (size != 0) {
         throw new AlreadyClosedException("Unexpected lock file size: " + size + ", (lock=" + this + ")");
       }
@@ -183,7 +187,7 @@ public final class NativeFSLockFactory e
       }
       // NOTE: we don't validate, as unlike SimpleFSLockFactory, we can't break others locks
       // first release the lock, then the channel
-      try (FileChannel channel = lock.channel();
+      try (FileChannel channel = this.channel;
            FileLock lock = this.lock) {
         assert lock != null;
         assert channel != null;

Modified: lucene/dev/branches/lucene6508/lucene/core/src/java/org/apache/lucene/store/SimpleFSLockFactory.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6508/lucene/core/src/java/org/apache/lucene/store/SimpleFSLockFactory.java?rev=1682520&r1=1682519&r2=1682520&view=diff
==============================================================================
--- lucene/dev/branches/lucene6508/lucene/core/src/java/org/apache/lucene/store/SimpleFSLockFactory.java (original)
+++ lucene/dev/branches/lucene6508/lucene/core/src/java/org/apache/lucene/store/SimpleFSLockFactory.java Fri May 29 17:42:07 2015
@@ -93,7 +93,7 @@ public final class SimpleFSLockFactory e
   static final class SimpleFSLock extends Lock {
     private final Path path;
     private final FileTime creationTime;
-    private boolean closed;
+    private volatile boolean closed;
 
     SimpleFSLock(Path path, FileTime creationTime) throws IOException {
       this.path = path;
@@ -101,7 +101,7 @@ public final class SimpleFSLockFactory e
     }
 
     @Override
-    public synchronized void ensureValid() throws IOException {
+    public void ensureValid() throws IOException {
       if (closed) {
         throw new AlreadyClosedException("Lock instance already released: " + this);
       }

Modified: lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java?rev=1682520&r1=1682519&r2=1682520&view=diff
==============================================================================
--- lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java (original)
+++ lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java Fri May 29 17:42:07 2015
@@ -67,7 +67,6 @@ import org.apache.lucene.store.BaseDirec
 import org.apache.lucene.store.Directory;
 import org.apache.lucene.store.IOContext;
 import org.apache.lucene.store.IndexOutput;
-import org.apache.lucene.store.LockObtainFailedException;
 import org.apache.lucene.store.MockDirectoryWrapper;
 import org.apache.lucene.store.NoLockFactory;
 import org.apache.lucene.store.RAMDirectory;
@@ -96,14 +95,7 @@ public class TestIndexWriter extends Luc
         IndexReader reader = null;
         int i;
 
-        long savedWriteLockTimeout = IndexWriterConfig.getDefaultWriteLockTimeout();
-        try {
-          IndexWriterConfig.setDefaultWriteLockTimeout(2000);
-          assertEquals(2000, IndexWriterConfig.getDefaultWriteLockTimeout());
-          writer  = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
-        } finally {
-          IndexWriterConfig.setDefaultWriteLockTimeout(savedWriteLockTimeout);
-        }
+        writer  = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
 
         // add 100 documents
         for (i = 0; i < 100; i++) {
@@ -1725,10 +1717,9 @@ public class TestIndexWriter extends Luc
     RandomIndexWriter w1 = new RandomIndexWriter(random(), d);
     w1.deleteAll();
     try {
-      new RandomIndexWriter(random(), d, newIndexWriterConfig(null)
-                                           .setWriteLockTimeout(100));
+      new RandomIndexWriter(random(), d, newIndexWriterConfig(null));
       fail("should not be able to create another writer");
-    } catch (LockObtainFailedException lofe) {
+    } catch (IOException lofe) {
       // expected
     }
     w1.close();

Modified: lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterConfig.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterConfig.java?rev=1682520&r1=1682519&r2=1682520&view=diff
==============================================================================
--- lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterConfig.java (original)
+++ lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterConfig.java Fri May 29 17:42:07 2015
@@ -63,8 +63,6 @@ public class TestIndexWriterConfig exten
     assertEquals(OpenMode.CREATE_OR_APPEND, conf.getOpenMode());
     // we don't need to assert this, it should be unspecified
     assertTrue(IndexSearcher.getDefaultSimilarity() == conf.getSimilarity());
-    assertEquals(IndexWriterConfig.getDefaultWriteLockTimeout(), conf.getWriteLockTimeout());
-    assertEquals(IndexWriterConfig.WRITE_LOCK_TIMEOUT, IndexWriterConfig.getDefaultWriteLockTimeout());
     assertEquals(IndexWriterConfig.DEFAULT_MAX_BUFFERED_DELETE_TERMS, conf.getMaxBufferedDeleteTerms());
     assertEquals(IndexWriterConfig.DEFAULT_RAM_BUFFER_SIZE_MB, conf.getRAMBufferSizeMB(), 0.0);
     assertEquals(IndexWriterConfig.DEFAULT_MAX_BUFFERED_DOCS, conf.getMaxBufferedDocs());
@@ -179,7 +177,6 @@ public class TestIndexWriterConfig exten
   @Test
   public void testConstants() throws Exception {
     // Tests that the values of the constants does not change
-    assertEquals(1000, IndexWriterConfig.WRITE_LOCK_TIMEOUT);
     assertEquals(-1, IndexWriterConfig.DISABLE_AUTO_FLUSH);
     assertEquals(IndexWriterConfig.DISABLE_AUTO_FLUSH, IndexWriterConfig.DEFAULT_MAX_BUFFERED_DELETE_TERMS);
     assertEquals(IndexWriterConfig.DISABLE_AUTO_FLUSH, IndexWriterConfig.DEFAULT_MAX_BUFFERED_DOCS);

Modified: lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java?rev=1682520&r1=1682519&r2=1682520&view=diff
==============================================================================
--- lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java (original)
+++ lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java Fri May 29 17:42:07 2015
@@ -2199,7 +2199,7 @@ public class TestIndexWriterExceptions e
     
     // even though we hit exception: we are closed, no locks or files held, index in good state
     assertTrue(iw.isClosed());
-    assertFalse(IndexWriter.isLocked(dir));
+    dir.obtainLock(IndexWriter.WRITE_LOCK_NAME).close();
     
     r = DirectoryReader.open(dir);
     assertEquals(10, r.maxDoc());
@@ -2268,7 +2268,7 @@ public class TestIndexWriterExceptions e
       
       // even though we hit exception: we are closed, no locks or files held, index in good state
       assertTrue(iw.isClosed());
-      assertFalse(IndexWriter.isLocked(dir));
+      dir.obtainLock(IndexWriter.WRITE_LOCK_NAME).close();
       
       r = DirectoryReader.open(dir);
       assertEquals(10, r.maxDoc());

Modified: lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java?rev=1682520&r1=1682519&r2=1682520&view=diff
==============================================================================
--- lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java (original)
+++ lucene/dev/branches/lucene6508/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java Fri May 29 17:42:07 2015
@@ -86,15 +86,13 @@ public class TestDirectory extends Lucen
         assertFalse(slowFileExists(d2, fname));
       }
 
-      Lock lock = dir.makeLock(lockname);
-      assertTrue(lock.obtain());
+      Lock lock = dir.obtainLock(lockname);
 
-      for (int j=0; j<dirs.length; j++) {
-        FSDirectory d2 = dirs[j];
-        Lock lock2 = d2.makeLock(lockname);
+      for (Directory other : dirs) {
         try {
-          assertFalse(lock2.obtain(1));
-        } catch (LockObtainFailedException e) {
+          other.obtainLock(lockname);
+          fail("didnt get exception");
+        } catch (IOException e) {
           // OK
         }
       }
@@ -102,8 +100,7 @@ public class TestDirectory extends Lucen
       lock.close();
       
       // now lock with different dir
-      lock = dirs[(i+1)%dirs.length].makeLock(lockname);
-      assertTrue(lock.obtain());
+      lock = dirs[(i+1)%dirs.length].obtainLock(lockname);
       lock.close();
     }