apache / apache/pulsar-client-cpp
[pulsar-client-cpp] Excessive locking cause significant performance degradation
- 主要语言
- C++
- 星标
- 71
- 派生
- 90
- 平均合并
- 2 小时 33 分钟
- 30 天内合并 PR
- 3
描述
**Describe the bug**
Implementation of statistics in cpp client have two concurrency issues.
1. 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.

2. ProducerStatsImpl implementation has races between destructor and DeadlineTimer callback. Consider following scenario:
1. ProducerStatsImpl destructor acquire the mutex
2. DeadlineTimer calls calback flushAndReset and blocked on mutex
3. ProducerStatsImpl calls timer.cancel and cancel any pending operation but it cannot cancel already executed callback at step 2
4. ProducerStatsImpl destructor release mutex
5. DeadlineTimer acquire the mutex
6. ProducerStatsImpl destructor destroy object
7. DeadlineTimer callback access to deallocated memory
Are you willing accept PR for issue number one or both?
贡献指南
调研方向
首先阅读 ProducerStatsImpl 和 ConsumerStatsImpl 的实现及其 DeadlineTimer callback 路径。使用高负载 profiling 验证锁竞争;对于已接受的范围,完成标准是降低 send/receive mutex 竞争,并防止 callback 访问已销毁的 ProducerStatsImpl。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- cpp
- 领域
- distributed-systems, performance
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100