LUCENE-2104: Fix NativeFSLock.release() to throw exception if lock is held by another thread/process.

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@891205 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2009-12-16 12:01:45 +00:00
parent dc3f92e6e5
commit 383fc2d635
3 changed files with 42 additions and 1 deletions

View File

@ -57,6 +57,9 @@ Bug fixes
* LUCENE-2166: Don't incorrectly keep warning about the same immense
term, when IndexWriter.infoStream is on. (Mike McCandless)
* LUCENE-2104: NativeFSLock.release() would silently fail if the lock is held by
another thread/process. (Shai Erera via Uwe Schindler)
New features
* LUCENE-2128: Parallelized fetching document frequencies during weight

View File

@ -146,7 +146,7 @@ public class NativeFSLockFactory extends FSLockFactory {
}
}
}
};
}
class NativeFSLock extends Lock {
@ -300,6 +300,24 @@ class NativeFSLock extends Lock {
}
if (!path.delete())
throw new LockReleaseFailedException("failed to delete " + path);
} else {
// if we don't hold the lock, and somebody still called release(), for
// example as a result of calling IndexWriter.unlock(), we should attempt
// to obtain the lock and release it. If the obtain fails, it means the
// lock cannot be released, and we should throw a proper exception rather
// than silently failing/not doing anything.
boolean obtained = false;
try {
if (!(obtained = obtain())) {
throw new LockReleaseFailedException(
"Cannot forcefully unlock a NativeFSLock which is held by another indexer component: "
+ path);
}
} finally {
if (obtained) {
release();
}
}
}
}

View File

@ -198,6 +198,26 @@ public class TestLockFactory extends LuceneTestCase {
assertFalse(l2.isLocked());
}
public void testNativeFSLockReleaseByOtherLock() throws IOException {
NativeFSLockFactory f = new NativeFSLockFactory(System.getProperty("tempDir"));
f.setLockPrefix("test");
Lock l = f.makeLock("commit");
Lock l2 = f.makeLock("commit");
assertTrue("failed to obtain lock", l.obtain());
try {
assertTrue(l2.isLocked());
l2.release();
fail("should not have reached here. LockReleaseFailedException should have been thrown");
} catch (IOException e) {
assertTrue("Unexpected exception", e instanceof LockReleaseFailedException);
} finally {
l.release();
}
}
// Verify: NativeFSLockFactory assigns null as lockPrefix if the lockDir is inside directory
public void testNativeFSLockFactoryPrefix() throws IOException {