Drifter-yh opened a new issue, #11649:
URL: https://github.com/apache/rocketmq/issues/11649

   ### Before Creating the Bug Report
   - [x] I found a bug, not just asking a question, which should be created in 
GitHub Discussions.
   - [x] I have searched the GitHub Issues and GitHub Discussions of this 
repository and believe that this is not a duplicate.
   - [x] I have confirmed that this bug belongs to the current repository, not 
other repositories of RocketMQ.
   
   ### Runtime platform environment
   
   OS: Windows 11 (also reproducible on Linux, the failure is in pure recovery 
logic)
   Store type: file-based CommitLog + file-based ConsumeQueue (default), 
message index enabled
   
   ### RocketMQ version
   
   branch: develop
   version: 5.5.1
   Git commit id: 19352a24679337276e041f67307c6d284dd48de9
   
   ### JDK Version
   
   Compiler: OpenJDK 17.0.14
   Runtime: OpenJDK 17.0.14
   
   ### Describe the Bug
   
   When both `enableAcceleratedRecovery=true` and `messageIndexSafe=true` are 
configured, a broker that restarts after an abnormal shutdown (abort file 
present) can fail to load the message store:
   
   ```
   java.lang.IllegalArgumentException: newPosition > limit: (10285 > 4096)
       at java.base/java.nio.Buffer.createPositionException(Buffer.java:341)
       at java.base/java.nio.Buffer.position(Buffer.java:316)
       at java.base/java.nio.ByteBuffer.position(ByteBuffer.java:1516)
       at 
java.base/java.nio.MappedByteBuffer.position(MappedByteBuffer.java:321)
       at 
org.apache.rocketmq.store.CommitLog.recoverAbnormally(CommitLog.java:814)
       at 
org.apache.rocketmq.store.DefaultMessageStore.recover(DefaultMessageStore.java:416)
       at 
org.apache.rocketmq.store.DefaultMessageStore.load(DefaultMessageStore.java:369)
   ```
   
   `DefaultMessageStore.load()` catches the exception, logs `load exception` 
and returns `false`, so the broker cannot start. `newPosition` is the consume 
queue dispatch checkpoint and `limit` is the commit log mapped file size, which 
means the recovery scan selected a commit log file older than the dispatch 
checkpoint and the accelerated path still tried to position the reader at the 
checkpoint inside that older file.
   
   A second, silent variant of the same problem exists when the store currently 
fits in a single commit log file: the store loads, but the message index is 
only rebuilt from the consume queue checkpoint onward. Queries by key for 
messages written before the checkpoint return no result after the restart (the 
message data and consume queue are intact, only the rebuilt index is 
incomplete).
   
   ### Steps to Reproduce
   
   1. Configure a file-based store with:
      - `enableAcceleratedRecovery=true`
      - `messageIndexSafe=true`
      - small commit log segments so the store spans several files (or just a 
store with more than one 1 GB segment)
   2. Put messages that have keys.
   3. Wait until the messages are dispatched to the consume queue.
   4. Shut the store down and create the `abort` file to simulate an abnormal 
exit.
   5. Reload the store.
   
   The reproduction in 
`DefaultMessageStoreTest#testAcceleratedRecoveryWithSafeIndexStartingInEarlierFile`
 (attached in the PR) fails at `messageStore.load()` on the current develop 
with the exception above.
   
   ### What Did You Expect to See?
   
   The store loads after the abnormal restart:
   - `getMaxPhyOffset()` and the consume queue max offset are unchanged;
   - the message index is rebuilt from the index checkpoint, so 
`queryMessage(topic, key, ...)` still finds messages written before the restart;
   - the next message continues at the next queue offset.
   
   ### What Did You See Instead?
   
   - Multi-segment store: `load()` returns false with `newPosition > limit`.
   - Single-segment store: `load()` succeeds, but `queryMessage(topic, "key-0", 
...)` returns 0 messages for messages written before the restart.
   
   ### Additional Context
   
   Root cause: `CommitLog#recoverAbnormally(long dispatchFromPhyOffset)` first 
scans the commit log backwards and picks the file to replay, using 
`isMappedFileMatchedRecover` from every `CommitLogDispatchStore`. For the 
message index, `IndexService#isMappedFileMatchedRecover` vetoes files whose 
first message is newer than `StoreCheckpoint#indexMsgTimestamp` when 
`messageIndexSafe` is enabled (IndexService.java:432-444). Because index files 
newer than the index checkpoint are deleted on abnormal load 
(IndexService.java:72-78), the scan commonly falls back to the oldest commit 
log file and the index is expected to be rebuilt by replaying that file from 
its beginning.
   
   However, the accelerated branch then unconditionally computes 
`mappedFileOffset = dispatchFromPhyOffset - mappedFile.getFileFromOffset()` and 
calls `byteBuffer.position((int) mappedFileOffset)` (CommitLog.java:803-815). 
When the selected file is older than the file that contains the dispatch 
checkpoint, this value is larger than the mapped file size and the load fails. 
When both offsets are in the same file, the reader starts after the index 
rebuild point and the index is only partially rebuilt.
   
   `IndexService#getDispatchFromPhyOffset` returns `-1` 
(IndexService.java:427-429), so the index rebuild start is not considered when 
`DefaultMessageStore#recover` calculates the dispatch offset 
(DefaultMessageStore.java:404-411).
   
   This code path became reachable with #10012 (merged in 2026-02-28, first 
released in 5.5.0), which enabled accelerated recovery for the file-based 
consume queue; before that, accelerated recovery only applied when 
`storeType=default_rocksdb`. `messageIndexSafe` has no effect on 
`recoverNormally`, and there is no warning that the combination is unsupported.
   
   Workaround: disable `enableAcceleratedRecovery` or `messageIndexSafe`.
   
   A fix is proposed in the PR: when `messageIndexEnable` and 
`messageIndexSafe` are both enabled, accelerated recovery is skipped and the 
selected commit log file is replayed from its beginning, which is required to 
rebuild the index. This keeps the pre-#10012 behavior for that configuration 
and makes `messageIndexSafe` work as documented again.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to