Skip to content

Commit ee10411

Browse files
guyinyouguyinyou
andauthored
[ISSUE #10015] Optimize writeWithoutMmap. add page alignment to avoid read-modify-write
Change-Id: I41ae3b71a4803295c2487cdf8f5e458764b64ebc Co-authored-by: guyinyou <guyinyou.gyy@alibaba-inc.com>
1 parent 1b6a919 commit ee10411

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,19 @@ public AppendMessageResult appendMessagesInner(final MessageExt messageExt, fina
385385

386386
if (sharedByteBuffer != null) {
387387
try {
388+
int msgLen = result.getWroteBytes();
389+
int endpos = currentPos + msgLen;
390+
// alignment end position
391+
int extraAppendSize = UNSAFE_PAGE_SIZE - endpos % UNSAFE_PAGE_SIZE;
392+
int actualAppendSize = msgLen + extraAppendSize;
393+
388394
this.fileChannel.position(currentPos);
389-
byteBuffer.position(0).limit(result.getWroteBytes());
395+
// commitlog can contain dirty data at the end.
396+
if (byteBuffer.capacity() >= actualAppendSize) {
397+
byteBuffer.position(0).limit(actualAppendSize);
398+
} else {
399+
byteBuffer.position(0).limit(msgLen);
400+
}
390401
this.fileChannel.write(byteBuffer);
391402
} catch (Throwable t) {
392403
log.error("Failed to write to mappedFile {}", this.fileName, t);

0 commit comments

Comments
 (0)