You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/12/11 18:12:42 UTC

[GitHub] [pulsar] BewareMyPower commented on a diff in pull request #18877: [fix] [broker] getLastMessageId returns a wrong batch index of last message if enabled read compacted

BewareMyPower commented on code in PR #18877:
URL: https://github.com/apache/pulsar/pull/18877#discussion_r1045275815


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -2017,6 +2027,25 @@ private void handleLastMessageIdFromCompactedLedger(PersistentTopic persistentTo
         });
     }
 
+    private int calculateTheLastBatchIndexInBatch(MessageMetadata metadata, ByteBuf payload) throws IOException {
+        int batchSize = metadata.getNumMessagesInBatch();
+        if (batchSize <= 1){
+            return -1;
+        }
+        SingleMessageMetadata singleMessageMetadata = new SingleMessageMetadata();
+        int lastBatchIndexInBatch = -1;
+        for (int i = 0; i < batchSize; i++){
+            ByteBuf singleMessagePayload =
+                    Commands.deSerializeSingleMessageInBatch(payload, singleMessageMetadata, i, batchSize);
+            singleMessagePayload.release();
+            if (singleMessageMetadata.isCompactedOut()){
+                continue;
+            }
+            lastBatchIndexInBatch = i;
+        }
+        return lastBatchIndexInBatch;

Review Comment:
   ```suggestion
           for (int i = batchSize - 1; i >= 0; i--) {
               ByteBuf singleMessagePayload =
                       Commands.deSerializeSingleMessageInBatch(payload, singleMessageMetadata, i, batchSize);
               singleMessagePayload.release();
               if (!singleMessageMetadata.isCompactedOut()) {
                   return i;
               }
           }
           return -1;
   ```
   
   Use a reversed iteration to avoid unnecessary iterations because the most of time



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org