GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][broker] Inline Shared dispatcher flow accounting · void-ptr974/pulsar@cf57407

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pulsar-broker/src
    • main/java/org/apache/pulsar/broker/service
      • Consumer.java
      • persistent
        • PersistentDispatcherMultipleConsumers.java
        • PersistentDispatcherMultipleConsumersClassic.java
    • test/java/org/apache/pulsar/broker/service/persistent
      • SharedDispatcherPermitAccountingTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Consumer.java‎

Copy file name to clipboardExpand all lines: pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Consumer.java
+17-16Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ public class Consumer {
119119
private volatile int messagePermits = 0;
120120
/**
121121
* Guards the Flow-side compound update of {@link #messagePermits} and
122-
* {@link #pendingDispatcherFlowPermits}. A Flow command increases the consumer permits before the dispatcher
123-
* processes the corresponding update asynchronously. Consumer removal can happen between those two operations,
124-
* so both values must be observed consistently when calculating how many permits are already included in the
125-
* dispatcher total.
122+
* {@link #pendingDispatcherFlowPermits}. A Flow command increases the consumer permits before entering the
123+
* dispatcher. Consumer removal can happen while the Flow caller is waiting for the dispatcher monitor, so both
124+
* values must be observed consistently when calculating how many permits are already included in the dispatcher
125+
* total.
126126
*
127127
* <p>The dispatcher callback is invoked only after this lock is released. This avoids holding the lock while
128128
* calling into the subscription and preserves the lock order used by dispatcher flow processing and removal.
@@ -969,7 +969,8 @@ public int getAvailablePermits() {
969969

970970
/**
971971
* Adds permits after a Flow command is accepted and immediately before notifying the dispatcher. The pending
972-
* count covers the interval until the dispatcher's asynchronous Flow task starts processing the same permits.
972+
* count covers the interval until the dispatcher starts processing the same permits, including time spent waiting
973+
* for the dispatcher monitor.
973974
*/
974975
private int addPermitsPendingDispatcherUpdate(int additionalNumberOfPermits) {
975976
if (!shouldTrackPendingDispatcherFlowPermits()) {
@@ -986,9 +987,9 @@ private boolean shouldTrackPendingDispatcherFlowPermits() {
986987
}
987988

988989
/**
989-
* Called at the start of the dispatcher's asynchronous Flow task, before checking whether this consumer is still
990-
* connected. At this point the Flow update is no longer pending: the dispatcher will either add the permits to
991-
* its total or ignore them because the consumer has already been removed.
990+
* Called at the start of dispatcher Flow processing, before checking whether this consumer is still connected. At
991+
* this point the Flow update is no longer pending: the dispatcher will either add the permits to its total or
992+
* ignore them because the consumer has already been removed.
992993
*/
993994
public void completePendingDispatcherFlow(int additionalNumberOfPermits) {
994995
synchronized (flowPermitAccountingLock) {
@@ -998,19 +999,19 @@ public void completePendingDispatcherFlow(int additionalNumberOfPermits) {
998999
}
9991000

10001001
/**
1001-
* Called while the dispatcher removes this consumer. Permits belonging to Flow tasks that have not started yet
1002-
* are excluded because those permits have not been added to the dispatcher total and must not be subtracted from
1003-
* it during removal.
1002+
* Called while the dispatcher removes this consumer. Permits belonging to Flow updates that the dispatcher has
1003+
* not started processing are excluded because those permits have not been added to the dispatcher total and must
1004+
* not be subtracted from it during removal.
10041005
*
1005-
* <p>This accounting is enabled for persistent Shared and Key_Shared dispatchers. It relies on every dispatcher
1006-
* Flow task calling {@link #completePendingDispatcherFlow(int)} before applying or ignoring the update. For these
1006+
* <p>This accounting is enabled for persistent Shared and Key_Shared dispatchers. It requires every dispatcher
1007+
* Flow update to call {@link #completePendingDispatcherFlow(int)} before applying or ignoring it. For these
10071008
* dispatchers, when observed under the dispatcher monitor, the total available permits equal the sum of this
10081009
* balance over all connected consumers.
10091010
*
10101011
* <p>The returned balance can be negative. A pending Flow makes permits visible on the consumer before its
1011-
* asynchronous dispatcher update runs, so the dispatcher can consume those permits while they are still counted
1012-
* as pending. Subtracting the negative balance during removal is required to restore the dispatcher total; callers
1013-
* must not clamp it to zero.
1012+
* dispatcher update runs, so the dispatcher can consume those permits while they are still counted as pending.
1013+
* Subtracting the negative balance during removal is required to restore the dispatcher total; callers must not
1014+
* clamp it to zero.
10141015
*/
10151016
public int getAvailablePermitsForDispatcherRemoval() {
10161017
synchronized (flowPermitAccountingLock) {
Collapse file

‎pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumers.java‎

Copy file name to clipboardExpand all lines: pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumers.java
+12-11Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -303,26 +303,27 @@ protected synchronized void clearComponentsAfterRemovedAllConsumers() {
303303

304304
@Override
305305
public void consumerFlow(Consumer consumer, int additionalNumberOfMessages) {
306-
topic.getBrokerService().executor().execute(() -> {
307-
internalConsumerFlow(consumer, additionalNumberOfMessages);
308-
});
309-
}
306+
boolean connected;
307+
int updatedTotalAvailablePermits = 0;
308+
synchronized (this) {
309+
consumer.completePendingDispatcherFlow(additionalNumberOfMessages);
310+
connected = consumerSet.contains(consumer);
311+
if (connected) {
312+
totalAvailablePermits += additionalNumberOfMessages;
313+
updatedTotalAvailablePermits = totalAvailablePermits;
314+
}
315+
}
310316

311-
private synchronized void internalConsumerFlow(Consumer consumer, int additionalNumberOfMessages) {
312-
// The queued Flow task is no longer pending, even if the consumer was removed while the task was waiting.
313-
consumer.completePendingDispatcherFlow(additionalNumberOfMessages);
314-
if (!consumerSet.contains(consumer)) {
317+
if (!connected) {
315318
log.debug()
316319
.attr("consumer", consumer)
317320
.log("Ignoring flow control from disconnected consumer");
318321
return;
319322
}
320323

321-
totalAvailablePermits += additionalNumberOfMessages;
322-
323324
log.debug()
324325
.attr("consumer", consumer)
325-
.attr("totalAvailablePermits", totalAvailablePermits)
326+
.attr("totalAvailablePermits", updatedTotalAvailablePermits)
326327
.attr("additionalNumberOfMessages", additionalNumberOfMessages)
327328
.log("Trigger new read after receiving flow control message");
328329
readMoreEntriesAsync();
Collapse file

‎pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumersClassic.java‎

Copy file name to clipboardExpand all lines: pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumersClassic.java
+14-12Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -283,35 +283,37 @@ private synchronized void clearComponentsAfterRemovedAllConsumers() {
283283

284284
@Override
285285
public void consumerFlow(Consumer consumer, int additionalNumberOfMessages) {
286-
topic.getBrokerService().executor().execute(() -> {
287-
internalConsumerFlow(consumer, additionalNumberOfMessages);
288-
});
289-
}
286+
boolean connected;
287+
int updatedTotalAvailablePermits = 0;
288+
synchronized (this) {
289+
consumer.completePendingDispatcherFlow(additionalNumberOfMessages);
290+
connected = consumerSet.contains(consumer);
291+
if (connected) {
292+
totalAvailablePermits += additionalNumberOfMessages;
293+
updatedTotalAvailablePermits = totalAvailablePermits;
294+
}
295+
}
290296

291-
private synchronized void internalConsumerFlow(Consumer consumer, int additionalNumberOfMessages) {
292-
// The queued Flow task is no longer pending, even if the consumer was removed while the task was waiting.
293-
consumer.completePendingDispatcherFlow(additionalNumberOfMessages);
294-
if (!consumerSet.contains(consumer)) {
297+
if (!connected) {
295298
log.debug()
296299
.attr("consumer", consumer)
297300
.log("Ignoring flow control from disconnected consumer");
298301
return;
299302
}
300303

301-
totalAvailablePermits += additionalNumberOfMessages;
302-
303304
log.debug()
304305
.attr("consumer", consumer)
305-
.attr("totalAvailablePermits", totalAvailablePermits)
306+
.attr("totalAvailablePermits", updatedTotalAvailablePermits)
306307
.attr("additionalNumberOfMessages", additionalNumberOfMessages)
307308
.log("- Trigger new read after receiving flow control message with permits " + "after adding permits");
308-
readMoreEntries();
309+
readMoreEntriesAsync();
309310
}
310311

311312
/**
312313
* We should not call readMoreEntries() recursively in the same thread as there is a risk of StackOverflowError.
313314
*
314315
*/
316+
@Override
315317
public void readMoreEntriesAsync() {
316318
topic.getBrokerService().executor().execute(this::readMoreEntries);
317319
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.