pulsar icon indicating copy to clipboard operation
pulsar copied to clipboard

[improve][client]pulsar-client-io thread has high CPU usage when client idle and batch message enable

Open stillerrr opened this issue 1 year ago • 4 comments

Search before asking

  • [X] I searched in the issues and found nothing similar.

Motivation

Create a pulsar client and to initialize, send a little message to topic, then the pulsar-client-io thread(just 1 thread by default) cpu usage is 14%, but client is idle, no action for it. image

the root cause is that batch message sending is enable, ProducerImpl would use pulsar-client-io thread to send message per 1ms by default image

pulsar-client use EventLoopGroup as thread pool, the thread name is "pulsar-client-io",EpollEventLoopGroup is preferred. When switch to NioEventLoopGroup, the CPU usage just reduce little to 9.6% image

Solution

Use ScheduledExecutorService to send batch message to replace the EventLoopGroup. It would reduce CPU usage less than 1% image

Alternatives

No response

Anything else?

No response

Are you willing to submit a PR?

  • [X] I'm willing to submit a PR!

stillerrr avatar Aug 17 '24 02:08 stillerrr

@stillerrr When the client is idle, batchFlushTasks aren't scheduled in the Pulsar client code. The current problem description where it says "when client idle" doesn't make sense to my.

lhotari avatar Aug 20 '24 08:08 lhotari

It could be useful to use a real profiler sync as https://github.com/async-profiler/async-profiler to pinpoint the problem.

lhotari avatar Aug 20 '24 08:08 lhotari

It could be useful to use a real profiler sync as https://github.com/async-profiler/async-profiler to pinpoint the problem.

image EventLoopGroup is always running, and NIOEventLoopGroup use less CPU (about 9%) than EpollEventLoopGroup

stillerrr avatar Aug 20 '24 09:08 stillerrr

@stillerrr When the client is idle, batchFlushTasks aren't scheduled in the Pulsar client code. The current problem description where it says "when client idle" doesn't make sense to my.

EventLoopGroup do scheduled task would use more CPU than ScheduledExecutorService

"when client idle" means few requests, maybe 1000 request per second, and it would cause batchFlushTasks scheduled

stillerrr avatar Aug 20 '24 09:08 stillerrr

"when client idle" means few requests, maybe 1000 request per second, and it would cause batchFlushTasks scheduled

well, it's not really idling if there's 1000 requests per second. I think that the title of this issue is very misleading.

lhotari avatar Sep 03 '24 11:09 lhotari