mirror of https://github.com/apache/lucene.git
LUCENE-2689: don't attemp to acquire a test lock in NativeFSLockFactory
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1005363 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1c340dda46
commit
6eaed1ad94
|
@ -382,6 +382,10 @@ Changes in runtime behavior
|
|||
usage, allowing applications to accidentally open two writers on the
|
||||
same directory. (Mike McCandless)
|
||||
|
||||
* LUCENE-2689: NativeFSLockFactory no longer attempts to acquire a
|
||||
test lock just before the real lock is acquired. (Surinder Pal
|
||||
Singh Bindra via Mike McCandless)
|
||||
|
||||
API Changes
|
||||
|
||||
* LUCENE-2076: Rename FSDirectory.getFile -> getDirectory. (George
|
||||
|
|
|
@ -17,14 +17,12 @@ package org.apache.lucene.store;
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.FileLock;
|
||||
import java.io.File;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* <p>Implements {@link LockFactory} using native OS file
|
||||
|
@ -58,54 +56,6 @@ import java.util.Random;
|
|||
|
||||
public class NativeFSLockFactory extends FSLockFactory {
|
||||
|
||||
private volatile boolean tested = false;
|
||||
|
||||
// Simple test to verify locking system is "working". On
|
||||
// NFS, if it's misconfigured, you can hit long (35
|
||||
// second) timeouts which cause Lock.obtain to take far
|
||||
// too long (it assumes the obtain() call takes zero
|
||||
// time).
|
||||
private synchronized void acquireTestLock() {
|
||||
if (tested) return;
|
||||
tested = true;
|
||||
|
||||
// Ensure that lockDir exists and is a directory.
|
||||
if (!lockDir.exists()) {
|
||||
if (!lockDir.mkdirs())
|
||||
throw new RuntimeException("Cannot create directory: " +
|
||||
lockDir.getAbsolutePath());
|
||||
} else if (!lockDir.isDirectory()) {
|
||||
throw new RuntimeException("Found regular file where directory expected: " +
|
||||
lockDir.getAbsolutePath());
|
||||
}
|
||||
|
||||
// add the RuntimeMXBean's name to the lock file, to reduce the chance for
|
||||
// name collisions when this code is invoked by multiple JVMs (such as in
|
||||
// our tests). On most systems, the name includes the process Id.
|
||||
// Also, remove any non-alphanumeric characters, so that the lock file will
|
||||
// be created for sure on all systems.
|
||||
String randomLockName = "lucene-"
|
||||
+ ManagementFactory.getRuntimeMXBean().getName().replaceAll("[^a-zA-Z0-9]+","") + "-"
|
||||
+ Long.toString(new Random().nextInt(), Character.MAX_RADIX)
|
||||
+ "-test.lock";
|
||||
|
||||
Lock l = makeLock(randomLockName);
|
||||
try {
|
||||
l.obtain();
|
||||
l.release();
|
||||
// If the test lock failed to delete after all the attempts, attempt a
|
||||
// delete when the JVM exits.
|
||||
File lockFile = new File(lockDir, randomLockName);
|
||||
if (lockFile.exists()) {
|
||||
lockFile.deleteOnExit();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
RuntimeException e2 = new RuntimeException("Failed to acquire random test lock; please verify filesystem for lock directory '" + lockDir + "' supports locking");
|
||||
e2.initCause(e);
|
||||
throw e2;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a NativeFSLockFactory instance, with null (unset)
|
||||
* lock directory. When you pass this factory to a {@link FSDirectory}
|
||||
|
@ -139,7 +89,6 @@ public class NativeFSLockFactory extends FSLockFactory {
|
|||
|
||||
@Override
|
||||
public synchronized Lock makeLock(String lockName) {
|
||||
acquireTestLock();
|
||||
if (lockPrefix != null)
|
||||
lockName = lockPrefix + "-" + lockName;
|
||||
return new NativeFSLock(lockDir, lockName);
|
||||
|
|
Loading…
Reference in New Issue