[TRANSLOG] Translog leaks filehandles if it's corrupted or truncated

If the translog file is corrupted or truncated the stream is never closed
and the filehandle leaks. This commit closes the stream in the case of an
exception.
This commit is contained in:
Simon Willnauer 2014-11-06 20:45:12 +01:00
parent 95171e2bc2
commit 17fdee9441
1 changed files with 9 additions and 1 deletions

View File

@ -22,6 +22,7 @@ package org.elasticsearch.index.translog;
import org.apache.lucene.codecs.CodecUtil;
import org.apache.lucene.store.InputStreamDataInput;
import org.apache.lucene.store.OutputStreamDataOutput;
import org.apache.lucene.util.IOUtils;
import org.elasticsearch.common.io.stream.*;
import java.io.EOFException;
@ -106,14 +107,21 @@ public class ChecksummedTranslogStream implements TranslogStream {
@Override
public StreamInput openInput(File translogFile) throws IOException {
final FileInputStream fileInputStream = new FileInputStream(translogFile);
boolean success = false;
try {
InputStreamStreamInput in = new InputStreamStreamInput(new FileInputStream(translogFile));
final InputStreamStreamInput in = new InputStreamStreamInput(fileInputStream);
CodecUtil.checkHeader(new InputStreamDataInput(in), TranslogStreams.TRANSLOG_CODEC, VERSION, VERSION);
success = true;
return in;
} catch (EOFException e) {
throw new TruncatedTranslogException("translog header truncated", e);
} catch (IOException e) {
throw new TranslogCorruptedException("translog header corrupted", e);
} finally {
if (success == false) {
IOUtils.closeWhileHandlingException(fileInputStream);
}
}
}
}