You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "rdhabalia (via GitHub)" <gi...@apache.org> on 2024/09/24 22:40:54 UTC

[PR] [fix][client] Fix failover consumer-listener stuck with cumulative ack and epoch time [pulsar]

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

   ### Motivation
   
   Currently, Pulsar-client lib doesn't increase the permits for the failover/exclusive consumer if it has listener and if that consumer has to filter out the messages because of incorrect epoch time. Because of that message listener will eventually stop consuming messages as you can see below that consumer doesn't have availablePermit and broker stops dispatching the messages, even if consumer has processed all sent messages by the broker.
   
   ```
   stats
   
   subscriptions" : {
   "activeConsumerName" : "f8051",
   :
   "consumers" : [ {
           "msgRateOut" : 0.0,
           "msgThroughputOut" : 0.0,
           "bytesOutCounter" : 3623009,
           "msgOutCounter" : 6752,
           "msgRateRedeliver" : 0.0,
           "messageAckRate" : 0.0,
           "chunkedMessageRate" : 0.0,
           "consumerName" : "f8051",
           "availablePermits" : -2,
   
   
   stats-internal
   
   
   cursors" : {
       "my-failover-sub" : {
         "markDeletePosition" : "4748166582:37897",
         "readPosition" : "4748166582:37898",
   
   
   18:52:18.473 [pulsar-client-internal-1003-1] WARN  org.apache.pulsar.client.impl.ConsumerBase - Consumer filter old epoch message, topic : [persistent://myProp/ns1/t1], messageId : [4748166582:37897:23], messageConsumerEpoch : [4], consumerEpoch : [5]  yrid: 
   18:52:18.476 [pulsar-client-internal-1003-1] DEBUG org.apache.pulsar.client.impl.MultiTopicsConsumerImpl - [persistent://myProp/ns1/t1][my-failover-sub] Received message from topics-consumer 4748166582:37897:23
   18:52:18.823 [pulsar-external-listener-863-1] DEBUG org.apache.pulsar.client.impl.ConsumerBase - [persistent://myProp/ns1/t1][my-failover-sub] Calling message listener for message 4748166582:37897:23
   ```
   
   ### Modifications
   
   Pulsar consumer should increase permits if it discard messages due to epoch mismatch and continue processing messages with correct epoch.
   
   ### Verifying this change
   
   - [ ] Make sure that the change passes the CI checks.
   
   *(Please pick either of the following options)*
   
   This change is a trivial rework / code cleanup without any test coverage.
   
   *(or)*
   
   This change is already covered by existing tests, such as *(please describe tests)*.
   
   *(or)*
   
   This change added tests and can be verified as follows:
   
   *(example:)*
     - *Added integration tests for end-to-end deployment with large payloads (10MB)*
     - *Extended integration test for recovery after broker failure*
   
   ### 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 -->
   - [ ] `doc-not-needed` <!-- Your PR changes do not impact docs -->
   - [ ] `doc-complete` <!-- Docs have been already added -->
   
   ### Matching PR in forked repository
   
   PR in forked repository: <!-- ENTER URL HERE -->
   
   <!--
   After opening this PR, the build in apache/pulsar will fail and instructions will
   be provided for opening a PR in the PR author's forked repository.
   
   apache/pulsar pull requests should be first tested in your own fork since the 
   apache/pulsar CI based on GitHub Actions has constrained resources and quota.
   GitHub Actions provides separate quota for pull requests that are executed in 
   a forked repository.
   
   The tests will be run in the forked repository until all PR review comments have
   been handled, the tests pass and the PR is approved by a reviewer.
   -->
   


-- 
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] [fix][client] Fix failover consumer-listener stuck with cumulative ack and epoch time [pulsar]

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


##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerBase.java:
##########
@@ -1161,25 +1161,16 @@ private void executeKeySharedMessageListener(Message<?> message, Runnable runnab
         executorProvider.getExecutor(peekMessageKey(message)).execute(runnable);
     }
 
+    protected void callMessageListenerAndIncreasePermits(Message<T> msg) {
+        if (log.isDebugEnabled()) {
+            log.debug("[{}][{}] Calling message listener for message {}", topic, subscription, msg.getMessageId());
+        }
+        increasePermits(msg);
+        callMessageListener(msg);
+    }
+
     protected void callMessageListener(Message<T> msg) {
         try {
-            if (log.isDebugEnabled()) {

Review Comment:
   the fix is in `isValidConsumerEpoch` where it increases permits if message is discarded due invalid epoch time and if listener is configured.



-- 
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] [fix][client] Fix failover consumer-listener stuck with cumulative ack and epoch time [pulsar]

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


##########
pulsar-broker/src/test/java/org/apache/pulsar/client/impl/MessageRedeliveryTest.java:
##########
@@ -539,4 +542,45 @@ public void testMultiConsumerBatchRedeliveryAddEpoch(boolean enableBatch) throws
         // can't receive message again
         assertEquals(consumer.batchReceive().size(), 0);
     }
+
+    /**
+     * This test validates that failover consumer with message listener keeps receiving messages even after filtering
+     * out epoch messages.
+     * @throws Exception
+     */
+    @Test
+    public void testRedeliveryWithListenerAddEpoch() throws Exception {
+        final String topic = "testRedeliveryListenerAddEpoch";
+        final String subName = "my-sub";
+        int totalMessages = 100;
+
+        Set<MessageId> ids = new HashSet<>();
+        CountDownLatch latch = new CountDownLatch(totalMessages);
+        MessageListener<String> msgListener = (Consumer<String> consumer, Message<String> msg) -> {
+            String id = msg.getMessageId().toString();
+            consumer.acknowledgeCumulativeAsync(msg);
+            // increase epoch to make next message invalid and filter out for the next redelivery
+            long epoch = ((ConsumerBase) consumer).getConsumerEpoch() + 1;
+            ((ConsumerBase) consumer).setConsumerEpoch(epoch);
+            if (ids.add(msg.getMessageId())) {
+                // count unique messages
+                latch.countDown();
+            }
+            consumer.redeliverUnacknowledgedMessages();
+        };
+        ConsumerBase<String> consumer = ((ConsumerBase<String>) pulsarClient.newConsumer(Schema.STRING).topic(topic)
+                .subscriptionName(subName).messageListener(msgListener).subscriptionType(SubscriptionType.Failover)
+                .receiverQueueSize(totalMessages / 10).subscribe());
+
+        Producer<String> producer = pulsarClient.newProducer(Schema.STRING).topic(topic).enableBatching(false)
+                .create();
+
+        for (int i = 0; i < totalMessages; i++) {
+            producer.sendAsync("test" + i);
+        }
+        producer.flush();
+
+        latch.await(60, TimeUnit.SECONDS);

Review Comment:
   close the `producer` and `consumer`



-- 
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] [fix][client] Fix failover consumer-listener stuck with cumulative ack and epoch time [pulsar]

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


##########
pulsar-broker/src/test/java/org/apache/pulsar/client/impl/MessageRedeliveryTest.java:
##########
@@ -539,4 +542,45 @@ public void testMultiConsumerBatchRedeliveryAddEpoch(boolean enableBatch) throws
         // can't receive message again
         assertEquals(consumer.batchReceive().size(), 0);
     }
+
+    /**
+     * This test validates that failover consumer with message listener keeps receiving messages even after filtering
+     * out epoch messages.
+     * @throws Exception
+     */
+    @Test
+    public void testRedeliveryWithListenerAddEpoch() throws Exception {
+        final String topic = "testRedeliveryListenerAddEpoch";
+        final String subName = "my-sub";
+        int totalMessages = 100;
+
+        Set<MessageId> ids = new HashSet<>();
+        CountDownLatch latch = new CountDownLatch(totalMessages);
+        MessageListener<String> msgListener = (Consumer<String> consumer, Message<String> msg) -> {
+            String id = msg.getMessageId().toString();
+            consumer.acknowledgeCumulativeAsync(msg);
+            // increase epoch to make next message invalid and filter out for the next redelivery
+            long epoch = ((ConsumerBase) consumer).getConsumerEpoch() + 1;
+            ((ConsumerBase) consumer).setConsumerEpoch(epoch);
+            if (ids.add(msg.getMessageId())) {
+                // count unique messages
+                latch.countDown();
+            }
+            consumer.redeliverUnacknowledgedMessages();
+        };
+        ConsumerBase<String> consumer = ((ConsumerBase<String>) pulsarClient.newConsumer(Schema.STRING).topic(topic)
+                .subscriptionName(subName).messageListener(msgListener).subscriptionType(SubscriptionType.Failover)
+                .receiverQueueSize(totalMessages / 10).subscribe());
+
+        Producer<String> producer = pulsarClient.newProducer(Schema.STRING).topic(topic).enableBatching(false)
+                .create();
+
+        for (int i = 0; i < totalMessages; i++) {
+            producer.sendAsync("test" + i);
+        }
+        producer.flush();
+
+        latch.await(60, TimeUnit.SECONDS);

Review Comment:
   added `@Cleanup` annotation.



-- 
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] [fix][client] Fix failover consumer-listener stuck with cumulative ack and epoch time [pulsar]

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


##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerBase.java:
##########
@@ -1161,25 +1161,16 @@ private void executeKeySharedMessageListener(Message<?> message, Runnable runnab
         executorProvider.getExecutor(peekMessageKey(message)).execute(runnable);
     }
 
+    protected void callMessageListenerAndIncreasePermits(Message<T> msg) {
+        if (log.isDebugEnabled()) {
+            log.debug("[{}][{}] Calling message listener for message {}", topic, subscription, msg.getMessageId());
+        }
+        increasePermits(msg);
+        callMessageListener(msg);
+    }
+
     protected void callMessageListener(Message<T> msg) {
         try {
-            if (log.isDebugEnabled()) {

Review Comment:
   it increases permits if message is discarded and listener is configured.



-- 
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] [fix][client] Fix failover consumer-listener stuck with cumulative ack and epoch time [pulsar]

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

   ## [Codecov](https://app.codecov.io/gh/apache/pulsar/pull/23345?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   Attention: Patch coverage is `76.47059%` with `4 lines` in your changes missing coverage. Please review.
   > Project coverage is 74.57%. Comparing base [(`bbc6224`)](https://app.codecov.io/gh/apache/pulsar/commit/bbc62245c5ddba1de4b1e7cee4ab49334bc36277?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) to head [(`2c88663`)](https://app.codecov.io/gh/apache/pulsar/commit/2c88663e7ede72bd392f242fd480fc908c4ee71e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   > Report is 603 commits behind head on master.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/pulsar/pull/23345?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [...va/org/apache/pulsar/client/impl/ConsumerBase.java](https://app.codecov.io/gh/apache/pulsar/pull/23345?src=pr&el=tree&filepath=pulsar-client%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpulsar%2Fclient%2Fimpl%2FConsumerBase.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cHVsc2FyLWNsaWVudC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2NsaWVudC9pbXBsL0NvbnN1bWVyQmFzZS5qYXZh) | 86.66% | [1 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/pulsar/pull/23345?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [...va/org/apache/pulsar/client/impl/ConsumerImpl.java](https://app.codecov.io/gh/apache/pulsar/pull/23345?src=pr&el=tree&filepath=pulsar-client%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpulsar%2Fclient%2Fimpl%2FConsumerImpl.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cHVsc2FyLWNsaWVudC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2NsaWVudC9pbXBsL0NvbnN1bWVySW1wbC5qYXZh) | 0.00% | [1 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/pulsar/pull/23345?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   [![Impacted file tree graph](https://app.codecov.io/gh/apache/pulsar/pull/23345/graphs/tree.svg?width=650&height=150&src=pr&token=acYqCpsK9J&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)](https://app.codecov.io/gh/apache/pulsar/pull/23345?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   
   ```diff
   @@             Coverage Diff              @@
   ##             master   #23345      +/-   ##
   ============================================
   + Coverage     73.57%   74.57%   +0.99%     
   - Complexity    32624    33923    +1299     
   ============================================
     Files          1877     1934      +57     
     Lines        139502   144986    +5484     
     Branches      15299    15839     +540     
   ============================================
   + Hits         102638   108118    +5480     
   + Misses        28908    28597     -311     
   - Partials       7956     8271     +315     
   ```
   
   | [Flag](https://app.codecov.io/gh/apache/pulsar/pull/23345/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [inttests](https://app.codecov.io/gh/apache/pulsar/pull/23345/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `27.60% <0.00%> (+3.01%)` | :arrow_up: |
   | [systests](https://app.codecov.io/gh/apache/pulsar/pull/23345/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `24.55% <52.94%> (+0.23%)` | :arrow_up: |
   | [unittests](https://app.codecov.io/gh/apache/pulsar/pull/23345/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `73.91% <76.47%> (+1.06%)` | :arrow_up: |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/pulsar/pull/23345?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [...va/org/apache/pulsar/client/impl/ConsumerBase.java](https://app.codecov.io/gh/apache/pulsar/pull/23345?src=pr&el=tree&filepath=pulsar-client%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpulsar%2Fclient%2Fimpl%2FConsumerBase.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cHVsc2FyLWNsaWVudC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2NsaWVudC9pbXBsL0NvbnN1bWVyQmFzZS5qYXZh) | `75.30% <86.66%> (+1.17%)` | :arrow_up: |
   | [...va/org/apache/pulsar/client/impl/ConsumerImpl.java](https://app.codecov.io/gh/apache/pulsar/pull/23345?src=pr&el=tree&filepath=pulsar-client%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpulsar%2Fclient%2Fimpl%2FConsumerImpl.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cHVsc2FyLWNsaWVudC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2NsaWVudC9pbXBsL0NvbnN1bWVySW1wbC5qYXZh) | `79.85% <0.00%> (+2.28%)` | :arrow_up: |
   
   ... and [596 files with indirect coverage changes](https://app.codecov.io/gh/apache/pulsar/pull/23345/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   
   </details>


-- 
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] [fix][client] Fix failover consumer-listener stuck with cumulative ack and epoch time [pulsar]

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


##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerBase.java:
##########
@@ -1161,25 +1161,16 @@ private void executeKeySharedMessageListener(Message<?> message, Runnable runnab
         executorProvider.getExecutor(peekMessageKey(message)).execute(runnable);
     }
 
+    protected void callMessageListenerAndIncreasePermits(Message<T> msg) {
+        if (log.isDebugEnabled()) {
+            log.debug("[{}][{}] Calling message listener for message {}", topic, subscription, msg.getMessageId());
+        }
+        increasePermits(msg);
+        callMessageListener(msg);
+    }
+
     protected void callMessageListener(Message<T> msg) {
         try {
-            if (log.isDebugEnabled()) {

Review Comment:
   What's the main fix there? only split this method into `increasePermits` and `callMessageListener`?



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