Before Creating the Enhancement Request
Summary
Optimize write performance in writeWithoutMmap mode by aligning write operations to page boundaries, avoiding expensive read-modify-write operations when pagecache is not available.
Motivation
When warmMappedFile is enabled, files are preloaded into pagecache. However, after system restarts or when pagecache is evicted, the file may not be in pagecache. In writeWithoutMmap mode, append writes become overwrite operations via FileChannel.write().
When write end positions are not aligned to page boundaries, the OS must perform read-modify-write operations:
- Read the entire page containing the write position
- Merge the new data with existing page data
- Write back the entire merged page
This is inefficient compared to direct page-aligned writes. By aligning writes to page boundaries, we can achieve pure write operations without read-modify-write overhead, significantly improving write performance in scenarios where pagecache is unavailable.
Describe the Solution You'd Like
Implement page alignment for writes in writeWithoutMmap mode:
- Calculate the write end position:
endpos = currentPos + msgLen
- Calculate extra bytes needed for alignment:
extraAppendSize = UNSAFE_PAGE_SIZE - endpos % UNSAFE_PAGE_SIZE
- Calculate aligned write size:
actualAppendSize = msgLen + extraAppendSize
- If buffer capacity allows, write the aligned size; otherwise, write only the original message length
Implementation location: DefaultMappedFile.appendMessagesInner() method, specifically in the writeWithoutMmap path when using sharedByteBuffer.
The solution should:
- Align write end positions to
UNSAFE_PAGE_SIZE boundaries
- Handle cases where buffer capacity is insufficient for full alignment
- Maintain backward compatibility (commitlog can contain dirty data at the end, which is acceptable)
Describe Alternatives You've Considered
-
Always keep files in pagecache: This is not always possible due to system memory pressure and OS pagecache eviction policies.
-
Use mmap for all writes: The writeWithoutMmap mode exists for specific use cases where mmap is not desired, so this alternative doesn't address the requirement.
-
Pre-allocate aligned buffers: While this could help, it doesn't solve the fundamental issue of unaligned write positions causing read-modify-write operations.
-
Ignore alignment and accept read-modify-write overhead: This maintains current behavior but sacrifices performance, which is not ideal for high-throughput scenarios.
The proposed page alignment solution is the most effective approach as it directly addresses the root cause (unaligned writes) with minimal overhead and maintains compatibility with existing behavior.
Additional Context
No response
Before Creating the Enhancement Request
Summary
Optimize write performance in writeWithoutMmap mode by aligning write operations to page boundaries, avoiding expensive read-modify-write operations when pagecache is not available.
Motivation
When warmMappedFile is enabled, files are preloaded into pagecache. However, after system restarts or when pagecache is evicted, the file may not be in pagecache. In writeWithoutMmap mode, append writes become overwrite operations via FileChannel.write().
When write end positions are not aligned to page boundaries, the OS must perform read-modify-write operations:
This is inefficient compared to direct page-aligned writes. By aligning writes to page boundaries, we can achieve pure write operations without read-modify-write overhead, significantly improving write performance in scenarios where pagecache is unavailable.
Describe the Solution You'd Like
Implement page alignment for writes in writeWithoutMmap mode:
endpos = currentPos + msgLenextraAppendSize = UNSAFE_PAGE_SIZE - endpos % UNSAFE_PAGE_SIZEactualAppendSize = msgLen + extraAppendSizeImplementation location:
DefaultMappedFile.appendMessagesInner()method, specifically in thewriteWithoutMmappath when usingsharedByteBuffer.The solution should:
UNSAFE_PAGE_SIZEboundariesDescribe Alternatives You've Considered
Always keep files in pagecache: This is not always possible due to system memory pressure and OS pagecache eviction policies.
Use mmap for all writes: The writeWithoutMmap mode exists for specific use cases where mmap is not desired, so this alternative doesn't address the requirement.
Pre-allocate aligned buffers: While this could help, it doesn't solve the fundamental issue of unaligned write positions causing read-modify-write operations.
Ignore alignment and accept read-modify-write overhead: This maintains current behavior but sacrifices performance, which is not ideal for high-throughput scenarios.
The proposed page alignment solution is the most effective approach as it directly addresses the root cause (unaligned writes) with minimal overhead and maintains compatibility with existing behavior.
Additional Context
No response