Skip to content

Commit

Permalink
Wrap EOFException in TranslogSnapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveCTurner committed Jun 6, 2019
1 parent ce2598a commit b51abde
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.elasticsearch.common.io.stream.ByteBufferStreamInput;
import org.elasticsearch.index.seqno.SequenceNumbers;

import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
Expand Down Expand Up @@ -77,7 +76,7 @@ protected final int readSize(ByteBuffer reusableBuffer, long position) throws IO
reusableBuffer.capacity() + "]";
reusableBuffer.clear();
reusableBuffer.limit(4);
readBytesDetectingTruncation(reusableBuffer, position);
readBytes(reusableBuffer, position);
reusableBuffer.flip();
// Add an extra 4 to account for the operation size integer itself
final int size = reusableBuffer.getInt() + 4;
Expand Down Expand Up @@ -108,19 +107,11 @@ protected final BufferedChecksumStreamInput checksummedStream(ByteBuffer reusabl
}
buffer.clear();
buffer.limit(opSize);
readBytesDetectingTruncation(buffer, position);
readBytes(buffer, position);
buffer.flip();
return new BufferedChecksumStreamInput(new ByteBufferStreamInput(buffer), path.toString(), reuse);
}

private void readBytesDetectingTruncation(ByteBuffer buffer, long position) throws IOException {
try {
readBytes(buffer, position);
} catch (EOFException e) {
throw new TranslogCorruptedException(path.toString(), "translog truncated", e);
}
}

protected Translog.Operation read(BufferedChecksumStreamInput inStream) throws IOException {
final Translog.Operation op = Translog.readOperation(inStream);
if (op.primaryTerm() > getPrimaryTerm() && getPrimaryTerm() != SequenceNumbers.UNASSIGNED_PRIMARY_TERM) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,19 @@ public long sizeInBytes() {
* reads an operation at the given position into the given buffer.
*/
protected void readBytes(ByteBuffer buffer, long position) throws IOException {
if (position >= length) {
throw new EOFException("read requested past EOF. pos [" + position + "] end: [" + length + "], generation: [" +
getGeneration() + "], path: [" + path + "]");
}
if (position < getFirstOperationOffset()) {
throw new IOException("read requested before position of first ops. pos [" + position + "] first op on: [" +
getFirstOperationOffset() + "], generation: [" + getGeneration() + "], path: [" + path + "]");
try {
if (position >= length) {
throw new EOFException("read requested past EOF. pos [" + position + "] end: [" + length + "], generation: [" +
getGeneration() + "], path: [" + path + "]");
}
if (position < getFirstOperationOffset()) {
throw new IOException("read requested before position of first ops. pos [" + position + "] first op on: [" +
getFirstOperationOffset() + "], generation: [" + getGeneration() + "], path: [" + path + "]");
}
Channels.readFromFileChannelWithEofException(channel, position, buffer);
} catch (EOFException e) {
throw new TranslogCorruptedException(path.toString(), "translog truncated", e);
}
Channels.readFromFileChannelWithEofException(channel, position, buffer);
}

@Override
Expand Down

0 comments on commit b51abde

Please sign in to comment.