[ISSUE #10015] Optimize writeWithoutMmap. add page alignment to avoid read-modify-write

Change-Id: I41ae3b71a4803295c2487cdf8f5e458764b64ebc

Co-authored-by: guyinyou <guyinyou.gyy@alibaba-inc.com>
This commit is contained in:
guyinyou
2026-01-16 17:59:02 +08:00
committed by GitHub
parent 1b6a919bde
commit ee104114ac
@@ -385,8 +385,19 @@ public class DefaultMappedFile extends AbstractMappedFile {
if (sharedByteBuffer != null) {
try {
int msgLen = result.getWroteBytes();
int endpos = currentPos + msgLen;
// alignment end position
int extraAppendSize = UNSAFE_PAGE_SIZE - endpos % UNSAFE_PAGE_SIZE;
int actualAppendSize = msgLen + extraAppendSize;
this.fileChannel.position(currentPos);
byteBuffer.position(0).limit(result.getWroteBytes());
// commitlog can contain dirty data at the end.
if (byteBuffer.capacity() >= actualAppendSize) {
byteBuffer.position(0).limit(actualAppendSize);
} else {
byteBuffer.position(0).limit(msgLen);
}
this.fileChannel.write(byteBuffer);
} catch (Throwable t) {
log.error("Failed to write to mappedFile {}", this.fileName, t);