From d429aa24d3f5743cd8944a1cd9f04aee112503a4 Mon Sep 17 00:00:00 2001 From: WJL3333 Date: Sun, 26 Sep 2021 18:47:27 +0800 Subject: [PATCH] [ISSUE #3375] Lazy init GetMessageResult only when find message --- .../apache/rocketmq/store/DefaultMessageStore.java | 11 ++++++++++- .../org/apache/rocketmq/store/GetMessageResult.java | 13 +++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java b/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java index bf61789d..f6c652e9 100644 --- a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java +++ b/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java @@ -537,7 +537,8 @@ public class DefaultMessageStore implements MessageStore { long minOffset = 0; long maxOffset = 0; - GetMessageResult getResult = new GetMessageResult(); + // lazy init when find msg. + GetMessageResult getResult = null; final long maxOffsetPy = this.commitLog.getMaxOffset(); @@ -574,6 +575,9 @@ public class DefaultMessageStore implements MessageStore { int i = 0; final int maxFilterMessageCount = Math.max(16000, maxMsgNums * ConsumeQueue.CQ_STORE_UNIT_SIZE); final boolean diskFallRecorded = this.messageStoreConfig.isDiskFallRecorded(); + + getResult = new GetMessageResult(maxMsgNums); + ConsumeQueueExt.CqExtUnit cqExtUnit = new ConsumeQueueExt.CqExtUnit(); for (; i < bufferConsumeQueue.getSize() && i < maxFilterMessageCount; i += ConsumeQueue.CQ_STORE_UNIT_SIZE) { long offsetPy = bufferConsumeQueue.getByteBuffer().getLong(); @@ -677,6 +681,11 @@ public class DefaultMessageStore implements MessageStore { long elapsedTime = this.getSystemClock().now() - beginTime; this.storeStatsService.setGetMessageEntireTimeMax(elapsedTime); + // lazy init no data found. + if (getResult == null) { + getResult = new GetMessageResult(0); + } + getResult.setStatus(status); getResult.setNextBeginOffset(nextBeginOffset); getResult.setMaxOffset(maxOffset); diff --git a/store/src/main/java/org/apache/rocketmq/store/GetMessageResult.java b/store/src/main/java/org/apache/rocketmq/store/GetMessageResult.java index 996e24d8..4e6eccbf 100644 --- a/store/src/main/java/org/apache/rocketmq/store/GetMessageResult.java +++ b/store/src/main/java/org/apache/rocketmq/store/GetMessageResult.java @@ -23,10 +23,8 @@ import org.apache.rocketmq.store.stats.BrokerStatsManager; public class GetMessageResult { - private final List messageMapedList = - new ArrayList(100); - - private final List messageBufferList = new ArrayList(100); + private final List messageMapedList; + private final List messageBufferList; private GetMessageStatus status; private long nextBeginOffset; @@ -40,6 +38,13 @@ public class GetMessageResult { private int msgCount4Commercial = 0; public GetMessageResult() { + messageMapedList = new ArrayList<>(100); + messageBufferList = new ArrayList<>(100); + } + + public GetMessageResult(int resultSize) { + messageMapedList = new ArrayList<>(resultSize); + messageBufferList = new ArrayList<>(resultSize); } public GetMessageStatus getStatus() { -- GitLab