-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: remove an unnecessary shared_lock in hot path #2773
fix: remove an unnecessary shared_lock in hot path #2773
Conversation
…nduct this unnecessary lock)
WalkthroughThe changes primarily introduce a new method Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant ConsensusCoordinator
participant Binlog
Client->>ConsensusCoordinator: InternalAppendBinlog(cmd)
ConsensusCoordinator->>Binlog: IsOpened()
Binlog-->>ConsensusCoordinator: Status (Open/Closed)
ConsensusCoordinator-->>Client: Return Status
Poem
Tip Early access features: enabledWe are currently testing the following features in early access:
Note:
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (3)
- include/pika_binlog.h (1 hunks)
- src/pika_binlog.cc (1 hunks)
- src/pika_consensus.cc (1 hunks)
Additional comments not posted (3)
include/pika_binlog.h (1)
55-55
: New MethodIsOpened
AddedThe
IsOpened
method checks if the binlog is open and returns an appropriate status. This is a good addition to reduce contention in the hot path.src/pika_binlog.cc (1)
148-153
: New MethodIsOpened
ImplementationThe
IsOpened
method is implemented correctly. It checks if the binlog is open and returns an appropriate status. This will help reduce contention in the hot path.src/pika_consensus.cc (1)
377-377
: OptimizedInternalAppendBinlog
MethodThe
InternalAppendBinlog
method now callsIsOpened
instead ofGetProducerStatus
, reducing unnecessary locking and contention in the hot path. This change is well-aligned with the PR objective.
f7350a8
into
OpenAtomFoundation:unstable
…nduct this unnecessary lock) (#2773) Co-authored-by: cheniujh <[email protected]>
…nduct this unnecessary lock) (OpenAtomFoundation#2773) Co-authored-by: cheniujh <[email protected]>
…nduct this unnecessary lock) (OpenAtomFoundation#2773) Co-authored-by: cheniujh <[email protected]>
问题:每次写binlog 后,在ConsensusCoordinator::InternalAppendBinlog结尾,都要执行一次stable_logger_->Logger()->GetProducerStatus(&filenum, &offset), 但是这里的filenum, offset取了之后根本不用,只是取用了这个函数的返回值。而这里面要取filenum, offset要对全局唯一的version_对象加锁,尽管只是shared_lock,但这个锁对象是一把全局都在抢的锁,而且这个位置是热点,开启binlog的情况下,每一条请求都要去做这件事情,这就会有较大竞争,最重要的是这个竞争是完全没有必要的。
这个PR做的事情是将GetProducerStatus中除了"加锁获取filename, offset"之外的逻辑复制封装成了一个新的函数 IsOpened(),该函数同GetProducerStatus有一样的返回值,用于在ConsensusCoordinator::InternalAppendBinlog替换对GetProducerStatus的调用。
Issue: Each time after writing "bingo", at the end of the ConsensusCoordinator::InternalAppendBinlog function, the stable_logger_->Logger()->GetProducerStatus(&filenum, &offset) is executed. However, the filenum and offset variables are not used after being retrieved; only the return value of this function is utilized. Acquiring these variables requires locking the globally unique version_ object, even though it is just a shared_lock, which is a lock object that is highly contended globally. This spot is a hotspot, and in scenarios where binlog is enabled, each request has to perform this action, leading to significant competition. More crucially, this competition is entirely unnecessary.
This PR addresses the issue by copying and encapsulating the logic within GetProducerStatus, except for the "locking and retrieving filenum and offset" part, into a new function called IsOpened(). This new function has the same return value as GetProducerStatus and is utilized to replace the call to GetProducerStatus in ConsensusCoordinator::InternalAppendBinlog.
Summary by CodeRabbit