Repository navigation
Description
Search before reporting
- I searched in the issues and found nothing similar.
Read release policy
- I understand that unsupported versions don't get bug fixes. I reproduced the issue on the
masterbranch.
User environment
- Version:
master(b63a1f2c4e3). The code involved predates [fix][client] Fix message leaks and size and permit accounting races when clearing the consumer's incoming queue #21767. - Component: Java client,
MultiTopicsConsumerImplwith aMessageListener.
Issue Description
When a multi-topic or partitioned consumer has a message listener, the child ConsumerImpl instances are created with parentConsumerHasListener=true. When a message moves from a child's queue to the parent's queue, ConsumerImpl.messageProcessed() doesn't give the flow permit back. The parent gives it back later, in ConsumerBase.callMessageListener(), through receivedConsumer.increaseAvailablePermits(...), just before the listener runs.
The permit therefore goes back only if the message reaches callMessageListener(). If the parent removes the message from its own queue first, the permit is lost:
MultiTopicsConsumerImpl.redeliverUnacknowledgedMessages()callsconsumer.redeliverUnacknowledgedMessages()on each child, thenclearIncomingMessages()on the parent. Each child gives back permits only for the messages in its own queue (increaseAvailablePermits(cnx, currentSize)). The messages already in the parent's queue are released, and their permits are never given back. Exclusive and Failover subscriptions also use this path for ack-timeout and negative-ack redelivery:redeliverUnacknowledgedMessages(Set)falls back to it for subscription types other than Shared and Key_Shared.MultiTopicsConsumerImpl.removeExpiredMessagesFromQueue(), used by the Shared/Key_SharedredeliverUnacknowledgedMessages(Set), discards matching messages from the parent queue the same way. The child'sremoveExpiredMessagesFromQueue()only counts and gives back permits for messages in the child's own queue.
Without a listener, this doesn't happen: the child gives the permit back in messageProcessed() when the message moves to the parent.
Impact
Each redelivery can lose up to one permit for every message that was waiting in the parent queue, which can be up to the receiver queue size. Messages wait in the parent queue whenever the listener runs slower than messages arrive. The lost permits stay lost until the child reconnects, because a reconnect resets availablePermits and sends a full flow. With repeated redeliveries, such as ack timeouts or negative acks on a slow listener, the broker-side permits for a child can drop to zero. That topic or partition then stops delivering until a reconnect.
beforeSeek() also clears the parent queue, but a seek disconnects and resubscribes the child consumers, which resets their permits, so the seek path isn't affected.
Error messages
None. The consumer stops receiving messages from the affected topic or partition.
Reproducing the issue
- Subscribe a multi-topic or partitioned consumer with a
MessageListenerthat processes messages slowly (for example, it sleeps), so that messages build up in the parentincomingMessagesqueue. Use a smallreceiverQueueSizeand an Exclusive or Failover subscription, with an ack timeout or negative acks that never get acknowledged. - Publish more messages than the receiver queue size.
- Let several redeliveries run while the parent queue has messages in it.
- Watch the child consumers'
availablePermits, or the broker-side consumeravailablePermitsin topic stats. They go down with each redelivery and don't recover, and delivery can stop until a reconnect.
Additional information
This came up in a review of #21767 (review comment). That PR changes how incoming queues are cleared, but it doesn't change this permit handling.
A possible fix: when the parent discards a TopicMessageImpl from its queue while listener != null, give its permit back to receivedByconsumer with increaseAvailablePermits(MessageImpl), which already ignores messages from an old connection. Do this in both clearIncomingMessages() on the redelivery path and removeExpiredMessagesFromQueue().
Are you willing to submit a PR?
- I'm willing to submit a PR!
Activity