You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "gaoran10 (via GitHub)" <gi...@apache.org> on 2025/06/19 10:41:18 UTC

[PR] [feat] Support get retained message count from MessageMetadata [pulsar]

gaoran10 opened a new pull request, #24431:
URL: https://github.com/apache/pulsar/pull/24431

   ### Motivation
   
   Currently, if the broker wants to determine the valid batch index in a compacted entry, it can only calculate it by examining the compactedOut flag in SingleMessageMetadata. Pulsar supports different protocols, if other protocols doesn't add SingleMessageMetadata for each message in a batch, such as Kafka protocol handler may build the payload in Kafka format.
   
   ### Modifications
   
   Provide a way to retrieve the retained message count from MessageMetadata.
   
   ### Does this pull request potentially affect one of the following parts:
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   *If the box was checked, please highlight the changes*
   
   - [ ] Dependencies (add or upgrade a dependency)
   - [ ] The public API
   - [ ] The schema
   - [ ] The default values of configurations
   - [ ] The threading model
   - [ ] The binary protocol
   - [ ] The REST endpoints
   - [ ] The admin CLI options
   - [ ] The metrics
   - [ ] Anything that affects deployment
   
   ### Documentation
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   - [ ] `doc` <!-- Your PR contains doc changes. -->
   - [ ] `doc-required` <!-- Your PR changes impact docs and you will update later -->
   - [x] `doc-not-needed` <!-- Your PR changes do not impact docs -->
   - [ ] `doc-complete` <!-- Docs have been already added -->
   


-- 
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


Re: [PR] [feat] Support get retained message count from MessageMetadata [pulsar]

Posted by "codelipenghui (via GitHub)" <gi...@apache.org>.
codelipenghui commented on code in PR #24431:
URL: https://github.com/apache/pulsar/pull/24431#discussion_r2159754112


##########
pulsar-broker/src/main/java/org/apache/pulsar/compaction/Compactor.java:
##########
@@ -35,6 +35,7 @@ public abstract class Compactor {
     private static final Logger log = LoggerFactory.getLogger(Compactor.class);
     public static final String COMPACTION_SUBSCRIPTION = "__compaction";
     public static final String COMPACTED_TOPIC_LEDGER_PROPERTY = "CompactedTopicLedger";
+    public static final String RETAINED_MESSAGE_COUNT_PROPERTY = "RetainedMessageCount";

Review Comment:
   Is this a concept of Pulsar?
   Will the client side set the value for this property?



-- 
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


Re: [PR] [feat] Support get retained message count from MessageMetadata [pulsar]

Posted by "gaoran10 (via GitHub)" <gi...@apache.org>.
gaoran10 closed pull request #24431: [feat] Support get retained message count from MessageMetadata
URL: https://github.com/apache/pulsar/pull/24431


-- 
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


Re: [PR] [feat] Support get retained message count from MessageMetadata [pulsar]

Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on code in PR #24431:
URL: https://github.com/apache/pulsar/pull/24431#discussion_r2158558605


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -2453,33 +2450,6 @@ private void handleLastMessageIdFromCompactionService(PersistentTopic persistent
         });
     }
 
-    private int calculateTheLastBatchIndexInBatch(MessageMetadata metadata, ByteBuf payload) throws IOException {
-        int batchSize = metadata.getNumMessagesInBatch();
-        if (batchSize <= 1){
-            return -1;
-        }
-        if (metadata.hasCompression()) {
-            var tmp = payload;
-            CompressionType compressionType = metadata.getCompression();
-            CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(compressionType);
-            int uncompressedSize = metadata.getUncompressedSize();
-            payload = codec.decode(payload, uncompressedSize);
-            tmp.release();
-        }
-        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;

Review Comment:
   The key point is here assumes the compaction batch should have `SingleMessageMetadata` for each single message. The behavior was introduced from https://github.com/apache/pulsar/pull/18877. However, it requires the message payload can always have a serialized `SingleMessageMetadata` if the batch size is greater than 1, which could be too strict.
   
   ~~This comment also makes sense: https://github.com/apache/pulsar/pull/18877/files#r1165000498~~ (it actually works for compaction or encryption)



-- 
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


Re: [PR] [feat] Support get retained message count from MessageMetadata [pulsar]

Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on code in PR #24431:
URL: https://github.com/apache/pulsar/pull/24431#discussion_r2158558605


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -2453,33 +2450,6 @@ private void handleLastMessageIdFromCompactionService(PersistentTopic persistent
         });
     }
 
-    private int calculateTheLastBatchIndexInBatch(MessageMetadata metadata, ByteBuf payload) throws IOException {
-        int batchSize = metadata.getNumMessagesInBatch();
-        if (batchSize <= 1){
-            return -1;
-        }
-        if (metadata.hasCompression()) {
-            var tmp = payload;
-            CompressionType compressionType = metadata.getCompression();
-            CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(compressionType);
-            int uncompressedSize = metadata.getUncompressedSize();
-            payload = codec.decode(payload, uncompressedSize);
-            tmp.release();
-        }
-        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;

Review Comment:
   The key point is here assumes the compaction batch should have `SingleMessageMetadata` for each single message. The behavior was introduced from https://github.com/apache/pulsar/pull/18877. However, it requires the message payload can always have a serialized `SingleMessageMetadata` if the batch size is greater than 1, which could be too strict.
   
   This comment also makes sense: https://github.com/apache/pulsar/pull/18877/files#r1165000498 



-- 
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


Re: [PR] [feat] Support get retained message count from MessageMetadata [pulsar]

Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on code in PR #24431:
URL: https://github.com/apache/pulsar/pull/24431#discussion_r2158558605


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -2453,33 +2450,6 @@ private void handleLastMessageIdFromCompactionService(PersistentTopic persistent
         });
     }
 
-    private int calculateTheLastBatchIndexInBatch(MessageMetadata metadata, ByteBuf payload) throws IOException {
-        int batchSize = metadata.getNumMessagesInBatch();
-        if (batchSize <= 1){
-            return -1;
-        }
-        if (metadata.hasCompression()) {
-            var tmp = payload;
-            CompressionType compressionType = metadata.getCompression();
-            CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(compressionType);
-            int uncompressedSize = metadata.getUncompressedSize();
-            payload = codec.decode(payload, uncompressedSize);
-            tmp.release();
-        }
-        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;

Review Comment:
   The key point is here assumes the compaction batch should have `SingleMessageMetadata` for each single message. The behavior was introduced from https://github.com/apache/pulsar/pull/18877. However, it requires the message payload can always have a serialized `SingleMessageMetadata` if the batch size is greater than 1, which could be too strict.
   
   This comment also makes sense: https://github.com/apache/pulsar/pull/18877/files#r1165000498



-- 
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


Re: [PR] [feat] Support get retained message count from MessageMetadata [pulsar]

Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on PR #24431:
URL: https://github.com/apache/pulsar/pull/24431#issuecomment-2989710668

   I mean, this PR only adds a static method `calculateTheLastBatchIndexInBatch`, which does not depend on any private method.
   
   I don't see any extra change in `ServerCnx`, did you miss something?


-- 
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


Re: [PR] [feat] Support get retained message count from MessageMetadata [pulsar]

Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on code in PR #24431:
URL: https://github.com/apache/pulsar/pull/24431#discussion_r2158542182


##########
pulsar-broker/src/main/java/org/apache/pulsar/compaction/CompactedTopicUtils.java:
##########
@@ -104,4 +114,60 @@ public static void asyncReadCompactedEntries(TopicCompactionService topicCompact
             return null;
         });
     }
+
+    public static int calculateTheLastBatchIndexInBatch(MessageMetadata metadata, ByteBuf payload) throws IOException {
+        int retainedMessageCount = getRetainedMessageCount(metadata);
+        if (retainedMessageCount > 0) {
+            return retainedMessageCount;
+        }
+        if (retainedMessageCount == 0) {
+            return -1;
+        }
+
+        int batchSize = metadata.getNumMessagesInBatch();
+        if (batchSize <= 1){
+            return -1;
+        }
+        if (metadata.hasCompression()) {
+            var tmp = payload;
+            CompressionType compressionType = metadata.getCompression();
+            CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(compressionType);
+            int uncompressedSize = metadata.getUncompressedSize();
+            payload = codec.decode(payload, uncompressedSize);
+            tmp.release();
+        }
+        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;
+    }
+
+    /**
+     * Use `-1` as default value if the property is not set or cannot be parsed.
+     */
+    private static int getRetainedMessageCount(MessageMetadata metadata) {
+        if (CollectionUtils.isEmpty(metadata.getPropertiesList())) {
+            return -1;
+        }
+        for (KeyValue kv : metadata.getPropertiesList()) {
+            if (RETAINED_MESSAGE_COUNT_PROPERTY.equals(kv.getKey())) {
+                try {
+                    return Integer.parseInt(kv.getValue());
+                } catch (NumberFormatException e) {
+                    // Ignore and return default value
+                    return -1;
+                }

Review Comment:
   IMO, this hacky behavior should be avoided. It gives a new meaning that if the value of "RetainedMessageCount" property is a valid integer, the value will represent the number of messages in the batch.



-- 
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


Re: [PR] [feat] Support get retained message count from MessageMetadata [pulsar]

Posted by "gaoran10 (via GitHub)" <gi...@apache.org>.
gaoran10 closed pull request #24431: [feat] Support get retained message count from MessageMetadata
URL: https://github.com/apache/pulsar/pull/24431


-- 
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


Re: [PR] [feat] Support get retained message count from MessageMetadata [pulsar]

Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on code in PR #24431:
URL: https://github.com/apache/pulsar/pull/24431#discussion_r2159974242


##########
pulsar-broker/src/main/java/org/apache/pulsar/compaction/Compactor.java:
##########
@@ -35,6 +35,7 @@ public abstract class Compactor {
     private static final Logger log = LoggerFactory.getLogger(Compactor.class);
     public static final String COMPACTION_SUBSCRIPTION = "__compaction";
     public static final String COMPACTED_TOPIC_LEDGER_PROPERTY = "CompactedTopicLedger";
+    public static final String RETAINED_MESSAGE_COUNT_PROPERTY = "RetainedMessageCount";

Review Comment:
   No. The background is complicated. I'm writing a proposal to fix the root cause.
   
   This draft implementation might help you understand: https://github.com/BewareMyPower/pulsar/pull/42/files#diff-f2b074eb97e870ec15676e349d61de41fc94067e2b01e86335d05b4bc4019407
   
   i.e. the customized compaction service could use a property to determine which messages are compacted out rather than checking the `compactedOut` field. The root cause is that each entry is guaranteed to have a `MessageMetadata`, even the message was produced via the protocol handler, but it's not guaranteed to have a `SingleMessageMetadata`.
   
   You can also wait for my proposal (PIP-429) to be available



-- 
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


Re: [PR] [feat] Support get retained message count from MessageMetadata [pulsar]

Posted by "gaoran10 (via GitHub)" <gi...@apache.org>.
gaoran10 commented on PR #24431:
URL: https://github.com/apache/pulsar/pull/24431#issuecomment-2989706586

   > You don't have to add this util method to the upstream. You can implement the same function if you have pulsar-broker as the dependency.
   
   Do you mean extend the ServerCnx and override the method?


-- 
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


Re: [PR] [feat] Support get retained message count from MessageMetadata [pulsar]

Posted by "coderzc (via GitHub)" <gi...@apache.org>.
coderzc commented on PR #24431:
URL: https://github.com/apache/pulsar/pull/24431#issuecomment-2996248805

   If we purpose are to ensure that the compaction task is successful, we only need to check `lastCompactedPosition` and `lastDispatchablePosition` beforen trigger compaction task. If `lastDispatchablePosition <= lastCompactedPosition`, then I just need to skip the compact task because there is no new data need to be compacted.
   
   If we need make the pulsar reader to read Kafka format data, then we need this change.


-- 
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