Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

Fix so that lock files created in /tmp are removed when a directory is re-created. This way attempts to create a new index after a crashed indexing run no longer have to manually remove lock files from /tmp.

Doug Cutting1b36a9ddcc9c
diff --git a/src/java/org/apache/lucene/store/FSDirectory.java b/src/java/org/apache/lucene/store/FSDirectory.java
index 3136b730102..37a488de8d1 100644
--- a/src/java/org/apache/lucene/store/FSDirectory.java
+++ b/src/java/org/apache/lucene/store/FSDirectory.java
@@ -156,8 +156,8 @@ public final class FSDirectory extends Directory {
private synchronized void create() throws IOException {
if (!directory.exists())
- if (!directory.mkdir())
- throw new IOException("Cannot create directory: " + directory);
+ if (!directory.mkdir())
+ throw new IOException("Cannot create directory: " + directory);
String[] files = directory.list(); // clear old files
for (int i = 0; i < files.length; i++) {
@@ -165,6 +165,17 @@ public final class FSDirectory extends Directory {
if (!file.delete())
throw new IOException("couldn't delete " + files[i]);
}
+
+ String lockPrefix = getLockPrefix().toString(); // clear old locks
+ File tmpdir = new File(System.getProperty("java.io.tmpdir"));
+ files = tmpdir.list();
+ for (int i = 0; i < files.length; i++) {
+ if (!files[i].startsWith(lockPrefix))
+ continue;
+ File file = new File(tmpdir, files[i]);
+ if (!file.delete())
+ throw new IOException("couldn't delete " + files[i]);
+ }
}
/** Returns an array of strings, one for each file in the directory. */
@@ -298,27 +309,9 @@ public final class FSDirectory extends Directory {
* @return an instance of <code>Lock</code> holding the lock
*/
public final Lock makeLock(String 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");
+ StringBuffer buf = getLockPrefix();
+ buf.append("-");
+ buf.append(name);
// make the lock file in tmp, where anyone can create files.
final File lockFile = new File(System.getProperty("java.io.tmpdir"),
@@ -347,6 +340,29 @@ public final class FSDirectory extends Directory {
};
}
+ private StringBuffer getLockPrefix() {
+ String dirName; // name to be hashed
+ try {
+ dirName = directory.getCanonicalPath();
+ } catch (IOException e) {
+ throw new RuntimeException(e.toString());
+ }
+
+ byte digest[];
+ synchronized (DIGESTER) {
+ digest = DIGESTER.digest(dirName.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]);
+ }
+
+ return buf;
+ }
+
/** Closes the store to future operations. */
public final synchronized void close() throws IOException {
if (--refCount <= 0) {