This is an automated email from the ASF dual-hosted git repository.

ShannonDing pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git


The following commit(s) were added to refs/heads/develop by this push:
     new 4b75fb3a6e Fix mixed reads at tiered storage buffer boundaries (#11633)
4b75fb3a6e is described below

commit 4b75fb3a6e609c865ff357787c5c547d20f59d7f
Author: Ezan <[email protected]>
AuthorDate: Fri Nov 27 15:35:05 2026 +0800

    Fix mixed reads at tiered storage buffer boundaries (#11633)
    
    * Fix mixed reads at tiered storage buffer boundaries
    
    * Test mixed stream reads at buffer boundaries
---
 .../tieredstore/stream/CommitLogInputStream.java   |  8 ++------
 .../tieredstore/stream/FileSegmentInputStream.java | 22 ++++++++++------------
 .../stream/FileSegmentInputStreamTest.java         | 22 ++++++++++++++++++++++
 3 files changed, 34 insertions(+), 18 deletions(-)

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 5175e6b4da..8191804133 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 class CommitLogInputStream extends 
FileSegmentInputStream {
         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 class CommitLogInputStream extends 
FileSegmentInputStream {
                 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 class CommitLogInputStream extends 
FileSegmentInputStream {
                 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 5020ff199d..228165007c 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 class FileSegmentInputStream extends InputStream {
         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 class FileSegmentInputStream extends InputStream {
         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 class FileSegmentInputStream extends InputStream {
                 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 3d0dd57a8b..6d983384e5 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<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<>();

Reply via email to