Repository navigation
Description
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
consumerFlowtrigger 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
pulsar/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/CreateSubscriptionTest.java
Line 66 in 1ba180c
| 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
eolivelli commentedon Jul 6, 2022
The problem is here, when you use MessageListener we increase permits of 1, even for Messages that are part of a batch.
pulsar/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerBase.java
Line 1087 in 926834e
So the problem is mostly with the MessageListener API, used by pulsar-perf (and also OpenMessaging benchmark)
eolivelli commentedon Jul 7, 2022
with batch messages we fall into this case:
pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumers.java
Line 625 in 8b1d7a0
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.
eolivelli commentedon Jul 7, 2022
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
eolivelli commentedon Jul 7, 2022
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).
eolivelli commentedon Jul 7, 2022
I believe that this implementation about
cacheEvictionByMarkDeletedPositionwill also help with this problem, because it won't let the "messages to replay" to be evicted from the cache.#14985
github-actions commentedon Aug 7, 2022
The issue had no activity for 30 days, mark with Stale label.
lhotari commentedon Jan 28, 2025
There's currently an open issue #23900 where the broker cache is always by-passed for replay queue reads, also when
cacheEvictionByMarkDeletedPosition=trueis 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.lhotari commentedon Jun 23, 2025
"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.
Improve test, fix tests and add test for issue apache#16421
lhotari commentedon Aug 27, 2025
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.