This is an automated email from the ASF dual-hosted git repository.

jinrongtong pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git


The following commit(s) were added to refs/heads/develop by this push:
     new f0f15b5e21 [ISSUE #7454] Utilizing cache to avoid duplicate parsing 
(#7455)
f0f15b5e21 is described below

commit f0f15b5e21acd3caf9141375be5db3ef726a2173
Author: guyinyou <[email protected]>
AuthorDate: Thu Oct 19 10:14:29 2023 +0800

    [ISSUE #7454] Utilizing cache to avoid duplicate parsing (#7455)
    
    * Utilizing cache to avoid duplicate parsing
    
    * add a method argument to decide cacheable
    
    * Renaming variable names from cacheAble to isCached
    
    ---------
    
    Co-authored-by: guyinyou <[email protected]>
    Co-authored-by: RongtongJin <[email protected]>
---
 .../rocketmq/broker/processor/PopMessageProcessor.java     |  2 +-
 .../apache/rocketmq/remoting/protocol/RemotingCommand.java | 14 ++++++++++++--
 .../remoting/protocol/header/FastCodesHeaderTest.java      |  2 +-
 3 files changed, 14 insertions(+), 4 deletions(-)

diff --git 
a/broker/src/main/java/org/apache/rocketmq/broker/processor/PopMessageProcessor.java
 
b/broker/src/main/java/org/apache/rocketmq/broker/processor/PopMessageProcessor.java
index f5d07c5aae..7ed4d53ab1 100644
--- 
a/broker/src/main/java/org/apache/rocketmq/broker/processor/PopMessageProcessor.java
+++ 
b/broker/src/main/java/org/apache/rocketmq/broker/processor/PopMessageProcessor.java
@@ -204,7 +204,7 @@ public class PopMessageProcessor implements 
NettyRequestProcessor {
         RemotingCommand response = 
RemotingCommand.createResponseCommand(PopMessageResponseHeader.class);
         final PopMessageResponseHeader responseHeader = 
(PopMessageResponseHeader) response.readCustomHeader();
         final PopMessageRequestHeader requestHeader =
-            (PopMessageRequestHeader) 
request.decodeCommandCustomHeader(PopMessageRequestHeader.class);
+            (PopMessageRequestHeader) 
request.decodeCommandCustomHeader(PopMessageRequestHeader.class, true);
         StringBuilder startOffsetInfo = new StringBuilder(64);
         StringBuilder msgOffsetInfo = new StringBuilder(64);
         StringBuilder orderCountInfo = null;
diff --git 
a/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingCommand.java
 
b/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingCommand.java
index d27135132c..e93072adff 100644
--- 
a/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingCommand.java
+++ 
b/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingCommand.java
@@ -89,6 +89,7 @@ public class RemotingCommand {
     private String remark;
     private HashMap<String, String> extFields;
     private transient CommandCustomHeader customHeader;
+    private transient CommandCustomHeader cachedHeader;
 
     private SerializeType serializeTypeCurrentRPC = 
serializeTypeConfigInThisServer;
 
@@ -260,10 +261,19 @@ public class RemotingCommand {
 
     public CommandCustomHeader decodeCommandCustomHeader(
         Class<? extends CommandCustomHeader> classHeader) throws 
RemotingCommandException {
-        return decodeCommandCustomHeader(classHeader, true);
+        return decodeCommandCustomHeader(classHeader, false);
     }
 
-    public CommandCustomHeader decodeCommandCustomHeader(Class<? extends 
CommandCustomHeader> classHeader,
+    public CommandCustomHeader decodeCommandCustomHeader(
+        Class<? extends CommandCustomHeader> classHeader, boolean isCached) 
throws RemotingCommandException {
+        if (isCached && cachedHeader != null) {
+            return cachedHeader;
+        }
+        cachedHeader = decodeCommandCustomHeaderDirectly(classHeader, true);
+        return cachedHeader;
+    }
+
+    public CommandCustomHeader decodeCommandCustomHeaderDirectly(Class<? 
extends CommandCustomHeader> classHeader,
         boolean useFastEncode) throws RemotingCommandException {
         CommandCustomHeader objectHeader;
         try {
diff --git 
a/remoting/src/test/java/org/apache/rocketmq/remoting/protocol/header/FastCodesHeaderTest.java
 
b/remoting/src/test/java/org/apache/rocketmq/remoting/protocol/header/FastCodesHeaderTest.java
index 6bb100f574..b6a0d63112 100644
--- 
a/remoting/src/test/java/org/apache/rocketmq/remoting/protocol/header/FastCodesHeaderTest.java
+++ 
b/remoting/src/test/java/org/apache/rocketmq/remoting/protocol/header/FastCodesHeaderTest.java
@@ -73,7 +73,7 @@ public class FastCodesHeaderTest {
 
     private void check(RemotingCommand command, List<Field> fields,
             Class<? extends CommandCustomHeader> classHeader) throws Exception 
{
-        CommandCustomHeader o1 = 
command.decodeCommandCustomHeader(classHeader, false);
+        CommandCustomHeader o1 = 
command.decodeCommandCustomHeaderDirectly(classHeader, false);
         CommandCustomHeader o2 = 
classHeader.getDeclaredConstructor().newInstance();
         ((FastCodesHeader)o2).decode(command.getExtFields());
         for (Field f : fields) {

Reply via email to