Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-773: fixed NPE caused by IndexWriter assuming LockFactory was non-null

Michael McCandlessd8979209bc31
diff --git a/CHANGES.txt b/CHANGES.txt
index 96051d7421f..d8c46052be7 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -193,6 +193,10 @@ API Changes
17. LUCENE-780: Add a static Directory.copy() method to copy files
from one Directory to another. (Jiri Kuhn via Mike McCandless)
+18. LUCENE-773: Added Directory.clearLock(String name) to forcefully
+ remove an old lock. The default implementation is to ask the
+ lockFactory (if non null) to clear the lock. (Mike McCandless)
+
Bug fixes
1. Fixed the web application demo (built with "ant war-demo") which
diff --git a/src/java/org/apache/lucene/index/IndexWriter.java b/src/java/org/apache/lucene/index/IndexWriter.java
index a5140416e9c..473ba4270cd 100644
--- a/src/java/org/apache/lucene/index/IndexWriter.java
+++ b/src/java/org/apache/lucene/index/IndexWriter.java
@@ -333,7 +333,7 @@ public class IndexWriter {
if (create) {
// Clear the write lock in case it's leftover:
- directory.getLockFactory().clearLock(IndexWriter.WRITE_LOCK_NAME);
+ directory.clearLock(IndexWriter.WRITE_LOCK_NAME);
}
Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME);
diff --git a/src/java/org/apache/lucene/store/Directory.java b/src/java/org/apache/lucene/store/Directory.java
index c10a280df15..158a7a47706 100644
--- a/src/java/org/apache/lucene/store/Directory.java
+++ b/src/java/org/apache/lucene/store/Directory.java
@@ -88,6 +88,17 @@ public abstract class Directory {
public Lock makeLock(String name) {
return lockFactory.makeLock(name);
}
+ /**
+ * Attempt to clear (forcefully unlock and remove) the
+ * specified lock. Only call this at a time when you are
+ * certain this lock is no longer in use.
+ * @param lockName name of the lock to be cleared.
+ */
+ public void clearLock(String name) throws IOException {
+ if (lockFactory != null) {
+ lockFactory.clearLock(name);
+ }
+ }
/** Closes the store. */
public abstract void close()
@@ -106,8 +117,12 @@ public abstract class Directory {
this.lockFactory = lockFactory;
lockFactory.setLockPrefix(this.getLockID());
}
+
/**
- * Get the LockFactory that this Directory instance is using for its locking implementation.
+ * Get the LockFactory that this Directory instance is
+ * using for its locking implementation. Note that this
+ * may be null for Directory implementations that provide
+ * their own locking implementation.
*/
public LockFactory getLockFactory() {
return this.lockFactory;
diff --git a/src/test/org/apache/lucene/index/TestIndexWriter.java b/src/test/org/apache/lucene/index/TestIndexWriter.java
index a69d56c503f..f6744364eb3 100644
--- a/src/test/org/apache/lucene/index/TestIndexWriter.java
+++ b/src/test/org/apache/lucene/index/TestIndexWriter.java
@@ -21,6 +21,9 @@ import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.MockRAMDirectory;
+import org.apache.lucene.store.LockFactory;
+import org.apache.lucene.store.Lock;
+import org.apache.lucene.store.SingleInstanceLockFactory;
/**
* @author goller
@@ -690,6 +693,43 @@ public class TestIndexWriter extends TestCase
}
}
+ // Make sure that a Directory implementation that does
+ // not use LockFactory at all (ie overrides makeLock and
+ // implements its own private locking) works OK. This
+ // was raised on java-dev as loss of backwards
+ // compatibility.
+ public void testNullLockFactory() throws IOException {
+
+ final class MyRAMDirectory extends RAMDirectory {
+ private LockFactory myLockFactory;
+ MyRAMDirectory() {
+ lockFactory = null;
+ myLockFactory = new SingleInstanceLockFactory();
+ }
+ public Lock makeLock(String name) {
+ return myLockFactory.makeLock(name);
+ }
+ }
+
+ Directory dir = new MyRAMDirectory();
+ IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
+ for (int i = 0; i < 100; i++) {
+ addDoc(writer);
+ }
+ writer.close();
+ IndexReader reader = IndexReader.open(dir);
+ Term searchTerm = new Term("content", "aaa");
+ IndexSearcher searcher = new IndexSearcher(dir);
+ Hits hits = searcher.search(new TermQuery(searchTerm));
+ assertEquals("did not get right number of hits", 100, hits.length());
+ writer.close();
+
+ writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
+ writer.close();
+
+ dir.close();
+ }
+
private void rmDir(File dir) {
File[] files = dir.listFiles();
if (files != null) {