Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ public int read(byte[] b, int off, int len) {
int bufIndex = curReadBufferIndex;
int posInCurBuffer = readPosInCurBuffer;
long curCommitLogOffset = commitLogOffset;
ByteBuffer curBuf = curBuffer;
while (needRead > 0 && bufIndex <= bufferList.size()) {
int readLen, remaining, realReadLen = 0;
if (bufIndex == bufferList.size()) {
Expand All @@ -147,7 +146,7 @@ public int read(byte[] b, int off, int len) {
posInCurBuffer += readLen;
continue;
}
curBuf = bufferList.get(bufIndex);
ByteBuffer curBuf = bufferList.get(bufIndex);
remaining = curBuf.remaining() - posInCurBuffer;
readLen = Math.min(remaining, needRead);
if (posInCurBuffer < MessageFormatUtil.PHYSICAL_OFFSET_POSITION) {
Expand Down Expand Up @@ -183,11 +182,8 @@ public int read(byte[] b, int off, int len) {
posInCurBuffer = 0;
}
}
readPosition = pos;
curReadBufferIndex = bufIndex;
readPosInCurBuffer = posInCurBuffer;
setReadPosition(pos, bufIndex, posInCurBuffer);
commitLogOffset = curCommitLogOffset;
curBuffer = curBuf;
return len;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,14 @@ public synchronized void reset() throws IOException {
if (this.markReadPosition == -1) {
throw new IOException("mark not set");
}
this.readPosition = markReadPosition;
this.curReadBufferIndex = markCurReadBufferIndex;
this.readPosInCurBuffer = markReadPosInCurBuffer;
if (this.curReadBufferIndex < bufferList.size()) {
this.curBuffer = bufferList.get(curReadBufferIndex);
}
setReadPosition(markReadPosition, markCurReadBufferIndex, markReadPosInCurBuffer);
}

void setReadPosition(int position, int bufferIndex, int positionInBuffer) {
readPosition = position;
curReadBufferIndex = bufferIndex;
readPosInCurBuffer = positionInBuffer;
curBuffer = bufferIndex < bufferList.size() ? bufferList.get(bufferIndex) : null;
}

public synchronized void rewind() {
Expand Down Expand Up @@ -169,9 +171,8 @@ public int read(byte[] b, int off, int len) {
int pos = readPosition;
int bufIndex = curReadBufferIndex;
int posInCurBuffer = readPosInCurBuffer;
ByteBuffer curBuf = curBuffer;
while (needRead > 0 && bufIndex < bufferList.size()) {
curBuf = bufferList.get(bufIndex);
ByteBuffer curBuf = bufferList.get(bufIndex);
int remaining = curBuf.remaining() - posInCurBuffer;
int readLen = Math.min(remaining, needRead);
// read from curBuf
Expand All @@ -189,10 +190,7 @@ public int read(byte[] b, int off, int len) {
posInCurBuffer = 0;
}
}
readPosition = pos;
curReadBufferIndex = bufIndex;
readPosInCurBuffer = posInCurBuffer;
curBuffer = curBuf;
setReadPosition(pos, bufIndex, posInCurBuffer);
return len;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ public class FileSegmentInputStreamTest {

private final static Random RANDOM = new Random();

@Test
public void testConsumeQueueSingleReadAfterBulkReadAtBufferBoundary() throws IOException {
verifySingleReadAfterBulkReadAtBufferBoundary(FileSegmentType.CONSUME_QUEUE);
}

@Test
public void testCommitLogSingleReadAfterBulkReadAtBufferBoundary() throws IOException {
verifySingleReadAfterBulkReadAtBufferBoundary(FileSegmentType.COMMIT_LOG);
}

private void verifySingleReadAfterBulkReadAtBufferBoundary(FileSegmentType fileType) throws IOException {
List<ByteBuffer> buffers = Arrays.asList(
ByteBuffer.wrap(new byte[] {1, 2}), ByteBuffer.wrap(new byte[] {3, 4}));
InputStream stream = FileSegmentInputStreamFactory.build(fileType, 0, buffers, null, 4);
byte[] first = new byte[2];
Assert.assertEquals(2, stream.read(first));
Assert.assertArrayEquals(new byte[] {1, 2}, first);
Assert.assertEquals(3, stream.read());
Assert.assertEquals(4, stream.read());
Assert.assertEquals(-1, stream.read());
}

@Test
public void testCommitLogTypeInputStream() {
List<ByteBuffer> uploadBufferList = new ArrayList<>();
Expand Down