diff --git a/tieredstore/src/main/java/org/apache/rocketmq/tieredstore/stream/CommitLogInputStream.java b/tieredstore/src/main/java/org/apache/rocketmq/tieredstore/stream/CommitLogInputStream.java index 5175e6b4da4..81918041334 100644 --- a/tieredstore/src/main/java/org/apache/rocketmq/tieredstore/stream/CommitLogInputStream.java +++ b/tieredstore/src/main/java/org/apache/rocketmq/tieredstore/stream/CommitLogInputStream.java @@ -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()) { @@ -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) { @@ -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; } } diff --git a/tieredstore/src/main/java/org/apache/rocketmq/tieredstore/stream/FileSegmentInputStream.java b/tieredstore/src/main/java/org/apache/rocketmq/tieredstore/stream/FileSegmentInputStream.java index 5020ff199d0..228165007c9 100644 --- a/tieredstore/src/main/java/org/apache/rocketmq/tieredstore/stream/FileSegmentInputStream.java +++ b/tieredstore/src/main/java/org/apache/rocketmq/tieredstore/stream/FileSegmentInputStream.java @@ -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() { @@ -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 @@ -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; } } diff --git a/tieredstore/src/test/java/org/apache/rocketmq/tieredstore/stream/FileSegmentInputStreamTest.java b/tieredstore/src/test/java/org/apache/rocketmq/tieredstore/stream/FileSegmentInputStreamTest.java index 3d0dd57a8b9..6d983384e5b 100644 --- a/tieredstore/src/test/java/org/apache/rocketmq/tieredstore/stream/FileSegmentInputStreamTest.java +++ b/tieredstore/src/test/java/org/apache/rocketmq/tieredstore/stream/FileSegmentInputStreamTest.java @@ -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 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 uploadBufferList = new ArrayList<>();