Apache Solr open-source search software
Put lock files in /tmp instead of with the index.
Doug Cutting1c8db6cfd019
diff --git a/CHANGES.txt b/CHANGES.txtindex 1037598c56b..b460ec5ef6e 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -7,6 +7,11 @@ $Id$1. Added getFieldNames(boolean) to IndexReader, SegmentReader, andSegmentsReader. (Julien Nioche via otis)+ 2. Changed file locking to place lock files in+ System.getProperty("java.io.tmpdir"), where all users are+ permitted to write files. This way folks can open and correctly+ lock indexes which are read-only to them.+1.3 RC1diff --git a/src/java/org/apache/lucene/index/IndexReader.java b/src/java/org/apache/lucene/index/IndexReader.javaindex 48ef356fe7f..ac46ecadd92 100644--- a/src/java/org/apache/lucene/index/IndexReader.java+++ b/src/java/org/apache/lucene/index/IndexReader.java@@ -335,7 +335,10 @@ public abstract class IndexReader {* @throws IOException if there is a problem with accessing the index*/public static boolean isLocked(Directory directory) throws IOException {- return directory.fileExists("write.lock");+ return+ directory.makeLock("write.lock").isLocked() ||+ directory.makeLock("commit.lock").isLocked();+}/**@@ -345,7 +348,7 @@ public abstract class IndexReader {* @throws IOException if there is a problem with accessing the index*/public static boolean isLocked(String directory) throws IOException {- return (new File(directory, "write.lock")).exists();+ return isLocked(FSDirectory.getDirectory(directory, false));}/**@@ -356,7 +359,7 @@ public abstract class IndexReader {* currently accessing this index.*/public static void unlock(Directory directory) throws IOException {- directory.deleteFile("write.lock");- directory.deleteFile("commit.lock");+ directory.makeLock("write.lock").release();+ directory.makeLock("commit.lock").release();}}diff --git a/src/java/org/apache/lucene/store/FSDirectory.java b/src/java/org/apache/lucene/store/FSDirectory.javaindex 7077c3b3a75..5c644fbd437 100644--- a/src/java/org/apache/lucene/store/FSDirectory.java+++ b/src/java/org/apache/lucene/store/FSDirectory.java@@ -60,6 +60,8 @@ import java.io.RandomAccessFile;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.Hashtable;+import java.security.MessageDigest;+import java.security.NoSuchAlgorithmException;import org.apache.lucene.util.Constants;@@ -84,6 +86,16 @@ public final class FSDirectory extends Directory {private static final boolean DISABLE_LOCKS =Boolean.getBoolean("disableLuceneLocks") || Constants.JAVA_1_1;+ private static MessageDigest DIGESTER;++ static {+ try {+ DIGESTER = MessageDigest.getInstance("MD5");+ } catch (NoSuchAlgorithmException e) {+ throw new RuntimeException(e);+ }+ }+/** A buffer optionally used in renameTo method */private byte[] buffer = null;@@ -268,6 +280,12 @@ public final class FSDirectory extends Directory {return new FSInputStream(new File(directory, name));}+ /**+ * So we can do some byte-to-hexchar conversion below+ */+ private static final char[] HEX_DIGITS =+ {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};+/** Constructs a {@link Lock} with the specified name. Locks are implemented* with {@link File#createNewFile() }.*@@ -280,18 +298,49 @@ public final class FSDirectory extends Directory {* @return an instance of <code>Lock</code> holding the lock*/public final Lock makeLock(String name) {- final File lockFile = new File(directory, name);+ // the fully-qualified file name which uniquely identifies this lock+ String fullName;+ try {+ fullName = new File(directory, name).getCanonicalPath();+ } catch (IOException e) {+ throw new RuntimeException(e.toString());+ }++ // hash full name to create the tmp file name+ byte digest[];+ synchronized (DIGESTER) {+ digest = DIGESTER.digest(fullName.getBytes());+ }+ StringBuffer buf = new StringBuffer();+ buf.append("lucene-");+ for (int i = 0; i < digest.length; i++) {+ int b = digest[i];+ buf.append(HEX_DIGITS[(b >> 4) & 0xf]);+ buf.append(HEX_DIGITS[b & 0xf]);+ }+ buf.append(".lock");++ // make the lock file in tmp, where anyone can create files.+ final File lockFile = new File(System.getProperty("java.io.tmpdir"),+ buf.toString());+return new Lock() {public boolean obtain() throws IOException {if (DISABLE_LOCKS)return true;- return lockFile.createNewFile();+ return lockFile.createNewFile();}public void release() {if (DISABLE_LOCKS)return;lockFile.delete();}+ public boolean isLocked() {+ if (DISABLE_LOCKS)+ return false;+ return lockFile.exists();+ }+public String toString() {return "Lock@" + lockFile;}diff --git a/src/java/org/apache/lucene/store/Lock.java b/src/java/org/apache/lucene/store/Lock.javaindex 8e14f7982f0..c432bb48421 100644--- a/src/java/org/apache/lucene/store/Lock.java+++ b/src/java/org/apache/lucene/store/Lock.java@@ -79,6 +79,11 @@ public abstract class Lock {/** Release exclusive access. */public abstract void release();+ /** Returns true if the resource is currently locked. Note that one must+ * still call {@link #obtain()} before using the resource. */+ public abstract boolean isLocked();++/** Utility class for executing code with exclusive access. */public abstract static class With {private Lock lock;diff --git a/src/java/org/apache/lucene/store/RAMDirectory.java b/src/java/org/apache/lucene/store/RAMDirectory.javaindex 12b5a58ad73..bd18be6220c 100644--- a/src/java/org/apache/lucene/store/RAMDirectory.java+++ b/src/java/org/apache/lucene/store/RAMDirectory.java@@ -199,6 +199,9 @@ public final class RAMDirectory extends Directory {public void release() {deleteFile(name);}+ public boolean isLocked() {+ return fileExists(name);+ }};}diff --git a/src/test/org/apache/lucene/ThreadSafetyTest.java b/src/test/org/apache/lucene/ThreadSafetyTest.javaindex 4580fc415db..50f06a1e45b 100644--- a/src/test/org/apache/lucene/ThreadSafetyTest.java+++ b/src/test/org/apache/lucene/ThreadSafetyTest.java@@ -164,6 +164,8 @@ class ThreadSafetyTest {add = true;}+ IndexReader.unlock(FSDirectory.getDirectory("index", false));+if (!readOnly) {IndexWriter writer = new IndexWriter("index", ANALYZER, !add);