You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "liweiyuan (via GitHub)" <gi...@apache.org> on 2024/02/23 08:57:26 UTC

[D] [Bug] When the Consumer uses the asynchronous API, it leads to full CPU utilization. [pulsar]

GitHub user liweiyuan created a discussion: [Bug] When the Consumer uses the asynchronous API, it leads to full CPU utilization.

### Search before asking

- [X] I searched in the [issues](https://github.com/apache/pulsar/issues) and found nothing similar.


### Version

pulsar-client: 3.x, 2.x

### Minimal reproduce step

1.use JAVA client , Consumer use async api
2. code:
`public class ConsumerTestCast {

    public static void main(String[] args) {

        try {
            PulsarClient client = PulsarClient.builder()
                    .serviceUrl("pulsar://192.168.2.213:6650")
                    .build();

            Consumer consumer = client.newConsumer()
                    .topic("Bosh-3")
                    .subscriptionName("TestGroup")
                    .ackTimeout(1, TimeUnit.SECONDS)
                    .subscribe();
            while(true){
                receiveAsync(consumer);
            }
        }catch (PulsarClientException e) {
            throw new RuntimeException(e);
        }
    }

    private static void receiveAsync(Consumer consumer) {
        CompletableFuture<Message> completableFuture = consumer.receiveAsync();
        completableFuture.whenComplete((msg,th) ->{
            if(msg != null){
                System.out.println("Message:" + new String(msg.getData()));
                try {
                    consumer.acknowledge(msg);
                } catch (PulsarClientException e) {
                    throw new RuntimeException(e);
                }
            }
            if(th != null){
                System.err.println("Err: "+ th.getMessage());
            }
        });
    }
}`



### What did you expect to see?

When using asynchronous APIs, there is no CPU consumption when there are no messages.

### What did you see instead?

Using the top command to check CPU usage, it was found that the CPU utilization is very high, maxing out the machine.

![2024-02-23 15-07-52屏幕截图](https://github.com/apache/pulsar/assets/26542900/f5654542-1d32-491e-8859-3b3bb576b76f)

### Anything else?

_No response_

### Are you willing to submit a PR?

- [ ] I'm willing to submit a PR!

GitHub link: https://github.com/apache/pulsar/discussions/22105

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] When the Consumer uses the asynchronous API, it leads to full CPU utilization. [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
GitHub user lhotari added a comment to the discussion: When the Consumer uses the asynchronous API, it leads to full CPU utilization.

@liweiyuan Your code example doesn't make sense for using the asynchronous API. This is essentially a tight loop:
```java
        while(true){
            receiveAsync(consumer);
        }
```

It seems that you should be using the listener feature instead.
Please check the docs about ["Create a consumer with a message listener"](https://pulsar.apache.org/docs/3.2.x/client-libraries-consumers/#create-a-consumer-with-a-message-listener).

If you are looking for a better asynchronous abstraction for the Pulsar Java client, I'd recommend using [pulsar-client-reactive](https://github.com/apache/pulsar-client-reactive) and/or [Spring Pulsar](https://spring.io/projects/spring-pulsar).







GitHub link: https://github.com/apache/pulsar/discussions/22105#discussioncomment-8567846

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] When the Consumer uses the asynchronous API, it leads to full CPU utilization. [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
GitHub user lhotari deleted a comment on the discussion: When the Consumer uses the asynchronous API, it leads to full CPU utilization.

This isn't the correct way to use the async API. I'll move this into the Q&A section of Pulsar's GitHub Discussions.

GitHub link: https://github.com/apache/pulsar/discussions/22105#discussioncomment-8565723

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] When the Consumer uses the asynchronous API, it leads to full CPU utilization. [pulsar]

Posted by "liweiyuan (via GitHub)" <gi...@apache.org>.
GitHub user liweiyuan edited a discussion: When the Consumer uses the asynchronous API, it leads to full CPU utilization.

### Search before asking

- [X] I searched in the [issues](https://github.com/apache/pulsar/issues) and found nothing similar.


### Version

pulsar-client: 3.x, 2.x

### Minimal reproduce step

1.use JAVA client , Consumer use async api
2. code:
`public class ConsumerTestCast {

    public static void main(String[] args) {

        try {
            PulsarClient client = PulsarClient.builder()
                    .serviceUrl("pulsar://192.168.2.213:6650")
                    .build();

            Consumer consumer = client.newConsumer()
                    .topic("Bosh-3")
                    .subscriptionName("TestGroup")
                    .ackTimeout(1, TimeUnit.SECONDS)
                    .subscribe();
            while(true){
                receiveAsync(consumer);
            }
        }catch (PulsarClientException e) {
            throw new RuntimeException(e);
        }
    }

    private static void receiveAsync(Consumer consumer) {
        CompletableFuture<Message> completableFuture = consumer.receiveAsync();
        completableFuture.whenComplete((msg,th) ->{
            if(msg != null){
                System.out.println("Message:" + new String(msg.getData()));
                try {
                    consumer.acknowledge(msg);
                } catch (PulsarClientException e) {
                    throw new RuntimeException(e);
                }
            }
            if(th != null){
                System.err.println("Err: "+ th.getMessage());
            }
        });
    }
}`



### What did you expect to see?

When using asynchronous APIs, there is no CPU consumption when there are no messages.

### What did you see instead?

Using the top command to check CPU usage, it was found that the CPU utilization is very high, maxing out the machine.

![2024-02-23 15-07-52屏幕截图](https://github.com/apache/pulsar/assets/26542900/f5654542-1d32-491e-8859-3b3bb576b76f)

### Anything else?

_No response_

### Are you willing to submit a PR?

- [ ] I'm willing to submit a PR!

GitHub link: https://github.com/apache/pulsar/discussions/22105

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] When the Consumer uses the asynchronous API, it leads to full CPU utilization. [pulsar]

Posted by "liweiyuan (via GitHub)" <gi...@apache.org>.
GitHub user liweiyuan added a comment to the discussion: When the Consumer uses the asynchronous API, it leads to full CPU utilization.

ok

GitHub link: https://github.com/apache/pulsar/discussions/22105#discussioncomment-8586818

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org


Re: [D] [Bug] When the Consumer uses the asynchronous API, it leads to full CPU utilization. [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
GitHub user lhotari added a comment to the discussion: [Bug] When the Consumer uses the asynchronous API, it leads to full CPU utilization.

This isn't the correct way to use the async API. I'll move this into the Q&A section of Pulsar's GitHub Discussions.

GitHub link: https://github.com/apache/pulsar/discussions/22105#discussioncomment-8565723

----
This is an automatically sent email for commits@pulsar.apache.org.
To unsubscribe, please send an email to: commits-unsubscribe@pulsar.apache.org