merge branches/slowclosing: fix handling of exceptions during close, randomly close slowly in mockdir

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1393806 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2012-10-03 22:56:24 +00:00
commit 4e04d7b8b9
6 changed files with 85 additions and 15 deletions

View File

@ -159,6 +159,7 @@ public final class BloomFilteringPostingsFormat extends PostingsFormat {
String bloomFileName = IndexFileNames.segmentFileName(
state.segmentInfo.name, state.segmentSuffix, BLOOM_EXTENSION);
IndexInput bloomIn = null;
boolean success = false;
try {
bloomIn = state.dir.openInput(bloomFileName, state.context);
CodecUtil.checkHeader(bloomIn, BLOOM_CODEC_NAME, BLOOM_CODEC_VERSION,
@ -178,10 +179,13 @@ public final class BloomFilteringPostingsFormat extends PostingsFormat {
FieldInfo fieldInfo = state.fieldInfos.fieldInfo(fieldNum);
bloomsByFieldName.put(fieldInfo.name, bloom);
}
} finally {
IOUtils.close(bloomIn);
success = true;
} finally {
if (!success) {
IOUtils.closeWhileHandlingException(bloomIn, delegateFieldsProducer);
}
}
}
public Iterator<String> iterator() {

View File

@ -172,12 +172,15 @@ public class BlockTreeTermsReader extends FieldsProducer {
throw new CorruptIndexException("duplicate field: " + fieldInfo.name + " (resource=" + in + ")");
}
}
if (indexDivisor != -1) {
indexIn.close();
}
success = true;
} finally {
if (!success) {
// this.close() will close in:
IOUtils.closeWhileHandlingException(indexIn, this);
} else if (indexDivisor != -1) {
indexIn.close();
}
}
}

View File

@ -151,7 +151,7 @@ final class StandardDirectoryReader extends DirectoryReader {
}
boolean success = false;
IOException prior = null;
Throwable prior = null;
try {
SegmentReader newReader;
if (newReaders[i] == null || infos.info(i).info.getUseCompoundFile() != newReaders[i].getSegmentInfo().info.getUseCompoundFile()) {
@ -176,7 +176,7 @@ final class StandardDirectoryReader extends DirectoryReader {
}
}
success = true;
} catch (IOException ex) {
} catch (Throwable ex) {
prior = ex;
} finally {
if (!success) {
@ -192,14 +192,19 @@ final class StandardDirectoryReader extends DirectoryReader {
// closing we must decRef it
newReaders[i].decRef();
}
} catch (IOException ex) {
if (prior == null) prior = ex;
} catch (Throwable t) {
if (prior == null) prior = t;
}
}
}
}
// throw the first exception
if (prior != null) throw prior;
if (prior != null) {
if (prior instanceof IOException) throw (IOException) prior;
if (prior instanceof RuntimeException) throw (RuntimeException) prior;
if (prior instanceof Error) throw (Error) prior;
throw new RuntimeException(prior);
}
}
}
return new StandardDirectoryReader(directory, newReaders, writer, infos, termInfosIndexDivisor, false);
@ -329,13 +334,13 @@ final class StandardDirectoryReader extends DirectoryReader {
@Override
protected void doClose() throws IOException {
IOException ioe = null;
Throwable firstExc = null;
for (final AtomicReader r : getSequentialSubReaders()) {
// try to close each reader, even if an exception is thrown
try {
r.decRef();
} catch (IOException e) {
if (ioe == null) ioe = e;
} catch (Throwable t) {
if (t == null) firstExc = t;
}
}
@ -346,7 +351,12 @@ final class StandardDirectoryReader extends DirectoryReader {
}
// throw the first exception
if (ioe != null) throw ioe;
if (firstExc != null) {
if (firstExc instanceof IOException) throw (IOException) firstExc;
if (firstExc instanceof RuntimeException) throw (RuntimeException) firstExc;
if (firstExc instanceof Error) throw (Error) firstExc;
throw new RuntimeException(firstExc);
}
}
@Override

View File

@ -1025,7 +1025,6 @@ public class TestIndexWriter extends LuceneTestCase {
}
w.close();
w = null;
_TestUtil.checkIndex(dir);
DirectoryReader.open(dir).close();
// Strangely, if we interrupt a thread before

View File

@ -498,7 +498,14 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {
throw fillOpenTrace(new IOException("MockDirectoryWrapper: file \"" + name + "\" is still open for writing"), name, false);
}
IndexInput ii = new MockIndexInputWrapper(this, name, delegate.openInput(name, LuceneTestCase.newIOContext(randomState, context)));
IndexInput delegateInput = delegate.openInput(name, LuceneTestCase.newIOContext(randomState, context));
final IndexInput ii;
if (randomState.nextInt(500) == 0) {
ii = new SlowClosingMockIndexInputWrapper(this, name, delegateInput);
} else {
ii = new MockIndexInputWrapper(this, name, delegateInput);
}
addFileHandle(ii, name, Handle.Input);
return ii;
}

View File

@ -0,0 +1,47 @@
package org.apache.lucene.store;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.IOException;
import org.apache.lucene.util.ThreadInterruptedException;
/**
* hangs onto files a little bit longer (50ms in close).
* MockDirectoryWrapper acts like windows: you can't delete files
* open elsewhere. so the idea is to make race conditions for tiny
* files (like segments) easier to reproduce.
*/
class SlowClosingMockIndexInputWrapper extends MockIndexInputWrapper {
public SlowClosingMockIndexInputWrapper(MockDirectoryWrapper dir,
String name, IndexInput delegate) {
super(dir, name, delegate);
}
@Override
public void close() throws IOException {
try {
Thread.sleep(50);
} catch (InterruptedException ie) {
throw new ThreadInterruptedException(ie);
} finally {
super.close();
}
}
}