Repository navigation
Closed
Description
Describe the bug
When use Key_Shared, if one consumer consume slowly and others are normal, MessageRedeliveryController#messagesToRedeliver will keep growing and could be very large.
MessageRedeliveryController#getMessagesToReplayNow will be very expensive cause messagesToRedeliver.items() is a O(n) operation in both time and space complexity.
Lines 109 to 122 in 2b2e0c5
| public Set<PositionImpl> getMessagesToReplayNow(int maxMessagesToRead) { | |
| if (hashesToBeBlocked != null) { | |
| // allowOutOfOrderDelivery is false | |
| return messagesToRedeliver.items().stream() | |
| .sorted((l1, l2) -> ComparisonChain.start().compare(l1.first, l2.first) | |
| .compare(l1.second, l2.second).result()) | |
| .limit(maxMessagesToRead).map(longPair -> new PositionImpl(longPair.first, longPair.second)) | |
| .collect(Collectors.toCollection(TreeSet::new)); | |
| } else { | |
| // allowOutOfOrderDelivery is true | |
| return messagesToRedeliver.items(maxMessagesToRead, | |
| (ledgerId, entryId) -> new PositionImpl(ledgerId, entryId)); | |
| } | |
| } |
In this situation, one thread cpu usage could be 100%, this could effects the publish latency of other topics on the same broker instance.
To Reproduce
Steps to reproduce the behavior:
- start a standalone cluster
- start a producer
$ bin/pulsar-perf produce -mk random persistent://public/default/test- start a slow consumer and a normal consumer
$ bin/pulsar-perf consume -st Key_Shared persistent://public/default/test
$ bin/pulsar-perf consume -st Key_Shared -r 0.1 persistent://public/default/testAdditional context
- pulsar:
2.8.1

Activity
Shawyeok commentedon May 5, 2022
Additional context:
The slow consumer will ends with zero availablePermits, and message positions will be recorded in
MessageRedeliveryController#messagesToRedeliver, when none of the whole batch of messages sent to consumer,isDispatcherStuckOnReplayswill be marked.pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentStickyKeyDispatcherMultipleConsumers.java
Lines 310 to 330 in 2b2e0c5
Next time call
getMessagesToReplayNowin readMoreEntries, it'll returns empty, then it'll read next batch of messages by cursor, therefor more message positions will be recorded.pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentStickyKeyDispatcherMultipleConsumers.java
Lines 436 to 446 in 2b2e0c5
pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentStickyKeyDispatcherMultipleConsumers.java
Lines 310 to 330 in 2b2e0c5
merlimat commentedon May 5, 2022
@Shawyeok Can you attach the flame graph directly?
Shawyeok commentedon May 6, 2022
@merlimat cpu_profile.html.zip attached
surahman commentedon May 11, 2022
This is a very interesting issue to examine.
I think the time bound is likely
O(n*log(n))due to theTreeSetgeneration (sorting) here:pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageRedeliveryController.java
Line 116 in 2b2e0c5
Edit:
We sort here to get the
limitof items (top few?) in the collection:pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageRedeliveryController.java
Lines 113 to 115 in 2b2e0c5
We then generate a
TreeSet, which is anLLRB Tree, with a sorted list. This will rebalance the tree during each insert (expensive):pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageRedeliveryController.java
Line 116 in 2b2e0c5
I know there is typically an order guarantee within, but not across, partitions of topics in pub-sub systems. If there is no need for an ordered iteration through the set it might be possible to squeeze a little bit of performance out of using a
HashSet. You would need a custom comparator and equality implementation for the data type being inserted.github-actions commentedon Jun 12, 2022
The issue had no activity for 30 days, mark with Stale label.
zhanghaou commentedon Dec 5, 2022
We also encounter such problem,topics in the same broker publish latency is very high. Any suggestions? @merlimat @codelipenghui
codelipenghui commentedon Dec 5, 2022
The issue should be fixed by #17804
@zhanghaou @Shawyeok
zhanghaou commentedon Dec 5, 2022
@codelipenghui Does the fix pr can be cherry pick to branch 2.8.X?
codelipenghui commentedon Dec 5, 2022
@zhanghaou We are only applying security patches for 2.8.x release for now.
zhanghaou commentedon Dec 5, 2022
@codelipenghui OK, thanks.