Add more information to IndexInput failures related to abusing closed input

This commit is contained in:
Simon Willnauer 2013-12-12 10:44:01 +01:00
parent 0554f8e875
commit d95420cc67
1 changed files with 129 additions and 0 deletions

View File

@ -39,8 +39,10 @@ import org.elasticsearch.index.store.ram.RamDirectoryService;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import java.io.IOException;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
public class MockDirectoryHelper {
public static final String RANDOM_IO_EXCEPTION_RATE = "index.store.mock.random.io_exception_rate";
@ -160,5 +162,132 @@ public class MockDirectoryHelper {
public void closeWithRuntimeException() throws IOException {
super.close(); // force fail if open files etc. called in tear down of ElasticsearchIntegrationTest
}
@Override
public synchronized IndexInput openInput(String name, IOContext context) throws IOException {
return new CloseTrackingMockIndexInputWrapper(name, super.openInput(name, context), logger);
}
}
private static class CloseTrackingMockIndexInputWrapper extends IndexInput {
private final AtomicBoolean closed = new AtomicBoolean(false);
private final String name;
private IndexInput delegate;
private final ESLogger logger;
private volatile RuntimeException closingStack;
public CloseTrackingMockIndexInputWrapper(String name, IndexInput delegate, ESLogger logger) {
super(name);
this.delegate = delegate;
this.logger = logger;
this.name = name;
}
@Override
public void close() throws IOException {
if (closed.compareAndSet(false, true)) {
closingStack = new RuntimeException("IndexInput closed");
delegate.close();
}
}
@Override
public IndexInput clone() {
ensureOpen();
return delegate.clone();
}
private void ensureOpen() {
if (closed.get()) {
logger.debug("Abusing IndexInput for: [" + name + "] - already closed", closingStack);
throw new RuntimeException("Abusing closed IndexInput!");
}
}
@Override
public long getFilePointer() {
ensureOpen();
return delegate.getFilePointer();
}
@Override
public void seek(long pos) throws IOException {
ensureOpen();
delegate.seek(pos);
}
@Override
public long length() {
ensureOpen();
return delegate.length();
}
@Override
public byte readByte() throws IOException {
ensureOpen();
return delegate.readByte();
}
@Override
public void readBytes(byte[] b, int offset, int len) throws IOException {
ensureOpen();
delegate.readBytes(b, offset, len);
}
@Override
public void readBytes(byte[] b, int offset, int len, boolean useBuffer)
throws IOException {
ensureOpen();
delegate.readBytes(b, offset, len, useBuffer);
}
@Override
public short readShort() throws IOException {
ensureOpen();
return delegate.readShort();
}
@Override
public int readInt() throws IOException {
ensureOpen();
return delegate.readInt();
}
@Override
public long readLong() throws IOException {
ensureOpen();
return delegate.readLong();
}
@Override
public String readString() throws IOException {
ensureOpen();
return delegate.readString();
}
@Override
public Map<String,String> readStringStringMap() throws IOException {
ensureOpen();
return delegate.readStringStringMap();
}
@Override
public int readVInt() throws IOException {
ensureOpen();
return delegate.readVInt();
}
@Override
public long readVLong() throws IOException {
ensureOpen();
return delegate.readVLong();
}
@Override
public String toString() {
return "CloseTrackingMockIndexInputWrapper(" + delegate + ")";
}
}
}