Apache Solr open-source search software
LUCENE-3660: release write lock if IW hits a non-ioexception from IR.indexExists
Robert Muir53fef3a0ace6
diff --git a/lucene/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/src/java/org/apache/lucene/index/IndexWriter.javaindex a40fb4e5c31..3dbb85b069c 100644--- a/lucene/src/java/org/apache/lucene/index/IndexWriter.java+++ b/lucene/src/java/org/apache/lucene/index/IndexWriter.java@@ -862,22 +862,23 @@ public class IndexWriter implements Closeable, TwoPhaseCommit {if (!writeLock.obtain(conf.getWriteLockTimeout())) // obtain write lockthrow new LockObtainFailedException("Index locked for write: " + writeLock);- OpenMode mode = conf.getOpenMode();- boolean create;- if (mode == OpenMode.CREATE) {- create = true;- } else if (mode == OpenMode.APPEND) {- create = false;- } else {- // CREATE_OR_APPEND - create only if an index does not exist- create = !IndexReader.indexExists(directory);- }boolean success = false;-- // If index is too old, reading the segments will throw- // IndexFormatTooOldException.- segmentInfos = new SegmentInfos();try {+ OpenMode mode = conf.getOpenMode();+ boolean create;+ if (mode == OpenMode.CREATE) {+ create = true;+ } else if (mode == OpenMode.APPEND) {+ create = false;+ } else {+ // CREATE_OR_APPEND - create only if an index does not exist+ create = !IndexReader.indexExists(directory);+ }++ // If index is too old, reading the segments will throw+ // IndexFormatTooOldException.+ segmentInfos = new SegmentInfos();+if (create) {// Try to read first. This is to allow create// against an index that's currently open fordiff --git a/lucene/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java b/lucene/src/test/org/apache/lucene/index/TestIndexWriterExceptions.javaindex fb9e6167efb..9d3670deacc 100644--- a/lucene/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java+++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java@@ -37,6 +37,7 @@ import org.apache.lucene.search.DocIdSetIterator;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.PhraseQuery;import org.apache.lucene.store.Directory;+import org.apache.lucene.store.IOContext;import org.apache.lucene.store.IndexInput;import org.apache.lucene.store.IndexOutput;import org.apache.lucene.store.MockDirectoryWrapper;@@ -1459,4 +1460,38 @@ public class TestIndexWriterExceptions extends LuceneTestCase {r.close();dir.close();}++ static class UOEDirectory extends RAMDirectory {+ boolean doFail = false;++ @Override+ public IndexInput openInput(String name, IOContext context) throws IOException {+ if (doFail && name.startsWith("segments_")) {+ StackTraceElement[] trace = new Exception().getStackTrace();+ for (int i = 0; i < trace.length; i++) {+ if ("indexExists".equals(trace[i].getMethodName())) {+ throw new UnsupportedOperationException("expected UOE");+ }+ }+ }+ return super.openInput(name, context);+ }+ }++ public void testExceptionOnCtor() throws Exception {+ UOEDirectory uoe = new UOEDirectory();+ Directory d = new MockDirectoryWrapper(random, uoe);+ IndexWriter iw = new IndexWriter(d, newIndexWriterConfig(TEST_VERSION_CURRENT, null));+ iw.addDocument(new Document());+ iw.close();+ uoe.doFail = true;+ try {+ new IndexWriter(d, newIndexWriterConfig(TEST_VERSION_CURRENT, null));+ fail("should have gotten a UOE");+ } catch (UnsupportedOperationException expected) {++ }+ uoe.doFail = false;+ d.close();+ }}