@@ -112,6 +112,18 @@ public class Consumer { |
112 | 112 | private static final AtomicIntegerFieldUpdater<Consumer> MESSAGE_PERMITS_UPDATER = |
113 | 113 | AtomicIntegerFieldUpdater.newUpdater(Consumer.class, "messagePermits"); |
114 | 114 | private volatile int messagePermits = 0; |
| 115 | + /** |
| 116 | + * Guards the Flow-side compound update of {@link #messagePermits} and |
| 117 | + * {@link #pendingDispatcherFlowPermits}. A Flow command increases the consumer permits before the dispatcher |
| 118 | + * processes the corresponding update asynchronously. Consumer removal can happen between those two operations, |
| 119 | + * so both values must be observed consistently when calculating how many permits are already included in the |
| 120 | + * dispatcher total. |
| 121 | + * |
| 122 | + * <p>The dispatcher callback is invoked only after this lock is released. This avoids holding the lock while |
| 123 | + * calling into the subscription and preserves the lock order used by dispatcher flow processing and removal. |
| 124 | + */ |
| 125 | + private final Object flowPermitAccountingLock = new Object(); |
| 126 | + private int pendingDispatcherFlowPermits = 0; |
115 | 127 | // It starts keep tracking of messagePermits once consumer gets blocked, as consumer needs two separate counts: |
116 | 128 | // messagePermits (1) before and (2) after being blocked: to dispatch only blockedPermit number of messages at the |
117 | 129 | // time of redelivery |
@@ -840,7 +852,7 @@ public void flowPermits(int additionalNumberOfMessages) { |
840 | 852 | } |
841 | 853 | int oldPermits; |
842 | 854 | if (!blockedConsumerOnUnackedMsgs) { |
843 | | - oldPermits = MESSAGE_PERMITS_UPDATER.getAndAdd(this, additionalNumberOfMessages); |
| 855 | + oldPermits = addPermitsPendingDispatcherUpdate(additionalNumberOfMessages); |
844 | 856 | if (log.isDebugEnabled()) { |
845 | 857 | log.debug("[{}-{}] Added {} message permits in broker.service.Consumer before updating dispatcher " |
846 | 858 | + "for consumer {}", topicName, subscription, additionalNumberOfMessages, consumerId); |
@@ -867,7 +879,7 @@ public void flowPermits(int additionalNumberOfMessages) { |
867 | 879 | void flowConsumerBlockedPermits(Consumer consumer) { |
868 | 880 | int additionalNumberOfPermits = PERMITS_RECEIVED_WHILE_CONSUMER_BLOCKED_UPDATER.getAndSet(consumer, 0); |
869 | 881 | // add newly flow permits to actual consumer.messagePermits |
870 | | - MESSAGE_PERMITS_UPDATER.getAndAdd(consumer, additionalNumberOfPermits); |
| 882 | + consumer.addPermitsPendingDispatcherUpdate(additionalNumberOfPermits); |
871 | 883 | if (log.isDebugEnabled()){ |
872 | 884 | log.debug("[{}-{}] Added {} blocked permits to broker.service.Consumer for consumer {}", topicName, |
873 | 885 | subscription, additionalNumberOfPermits, consumerId); |
@@ -880,6 +892,57 @@ public int getAvailablePermits() { |
880 | 892 | return MESSAGE_PERMITS_UPDATER.get(this); |
881 | 893 | } |
882 | 894 |
|
| 895 | + /** |
| 896 | + * Adds permits after a Flow command is accepted and immediately before notifying the dispatcher. The pending |
| 897 | + * count covers the interval until the dispatcher's asynchronous Flow task starts processing the same permits. |
| 898 | + */ |
| 899 | + private int addPermitsPendingDispatcherUpdate(int additionalNumberOfPermits) { |
| 900 | + if (!shouldTrackPendingDispatcherFlowPermits()) { |
| 901 | + return MESSAGE_PERMITS_UPDATER.getAndAdd(this, additionalNumberOfPermits); |
| 902 | + } |
| 903 | + synchronized (flowPermitAccountingLock) { |
| 904 | + pendingDispatcherFlowPermits += additionalNumberOfPermits; |
| 905 | + return MESSAGE_PERMITS_UPDATER.getAndAdd(this, additionalNumberOfPermits); |
| 906 | + } |
| 907 | + } |
| 908 | + |
| 909 | + private boolean shouldTrackPendingDispatcherFlowPermits() { |
| 910 | + return isPersistentTopic && Subscription.isIndividualAckMode(subType); |
| 911 | + } |
| 912 | + |
| 913 | + /** |
| 914 | + * Called at the start of the dispatcher's asynchronous Flow task, before checking whether this consumer is still |
| 915 | + * connected. At this point the Flow update is no longer pending: the dispatcher will either add the permits to |
| 916 | + * its total or ignore them because the consumer has already been removed. |
| 917 | + */ |
| 918 | + public void completePendingDispatcherFlow(int additionalNumberOfPermits) { |
| 919 | + synchronized (flowPermitAccountingLock) { |
| 920 | + // Preserve the accounting delta across signed int wrap, matching the other permit counters. |
| 921 | + pendingDispatcherFlowPermits -= additionalNumberOfPermits; |
| 922 | + } |
| 923 | + } |
| 924 | + |
| 925 | + /** |
| 926 | + * Called while the dispatcher removes this consumer. Permits belonging to Flow tasks that have not started yet |
| 927 | + * are excluded because those permits have not been added to the dispatcher total and must not be subtracted from |
| 928 | + * it during removal. |
| 929 | + * |
| 930 | + * <p>This accounting is enabled for persistent Shared and Key_Shared dispatchers. It relies on every dispatcher |
| 931 | + * Flow task calling {@link #completePendingDispatcherFlow(int)} before applying or ignoring the update. For these |
| 932 | + * dispatchers, when observed under the dispatcher monitor, the total available permits equal the sum of this |
| 933 | + * balance over all connected consumers. |
| 934 | + * |
| 935 | + * <p>The returned balance can be negative. A pending Flow makes permits visible on the consumer before its |
| 936 | + * asynchronous dispatcher update runs, so the dispatcher can consume those permits while they are still counted |
| 937 | + * as pending. Subtracting the negative balance during removal is required to restore the dispatcher total; callers |
| 938 | + * must not clamp it to zero. |
| 939 | + */ |
| 940 | + public int getAvailablePermitsForDispatcherRemoval() { |
| 941 | + synchronized (flowPermitAccountingLock) { |
| 942 | + return MESSAGE_PERMITS_UPDATER.get(this) - pendingDispatcherFlowPermits; |
| 943 | + } |
| 944 | + } |
| 945 | + |
883 | 946 | /** |
884 | 947 | * return 0 if there is no entry dispatched yet. |
885 | 948 | */ |
|