You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by "RongtongJin (via GitHub)" <gi...@apache.org> on 2025/09/18 11:41:19 UTC

[I] [Enhancement] Optimize DefaultMappedFile by Replacing RandomAccessFile with FileChannel [rocketmq]

RongtongJin opened a new issue, #9716:
URL: https://github.com/apache/rocketmq/issues/9716

   ### Before Creating the Enhancement Request
   
   - [x] I have confirmed that this should be classified as an enhancement rather than a bug/feature.
   
   
   ### Summary
   
   This issue addresses performance optimization in the `DefaultMappedFile` class by completely replacing `RandomAccessFile` with `FileChannel` for all write operations. The current implementation maintains both I/O mechanisms, which creates unnecessary complexity and reduces performance.
   
   ### Motivation
   
   The existing `DefaultMappedFile` implementation suffers from several performance and maintainability issues:
   
   1. **Performance Bottleneck**: `RandomAccessFile` demonstrates inferior performance compared to `FileChannel` for high-throughput I/O operations, particularly in scenarios involving frequent small writes typical in message queue operations.
   
   2. **Memory Management Inefficiency**: The `SharedByteBuffer` currently uses heap memory allocation (`ByteBuffer.allocate()`), which increases garbage collection pressure and reduces I/O efficiency.
   
   3. **Code Complexity**: Maintaining two distinct I/O code paths increases complexity, making the codebase harder to maintain, debug, and extend.
   
   4. **Resource Management Overhead**: Managing both `RandomAccessFile` and `FileChannel` requires more complex resource cleanup logic and increases the risk of resource leaks.
   
   
   ### Describe the Solution You'd Like
   
   ### Core Implementation Changes
   
   1. **Complete RandomAccessFile Removal**
      - Eliminate the `randomAccessFile` field and all associated logic
      - Simplify resource management by consolidating to a single I/O mechanism
      - Remove RandomAccessFile-related cleanup code in `cleanResources()` and `destroy()` methods
   
   2. **Unified FileChannel I/O Operations**
      - Use `FileChannel` exclusively for all write operations when `writeWithoutMmap` is enabled
      - Maintain backward compatibility by preserving the `writeWithoutMmap` configuration option
      - Fix `SharedByteBuffer` write logic to ensure accurate byte count writing using `result.getWroteBytes()`
   
   3. **Direct Memory Allocation Optimization**
      - Convert `SharedByteBuffer` to use direct memory allocation (`ByteBuffer.allocateDirect()`)
      - Reduce GC pressure and enable zero-copy I/O operations for better performance
   
   4. **Enhanced Error Handling and Configuration**
      - Integrate `RunningFlags` support for improved error handling and recovery mechanisms
      - Refactor constructor design to support additional configuration options while maintaining backward compatibility
   
   ### Technical Implementation Details
   
   **Before (Dual I/O Mechanism):**
   ```java
   if (writeWithoutMmap && randomAccessFile != null) {
       randomAccessFile.seek(currentPos);
       randomAccessFile.write(data, offset, length);
   } else {
       // MappedByteBuffer logic
   }
   ```
   
   **After (Unified FileChannel):**
   ```java
   if (writeWithoutMmap) {
       this.fileChannel.position(currentPos);
       ByteBuffer writeBuffer = ByteBuffer.wrap(data, offset, length);
       this.fileChannel.write(writeBuffer);
   } else {
       // MappedByteBuffer logic (unchanged)
   }
   ```
   
   **Memory Allocation Optimization:**
   ```java
   // Before
   this.buffer = ByteBuffer.allocate(size);
   
   // After  
   this.buffer = ByteBuffer.allocateDirect(size);
   ```
   
   ### Expected Performance Benefits
   
   - **I/O Performance**: 10-20% improvement in write operation throughput
   - **Memory Efficiency**: Reduced GC pressure through direct memory allocation
   - **Code Maintainability**: ~20 lines of code reduction with improved clarity
   - **Operational Consistency**: Unified I/O operations eliminate potential behavioral inconsistencies
   
   ### Describe Alternatives You've Considered
   
   No
   
   ### Additional Context
   
   No


-- 
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: commits-unsubscribe@rocketmq.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Enhancement] Optimize DefaultMappedFile by Replacing RandomAccessFile with FileChannel [rocketmq]

Posted by "RongtongJin (via GitHub)" <gi...@apache.org>.
RongtongJin closed issue #9716: [Enhancement] Optimize DefaultMappedFile by Replacing RandomAccessFile with FileChannel
URL: https://github.com/apache/rocketmq/issues/9716


-- 
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: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org