[pulsar-client-cpp] Excessive locking cause significant performance degradation
Describe the bug Implementation of statistics in cpp client have two concurrency issues.
- ProducerStatsImpl (and ConsumerStatsImpl) classes use a single shared lock to protect access to internal data. The lock is taken on each sent or received message. Under high load this shared lock causes signficant contention and performance degradation. Profiler shows that sending and receiving threads block each-other.

Since sending and receving functions access different member subset they should be protected by different mutex or other approach should be selected.
As example after patching issue I've got about 1/3 throughtput improvement. As you can see on screenshot below threads are witing on I/O but not on mutexes.

-
ProducerStatsImpl implementation has races between destructor and DeadlineTimer callback. Consider following scenario:
- ProducerStatsImpl destructor acquire the mutex
- DeadlineTimer calls calback flushAndReset and blocked on mutex
- ProducerStatsImpl calls timer.cancel and cancel any pending operation but it cannot cancel already executed callback at step 2
- ProducerStatsImpl destructor release mutex
- DeadlineTimer acquire the mutex
- ProducerStatsImpl destructor destroy object
- DeadlineTimer callback access to deallocated memory
Are you willing accept PR for issue number one or both?
Are you willing accept PR for issue number one or both?
@AndrewJD79 Fixes for any of these are warmly welcomed.
I'll provide PR in a few days
The issue had no activity for 30 days, mark with Stale label.