Skip to content

Shared subscription + Batch messages: consumerFlow trigger reading the same messages from storage #16421

Closed

Description

@eolivelli

I am doing some testing on Shared subscription and Batch messages with the current Pulsar master.

The behaviour that I am observing is that when you have Batch messages the Consumer is sending flow control messages for more messages that it can handle.

This is how to reproduce the problem:

  • write 100.000 messages using batching
  • start a Consumer with a Shared subscription (from the beginning of the topic)
  • you will see that the PersistentDispatcherMultipleConsumers consumerFlow trigger the read of many messages

This is happening because consumerFlow calls readMoreEntries() and readMoreEntries() sees that there are messages to be re-delivered, because the consumer still haven't acknowledged them.

This is turn requests the ManagedCursor to read the data from storage.

I have observed this behaviour while working on offloader performances, but it also happens with regular BK based ledgers.

This simple test case reproduces the problem, I append it to this test

public class CreateSubscriptionTest extends ProducerConsumerBase {

  @Test
   public void testConsumerFlowOnSharedSubscription() throws Exception {
       String topic = "persistent://my-property/my-ns/topic" + UUID.randomUUID();
       admin.topics().createNonPartitionedTopic(topic);
       String subName = "my-sub";
       int numMessages = 20_000;
       final CountDownLatch count = new CountDownLatch(numMessages);
       try (Consumer<byte[]> consumer = pulsarClient.newConsumer()
               .subscriptionMode(SubscriptionMode.Durable)
               .subscriptionType(SubscriptionType.Shared)
               .topic(topic)
               .subscriptionName(subName)
               .messageListener(new MessageListener<byte[]>() {
                   @Override
                   public void received(Consumer<byte[]> consumer, Message<byte[]> msg) {
                       //log.info("received {} - {}", msg, count.getCount());
                       consumer.acknowledgeAsync(msg);
                       count.countDown();
                   }
               })
               .subscribe();
            Producer<byte[]> producer = pulsarClient
               .newProducer()
               .blockIfQueueFull(true)
               .enableBatching(true)
               .topic(topic)
                    .create()) {
           consumer.pause();
           byte[] message = "foo".getBytes(StandardCharsets.UTF_8);
           List<CompletableFuture<?>> futures = new ArrayList<>();
           for (int i = 0; i < numMessages; i++) {
               futures.add(producer.sendAsync(message).whenComplete( (id,e) -> {
                   if (e != null) {
                       log.error("error", e);
                   }
               }));
               if (futures.size() == 1000) {
                   FutureUtil.waitForAll(futures).get();
                   futures.clear();
               }
           }
           producer.flush();
           consumer.resume();
           assertTrue(count.await(20, TimeUnit.SECONDS));
       }

   }

Activity

  1. added
    type/bug
    on Jul 6, 2022
  2. eolivelli commented on Jul 6, 2022

    @eolivelli
    ContributorAuthor

    The problem is here, when you use MessageListener we increase permits of 1, even for Messages that are part of a batch.

    protected void callMessageListener(Message<T> msg) {

    So the problem is mostly with the MessageListener API, used by pulsar-perf (and also OpenMessaging benchmark)

  3. eolivelli commented on Jul 7, 2022

    @eolivelli
    ContributorAuthor

    with batch messages we fall into this case:

    basically the dispatcher reads N entries from Storage (BK or Offloader) and the Consumer does not have enough permits (because we sent entries with multiple messages), so we are throwing away part of the entries that we read.

    Those entries are added to the list of "messages to replay" and so they will be read again from storage next time.

  4. eolivelli commented on Jul 7, 2022

    @eolivelli
    ContributorAuthor

    After doing some testing I have found that the problem is that the ManagedLedger EntryCache is evicted only by taking into account the "readPosition" of the cursor.
    But the "messages to replay" are behind the "readPosition", so those entries are evicted from the cache

  5. eolivelli commented on Jul 7, 2022

    @eolivelli
    ContributorAuthor

    In order to solve the problem we should not evict from the cache the entries that are still in "messagesToReply", otherwise we will keep reading from BK (or from TieredStorage).

  6. eolivelli commented on Jul 7, 2022

    @eolivelli
    ContributorAuthor

    I believe that this implementation about cacheEvictionByMarkDeletedPosition will also help with this problem, because it won't let the "messages to replay" to be evicted from the cache.

    #14985

  7. github-actions commented on Aug 7, 2022

    @github-actions

    The issue had no activity for 30 days, mark with Stale label.

  8. added
    triage/lhotari/important
    on Nov 5, 2024
  9. self-assigned this
    on Jan 28, 2025
  10. lhotari commented on Jan 28, 2025

    @lhotari
    Member

    There's currently an open issue #23900 where the broker cache is always by-passed for replay queue reads, also when cacheEvictionByMarkDeletedPosition=true is used. That's addressed by #23903 . The limitation and issue described in this issue (#16421), remains. I have work in progress to address this issue.

  11. lhotari commented on Jun 23, 2025

    @lhotari
    Member

    "PIP-430: Pulsar Broker cache improvements: refactoring eviction and adding a new cache strategy based on expected read count", #24444 will finally address this issue.

  12. lhotari commented on Aug 27, 2025

    @lhotari
    Member

    PIP-430 will address this. One potential workaround is to use preciseDispatcherFlowControl=true (introduced in #6719) which will reduce the amount of unnecessary reads when batch messages are used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

triage/lhotari/importanttype/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions