make sure to release write lock in IndexWriter if we hit IOException during construction

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@476345 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2006-11-17 22:34:17 +00:00
parent 7e3c1bfb0d
commit bd6f012511
3 changed files with 89 additions and 10 deletions

View File

@ -171,6 +171,11 @@ Bug fixes
20. LUCENE-706: Updated fileformats.xml|html concerning the docdelta value in the frequency file. (Johan Stuyts, Doron Cohen via Grant Ingersoll)
21. LUCENE-715: Fixed private constructor in IndexWriter.java to
properly release the acquired write lock if there is an
IOException after acquiring the write lock but before finishing
instantiation (Matthew Bogosian via Mike McCandless)
Optimizations
1. LUCENE-586: TermDocs.skipTo() is now more efficient for

View File

@ -259,16 +259,23 @@ public class IndexWriter {
throw new IOException("Index locked for write: " + writeLock);
this.writeLock = writeLock; // save it
synchronized (directory) { // in- & inter-process sync
new Lock.With(directory.makeLock(IndexWriter.COMMIT_LOCK_NAME), commitLockTimeout) {
public Object doBody() throws IOException {
if (create)
segmentInfos.write(directory);
else
segmentInfos.read(directory);
return null;
}
}.run();
try {
synchronized (directory) { // in- & inter-process sync
new Lock.With(directory.makeLock(IndexWriter.COMMIT_LOCK_NAME), commitLockTimeout) {
public Object doBody() throws IOException {
if (create)
segmentInfos.write(directory);
else
segmentInfos.read(directory);
return null;
}
}.run();
}
} catch (IOException e) {
// the doBody method failed
this.writeLock.release();
this.writeLock = null;
throw e;
}
}

View File

@ -0,0 +1,67 @@
package org.apache.lucene.index;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import junit.framework.TestCase;
import org.apache.lucene.index.IndexModifier;
/**
* This tests the patch for issue #LUCENE-715 (IndexWriter does not
* release its write lock when trying to open an index which does not yet
* exist).
*
* @author mbogosian
* @version $Id$
*/
public class TestIndexWriterLockRelease extends TestCase {
private java.io.File __test_dir;
public void setUp() throws IOException {
if (this.__test_dir == null) {
String tmp_dir = System.getProperty("java.io.tmpdir", "tmp");
this.__test_dir = new File(tmp_dir, "testIndexWriter");
if (this.__test_dir.exists()) {
throw new IOException("test directory \"" + this.__test_dir.getPath() + "\" already exists (please remove by hand)");
}
if (!this.__test_dir.mkdirs()
&& !this.__test_dir.isDirectory()) {
throw new IOException("unable to create test directory \"" + this.__test_dir.getPath() + "\"");
}
}
}
public void tearDown() throws IOException {
if (this.__test_dir != null) {
File[] files = this.__test_dir.listFiles();
for (int i = 0;
i < files.length;
++i) {
if (!files[i].delete()) {
throw new IOException("unable to remove file in test directory \"" + this.__test_dir.getPath() + "\" (please remove by hand)");
}
}
if (!this.__test_dir.delete()) {
throw new IOException("unable to remove test directory \"" + this.__test_dir.getPath() + "\" (please remove by hand)");
}
}
}
public void testIndexWriterLockRelease() throws IOException {
IndexModifier im;
try {
im = new IndexModifier(this.__test_dir, new org.apache.lucene.analysis.standard.StandardAnalyzer(), false);
} catch (FileNotFoundException e) {
try {
im = new IndexModifier(this.__test_dir, new org.apache.lucene.analysis.standard.StandardAnalyzer(), false);
} catch (FileNotFoundException e1) {
}
}
}
}