You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2021/10/21 22:56:57 UTC

[GitHub] [pulsar] michaeljmarshall opened a new pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

michaeljmarshall opened a new pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456


   ### Motivation
   
   @lhotari and I discovered a race condition in the `MultiTopicsConsumerImpl<T>` class. The race allows for messages to be delivered out of order.
   
   We discovered the bug using the following steps:
   
   1. Create a 100 partition topic.
   2. Produce to the topic at 50k messages per second.
   3. Consume from the topic using a single, exclusive consumer in a single thread.
   4. Observe a small percentage of out of order messages (on average 13 in 100,000).
   
   However, the race can happen with a single message and on a topic with a single partition.
   
   The race comes in these two code blocks:
   
   https://github.com/apache/pulsar/blob/7ad46c8c18bb8365c9a2d1233a6cd58ecd6f541f/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiTopicsConsumerImpl.java#L412-L416
   
   https://github.com/apache/pulsar/blob/7ad46c8c18bb8365c9a2d1233a6cd58ecd6f541f/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiTopicsConsumerImpl.java#L294-L301
   
   The first block is executed on the application's calling thread. The second block is executed in either the `internalPinnedExecutor` or the topic partition consumer's `internalPinnedExecutor` (these threads are not necessarily the same). As such, if the two blocks are called at the same time, it is possible for `incomingMessages.poll();` to return a `null` result while `nextPendingReceive();` also returns a `null` result. If this happens, the next state of the `MultiTopicsConsumerImpl` will be to have a single message in the `incomingMessages` queue and a single pending receive in the `pendingReceives` queue. Then, a message will deliver out of order.
   
   This proposed solution follows the paradigm used by @Vanlightly in https://github.com/apache/pulsar/pull/11691. Essentially, the places where we need to inspect both the `pendingReceives` and the `incomingMessages` queues must be updated from a single thread: `internalPinnedExecutor`.
   
   ### Modifications
   
   * Run the callback for `consumer.receiveAsync()` on the `internalPinnedExecutor`. I chose to run the whole callback on the `internalPinnedExecutor` instead of just the `messageReceived` method. If we left the callback using `thenAccept` and ran `messageReceived` on the `internalPinnedExecutor`, there is a chance that the callback will run on the calling thread, which is always the `internalPinnedExecutor`. That would mean that the `messageReceived` logic would actually run after the remaining callback logic that inspects the `incomingMessages.size()` and decides whether or not to pause the consumer. By scheduling the callback on the `internalPinnedExecutor` using `thenAcceptAsync`, we guarantee that the code is run together without the data race we're fixing in this PR.
   * Run `MultiTopicsConsumerImpl#internalReceiveAsync` on the `internalPinnedExecutor`.
   * Remove the `checkState(message instanceof TopicMessageImpl);` method call from the `MultiTopicsConsumerImpl#internalReceiveAsync` method. This decision may be controversial. I removed the check because we only ever add `TopicMessageImpl` to the `MultiTopicsConsumerImpl`'s `incomingMessages` queue. If it is a necessary check, we could complete the future exceptionally when the message is not of type `TopicMessageImpl`.
   
   ### Verifying this change
   
   Since this is a fix for a data race, it is hard to test the change. I think the change is small enough that we don't need to add new tests for it, but please let me know if you think otherwise.
   
   ### Does this pull request potentially affect one of the following parts:
   
   *If `yes` was chosen, please highlight the changes*
   
   * Dependencies (does it add or upgrade a dependency): (no)
   * The public API: (no)
   * The schema: (no)
   * The default values of configurations: (no)
   * The wire protocol: (no)
   * The rest endpoints: (no)
   * The admin cli options: (no)
   * Anything that affects deployment: (no)
   
   ### Documentation
   We should document this in release notes. No other docs need to be updated.
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] michaeljmarshall commented on pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#issuecomment-949690739


   @lhotari - thank you for the solution. I ran the test locally without then with your proposed fix, and it works as expected.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari commented on pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
lhotari commented on pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#issuecomment-949485195


   > There is a test failure related to this change
   > 
   > ```
   > 
   > org.apache.pulsar.client.impl.MultiTopicsConsumerImplTest.testReceiveAsyncCanBeCancelled(org.apache.pulsar.client.impl.MultiTopicsConsumerImplTest)
   > [INFO]   Run 1: PASS
   > Error:    Run 2: MultiTopicsConsumerImplTest.testReceiveAsyncCanBeCancelled:168 expected [true] but found [false]
   > ```
   
   @michaeljmarshall the test should be fixed. This line https://github.com/apache/pulsar/blob/bd942e1730780f89b78799ca768fc68277f7965b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/MultiTopicsConsumerImplTest.java#L168
   should be wrapped with Awaitility
   ```java
           Awaitility.await().untilAsserted(() -> assertTrue(consumer.hasNextPendingReceive()));
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] codelipenghui commented on pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
codelipenghui commented on pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#issuecomment-953774514


   @michaeljmarshall We can only cherry-pick it after 2.9.0 released.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] michaeljmarshall commented on pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#issuecomment-953521363


   @codelipenghui - would you please also cherry pick this to `branch-2.9`? Thanks.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari commented on pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
lhotari commented on pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#issuecomment-950944707


   > > @michaeljmarshall can you allow edits for maintainers for this PR? I'd like to push the test case ([lhotari@1b0ad24](https://github.com/lhotari/pulsar/commit/1b0ad24b)) to be part of this PR.
   > 
   > @lhotari - done, and I’ll address the rest of your comments soon.
   
   thanks @michaeljmarshall , I pushed a commit with the test to this PR now. Please pull the changes to your local PR branch.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] eolivelli commented on pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
eolivelli commented on pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#issuecomment-949061513


   @michaeljmarshall:Thanks for your contribution. For this PR, do we need to update docs?
   (The [PR template contains info about doc](https://github.com/apache/pulsar/blob/master/.github/PULL_REQUEST_TEMPLATE.md#documentation), which helps others know more about the changes. Can you provide doc-related info in this and future PR descriptions? Thanks)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] MMirelli commented on a change in pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
MMirelli commented on a change in pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#discussion_r734255156



##########
File path: pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiTopicsConsumerImpl.java
##########
@@ -409,17 +410,18 @@ private void resumeReceivingFromPausedConsumersIfNeeded() {
     protected CompletableFuture<Message<T>> internalReceiveAsync() {
         CompletableFutureCancellationHandler cancellationHandler = new CompletableFutureCancellationHandler();
         CompletableFuture<Message<T>> result = cancellationHandler.createFuture();
-        Message<T> message = incomingMessages.poll();
-        if (message == null) {
-            pendingReceives.add(result);
-            cancellationHandler.setCancelAction(() -> pendingReceives.remove(result));
-        } else {
-            decreaseIncomingMessageSize(message);
-            checkState(message instanceof TopicMessageImpl);

Review comment:
       Third bullet point in **Modifications**: 
   
   > Remove the checkState(message instanceof TopicMessageImpl); method call from the MultiTopicsConsumerImpl#internalReceiveAsync method. This decision may be controversial. I removed the check because we only ever add TopicMessageImpl to the MultiTopicsConsumerImpl's incomingMessages queue. If it is a necessary check, we could complete the future exceptionally when the message is not of type TopicMessageImpl.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] michaeljmarshall commented on a change in pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on a change in pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#discussion_r735764999



##########
File path: pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiTopicsConsumerImpl.java
##########
@@ -269,7 +269,7 @@ private void receiveMessageFromConsumer(ConsumerImpl<T> consumer) {
                 // recursion and stack overflow
                 internalPinnedExecutor.execute(() -> receiveMessageFromConsumer(consumer));

Review comment:
       @lhotari - great catch. I just added a commit to remove the extra scheduling.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari commented on pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
lhotari commented on pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#issuecomment-950916919


   @michaeljmarshall can you allow edits for maintainers for this PR? I'd like to push the test case (https://github.com/lhotari/pulsar/commit/1b0ad24b) to be part of this PR.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari commented on pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
lhotari commented on pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#issuecomment-950145442


   @michaeljmarshall I created a repro test case for this PR in commit https://github.com/lhotari/pulsar/commit/9b738f70 .
   
   This is the repro case: https://github.com/lhotari/pulsar/blob/9b738f70817075d715fd6c662352d4f49ae2c41f/pulsar-broker/src/test/java/org/apache/pulsar/client/api/MultiTopicsConsumerTest.java#L133-L195


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari commented on a change in pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
lhotari commented on a change in pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#discussion_r734392445



##########
File path: pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiTopicsConsumerImpl.java
##########
@@ -409,17 +410,18 @@ private void resumeReceivingFromPausedConsumersIfNeeded() {
     protected CompletableFuture<Message<T>> internalReceiveAsync() {
         CompletableFutureCancellationHandler cancellationHandler = new CompletableFutureCancellationHandler();
         CompletableFuture<Message<T>> result = cancellationHandler.createFuture();
-        Message<T> message = incomingMessages.poll();
-        if (message == null) {
-            pendingReceives.add(result);
-            cancellationHandler.setCancelAction(() -> pendingReceives.remove(result));
-        } else {
-            decreaseIncomingMessageSize(message);
-            checkState(message instanceof TopicMessageImpl);

Review comment:
       @michaeljmarshall I would keep that change to another PR since it's better to just do the change in this PR that fixes the data race. The class contains similar checks in other locations and if those checks are removed, that change should be consist and complete.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari commented on pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
lhotari commented on pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#issuecomment-949488081


   > Run the callback for consumer.receiveAsync() on the internalPinnedExecutor. I chose to run the whole callback on the internalPinnedExecutor instead of just the messageReceived method. If we left the callback using thenAccept and ran messageReceived on the internalPinnedExecutor, there is a chance that the callback will run on the calling thread, which is always the internalPinnedExecutor. That would mean that the messageReceived logic would actually run after the remaining callback logic that inspects the incomingMessages.size() and decides whether or not to pause the consumer. By scheduling the callback on the internalPinnedExecutor using thenAcceptAsync, we guarantee that the code is run together without the data race we're fixing in this PR.
   
   Good catch @michaeljmarshall! Yes that's true that the other consumer will be another thread and thenAcceptAsync is needed to fix another potential race. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] eolivelli commented on pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
eolivelli commented on pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#issuecomment-949478478


   @michaeljmarshall:Thanks for providing doc info!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] merlimat merged pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
merlimat merged pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari commented on a change in pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
lhotari commented on a change in pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#discussion_r734410442



##########
File path: pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiTopicsConsumerImpl.java
##########
@@ -245,7 +245,7 @@ private void startReceivingMessages(List<ConsumerImpl<T>> newConsumers) {
     }
 
     private void receiveMessageFromConsumer(ConsumerImpl<T> consumer) {

Review comment:
       The `thenAccept` -> `thenAcceptAsync` change in `receiveMessageFromConsumer` method should be reverted. This method is executed in the context of the `internalPinnedExecutor` and the change to use `thenAcceptAsync` isn't useful because of that. It leads to an additional unnecessary scheduling step. Not a big deal, but it's unnecessary. 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] michaeljmarshall commented on a change in pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on a change in pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#discussion_r734603705



##########
File path: pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiTopicsConsumerImpl.java
##########
@@ -409,17 +410,18 @@ private void resumeReceivingFromPausedConsumersIfNeeded() {
     protected CompletableFuture<Message<T>> internalReceiveAsync() {
         CompletableFutureCancellationHandler cancellationHandler = new CompletableFutureCancellationHandler();
         CompletableFuture<Message<T>> result = cancellationHandler.createFuture();
-        Message<T> message = incomingMessages.poll();
-        if (message == null) {
-            pendingReceives.add(result);
-            cancellationHandler.setCancelAction(() -> pendingReceives.remove(result));
-        } else {
-            decreaseIncomingMessageSize(message);
-            checkState(message instanceof TopicMessageImpl);

Review comment:
       @lhotari - yes, it was intentional to remove it, but I agree that this shouldn't be removed for this PR. I just added it back in.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari edited a comment on pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
lhotari edited a comment on pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#issuecomment-950145442


   @michaeljmarshall I created a repro test case for this PR in commit https://github.com/lhotari/pulsar/commit/9b738f70 .
   
   This is the repro case: https://github.com/lhotari/pulsar/blob/1b0ad24b0a07a637c874d0aa76e06929ecea597f/pulsar-broker/src/test/java/org/apache/pulsar/client/api/MultiTopicsConsumerTest.java#L133-L196


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] michaeljmarshall commented on pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#issuecomment-950941793


   > @michaeljmarshall can you allow edits for maintainers for this PR? I'd like to push the test case ([lhotari@1b0ad24](https://github.com/lhotari/pulsar/commit/1b0ad24b)) to be part of this PR.
   
   @lhotari - done, and I’ll address the rest of your comments soon.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari commented on a change in pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
lhotari commented on a change in pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#discussion_r734410442



##########
File path: pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiTopicsConsumerImpl.java
##########
@@ -245,7 +245,7 @@ private void startReceivingMessages(List<ConsumerImpl<T>> newConsumers) {
     }
 
     private void receiveMessageFromConsumer(ConsumerImpl<T> consumer) {

Review comment:
       The `thenAccept` -> `thenAcceptAsync` change in `receiveMessageFromConsumer` method should be reverted. This method is executed in the context of the `internalPinnedExecutor` and the change to use `thenAcceptAsync` isn't useful because of that. It leads to an additional unnecessary scheduling step. Not a big deal, but it's unnecessary. 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari commented on a change in pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
lhotari commented on a change in pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#discussion_r734864734



##########
File path: pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiTopicsConsumerImpl.java
##########
@@ -269,7 +269,7 @@ private void receiveMessageFromConsumer(ConsumerImpl<T> consumer) {
                 // recursion and stack overflow
                 internalPinnedExecutor.execute(() -> receiveMessageFromConsumer(consumer));

Review comment:
       Since the `thenAcceptAsync` now makes this code run on the internalPinnedExecutor, this extra scheduling to the internalPinnedExecutor should be removed. There isn't a risk of the stack growing infinitely since there is no direct recursive call chain.
   
   This change improved the throughput performance by about 8% in a simple local benchmark.
   
   ```bash
   # create topic with 10 partitions
   ./bin/pulsar-admin topics create-partitioned-topic -p 10 parttest
   # run producer (a lot of small messages to stress test the logic)
   ./bin/pulsar-perf produce -ioThreads 4 -s 10 -o 6000 -p 200000 -r 350000 parttest
   # run consumer (in another terminal)
   ./bin/pulsar-perf consume -q 6000 -p 200000 parttest
   ```
   Throughput performance went from 290k msgs/s to about 315k msgs/s.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari edited a comment on pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
lhotari edited a comment on pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#issuecomment-950145442


   @michaeljmarshall I created a repro test case for this PR in commit https://github.com/lhotari/pulsar/commit/1b0ad24b .
   
   This is the repro case: https://github.com/lhotari/pulsar/blob/1b0ad24b0a07a637c874d0aa76e06929ecea597f/pulsar-broker/src/test/java/org/apache/pulsar/client/api/MultiTopicsConsumerTest.java#L133-L196


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari commented on a change in pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
lhotari commented on a change in pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#discussion_r734253146



##########
File path: pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiTopicsConsumerImpl.java
##########
@@ -409,17 +410,18 @@ private void resumeReceivingFromPausedConsumersIfNeeded() {
     protected CompletableFuture<Message<T>> internalReceiveAsync() {
         CompletableFutureCancellationHandler cancellationHandler = new CompletableFutureCancellationHandler();
         CompletableFuture<Message<T>> result = cancellationHandler.createFuture();
-        Message<T> message = incomingMessages.poll();
-        if (message == null) {
-            pendingReceives.add(result);
-            cancellationHandler.setCancelAction(() -> pendingReceives.remove(result));
-        } else {
-            decreaseIncomingMessageSize(message);
-            checkState(message instanceof TopicMessageImpl);

Review comment:
       Is it intentional to remove this line?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] lhotari commented on a change in pull request #12456: [Java Client] Remove data race in MultiTopicsConsumerImpl to ensure correct message order

Posted by GitBox <gi...@apache.org>.
lhotari commented on a change in pull request #12456:
URL: https://github.com/apache/pulsar/pull/12456#discussion_r734253146



##########
File path: pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiTopicsConsumerImpl.java
##########
@@ -409,17 +410,18 @@ private void resumeReceivingFromPausedConsumersIfNeeded() {
     protected CompletableFuture<Message<T>> internalReceiveAsync() {
         CompletableFutureCancellationHandler cancellationHandler = new CompletableFutureCancellationHandler();
         CompletableFuture<Message<T>> result = cancellationHandler.createFuture();
-        Message<T> message = incomingMessages.poll();
-        if (message == null) {
-            pendingReceives.add(result);
-            cancellationHandler.setCancelAction(() -> pendingReceives.remove(result));
-        } else {
-            decreaseIncomingMessageSize(message);
-            checkState(message instanceof TopicMessageImpl);

Review comment:
       Is it intentional to remove `checkState(message instanceof TopicMessageImpl);`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org